Skip to content

Instantly share code, notes, and snippets.

@daminebenz
Last active November 13, 2020 16:40
Show Gist options
  • Save daminebenz/d44bdbea6a6bc1e3b303a5985f159adc to your computer and use it in GitHub Desktop.
Save daminebenz/d44bdbea6a6bc1e3b303a5985f159adc to your computer and use it in GitHub Desktop.
add domain to nginx VPS automatically => ./add-domain.sh your-new-domain.com
#!/bin/bash
# Info
# ---
# script can run with the domain as a command line input
# `sudo ./add-domain.sh your-new-domain.com` or without and
# the script will prompt the user for input
#config
web_root='/usr/share/nginx'
config_dir='/etc/nginx'
if [ -z "$1" ]
then
#user input
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; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
root $web_root/$DOMAIN/;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name $DOMAIN www.$DOMAIN;
location ~ /\.ht {
deny all;
}
access_log $web_root/$DOMAIN/log/access_log.txt;
error_log $web_root/$DOMAIN/log/error_log.txt error;
}
EOF
) > $config_dir/sites-available/$DOMAIN
echo "Making web directories"
mkdir -p $web_root/"$DOMAIN"
mkdir -p $web_root/"$DOMAIN"/{log,backup}
(
cat <<EOF
<h1>$DOMAIN is now Online</h1>
EOF
) > $web_root/"$DOMAIN"/index.html
ln -s $config_dir/sites-available/"$DOMAIN" $config_dir/sites-enabled/"$DOMAIN"
/etc/init.d/nginx reload
echo "Nginx - reload"
chown -R www-data:www-data $web_root/"$DOMAIN"
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