Sunday, November 22, 2026
TR
Linux

Automatic SSL Certificates and Apache VirtualHost Configuration on a cPanel Web Server

Automatic SSL Certificates and Apache VirtualHost Configuration on a cPanel Web Server
Contents
  1. 1. What the Bash Script Does
  2. 2. How the Script Works
  3. 3. Installing and Running the Script
  4. 4. Setting It Up as a Scheduled Task with Crontab
  5. Granting Apache Extra Access to Files and Directories (Using ACLs)
  6. 5. Conclusion

In this article, we will walk step by step through the use of a Bash script that obtains an SSL certificate from Let’s Encrypt on a web server and configures an Apache VirtualHost for the relevant domain. The script automates steps such as adding a new domain, obtaining an SSL certificate and creating the VirtualHost configuration.

To run a project on a cPanel server for a domain name that is pointed to it externally, and to obtain an SSL certificate, without creating an account in cPanel, you need to follow the steps below.

1. What the Bash Script Does

The script above aims to obtain SSL certificates for newly added domain names and to add these domain names to Apache VirtualHost configurations and enable them. It also restarts the Apache server after each operation.

2. How the Script Works

a. Reading the Domain List

The script reads domain names from the /etc/domains.txt file. The domains in this file are processed, with one domain name per line:

DOMAINS_FILE="/etc/domains.txt"

b. Defining the Log File

A log file is defined to record errors that may occur during certificate issuance and VirtualHost configuration:

LOG_FILE="/var/log/certbot-auto.log"

c. Obtaining an SSL Certificate with Certbot

The certbot command is used to obtain an SSL certificate from Let’s Encrypt for each domain. It checks whether a certificate has already been issued and, if not, obtains one:

CERTBOT_CMD="/usr/bin/certbot"
if $CERTBOT_CMD certificates | grep -q "$DOMAIN"; then
echo "$(date) - An SSL certificate already exists for $DOMAIN." >> "$LOG_FILE"
else
echo "$(date) - Obtaining SSL certificate for $DOMAIN..." >> "$LOG_FILE"
$CERTBOT_CMD certonly --webroot -w "$WEBROOT_PATH" -d "$DOMAIN" -d "www.$DOMAIN" --non-interactive --agree-tos --email "$EMAIL" >> "$LOG_FILE" 2>&1
fi

d. Apache VirtualHost Configuration

The script creates a separate VirtualHost configuration file for each domain and configures it to support both HTTP and HTTPS connections. HTTP requests arriving on port 80 are redirected to HTTPS on port 443:

conf_file="${apache_available}/${DOMAIN}.conf"
cat <<EOL > "$conf_file"
<VirtualHost 192.168.100.100:80>
ServerName $DOMAIN
ServerAlias www.$DOMAIN
DocumentRoot $WEBROOT_PATH
ServerAdmin root@sv1.server.com

# Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]</VirtualHost>

<VirtualHost 192.168.100.100:443>
ServerName $DOMAIN
ServerAlias www.$DOMAIN
DocumentRoot $WEBROOT_PATH
ServerAdmin root@sv1.server.com

# SSL settings
SSLEngine on
SSLCertificateFile $ssl_dir/$DOMAIN/fullchain.pem
SSLCertificateKeyFile $ssl_dir/$DOMAIN/privkey.pem
</VirtualHost>
EOL

e. Enabling the Apache Configuration

Each newly created VirtualHost configuration in Apache is enabled using the a2ensite command, and Apache is restarted afterwards:

sudo a2ensite "${DOMAIN}.conf"
sudo systemctl restart httpd

3. Installing and Running the Script

You can follow the steps below to run the script:

a. Creating the Script File

First, create a file in the directory where the script will live and paste the script above into it. For example, let's save the script under /usr/local/bin:

sudo nano /usr/local/bin/auto_ssl.sh

After pasting the script into the file, save it.

b. Granting Executable Permissions

Use the following command to make the script executable:

sudo chmod +x /usr/local/bin/auto_ssl.sh

c. Running the Script

You can use the following command to run the script:

sudo /usr/local/bin/auto_ssl.sh

This command will obtain SSL certificates for all domain names in the domains.txt file and set up the Apache configurations.

4. Setting It Up as a Scheduled Task with Crontab

If you want to run this script automatically every 6 hours, you can add a crontab job. Edit crontab by running the following command in the terminal:

sudo crontab -e

Add the following line to make the script run every 6 hours:

0 */6 * * * /bin/bash /usr/local/bin/auto_ssl.sh >> /var/log/server.log 2>&1

Granting Apache Extra Access to Files and Directories (Using ACLs)

If you want Apache to be able to access certain directories but want to keep the directory at 750 permissions, you can grant Apache extra permission using ACLs (Access Control Lists).

  1. Check whether ACL support is enabled: mount | grep acl If it is not enabled, you may need to add it to the /etc/fstab file.
  2. Open the user directory to the Apache user using ACL: setfacl -m u:nobody:rx /home/user/public_html If the Apache user is apache: setfacl -m u:apache:rx /home/user/public_html With this command you grant Apache read and execute permission.
  3. Restart Apache: systemctl restart httpd

5. Conclusion

In this article, we examined in detail a Bash script created to obtain Let’s Encrypt certificates on a web server and automate Apache VirtualHost configurations. This script simplifies operations on your server, allowing domain names to be added and secured quickly.

If you run into problems, you can check the Apache error logs or the Let’s Encrypt log file for more information:

sudo tail -f /var/log/httpd/error_log
sudo tail -f /var/log/certbot-auto.log

Click here for the full bash script.

Kerim Koç

Systems and infrastructure engineer. I write about Linux, open source, high availability and security.