Configure virtual hosts on Apache2

This configuration supposes the file configuration of the website is already done. Else, follow this tutorial to install the WordPress with Apache.

Note : This tutorial uses a new version of Apache so the old http.conf does not exist in this one.

Configuration of the domain connected to the server :

1. Go to
cd /etc/apache2/sites-availables

2. In this folder, a 000-default.conf exists. Duplicate and rename this file
cp /etc/apache2/sites-availables/000-default.conf /etc/apache2/sites-availables/domain_name.conf

3. Open the new file
nano domain_name.conf

4. Edit the file this way :
<VirtualHost *:80>
DocumentRoot /var/www/path_to_the_wordpress_configuration
ServerName domain_name.ext
ServerAlias *.domain_name.ext
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note : ServerAlias is used to configure the subdomains. In this configuration any word placed before the .domain_name.ext will redirect to the main domain.

5. Activate the new configuration
a2ensite domain_name.conf

6. Reload Apache
systemctl reload apache2

If the domain has an SSL configuration :

1. Follow the previous steps

2. Edit the file domain_name.conf :
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/path_to_the_wordpress_configuration
ServerName domain_name.ext
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/path_to_the_certificate/fullchain.pem
SSLCertificateKeyFile /etc/paths_to_the_private_key/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Note : Other files can be provided

3. Reload Apache
systemctl reload apache2

If the server hosts multiple domain names, a new file with the extension .conf should be created in the /etc/apache2/sites-available. The second domain has the same configuration, only the values in italics have to be changed.