Setup Apache with Virtual Hosts on RHEL

The Apache HTTP server is a software that runs in the background (daemon), which primary role is to server web pages and other services to applications that connect to it, such as client web browsers. It was first developed to work with Linux/Unix operating systems. Apache has been the world’s most popular web server since the boom of the World Wide Web and open source in the mid 90’s.

This tutorial will walk through setting up two virtual hosts – two websites – on your RHEL6/7 server.

Install the Apache package

Use yum to install the apache httpd package:
yum install httpd

Edit the /etc/sysconfig/network to set FQDN:
NETWORKING=yes
HOSTNAME=nerdtron-test.testdomain.com

Set the hostname of the server:
hostname nerdtron-test.testdomain.com

Edit the /etc/hosts to set FQDN
10.10.222.128 nerdtron-test nerdtron-test.testsite.com

Create sample pages for the two virtual host

Go to the html document root and create folder for the domain folders
cd /var/www/html
mkdir firstsite.com secondsite.com

Create index.html for each site directory. This will serve as the main page for site which you can always change later. We’ll the below html pages for testing purposes.

For firstsite.com/index.html:
<HTML>
<HEAD>
<TITLE>
First Site : TestONE.com
</TITLE>
</HEAD>
<BODY>
<H1>First Site: TestONE.com</H1>
<P>This is the first site main page. TESTone.com</P>
</BODY>
</HTML>

For secondsite.com/index.html:
<HTML>
<HEAD>
<TITLE>
Second Site : TestTWO.com
</TITLE>
</HEAD>
<BODY>
<H1>Second Site: TestTWO.com</H1>
<P>This is the second site page. TestTWO.com</P>
</BODY>
</HTML>

Update httpd config for using vhosts

Edit the /etc/httpd/conf/httpd.conf file to include the vhosts config and directory:
NameVirtualHost *:80 ##### Required only on RHEL6 or below servers
Include /etc/httpd/conf.d/vhosts/*.vhost

Create the vhosts folder to save the configuration files for each website.
mkdir /etc/httpd/conf.d/vhosts
cd /etc/httpd/conf.d/vhosts

Create the vhosts entries for each domain, be sure the filenames end in .vhost
For firstsite.com.vhost file save the following. This will setup initial URL on which the server will respond and the appropriate log files to used for that domain. Also included are directives for site admin and server alias.

ServerAdmin [email protected]
DocumentRoot /var/www/html/firstsite.com/
ServerName firstsite.com
ServerAlias www.firstsite.com www2.teione.com
ErrorLog /var/log/httpd/www.firstsite.com-error.log
CustomLog /var/log/httpd/www.firstsite.com-access.log common

For secondsite.com.vhost file and save the following. This will setup initial URL on which the server will respond and the appropriate log files to used for that domain. Also included are directives for site admin and server alias.

ServerAdmin [email protected]
DocumentRoot /var/www/html/secondsite.com/
ServerName secondsite.com
ServerAlias www.secondsite.com
ErrorLog /var/log/httpd/www.secondsite.com-error.log
CustomLog /var/log/httpd/www.secondsite.com-access.log common

Now test the configuration of apache to see if you have correct syntax.
/etc/init.d/httpd configtest

If the syntax is OK, proceed to starting apache
/etc/init.d/httpd start

View running sites with corresponding virtual hosts:
httpd –S

Add apache service on startup:
chkconfig httpd on

Start testing the URL of the two websites by accessing the http://www.firstsite.com and http://www.secondsite.com
You can view the logs of the two website on /var/log/httpd/ directory.


– masterkenneth

Leave a Reply

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