27.06.2025

How to Configure Apache Virtual Hosts on CentOS 8

Apache enables you to host multiple domains or websites on a single server using just one IP address, thanks to its powerful virtual host mechanism. Virtual hosts allow you to define separate configurations for each domain — including document root, log files, security rules, and more. This approach is ideal for web developers, hosting providers, or anyone managing multiple projects, as it provides isolation, flexibility, and efficient resource utilization without the need for multiple physical servers or IP addresses.

In the Serverspace you can create a server with already installed app "Apache".

In this tutorial, we will configure Apache virtual hosts. We recently installed the lamp stack on CentOS 8. Start by installing Apache and continue with this guide.

Create folders

Create a folder for each website in the /var/www/.

mkdir /var/www/domain-name.com

Make apache user the owner of this folder.

chown apache:apache /var/www/domain-name.com/

Configuration files for virtual hosts

Apache works with all files with the .conf extension from the /etc/httpd/conf.d/ folder. Create a configuration file for your site.

nano /etc/httpd/conf.d/domain-name.com.conf

Insert the following lines there. Replace domain-name.com with your domain name.

<virtualhost *:80>
ServerName domain-name.com
ServerAlias www.domain-name.com
DocumentRoot /var/www/domain-name.com
ErrorLog /var/log/httpd/domain-name.com-error.log
CustomLog /var/log/httpd/domain-name.com-access.log combined
</virtualhost>

If you want multiple domain names to work with the same content, just list them separated by a space in the ServerAlias line.

ServerAlias www.domain-name.com domain-name2.com domain-name3.com

Save and close the file, restart the service.

systemctl restart httpd

Apache virtual host testing

To check the operation of the virtual host, place an index.html file with some content in the site's home directory.

echo "<My first Apache virtual host>" > /var/www/domain-name.com/index.html

Open the server's domain name or IP address in the browser. You will see the contents of the index.html file.