1. Home
  2. VPS
  3. How to Create Apache Virtual Hosts on CentOS 7: A Beginner's ...

How to Create Apache Virtual Hosts on CentOS 7: A Beginner's ...

Rsdaa 03/12/2021 123

How to Create Apache Virtual Hosts on CentOS 7How to Create Apache Virtual Hosts on CentOS 7: A Beginner's ...

If you want to host more than one domain on your server, you need to create corresponding hosts on the webserver. That way, your server can deliver different content for different requests. In this tutorial, you will learn how to create Apache virtual hosts on CentOS 7.

1. Installing Apache

Before we begin, make sure that you have root access to your VPS or server using SSH connection. In Hostinger, the login credentials are located in the Servers tab of hPanel.

Install Apache on your CentOS 7 machine by typing the following command:sudo yum -y install httpdOnce the installation is completed, enable Apache as a CentOS service:sudo systemctl enable httpd.serviceVisit your server’s IP address to check whether Apache is already running or not. The page should look like this:

2. Creating Directory Tree

A directory tree is used to hold website data. First, set the working directory to /var/www by running this command:cd /var/www/You should use a unique document root for each virtual host:mkdir -p yourdomain.com/public_html

Remember to replace yourdomain.com with your actual domain name.

Make the directory accessible to Apache. Run chown to change the ownership and chmod to set correct permissions for the whole web directory.chown -R apache:apache /var/www/yourdomain.com/public_htmlchmod -R 755 /var/www

Apache now has the required access to create additional directories and serve content for incoming queries.

3. Creating a Demo Page

It is recommended to make a demo page for your Apache virtual hosts. This way, you can check whether the host is working before you actually move your website files. Here’s how you do it:

Use the nano editor to create index.html file in yourdomain.com/public_html directory:nano yourdomain.com/public_html/index.htmlPaste the following content to the file:This is a test page

It works!

Save the file by pressing CTRL + X and then Y.

4. Creating the Virtual Host

Create a new virtual host .conf file in the Apache configuration directory:nano /etc/httpd/yourdomain.com.confInsert the following content into the .conf file:ServerName www.yourdomain.comServerAlias yourdomain.comDocumentRoot /var/www/yourdomain.com/public_htmlErrorLog /var/www/yourdomain.com/error.logCustomLog /var/www/yourdomain.com/requests.log combined

In the example above, we tell Apache that we will be using port 80 for the communication and that yourdomain.com is the name of the virtual host. Additionally, we also specify directories for the website files (document root) and error logs.

Restart Apache for the changes to take effect:systemctl restart httpd.service

That’s it, you have just created an Apache virtual host for your domain! Now try to access the host and you should see the “It works!” text from the demo page we made earlier.

Conclusion

You have learned how to create an Apache virtual host in four easy steps. To summarize, let’s take a look at them once again:

Install apache from CentOS 7.Create a directory tree that will be used to hold all your website files.Make a demo page to see if Apache virtual host is working properly.Create the virtual host by making configuration files in the Apache directory.

We hope this tutorial is useful. Feel free to comment below if you have any questions!

Edgaras is a veteran server administrator at Hostinger. He makes sure that every server runs at full throttle and has all the latest technological advancements. When he's not working, Edgaras enjoys skiing and exploring the world.


PREV: 3 Types of Firewalls You Should Be Aware of - CyberInternAcademy

NEXT: The 5 Different Types of Firewalls Explained

Popular Articles

Hot Articles

Navigation Lists

Back to Top