I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

Set up Apache Virtualhosts on CentOS 8

- Advertisement -

Hello, friends. In this post, we will show you how to set up Apache virtualhosts on CentOS 8 / Rocky Linux 8.

It is normal that on the same server, we have several websites running. Each one of them needs a specific configuration because the needs of each one of them may vary. That is why it is necessary to create virtualhosts.

These virtualhosts allow having many websites running within the same server. Besides, it is the best way to configure them.

This is the objective of this post, to show you how to do it and in the best way. Let’s go for it.

Install Apache web server on CentOS 8 / Rocky Linux 8

Let’s start from the basics. First, we will install Apache web server.

sudo dnf update
sudo dnf install httpd

Once you have installed it, we have to start it.

sudo systemctl start httpd

Normally, it is enabled to be started along with the system.

sudo systemctl status httpd
- Advertisement -

And finally, check the status for any errors.

sudo systemctl status httpd

Most likely, you have an active firewall. In this case, we have to open ports 80 and 443.

sudo firewall-cmd --add-port=80/tcp
sudo firewall-cmd --add-port=443/tcp

Finally, restart the firewall to apply the changes.

sudo firewall-cmd --reload

Now this is enough.

Creating Apache virtualhosts on CentOS 8

The recommended way to create virtualhosts in CentOS 8 is to create a specific folder for each one of them. The name of the folder is not relevant, but it is convenient to use a name that you can quickly identify.

In this case, we will use as domain but it can be yours.

First create the folder:

sudo mkdir /var/www/

Thereafter, make Apache the owner of the folder so that everything can work fine.

sudo chown apache:apache /var/www/

In some cases, you may want to assign special permissions to the folder.

sudo chmod -R 755 /var/www/

Now you have to create a new configuration file. This file has to be in the /etc/httpd/conf.d/ folder. The name doesn’t matter much either, but it will be descriptive.

For example:

sudo nano /etc/httpd/conf.d/

In this file, you have to add all the Apache configuration for this site. For now, just add the following.

<virtualhost *:80>
DocumentRoot /var/www/

ErrorLog /var/log/httpd/
CustomLog /var/log/httpd/ combined

Everything is important, but ServerName defines the domain name; The DocumentRoot directive defines the path where our site is; If you want several domains to point to the site you can do it with the ServerAlias directive and separating by commas each one of them.

Save the changes and close the editor.

To apply the changes, restart the web server

sudo systemctl restart httpd

Accessing the new virtualhost

Now create an HTML file for testing. For example,

sudo nano /var/www/

And add some content like this

Hi, welcome to unixcop

Save the changes and close the editor.

Now open a web browser and visit and you will see your website working.

1.- Setup Apache Virtualhosts on CentOS 8 / Rocky Linux 8
1.- Setup Apache Virtualhosts on CentOS 8 / Rocky Linux 8

Enjoy it.


In this post, you learned how to configure Apache Virtualhosts on CentOS 8 / Rocky Linux 8 easily. In addition to this, we have shown you how to do it in the best way.


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook