Skip to content

Instantly share code, notes, and snippets.

@KhaledLela
Created November 8, 2021 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KhaledLela/946412cc76a2488c3b61bdaece3f30dd to your computer and use it in GitHub Desktop.
Save KhaledLela/946412cc76a2488c3b61bdaece3f30dd to your computer and use it in GitHub Desktop.
Create nginx site with SSL using script
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.site.com;
index index.html index.htm;
root /var/www/sites/app/production;
error_log /var/log/nginx/example.site.com_error.log;
access_log /var/log/nginx/example.site.com_access.log;
# Include site location config with regex handler.
include /etc/nginx/conf/site_locations.conf;
# expires handler from nginx config.
include /etc/nginx/conf/expires.conf;
# CSS and Javascript
# location ~* \.(?:css|js)$ {
# expires 1y;
# access_log off;
# add_header Cache-Control "public";
# }
}
server {
listen 80;
listen [::]:80;
server_name example.site.com www.example.site.com;
return 301 https://example.site.com$request_uri;
}
# Include site_maps that contains language variable mapping.
include /etc/nginx/conf/site_maps.conf;
#!/bin/bash
helpFunction()
{
echo ""
echo "Usage: $0 -d DOMAIN -n NAME"
echo "\t-d Root domain"
echo "\t-n Name/subdomain"
exit 1 # Exit script after printing help
}
while getopts ":d:n:" opt;
do
case "$opt" in
d ) DOMAIN="$OPTARG";;
n ) NAME="$OPTARG";;
? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done
# Print helpFunction in case parameters are empty
if [ -z "$DOMAIN" ] || [ -z "$NAME" ]
then
echo "Missing params";
helpFunction
fi
cd /etc/nginx/sites-available/
if [ -f "$NAME.$DOMAIN" ]; then
echo "$NAME.$DOMAIN already exists."
exit 1 # Exit script.
fi
cp example.site.com $NAME.$DOMAIN
sed -i "s/example.site.com/$NAME.$DOMAIN/g" $NAME.$DOMAIN
cd /etc/nginx/sites-enabled/
ln -s ../sites-available/$NAME.$DOMAIN
# Handle SSL certificate.
certbot --nginx --non-interactive -d "$NAME.$DOMAIN" -d "www.$NAME.$DOMAIN";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment