Last active
December 31, 2021 01:01
-
-
Save rexdivakar/5141ebda97ab0cf9dd8cc3a61ef4e53e to your computer and use it in GitHub Desktop.
Nginx Auto Config Updater
This file contains 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 | |
# ------------------------------------------------------------------# | |
# Auto Nginx deploys # | |
# Description: # | |
# This script automatically deploys nginx Services # | |
# # | |
#Input Arguments: # | |
# sudo bash enginX.sh <service_name> <port-no> # | |
# Dependency: # | |
# sudo apt install Nginx # | |
# sudo apt-get install python3-certbot-Nginx # | |
# sudo certbot --nginx -d <Server Name> # | |
# ------------------------------------------------------------------# | |
# Validate the Sudo Rights | |
if [[ $EUID > 0 ]]; then | |
echo "Please run with sudo rights!" | |
exit 1 | |
else | |
echo "" | |
fi | |
# Check if nginx is installed | |
if [ -e /var/run/nginx.pid ]; then | |
echo "Nginx is installed & running"; | |
else | |
echo "Nginx is not installed"; exit 1 | |
fi | |
# Create new nginx config file | |
echo "server { | |
listen 80; | |
server_name $1.pibeast.online; | |
access_log /var/log/nginx/$1-access.log; | |
error_log /var/log/nginx/$2-error.log; | |
location / { | |
proxy_pass http://localhost:$2;"' | |
proxy_http_version 1.1; | |
proxy_cache_bypass $http_upgrade; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection "upgrade"; | |
proxy_set_header Host $host; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header X-Forwarded-Proto $scheme; | |
proxy_set_header X-Forwarded-Host $host; | |
proxy_set_header X-Forwarded-Port $server_port; | |
} | |
}'>/etc/nginx/sites-available/$1 | |
# Create Symlinks | |
ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/ | |
echo "Creating a Symlink !" | |
# checks the state of nginx | |
echo `nginx -t` | |
# Restart Nginx Services | |
systemctl restart nginx.service | |
echo "Restarting nginx.service" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment