How to make a Virtual Hosts on Apache2

Apache is the most famous HTTP server especially in a Unix world.  As others HTTP servers Apache allows you to customize default configurations such as IP, Ports, Log Files, Virtual Host, etc.

Today we’ll focus in Virtual Hosts configuration, basically a Virtual Host is a configuration with a set of directives which allows you to point/direct requests from a specific domain for a specific directory in your machine. That means when the server receives a request form www.mydomain.com Apache is able to  point it to a directory to run scripts inside it. This technique provides that one single IP support several domains.

I’m using Xampp package which includes Apache2, PHP, MySQL, among other tools although this configuration is very similar using other packages or OS. To configure our Virtual Host we need to change two files  httpd.conf and httpd-vhosts.conf.

Below we’ve a simple example with the main directives.

//# httpd-vhosts.conf

<VirtualHost www.mydomain.com:80>
   ServerAdmin  webmaster@localhost
   DocumentRoot  c:\mysite
   ServerName  www. mydomain.com
   ErrorLog c:\mysite\apache_log.log
   .
   .
   .
</VirtualHost>

Let’s look to each configuraction parameter

  • ServerAdmin: Indicates who is a server administrator(may be shown in automatic errors messages by server);
  • DocumentRoot: Indicates where are the files to run when the requests occur.
  • ServerName: Indicates the domain name;
  • ErrorLog: Indicates what is the error log file, this is very important because this way you can have several error logs files one for each domain. Therefore you controls better the errors that can be occur.

You need to confirm if your httpd.conf file has Virtal Hosts enabled.

//# httpd.conf

# Virtual hosts
#Include "conf/extra/httpd-vhosts.conf"

replace to

//# httpd.conf

# Virtual hosts
Include "conf/extra/httpd-vhosts.conf"

Assuming you don’t have a server with a real domain and you want to test this configuration on your localhost machine. You just need to follow the following configurations.

1- Modify hosts file to point www.mydomain.com to localhost(127.0.0.1). You need add the following line (windows: C:\Windows\System32\drivers\etc\hosts and in Linux: /etc/hosts):

127.0.0.1           www.mydomain.com

2- Create a c:\mysite folder;

3- Add the virtual host configuration in httpd-vhosts.conf file.

<VirtualHost *:80>
   ServerAdmin  webmaster@localhost
   DocumentRoot  c:\mysite
   ServerName  www.mydomain.com
   ErrorLog c:\mysite\apache_log.log
   <Directory "C:\mypage">
        Order allow,deny
        Allow from all
   </Directory>
</VirtualHost>

4- Restart Apache server;

5- Go to your web browser and write www.mydomain.com.

 Conclusion

Virtual Host is a very important configuration on your HTTP server because allows one single IP represents several domains and  allows you to point easily requests to any directory. If you are interested learning more details about this subject you can find more information here.