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
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 fbi.unixcop.com
but it can be yours.
First create the folder:
sudo mkdir /var/www/fbi.unixcop.com
Thereafter, make Apache the owner of the folder so that everything can work fine.
sudo chown apache:apache /var/www/fbi.unixcop.com
In some cases, you may want to assign special permissions to the folder.
sudo chmod -R 755 /var/www/fbi.unixcop.com
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/fbi.unixcop.com.conf
In this file, you have to add all the Apache configuration for this site. For now, just add the following.
<virtualhost *:80>
ServerName fbi.unixcop.com
DocumentRoot /var/www/fbi.unixcop.com
ErrorLog /var/log/httpd/fbi.unixcop.com-error.log
CustomLog /var/log/httpd/fbi.unixcop.com-access.log combined
</virtualhost>
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/fbi.unixcop.com/index.html
And add some content like this
<html>
<body>
Hi, welcome to unixcop
</body>
</html>
Save the changes and close the editor.
Now open a web browser and visit http://fbi.unixcop.com
and you will see your website working.
Enjoy it.
Conclusion
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.