Customized Apache host files I'm using customized Apache host files for my Digitalocean websites. Please review the code, whether it's following the good standards or not, and please mention if I'm missing something important. Here is the host file template that I'm using for all my websites (on Ubuntu 16.04):



&lt;VirtualHost *:80&gt;
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
&lt;/VirtualHost&gt;

&lt;VirtualHost *:443&gt;
    ServerName www.example.com

    SSLEngine on
    SSLCertificateFile /home/username/certs/example.com.pem
    SSLCertificateKeyFile /home/username/certs/example.com.key

    Redirect permanent / https://example.com/
&lt;/VirtualHost&gt;

&lt;VirtualHost *:443&gt;
    ServerName example.com
    DocumentRoot /var/www/example.com/public_html

    SSLEngine on
    SSLCertificateFile /home/username/certs/example.com.pem
    SSLCertificateKeyFile /home/username/certs/example.com.key
&lt;/VirtualHost&gt;

&lt;Directory /var/www/example.com/public_html/&gt;
    Options +ExecCGI
            Options FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
&lt;/Directory&gt;
You don't actually need ServerName and ServerAlias directives for your port 80. This is due to the fact that your are using port-based virtual host. The "*" in the  tag indicates that all incoming connection to port 80 will follow the rules set with within the   tags. However, it is better to keep logs about incoming connections and errors when redirecting. 

Therefore, for your port 80, the configuration should be like this:

&lt;VirtualHost *:80&gt;

Redirect permanent / https://example.com/

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

&lt;/VirtualHost&gt;


For port 443, the Redirect directive you put there will cause a redirect loop and you will need to remove that. Furthermore, you will need to include the CGI path within the  tags.
It is recommended that you put the CGI files outside of the DocumentRoot for security reasons (malicious visitors might reveal your code and attempt to do something bad). In this case let's say your CGI executables are located under the directory /usr/local/cgi-bin/ . Following that You will need to add the ScriptAlias directive to indicate the path to the CGI directory. Lastly, you need the AddHanlder directive to tells Apache to permit the execution of CGI files. 

Remember to keep the logs for HTTPS connection too.

So here you go the configuration for port 443:

&lt;VirtualHost *:443&gt;

    ServerName example.com
    DocumentRoot /var/www/example.com/public_html

    SSLEngine on
    SSLCertificateFile /home/username/certs/example.com.pem
    SSLCertificateKeyFile /home/username/certs/example.com.key

    ErrorLog ${APACHE_LOG_DIR}/https_error.log
    CustomLog ${APACHE_LOG_DIR}/https_access.log combined

    ScriptAlias "/cgi-bin/" "/usr/local/cgi-bin/"

    &lt;Directory "/usr/local/cgi-bin/"&gt;
         Options +ExecCGI
         AddHandler cgi-script .cgi .pl
    &lt;/Directory&gt;

&lt;/VirtualHost&gt;


The tutorial section located under the Apache2.4 Documentation will give you a fairly complete overview for this.