This post is similar to Make and Unmake Sites, Automated with the difference that this is for a single-domain server created in Spinning up a single-domain web and email server.
Making a site in this version is a lot faster, as we already have the wildcard certificate for the domain we do not need to wait for CertBot to get a new one from Let’s Encrypt.
Let’s go over the sections of the script in detail first.
1 2 3 4 5 |
#!/bin/bash RED='\033[0;31m' GREEN='\033[0;32m' CYAN='\033[0;36m' NC='\033[0m' # No Color |
The above sets up our colors for the bash shell.
7 8 9 10 11 12 13 14 |
domain=`cat /root/domain` echo -e "${CYAN}Enter the subdomain for the new site on $domain: ${NC}" read subdomain fqdn="$subdomain.$domain" folder="/var/www/$fqdn" echo -e "${CYAN}Creating root folder $folder{NC}" mkdir "$folder" echo -e "${GREEN}Done.${NC}" |
We ask the user for the subdomain, and append that to the domain.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
echo -e "${CYAN}Creating index file at $folder/index.html${NC}" cat > "$folder/index.html" <<EOF <!doctype html> <html lang="en-us"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>$fqdn</title> <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css"> <style> html, body {background-color: #fff; color: #636b6f; font-family: 'Raleway', sans-serif; font-weight: 100; height: 100vh; margin: 0;} .full-height {height: 100vh;} .flex-center {align-items: center; display: flex; justify-content: center;} .position-ref {position: relative;} .top-right {position: absolute; right: 10px; top: 18px;} .content {text-align: center;} .title {font-size: 84px;} .links > a {color: #636b6f;padding: 0 25px;font-size: 12px;font-weight: 600;letter-spacing: .1rem;text-decoration: none;text-transform: uppercase;} .m-b-md {margin-bottom: 30px;} </style> </head> <body> <div class="flex-center position-ref full-height"> <div class="content"> <div class="title m-b-md">$fqdn</div> </div> </div> </body> </html> EOF chown www-data:www-data "$folder" echo -e "${GREEN}Done.${NC}" |
Create the index.html file and set the ownership of the new folder to the www-data
group.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
echo -e "${CYAN}Writing config files${NC}" cat > "/etc/apache2/sites-available/$fqdn.conf" <<EOF <VirtualHost *:80> DocumentRoot "$folder" ServerName $fqdn <Directory "$folder"> AllowOverride All allow from all Options +Indexes </Directory> RewriteEngine on RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost> EOF cat > "/etc/apache2/sites-available/$fqdn-le-ssl.conf" <<EOF <IfModule mod_ssl.c> <VirtualHost *:443> DocumentRoot "$folder" ServerName $fqdn <Directory "$folder"> AllowOverride All allow from all Options +Indexes </Directory> SSLCertificateFile /etc/letsencrypt/live/$domain/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/$domain/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf Header always set Strict-Transport-Security "max-age=31536000" </VirtualHost> </IfModule> EOF echo -e "${GREEN}Done.${NC}" |
Make the two config files for Apache. One for the http
connection, and the other for https
. The http
forwards to the https
, and we use HSTS
.
84 85 86 87 88 |
echo -e "${CYAN}Calling Apache2 to enable $fqdn${NC}" a2ensite "$fqdn.conf" a2ensite "$fqdn-le-ssl.conf" service apache2 reload echo -e "${GREEN}Done.${NC}" |
Finally we reload Apache to bring the new subdomain online.
Here is the full make-site.sh
file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#!/bin/bash RED='\033[0;31m' GREEN='\033[0;32m' CYAN='\033[0;36m' NC='\033[0m' # No Color domain=`cat /root/domain` echo -e "${CYAN}Enter the subdomain for the new site on $domain: ${NC}" read subdomain fqdn="$subdomain.$domain" folder="/var/www/$fqdn" echo -e "${CYAN}Creating root folder $folder{NC}" mkdir "$folder" echo -e "${GREEN}Done.${NC}" echo -e "${CYAN}Creating index file at $folder/index.html${NC}" cat > "$folder/index.html" <<EOF <!doctype html> <html lang="en-us"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>$fqdn</title> <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css"> <style> html, body {background-color: #fff; color: #636b6f; font-family: 'Raleway', sans-serif; font-weight: 100; height: 100vh; margin: 0;} .full-height {height: 100vh;} .flex-center {align-items: center; display: flex; justify-content: center;} .position-ref {position: relative;} .top-right {position: absolute; right: 10px; top: 18px;} .content {text-align: center;} .title {font-size: 84px;} .links > a {color: #636b6f;padding: 0 25px;font-size: 12px;font-weight: 600;letter-spacing: .1rem;text-decoration: none;text-transform: uppercase;} .m-b-md {margin-bottom: 30px;} </style> </head> <body> <div class="flex-center position-ref full-height"> <div class="content"> <div class="title m-b-md">$fqdn</div> </div> </div> </body> </html> EOF chown www-data:www-data "$folder" echo -e "${GREEN}Done.${NC}" echo -e "${CYAN}Writing config file $file${NC}" cat > "/etc/apache2/sites-available/$fqdn.conf" <<EOF <VirtualHost *:80> DocumentRoot "$folder" ServerName $fqdn <Directory "$folder"> AllowOverride All allow from all Options +Indexes </Directory> RewriteEngine on RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost> EOF cat > "/etc/apache2/sites-available/$fqdn-le-ssl.conf" <<EOF <IfModule mod_ssl.c> <VirtualHost *:443> DocumentRoot "$folder" ServerName $fqdn <Directory "$folder"> AllowOverride All allow from all Options +Indexes </Directory> SSLCertificateFile /etc/letsencrypt/live/$domain/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/$domain/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf Header always set Strict-Transport-Security "max-age=31536000" </VirtualHost> </IfModule> EOF echo -e "${GREEN}Done.${NC}" echo -e "${CYAN}Calling Apache2 to enable $fqdn${NC}" a2ensite "$fqdn.conf" a2ensite "$fqdn-le-ssl.conf" service apache2 reload echo -e "${GREEN}Done.${NC}" |
Removing the subdomain is super easy. We just delete the files and site root, after asking Apache to disable the subdomain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/bin/bash RED='\033[0;31m' GREEN='\033[0;32m' CYAN='\033[0;36m' NC='\033[0m' # No Color domain=`cat /root/domain` echo -e "${CYAN}Enter the subdomain for the site on $domain to DELETE: ${NC}" read subdomain fqdn="$subdomain.$domain" folder="/var/www/$fqdn" echo -e "${CYAN}Calling Apache2 to disable $fqdn${NC}" a2dissite "$fqdn.conf" unlink "/etc/apache2/sites-available/$fqdn.conf" a2dissite "$fqdn-le-ssl.conf" unlink "/etc/apache2/sites-available/$fqdn-le-ssl.conf" echo -e "${GREEN}Done.${NC}" echo -e "${CYAN}Restarting Apache2.${NC}" service apache2 reload echo -e "${GREEN}Done.${NC}" echo -e "${CYAN}Deleting root folder $folder${NC}" rm -rf "$folder" echo -e "${GREEN}Done.${NC}" |
The repository for this post can be found on GitLab here .
The repository for this post can be found on Lupe Code’s GitLab mirror here .