Skip to content

Instantly share code, notes, and snippets.

@insane-dev
Last active April 1, 2017 22:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insane-dev/61400750065f1a79b513959f9aa2f63c to your computer and use it in GitHub Desktop.
Save insane-dev/61400750065f1a79b513959f9aa2f63c to your computer and use it in GitHub Desktop.
Command that creates domain dir and its nginx config
#!/bin/bash
# Makedomain command
# ---
# script can run with the domain as a command line input
# `sudo ./nginx_domain.sh my_domain.com` or without and
# the script will prompt the user for input
#
# BTW: You can make it global e.g. by:
# sudo ln -s /opt/delagics/makedomain.sh /usr/bin/makedomain
# Configiguration
web_root='/var/www'
config_dir='/etc/nginx'
if [ -z "$1" ]
then
echo -e "Enter domain name:"
read DOMAIN
echo "Creating Nginx domain settings for: $DOMAIN"
if [ -z "$DOMAIN" ]
then
echo "Domain required"
exit 1
fi
fi
if [ -z "$DOMAIN" ]
then
DOMAIN=$1
fi
(cat <<EOF
server {
listen 80;
root $web_root/$DOMAIN;
server_name $DOMAIN www.$DOMAIN;
index index.php index.html index.htm;
# access_log $web_root/$DOMAIN/log/access_log.txt;
# error_log $web_root/$DOMAIN/log/error_log.txt error;
# Redirect everything that isn't a real file to index.php
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php\$ {
try_files \$uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
# fastcgi_split_path_info ^(.+\.php)(/.+)\$;
}
location ~ /\.(ht|svn|git) {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
}
EOF
) > $config_dir/sites-available/$DOMAIN.conf
echo "Making web directories"
sudo mkdir -p $web_root/"$DOMAIN"
# mkdir -p $web_root/"$DOMAIN"/{public,private,log,backup}
sudo ln -s $config_dir/sites-available/"$DOMAIN".conf $config_dir/sites-enabled/"$DOMAIN".conf
sudo /etc/init.d/nginx reload
echo "Nginx - reload"
sudo chown -R www-data:www-data $web_root/"$DOMAIN"
sudo chmod 755 $web_root/"$DOMAIN"
echo "Permissions have been set"
echo "$DOMAIN has been setup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment