-
Notifications
You must be signed in to change notification settings - Fork 1
/
nginx_server_setup
executable file
·32 lines (23 loc) · 1.4 KB
/
nginx_server_setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
# Configure Nginx so that its HTTP response contains a custom header (on web-01 and web-02)
# The name of the custom HTTP header must be X-Served-By
# The value of the custom HTTP header must be the hostname of the server Nginx is running on
sudo apt-get update
sudo apt-get -y install nginx
# configure firewall to allow request through port 80
sudo ufw allow 'Nginx HTTP'
sudo mkdir -p /var/www/html
# change permissions to allow us to easily create files in this directory
sudo chmod -R 755 /var/www
# create the index page
echo 'Hello World!' | sudo tee /var/www/html/index.html
# create a webpage for error 404
echo "Ceci n'est pas une page" | sudo tee /var/www/html/404.html
# code to configure redirect for a single page(/redirect_me) and add header to display hostname
string_for_replacement="server_name _;\n\tadd_header X-Served-By \$hostname;\n\trewrite ^\/redirect_me https:\/\/happyfelixchukwuma.netlify.app permanent;"
sudo sed -i "s/server_name _;/$string_for_replacement/" /etc/nginx/sites-enabled/default
# code to configure redirect for 404 error page
string_for_replacement="listen 80 default_server;\n\terror_page 404 \/404.html;\n\tlocation = \/404.html {\n\t\troot \/var\/www\/html;\n\t\tinternal;\n\t}"
sudo sed -i "s/listen 80 default_server;/$string_for_replacement/" /etc/nginx/sites-enabled/default
# restart the web server after updating the settings
sudo service nginx restart