March 15, 2014

HTTP Configure a Virtual Host

Introduction

Virtual hosts are good when you want to server multiple web sites from the same web server.

Prerequisite

Install Apache Web Server.

$ yum install httpd

Since I do not have a reliable DNS, we need to hardcode the host name in /etc/hosts on the client.

$ cat /etc/hosts
...
192.168.122.20 jbossas1.magnuskkarlsson.com
192.168.122.20 virtualhost1.magnuskkarlsson.com

Initial Configuration

Configure Listen and ServerName, to be able to start the server clean without warnings.

$ egrep "Listen|ServerName" /etc/httpd/conf/httpd.conf
Listen 192.168.122.20:80
ServerName jbossas1.magnuskkarlsson.com:80

Also verify that firewall (iptables) is configured or stop it 'service iptables stop'.

$ iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
...
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:443 
...
8        3   180 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:80 
...

Now check our installation by restarting apache web server and open url 'jbossas1.magnuskkarlsson.com' from client machine. You should be able to see Apache Welcome Page. If not go back and fix.

$ service httpd restart

Configure Name-Based Virtual Hosting

Now create two virtual host, one for the default host 'jbossas1.magnuskkarlsson.com' and one new 'virtualhost1.magnuskkarlsson.com'.

$ vi /etc/httpd/conf/httpd.conf 
...
NameVirtualHost 192.168.122.20:80

<VirtualHost 192.168.122.20:80>
    ServerName jbossas1.magnuskkarlsson.com
    ServerAdmin webmaster@jbossas1.magnuskkarlsson.com
    DocumentRoot /var/www/html
    ErrorLog logs/jbossas1.magnuskkarlsson.com-error_log
    CustomLog logs/jbossas1.magnuskkarlsson.com-access_log common
</VirtualHost>

<VirtualHost 192.168.122.20:80>
    ServerName virtualhost1.magnuskkarlsson.com
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/virtualhost1-magnuskkarlsson-com/html
    ErrorLog logs/virtualhost1.magnuskkarlsson.com-error_log
    CustomLog logs/virtualhost1.magnuskkarlsson.com-access_log common
</VirtualHost>

Now create the new document root for 'virtualhost1.magnuskkarlsson.com'.

$ mkdir -p /var/www/virtualhost1-magnuskkarlsson-com/html

Set file permissions.

$ chmod 755 /var/www/virtualhost1-magnuskkarlsson-com
$ chmod 755 /var/www/virtualhost1-magnuskkarlsson-com/html

Create test web page.

$ echo "

Hello World

" > /var/www/virtualhost1-magnuskkarlsson-com/html/index.html

Set SELinux for our new virtual host.

$ restorecon -RFv /var/www/

Test

Restart apache web server and test your new virtual host by accessing:

  1. http://virtualhost1.magnuskkarlsson.com/ (you should see default apache web server welcome page)
  2. http://jbossas1.magnuskkarlsson.com/ (you should see your 'Hello World' page)

No comments: