Add Virtual Host on CentOS 7 for your VPS or Dedicated Server for Apache

Introduction

How to add another domain to your Apache server ? It's easy.
In this guide we will cover the basics of how to add another virutal host to your VPS or Dedicated server.

First of all we must install Apache (only if it's not installed yet).

 

Step 1: Install Apache2.

sudo yum -y install httpd

When the installation is done we have to make our Apache server start automatically after server reboot. We can do that with the following command:

sudo systemctl enable httpd.service

 

Step 2: Create the Virtual Host configuration directories.

We must create 2 directories. These directories will hold our configuration files. So let's make them.

sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled

After we create these directories we are about to edit our Apache main configuration file. So we have to open the configuration file and edit it with:

sudo nano /etc/httpd/conf/httpd.conf

At it's end just add this line:

IncludeOptional sites-enabled/*.conf

Save the file and exit.

Step 3: Create the first Virtual Host file.

We have to create our first Virtual host file. In this example we will add example domain of example.com.

Now we have to create a configuration file for this virtual host by creating a new file like this:

sudo nano /etc/httpd/sites-available/example.com.conf

Note: Do not foget to add .conf at the end of command shown above. It is very important otherwise your Virtual host will not work.

We will have blank document opened. Now we have to add the following in it:

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/example.com/public_html
    ServerAlias example.com
    ErrorLog /var/www/example.com/error.log
    CustomLog /var/www/example.com/requests.log combined
</VirtualHost>

 

 Step 4: Enable the Virtual Host.

Before we enable it we must create a symbolic link for our new Virtual Host with the following command:

sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf

And now to enable the new configuration we must restart Apache. We can do that with this command:

sudo apachectl restart

Gongratulations!

Now you have your first Virtual Host for CentOS enabled. Enjoy and continue with other domains!

Kind regards,

VivasHost.com Team.

Share this post

Comments (0)

    There are no comments to this topic yet.


To post comments you must have account with VivasHost! To create an Account you must purchase any service you want.

Take a look at our VPS - Virtual Private Servers or our Dedicated Servers.