We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsHosting ServicesCloud Servers

How to Create Virtual Host on Apache?

Germany Cloud Servers
Brazil VPS
Linux KVM Hosting
Windows Cloud Server
KVM VPS
Ubuntu VPS
Linux Cloud Server
cPanel KVM
USA VPS
OpenVZ VPS

Apache is an open source web server software, developed by Apache Software Foundation. It is the most popular web server software in the world – almost 67% of servers are running their web servers using Apache. Apache HTTP server supports two types of virtual hosting: IP-Based and Name-Based.

IP-Based virtual host allows users to point different domain names (websites) to different IP addresses on the same physical server.

Name-Based virtual host allow users to host multiple websites on the same server using the same IP address.

Follow These Steps to Set up Apache Virtual Hosts on CentOS 6

Assuming that you have already installed Apache (sudo -y yum install httpd) and nano text editor (sudo yum -y install nano) on your server, so let’s begin the configuration of virtual hosts by creating a new directory, where you will keep your website:

sudo mkdir -p /var/www/host1plus.com/public_html

Now you need to change the ownership of the new directory:

sudo chown -R user:user /var/www/host1plus.com/public_html/

Also, you have to make sure that everyone can read your files:

sudo chmod 755 /var/www

Then you need to create your test index page:

nano /var/www/host1plus.com/public_html/index.html

After that, copy the following code in this file:

<!DOCTYPE html>
<html>
<body>
<h1>Host1Plus example website!</h1>
</body>
</html>

That’s it, you have created the foundation on which our domains website content will be served from. Now you need to let Apache know where it must look for the content while users are trying to access your website. Do that by editing httpd.conf file:

nano /etc/httpd/conf/httpd.conf

Make sure that Apache is listening on default port for web services:

#Listen 12.34.56.78:80
Listen 80

Everything is OK if you are seeing the same text. Now scroll down to the bottom, until Virtual Host section:

NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
##
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
ServerAdmin webmaster@host1plus.com
DocumentRoot /var/www/host1plus.com/public_html
ServerName www.host1plus.com
ServerAlias host1plus.com
ErrorLog /var/www/host1plus.com/error.log
CustomLog /var/www/host1plus.com/access.log
</VirtualHost>

Your Virtual Host section must look like the code displayed above. Now get familiar with terms used in Virtual Host section:

NameVirtualHost – This is one of the most important lines, it must be uncommented (# sign deleted), the star means that Apache is accessible via any IP address, via port 80. You can change the star to the any IP which is assigned to your server.

ServerAdmin – you have to write webmaster’s e-mail address.

DocumentRoot – this term is the most important – you have to write the correct path to the directory that was created before. You will not be able to set up virtual host if the path is incorrect.

Servername – refers to the domain name, you must write the full domain in this section, for example www.host1plus.com.

ServerAlias – is a new line in the configuration file that is not there by default. Adding it will allow you to list a few versions of the domain name, for example, without the www in the front.

Errorlog – path to the file where errors of your website will be kept, it is very important to log errors for debugging purposes.

Customlog – stores server’s requests to your newly created virtual host.

Save the file, exit and test your virtual host syntax before restarting your web server:

httpd -t

Sample output:

Syntax OK

After you are sure that your virtual host is configured properly, restart your web server for the changes to take effect:

sudo /etc/init.d/httpd restart

P.S. Apache may cause the following error for you, but it is normal and your virtual hosts are working properly now.

Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 191.101.5.72 for ServerName

In order to setup IP-based Virtual Host, you must have more than one IP address assigned to your server. The purpose of implementing IP-based virtual hosting is to assign implementation for each domain and that particular IP would not be used by any other domain.

You can check your IP addresses assigned to your server by running ifconfig command:

[root@4C3qJfVSKKTt ~]# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:673 (673.0 b) TX bytes:673 (673.0 b)venet0 Link encap:UNSPECHWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:127.0.0.1 P-t-P:127.0.0.1 Bcast:0.0.0.0 Mask:255.255.255.255
UP BROADCAST POINTOPOINT RUNNING NOARP MTU:1500 Metric:1
RX packets:11890 errors:0 dropped:0 overruns:0 frame:0
TX packets:5526 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:12709518 (12.1 MiB) TX bytes:564795 (551.5 KiB)venet0:0 Link encap:UNSPECHWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:191.101.5.72 P-t-P:191.101.5.72 Bcast:191.101.5.72 Mask:255.255.255.255
UP BROADCAST POINTOPOINT RUNNING NOARP MTU:1500 Metric:1venet0:1 Linkencap:UNSPECHWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:191.101.6.144 P-t-P:191.101.6.144 Bcast:191.101.6.144 Mask:255.255.255.255
UP BROADCAST POINTOPOINT RUNNING NOARP MTU:1500 Metric:1

As you see, you have two network interfaces and IP addresses on the server – 191.101.5.72 (venet0:0), 191.101.6.144 (venet0:1). Create a few IP based virtual hosts:

<VirtualHost191.101.5.72:80>
ServerAdmin webmaster@host1plus.com
DocumentRoot /var/www/host1plus.com/public_html
ServerName www.host1plus.com
ServerAlias host1plus.com
ErrorLog /var/www/host1plus.com/error.log
CustomLog /var/www/host1plus.com/access.log
</VirtualHost>
<VirtualHost191.101.6.144:80>
ServerAdmin webmaster@host1plus1.com
DocumentRoot /var/www/host1plus1.com/public_html
ServerName www.host1plus1.com
ServerAlias host1plus1.com
ErrorLog /var/www/host1plus.com1/error.log
CustomLog /var/www/host1plus.com1/access.log
</VirtualHost>

Check syntax and restart Apache web server for the changes to take effect:

httpd -t
/etc/init.d/httpd restart

If Apache is not popping up any errors during restart, it means that your virtual hosts are configured properly.

USA VPS
cPanel KVM
Linux Cloud Server
Brazil VPS
Linux KVM Hosting
Ubuntu VPS
KVM VPS
Germany Cloud Servers
Windows Cloud Server
OpenVZ VPS
Rate this Tutorial:
No Comments

Other (3)

Popular Keywords