Skip to content

Instantly share code, notes, and snippets.

@cooliscool
Last active November 8, 2025 14:44
Show Gist options
  • Select an option

  • Save cooliscool/d2ae364750ce17b27285a9ca6a1239cc to your computer and use it in GitHub Desktop.

Select an option

Save cooliscool/d2ae364750ce17b27285a9ca6a1239cc to your computer and use it in GitHub Desktop.
Add a new subdomain & Configure it for Apache2 & Get TLS certificate via LetsEncrypt certbot.
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Read site_name from stdin (supports piping or interactive prompt)
if [ -t 0 ]; then
read -rp "Enter site name (e.g. example.com): " site_name
else
read -r site_name
fi
TEMPLATE="/etc/apache2/site-template-prefix.conf"
SITES_DIR="/etc/apache2/sites-available"
CONF_FILE="${SITES_DIR}/${site_name}.conf"
echo "⏳ Stopping apache2 temporarily to enable certbot to create the files .."
sudo systemctl stop apache2
sudo certbot certonly --standalone -d "$site_name" --agree-tos --email [email protected] --non-interactive
echo "⏳ Copying template to ${CONF_FILE}…"
sudo cp "$TEMPLATE" "$CONF_FILE"
echo "⏳ Appending SSL directives to ${CONF_FILE}…"
{
echo "ServerName ${site_name}"
echo "SSLCertificateFile /etc/letsencrypt/live/${site_name}/fullchain.pem"
echo "SSLCertificateKeyFile /etc/letsencrypt/live/${site_name}/privkey.pem"
echo "</VirtualHost>"
echo "</IfModule>"
} | sudo tee -a "$CONF_FILE" > /dev/null
echo "⏳ Enabling site ${site_name}.conf…"
sudo a2ensite "${site_name}.conf"
echo "⏳ Reloading Apache…"
sudo systemctl reload apache2 || sudo systemctl start apache2
echo "⏳ Final Re-installing Cert for getting the site up and running…"
sudo certbot --apache -d "$site_name" -n
echo "✅ All done. ${site_name}.conf is live with SSL!"
<IfModule mod_ssl.c>
<VirtualHost *:443>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options -Indexes
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
#ErrorLog ${APACHE_LOG_DIR}/aaa.error.log
#CustomLog ${APACHE_LOG_DIR}/aaa.access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Include /etc/letsencrypt/options-ssl-apache.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment