Skip to content

Instantly share code, notes, and snippets.

@zarulizham
Last active April 18, 2019 01:29
Show Gist options
  • Save zarulizham/08194dc174a952852cd6e5409d19995d to your computer and use it in GitHub Desktop.
Save zarulizham/08194dc174a952852cd6e5409d19995d to your computer and use it in GitHub Desktop.
#!/bin/bash
# /root/new-domain-ssl.sh example.com /var/www/html [on/off] [/etc/ssl/...] [/etc/ssl/...]
clear
if [ -z "$1" ]
then
echo -e $" - Please specify a name for first parameter. \n For info: --info"
exit 0
fi
if [ $1 == '--info' ]
then
echo -e $"Param 1*: domain name (Eg: example.com)"
echo -e $"Param 2*: public path name (Eg: /var/www/html)"
echo -e $"Param 3*: SSL (Value: on/off)"
echo -e $"Param 4 : ssl_certificate (Eg: /etc/ssl/example.com.pem)"
echo -e $"Param 5 : ssl_certificate_key name (Eg: /etc/ssl/example.com.key)\n\n"
echo -e $" * mandatory parameter\n\n"
exit 0
fi
if [ -z $2 ]
then
echo -e $" - Please specify a public path for second parameter\n\n"
exit 0
fi
if [ -z "$3" ]
then
echo -e $" - Please specify SSL value for third parameter\n\n"
exit 0
elif [ $3 == on ]
then
if [ -z "$4" ]
then
echo -e $" - Please specify ssl_certificate path for forth parameter\n\n"
exit 0
elif [ -z "$5" ]
then
echo -e $" - Please specify ssl_certificate_key path for fifth parameter\n\n"
exit 0
fi
fi
name=$1
WEB_ROOT_DIR=$2
ssl=$3
sitesEnable='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
sitesAvailabledomain=$sitesAvailable$name
echo "Creating a vhost for $sitesAvailabledomain with a webroot $WEB_ROOT_DIR"
mkdir $WEB_ROOT_DIR
if [ $ssl == on ]
then
echo "
server {
listen 80;
listen [::]:80;
server_name $name;
return 301 https://$name\$request_uri;
}
server {
listen 443;
listen [::]:443;
server_name $name;
ssl_certificate $4;
ssl_certificate_key $5;
root $WEB_ROOT_DIR/public;
index index.html index.php;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files \$uri \$uri/ =404;
}
access_log $WEB_ROOT_DIR/storage/logs/access.log;
error_log $WEB_ROOT_DIR/storage/logs/error.log;
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
" > $sitesAvailabledomain
else
echo "
server {
listen 80;
# listen 443;
server_name $name;
root $WEB_ROOT_DIR/public;
index index.html index.php;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files \$uri \$uri/ =404;
}
access_log $WEB_ROOT_DIR/storage/logs/access.log;
error_log $WEB_ROOT_DIR/storage/logs/error.log;
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
" > $sitesAvailabledomain
fi
echo -e $"\nNew Virtual Host Created\n"
ln -s $sitesAvailabledomain /etc/nginx/sites-enabled/
service nginx reload
echo "Done, please browse to https://$name to check!"
### Fix permission for laravel storage
if [ -d $WEB_ROOT_DIR/storage ]
then
echo "Fixing storage permission..."
/bin/chmod -R 775 $WEB_ROOT_DIR/storage
/bin/chmod -R 775 $WEB_ROOT_DIR/bootstrap/cache
/bin/chown -R git:www-data $WEB_ROOT_DIR/storage
/bin/chown -R git:www-data $WEB_ROOT_DIR/bootstrap/cache
echo "Storage permission fixed."
else
echo "Storage dir not present. Skip fixing permission."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment