First, ensure that Apache is installed on your CentOS Stream 9 system.
Virtual Hosts are defined within configuration files with a .conf extension
Create a new file with your text editor
vim /etc/httpd/conf.d/mydomain.com.conf
It’s recommended to create a new file for each domain to keep things organized.
Port 80 is the default HTTP port
Port 443 is the default HTTPS port
In this file, you’ll set up a block for your domain:
<VirtualHost *:80> ServerName mydomain.com ServerAlias www.mydomain.com DocumentRoot /var/www/mydomain ErrorLog /var/log/httpd/mydomain-error.log </VirtualHost> <VirtualHost *:443> ServerName mydomain.com ServerAlias www.mydomain.com DocumentRoot /var/www/domain SSLEngine On SSLCertificateFile /path/to/ssl/cert.crt SSLCertificateKeyFile /path/to/ssl/key.key </VirtualHost>
Check for syntax errors after configuring your virtual hosts
apachectl configtest
If you get a ‘Syntax OK’ output, restart apache to apply the changes:
systemctl restart httpd
Setup local hosts file
If you’re testing your configuration locally, edit your /etc/hosts file to point the domain to your local server
127.0.0.1 mydomain.com www.mydomain.com
Let’s Encrypt is a Certificate Authority (CA) and provides a free way to obtain certificates.
For more information on securing your virtual host with an SSL certificate from Let’s Encrypt
https://letsencrypt.org/getting-started
If you’re on a Mac or Linux computer, with administrative privileges you can easily preview your website on the local network. This helps if your domain is not yet registered, not pointed to a hosting account, or is still propagating.
vim /etc/hosts
Your_Server_IP domain.com