How to Configure Apache Virtual Hosts on CentOS 8 (Step-by-Step Guide)
Apache allows you to host multiple websites on a single server using name-based virtual hosts. Each virtual host can have its own document root, log files, and configuration settings, enabling better isolation and flexible site management without requiring additional 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.
Creating Document Root Directories
Create a folder for each website in the /var/www/.
mkdir -p /var/www/domain-name.com/public_htmlMake 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.confInsert 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.comSave and close the file, restart the service.
systemctl restart httpdApache 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 "<h1>My first Apache virtual host</h1>" > /var/www/domain-name.com/public_html/index.htmlCommon Issues with Apache Virtual Hosts
Common issues include incorrect document root paths, missing permissions, and DNS records not pointing to the server IP. Always verify Apache configuration and domain DNS settings when troubleshooting.
Conclusion
Apache virtual hosts provide a flexible and efficient way to host multiple websites on a single CentOS 8 server. By using name-based virtual hosts, you can isolate site configurations, manage logs separately, and scale your web hosting environment without additional IP addresses.
700
300
700
300
700
300