Created
August 3, 2019 02:05
-
-
Save sefeng211/77fd7736b65d5c7f0c4dfef2a7d12b1a to your computer and use it in GitHub Desktop.
This is the script that I used to setup a server block for Nginx. Please make sure you have change $DOMAIN to your site and double check the $ROOT_LOCATION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script used https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04 as the reference | |
DOMAIN="sample.seanfeng.dev" # Change it to the domain you want to add | |
ROOT_LOCATION=/var/www # Make sure it is your root location | |
HTML_TEMPLATE=" | |
<html> | |
<head> | |
<title>Welcome to Example.com!</title> | |
</head> | |
<body> | |
<h1>Success! The example.com server block is working!</h1> | |
</body> | |
</html> | |
" | |
CONFIG_TEMPLATE=" | |
server { | |
listen 80; | |
listen [::]:80; | |
root ${ROOT_LOCATION}/${DOMAIN}/html; | |
index index.html index.htm; | |
server_name ${DOMAIN}; | |
location / { | |
try_files \$uri \$uri/ =404; | |
} | |
} | |
" | |
echo "Creating server block for ${DOMAIN}" | |
sudo mkdir -p ${ROOT_LOCATION}/${DOMAIN}/html | |
sudo chown -R $USER:$USER ${ROOT_LOCATION}/${DOMAIN}/html | |
sudo chmod -R 755 ${ROOT_LOCATION}/${DOMAIN} | |
echo "${HTML_TEMPLATE}" > ${ROOT_LOCATION}/${DOMAIN}/html/index.html | |
echo "${CONFIG_TEMPLATE}" > /etc/nginx/sites-available/${DOMAIN} | |
sudo ln -s /etc/nginx/sites-available/${DOMAIN} /etc/nginx/sites-enabled/ | |
sudo nginx -t | |
sudo systemctl restart nginx | |
echo "Setup complete, Nginx restarted" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment