Last active
January 21, 2018 09:28
-
-
Save bingdian/3f38295dedc3de8a1b1e0df8ad47c183 to your computer and use it in GitHub Desktop.
nginx host
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 | |
| ### Set Language | |
| TEXTDOMAIN=virtualhost | |
| ### Set default parameters | |
| action=$1 | |
| domain=$2 | |
| port=$3 | |
| rootDir=$4 | |
| owner=$(who am i | awk '{print $1}') | |
| sitesEnable='/etc/nginx/sites-enabled/' | |
| sitesAvailable='/etc/nginx/sites-available/' | |
| userDir='/home/wwwroot/' | |
| if [ "$(whoami)" != 'root' ]; then | |
| echo $"You have no permission to run $0 as non-root user. Use sudo" | |
| exit 1; | |
| fi | |
| if [ "$action" != 'create' ] && [ "$action" != 'delete' ] | |
| then | |
| echo $"You need to prompt for action (create or delete) -- Lower-case only" | |
| exit 1; | |
| fi | |
| while [ "$domain" == "" ] | |
| do | |
| echo -e $"Please provide domain. e.g.dev,staging" | |
| read domain | |
| done | |
| if [ "$rootDir" == "" ]; then | |
| rootDir=${domain//./} | |
| fi | |
| ### if root dir starts with '/', don't use /var/www as default starting point | |
| if [[ "$rootDir" =~ ^/ ]]; then | |
| userDir='' | |
| fi | |
| # rootDir=$userDir$rootDir | |
| if [ "$action" == 'create' ] | |
| then | |
| ### check if domain already exists | |
| if [ -e $sitesAvailable$domain ]; then | |
| echo -e $"This domain already exists.\nPlease Try Another one" | |
| exit; | |
| fi | |
| ### check if directory exists or not | |
| if ! [ -d $userDir$rootDir ]; then | |
| ### create the directory | |
| mkdir $userDir$rootDir | |
| ### give permission to root dir | |
| chmod 755 $userDir$rootDir | |
| fi | |
| ### create virtual host rules file | |
| if ! echo "server { | |
| listen 80; | |
| root $userDir$rootDir; | |
| server_name $domain; | |
| location / { | |
| proxy_pass http://localhost:$port; | |
| 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; | |
| } | |
| }" > $sitesAvailable$domain | |
| then | |
| echo -e $"There is an ERROR create $domain file" | |
| exit; | |
| else | |
| echo -e $"\nNew Virtual Host Created\n" | |
| fi | |
| ### Add domain in /etc/hosts | |
| if ! echo "127.0.0.1 $domain" >> /etc/hosts | |
| then | |
| echo $"ERROR: Not able write in /etc/hosts" | |
| exit; | |
| else | |
| echo -e $"Host added to /etc/hosts file \n" | |
| fi | |
| if [ "$owner" == "" ]; then | |
| chown -R $(whoami):www-data $userDir$rootDir | |
| else | |
| chown -R $owner:www-data $userDir$rootDir | |
| fi | |
| ### enable website | |
| ln -s $sitesAvailable$domain $sitesEnable$domain | |
| ### restart Nginx | |
| service nginx restart | |
| ### show the finished message | |
| echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $userDir$rootDir" | |
| exit; | |
| else | |
| ### check whether domain already exists | |
| if ! [ -e $sitesAvailable$domain ]; then | |
| echo -e $"This domain dont exists.\nPlease Try Another one" | |
| exit; | |
| else | |
| ### Delete domain in /etc/hosts | |
| newhost=${domain//./\\.} | |
| sed -i "/$newhost/d" /etc/hosts | |
| ### disable website | |
| rm $sitesEnable$domain | |
| ### restart Nginx | |
| service nginx restart | |
| ### Delete virtual host rules files | |
| rm $sitesAvailable$domain | |
| fi | |
| ### check if directory exists or not | |
| if [ -d $userDir$rootDir ]; then | |
| echo -e $"Delete host root directory ? (s/n)" | |
| read deldir | |
| if [ "$deldir" == 's' -o "$deldir" == 'S' ]; then | |
| ### Delete the directory | |
| rm -rf $userDir$rootDir | |
| echo -e $"Directory deleted" | |
| else | |
| echo -e $"Host directory conserved" | |
| fi | |
| else | |
| echo -e $"Host directory not found. Ignored" | |
| fi | |
| ### show the finished message | |
| echo -e $"Complete!\nYou just removed Virtual Host $domain" | |
| exit 0; | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment