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

Posted on

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

Before hosting multiple domains on your server, creating virtual hosts on the web server is crucial. This allows for serving different content based on incoming requests. This guide provides a step-by-step approach to configuring Apache virtual hosts on CentOS 7.

1. Install Apache

Ensure you have root access to your VPS. An SSH connection is also necessary. Hostinger login details can usually be found in hPanel under the Servers tab.

1. Use the following command to install Apache on your CentOS 7 server:

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

2. Enable Apache as a CentOS service by executing this command:

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

3. Confirm Apache is running correctly by visiting your server’s IP address in a web browser. You should see a default Apache welcome page, similar to this:

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

2. Generate a Directory Tree

1. A directory tree ensures secure storage of website data. Begin by setting /var/www/ as your working directory. Run this command:

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

2. Each virtual host requires a unique document root. Create a directory for your domain:

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

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

3. Ensure Apache can access the directory. Change the ownership using chown and set appropriate permissions with chmod:

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

3. Set Up a Demo Page

A demo page confirms the virtual host is configured correctly. Follow these steps:

1. Create an index.html file within the domain’s public_html directory using the nano editor:

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

2. Paste the following HTML content into the index.html file:

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

3. Save the file by pressing CTRL + X, then Y.

4. Create the Virtual Host

1. Create a virtual host configuration file (.conf) in the Apache configuration directory:

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

2. Add the following configuration to the yourdomain.com.conf file, modifying it with your actual domain name:

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

This configuration defines the virtual host for yourdomain.com, specifying the port (80), document root, and error logs.

3. Restart Apache to apply the changes:

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

You have now created an Apache virtual host for your domain! Access the domain in your web browser. If you see the “It works!” message from the demo page, the configuration is successful.

Concluding Remarks

Creating Apache virtual hosts is straightforward when following a clear process. These are the steps:

  1. Apache installation.
  2. Creation of a directory tree for website data security.
  3. Creation of a demo page to verify virtual host functionality.
  4. Creation of a virtual host configuration file.

Follow these steps, and successfully create your virtual hosts!


Blog

Leave a Reply

Your email address will not be published. Required fields are marked *