Skip to content

Instantly share code, notes, and snippets.

@Aitthi
Last active January 12, 2020 19:01
Show Gist options
  • Save Aitthi/4299329d6730af568514a8d0614afc25 to your computer and use it in GitHub Desktop.
Save Aitthi/4299329d6730af568514a8d0614afc25 to your computer and use it in GitHub Desktop.
Script Create Virtual Hosts on Nginx
#!/bin/bash
echo
echo "-----------------------------------"
echo "--- Create Host on Nginx Script ---"
echo "-----------------------------------"
echo
echo "Host Name example.com "
read -p 'Host Name: ' host
counter=1
while [ $counter -le 10000 ]
do
if [ "$host" = "" ]
then
echo
echo "Host Name example.com "
read -p 'Required Host Name: ' host
fi
((counter++))
done
read -p 'Set Up Nginx as a Reverse Proxy Server [yes|no] Enter : ' Proxy
counter=1
while [ $counter -le 10000 ]
do
echo
if [ "$Proxy" = '' ]
then
read -p 'Set Up Nginx as a Reverse Proxy Server [yes|no] Enter : ' Proxy
fi
if [ "$Proxy" = "yes" ] || [ "$Proxy" = "no" ]
then
if [ "$Proxy" = "yes" ]
then
echo "Example http://localhost:8080"
read -p 'Reverse Proxy Server : ' ProxyHost
config="server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name $host www.$host;
location / {
proxy_pass $ProxyHost;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
}
}"
break
else
config="server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
root /var/www/$host/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name $host www.$host;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}"
break
fi
else
read -p 'Set Up Nginx as a Reverse Proxy Server [yes|no] Enter : ' Proxy
fi
((counter++))
done
sudo mkdir -p /var/www/$host/html
sudo chown -R $USER:$USER /var/www/$host/html
sudo chmod -R 755 /var/www
echo "<html>
<head>
<title>Welcome to $host!</title>
</head>
<body>
<h1>Success! The $host server block is working!</h1>
</body>
</html>" > /var/www/$host/html/index.html
echo $config > /etc/nginx/sites-available/$host
sudo ln -s /etc/nginx/sites-available/$host /etc/nginx/sites-enabled/
echo
echo "Build Nginx"
sudo nginx -t
echo
echo "Restart Nginx"
sudo systemctl restart nginx
echo
echo "Create Host : $host Success!"
echo "----------------------------"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment