Web Server
Web Server
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
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.
and
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.
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" "[email protected]":~/```
Or this
If you will be using your private key
```scp -i "/path/to/private_key" -o StrictHostKeyChecking=no "myfile.txt"
"[email protected]":~/```
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
#!/usr/bin/env bash
# transfers a file from a client to a server with scp
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
Then, enter your domain name, add it to cart then proceed... You will now log in your GitHub...
Then comfirm order
#!/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;
}
}"
Task 4
1. vi 4-not_found_page_404
paste this there
#!/usr/bin/env bash
# sets up a new 404 error page
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:\/\/round-lake.dustinice.workers.dev:443\/https\/github.com\/thatagricboy permanent;/"
/etc/nginx/sites-available/default ; sudo service nginx start',
}
Max