Configure virtual hosts on CentOS
Thu Jan 26, 2017 · 545 words

About to host websites on linux server? Using Apache? Great. This article will show you how to do exactly that using Apache’s virtual hosts.

In Apache, you can use virtual hosts to direct http traffic for a given domain name to a particular directory (i.e. the root directory of the website for the domain in the request). This feature is commonly used to host multiple websites, but we recommend using it for every website on your server including the first.

Throughout this article, we’ll use an example domain – – but you should replace it with the domain name or subdomain you want to host on your server.

Install the Apache web server

To get Apache on your server, you can either install it as part of a LAMP stack, or you can install Apache by itself:

$ sudo yum update
$ sudo yum install httpd
$ sudo systemctl enable httpd.service
$ sudo service httpd start

Set up the virtual hosts

$ sudo mkdir -p /var/www/
$ sudo chown -R apache:apache /var/www/
# This lets Apache modify files in your web directories
$ sudo chmod -R 755 /var/www/

Create content for the website

$ sudo nano /var/www/
    <title>Welcome to!</title>
    <h1>Success! The virtual host is working!</h1>
Ctrl + o (Save File)
Ctrl + x (Exit)

Configure your virtual host directories

We’re going to copy a configuration usually used in Ubuntu/Debian and create two directories: one to store the virtual host files (sites-available) and another to hold symbolic links to virtual hosts that will be published (sites-enabled).

Create sites-available and sites-enabled directories

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

Edit your Apache configuration file

Edit the main configuration file (httpd.conf) so that Apache will look for virtual hosts in the sites-enabled directory.

sudo nano /etc/httpd/conf/httpd.conf
IncludeOptional sites-enabled/*.conf
# This way, we’re telling Apache to look for additional config files in the sites-enabled directory
Ctrl + o (Save File)
Ctrl + x (Exit)

Create virtual host file

We’re going to build it from a new file in your sites-available directory.

$ sudo nano /etc/httpd/sites-available/
<VirtualHost *:80>
    DocumentRoot /var/www/ 
    ErrorLog /var/www/ 
    CustomLog /var/www/ combined 

The lines ErrorLog and CustomLog are not required to set up your virtual host, but we’ve included them, in case you do want to tell Apache where to keep error and request logs for your site.

Ctrl + o (Save File)
Ctrl + x (Exit)
$ sudo ln -s /etc/httpd/sites-available/ /etc/httpd/sites-enabled/
$ sudo service httpd restart

back · posts · who is yuvi? · contact · home