Step-by-step Guide to Creating Apache Virtual Hosts on CentOS 7

Before hosting multiple domains on your server, you should create the appropriate hosts on the web server. This way, different content will be delivered for different types of requests in a matter of seconds. Then, carefully study the guide below and learn the specifics of making Apache virtual hosts on CentOS 7.

1. Install Apache

The first step you need to take is to make sure the VPS has root access. A server with an SSH connection will work as well. Hostinger login credentials are usually located in hPanel. The tab you need is called Servers.

1. To install Apache on your device with CentOS 7, you should enter this command:

step-by-step guide to creating apache virtual hosts on centos 7

2. Once installed, type the below command. It will enable Apache as a CentOS Service:

step-by-step guide to creating apache virtual hosts on centos 7

3. After completing the above steps, it is recommended to visit the IP address of your server. Make sure Apache is running successfully. Here's what the page should look like:

step-by-step guide to creating apache virtual hosts on centos 7

2. Generate a Directory Tree

1. The purpose of generating a directory tree is to securely store website data. The first thing you need to do is specify /var/www as your working directory. Here is the command to run:

step-by-step guide to creating apache virtual hosts on centos 7

2. Each virtual host requires an original document root:

step-by-step guide to creating apache virtual hosts on centos 7

Be sure to change yourdomain.com to your real domain name.

3. The directory must be accessible to Apache. You can change the ownership using chown. And chmod will allow you to set the right permissions for the entire web directory.

step-by-step guide to creating apache virtual hosts on centos 7

3. Set Up a Demo Page

Apache virtual hosts should have a demo page. This will allow you to check if the host is operating properly. Then, follow the steps described in the paragraphs below.

1. An index.html file can be easily created in the nano editor. Just use yourdomain.com/public_html directory:

step-by-step guide to creating apache virtual hosts on centos 7

2. Be sure to paste this content into the file:

step-by-step guide to creating apache virtual hosts on centos 7

3. Press CTRL + X and then Y to save the file.

4. Create the Virtual Host

1. A virtual host .conf file can be created in the Apache config directory:

step-by-step guide to creating apache virtual hosts on centos 7

2. Don't forget to include the following content in the generated .conf file:

step-by-step guide to creating apache virtual hosts on centos 7

The above content is kind of a message that the virtual host name is yourdomain.com. It also indicates that port 80 is being used for the connection. In addition, Apache obtains information about a document root and various error logs.

3. After completing all the above steps, it is necessary to restart Apache. Otherwise, the changes will not take effect:

step-by-step guide to creating apache virtual hosts on centos 7

So, you have completed the creation of an Apache virtual host for your domain! Now you need to access the host. If you see the message “It works!” from the demo page, be sure that everything is done correctly.

Concluding Remarks

Therefore, creating an Apache virtual host is a simple task if you follow clear steps. Let's list them once again:

  1. Apache installation;
  2. Creation of a directory tree, the purpose of which is to securely store your site's files;
  3. Creation of a demo page that allows checking the correct operation of the Apache virtual host;
  4. Creation of a virtual host by generating configuration files in the directory.

Start creating virtual hosts by following the above steps, and be sure you will succeed!



Blog