Skip to content

Instantly share code, notes, and snippets.

@maffinca69
Created August 28, 2020 20:27
Show Gist options
  • Save maffinca69/4c5e8612cb69790f7a8efe28fb796860 to your computer and use it in GitHub Desktop.
Save maffinca69/4c5e8612cb69790f7a8efe28fb796860 to your computer and use it in GitHub Desktop.
Automatic addition of new domains. Also ssl is supported using certbot. All config (security files, etc.) is nginxconfig.io with no modifications
#!/bin/bash
domain=$1
root="/var/www/$domain"
# Clear old config if exists
rm "/etc/nginx/sites-available/$domain.conf" || true
rm "/etc/nginx/sites-enabled/$domain.conf" || true
rm -rf $root || true
block="/etc/nginx/sites-available/$domain.conf"
# Creting domain folder
sudo mkdir "/var/www/$domain"
# Create the Nginx server block file:
sudo tee $block > /dev/null <<EOF
# HTTP to HTTPS redirect
server {
listen 80;
server_name $domain;
return 301 https://$domain\$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name $domain;
root $root;
# security (can be replaced with your config)
include nginxconfig.io/security.conf;
# index file
index index.php index.html;
# index.php fallback
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
# handle .php
location ~ \.php$ {
include nginxconfig.io/php_fastcgi.conf;
}
# additional config (can be replaced with your config)
include nginxconfig.io/general.conf;
}
EOF
# Link to make it available
sudo ln -s $block /etc/nginx/sites-enabled/
# Test configuration and reload if successful
sudo nginx -t && sudo service nginx reload
# Creating test file
touch "$root/index.php"
echo "$domain is working!!!" > "$root/index.php"
# SSL
certbot -d $domain
@maffinca69
Copy link
Author

After uploading script to server you need set rules:
sudo chmod +x domain.sh

Using
./domain.sh example.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment