Web Server Project
Lets try to make it short and hopefully concise
What You Need To Know
Resources
How to install nginx
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04
How to configure nginx
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-
hosts-on-ubuntu-16-04
What exactly is a bash script? : In a nutshell, bash script is automation. It's a way of putting a
couple of shell commands together in a file. Such that when you make this file executable and run
it all the commands get executed one after the other.
Why do we need DNS? : Well it lets us translate IP addresses into readable names that are easy to
remember. Usually, we do this using the A record to map an IP address to a Domain name.
Before you attempt this task it is important that already know how to connect to your server. I
believe you already know how to do this going by the SSH project. Just in case you are still having
trouble. To connect to your server you need 3 important things
1. Your hostname(server IP address)
2. Your public key
3. Your private key.
How do you get this things.
First lets get our keys
1. On your terminal run this command
ssh-keygen -t rsa -b 4096 -C "your_email"
#insert your mail
The command will prompt you to enter a file name to save the key pair to
Enter access_key
Then it will prompt for a passphrase (optional) to secure the private key. If you don't want to use a
passphrase, just press Enter.
#best not to use any phrase
Wait a few seconds Its going to generate your key for you.
2. Do ls to confirm if you have the file. You should have 2 files.
access_key (this is your private key)
and
access_key.pub (This is your public key)
Now you will need to add the content of access_key.pub to your intranet profile
3. cat
access_key.pub
You should see something like this ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDB2dOvGKj3o8VcLzNJ2Qk1wrsR1xW7VBBnIb
AhH7t5n5i5j8ZkdcIbFsiYnBwOKb2QjyGd5A5C7PBoLDUOc6UJnpbU+4OZsdQOQ2J6e85+6yM
JrtSxMqJ3qkiNkZl32xoog0+9Zxt/
ECzR1sHQsNCFzgfb+8VySc+bKRPdV7ZvGJkA9nKj3q+3VfJG2QWg24cG1+cQlKkh7gP0oWJf
Wej5d5ie5K5liwdbmnW1J8IsHz2QKjIY05mZf12ICZ9NSHL5d5uejKw0zRGxWTErH0KjRlf7Vv/
BLtPp8qljfeqI3b60VwB2yG0LzPqOX3dYTRi1jTgT9X9npiH/user@example.com
Copy it. That's all
4. You then paste that on the the space provided on intranet
Now come back to the project page and ask for a new server.
NOTE: When you ask for a new server its going to make use of your public key that is in your
profile to create the server. So in order to access this server you will need to provide the private key
that was generated with it.
FINALLY to connect to your server use this command
Before you try to connect there a few things you need to do
We need to change the mode of the access_key file
chmod 600 access_key
then we need to move our access_key(private key) to a secure location
mv access_key ~/.ssh
This is the command to connect to your server
ssh ubuntu@hostname -i ~/.ssh/access_key
Now the tasks
task 0.
A bash script that can transfer a file to our server
Normally this is the command we need if we need to transfer a file to our server
```scp -o StrictHostKeyChecking=no "myfile.txt" "user123@example.com":~/```
Or this
If you will be using your private key
```scp -i "/path/to/private_key" -o StrictHostKeyChecking=no "myfile.txt"
"user123@example.com":~/```
where
scp = secure copy
-i = identity(this takes your private key)
-o = Strict host key checking is disabled, so the remote host's key will not be verified.
myfile.txt = the file you want to transfer
user = ubuntu in our case
example.com = your server IP address
~/ = home directory where the file is going to be saved
But now to make this easy so we don't have to type
scp -i "/path/to/private_key" -o StrictHostKeyChecking=no "myfile.txt"
"user123@example.com":~/`
this very long command everytime we want to send a file to the server we need to write a script.
Below is the script.
Because of the conditions we have to satisfy. we needed to use if and elif.
This script works for the project. so save it in the 0-transfer_file if you want.
#!/usr/bin/env bash
# transfers a file from a client to a server with scp
if [ "$#" -lt 3 ]; then
echo "Usage: 0-transfer_file PATH_TO_FILE IP USERNAME PATH_TO_SSH_KEY"
elif [ "$#" -lt 4 ]; then
scp -o StrictHostKeyChecking=no "$1" "$3@$2":~/
else
scp -i "$4" -o StrictHostKeyChecking=no "$1" "$3@$2":~/
fi
You need to make the file executable so that you can run it.
chmod u+x 0-transfer_file
How to run it
this line tells you how to use the file
Usage: 0-transfer_file PATH_TO_FILE IP USERNAME PATH_TO_SSH_KEY"
meaning if you want to run the file you do this
./0-transfer_file file_to_transfer your_IP username private_key
Task 1
1-install_nginx_web_server
All you need to do here is write a script that can install nginx and insert the string Hello Holberton
inside a file called index.html.
#!/usr/bin/env bash
# Installs nginx server
apt-get update
apt-get -y install nginx
ufw allow 'Nginx HTTP'
echo "Hello World!" > /var/www/html/index.html
service nginx start
after writing and making the script and making executable you need to transfer it to your server and
run it there.
1. To transfer: remember task 0
0-transfer_file 1-install_nginx_web_server your_ip ubuntu ~/.ssh/access_key
2. Connect to your server and run the script
ssh ubuntu@your_ip -i ~/.ssh/access_key
After a successful connection
./1-install_nginx_web_server
Exit and use your checker
Task 2
Setup a domain name
1) Go to your project page, then click on .TECH Domains.. this is to create account with them.
2) Then comeback to the project page, click on tools space.
Don't bother clicking on clicking on redeem coupon.....
Just click on "Access to the pack here"
It will take you to GitHub student developer pack.
Then look for .tech Domains and click on it ..
Then, enter your domain name, add it to cart then proceed... You will now log in your GitHub...
Then comfirm order
After that you need to set up an A record
1. login into get.tech
2. click on account
3. you will see Jump to Domain under it you will see a textbox enter your domain name and
click on the double arrow it will take you to another page
4. scroll to the last part and click on manage DNS
5. Click on Add A Record
6. In the Host name field you can leave empty
7. In the Destination IPv4 put your server IP address
8. go to your profile and enter your domain in the Project website url field
9. Come back to your terminal and Write your domain name inside the file
10.push to github
Task 3
1. vi 3-redirection
paste this there
#!/usr/bin/env bash
# Installs, configures, and starts the server
apt-get update
apt-get -y install nginx
sudo ufw allow 'Nginx HTTP'
mkdir -p /var/www/html/
sudo chmod -R 755 /var/www
echo 'Hello World!' > /var/www/html/index.html
SERVER_CONFIG=\
"server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files \$uri \$uri/ =404;
}
location /redirect_me {
return 301 https://www.youtube.com/watch?v=QH2-TGUlwu4;
}
}"
bash -c "echo -e '$SERVER_CONFIG' > /etc/nginx/sites-enabled/default"
if [ "$(pgrep -c nginx)" -le 0 ]; then
service nginx start
else
service nginx restart
fi
2. save and exit
3. make it executable and push to github
4. transfer the file to your server
5. run the file in your server
6. Use your checker
Task 4
1. vi 4-not_found_page_404
paste this there
#!/usr/bin/env bash
# sets up a new 404 error page
sudo apt-get update
sudo apt-get install -y nginx
echo "Hello World!" > index.html
sudo mv index.html /var/www/html
echo "Ceci n'est pas une page" > 404.html
sudo mv 404.html /var/www/html
echo "server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html;
location /redirect_me {
return 301 https://www.youtube.com/watch?v=QH2-TGUlwu4;
}
error_page 404 /404.html;
location = /404.html{
internal;
}
}" > default
sudo mv -f default /etc/nginx/sites-available/default
sudo service nginx restart
2. save and exit
3. make it executable and push to github
4. transfer the file to your server
5. run the file in your server
6. Use your checker
Task 5
1. vi 7-puppet_install_nginx_web_server.pp
paste this there
# Installs a Nginx server
exec {'install':
provider => shell,
command => 'sudo apt-get -y update ; sudo apt-get -y install nginx ; echo "Hello World!" | sudo
tee /var/www/html/index.nginx-debian.html ; sudo sed -i "s/server_name _;/server_name _;\n\
trewrite ^\/redirect_me https:\/\/github.com\/thatagricboy permanent;/"
/etc/nginx/sites-available/default ; sudo service nginx start',
}
2. save and exit
3. make it executable and push to github
4. transfer the file to your server
5. run the file in your server
6. Use your checker
Max