Second virtual host under apache 2

After messing around with apache 2 configuration files, I finally finally figured out how to add a virtual host sharing the same IP address and port 80. The confusing part is that multiple config files are involved that might step into each other. The key to understand how these different configuration files work together is to be aware of the following statements in  apache2.conf:

# Include all the user configurations:
Include httpd.conf
# Include ports listing
Include ports.conf
# Include generic snippets of statements
Include conf.d/
# Include the virtual host configurations:
Include sites-enabled/

Initially the config files are as follows:

  • httpd.conf is essentially empty at installation time
  • ports.conf contains some default configuration that applies to the default site
  • conf.d is a folder containing module specific configuration files
  • sites-enabled is a folder containing virtual host specific configuration files
  • What you put into these configuration files is more a matter of convention than necessity to get everything working.
  • There is another folder called sites-available that contain the configuration files for wannabe virtual hosts. One of these configuration files is default.
  • To create a new virtual host
  • Copy default into let’s say mynewsite.
  • Edit mynewsite and modify modify the folder entries to point to the new site and a line containing “ServerName mynewsite” and save.
  • Edit default and add the following lines:
    ServerName default
    ServerAlias localhost
  • Now, the config files in sites-available are not included by apache2.conf. This folder is just there to safeguard your configuration files.
    • Copy mynewsite into sites-enabled folder. However, to do that, use “a2ensite mynewsite” (which I assume means apache 2 enable site mynewsite). Behind the scenes, this command has for effect to copy virtual host specific files from sites-available folder to sites-enabled folder. You can disable the new web site by issuing “a2dissite mynewsite”. (apache 2 disable site, I presume).
  • Edit /etc/hosts and modify the first line to  “ localhost mynewsite”. mynewsite should now be ping-able. Restart the apache 2 web server and it should work.
  • Create the new site home folder with a index.html file to be able to test that all works OK.
  • Leave a Reply