Apache VirtualHost setups

In PHP file structure, we discussed a more appropriate way of organizing PHP projects for security and maintainability. The take-away message was to place web-accessible resources under one directory (the DocumentRoot), and all other supplemental files in another directory:

  • www (the web-accessible root)
  • lib (outside the reach of the web server)

In this article, we discuss how to organize an Apache server to host multiple projects with this structure.

A note about third-party hosts#

If you are not lucky enough to host your own server and are relying on a hosted environment, you can still make use of the lessons from PHP file structure. Unfortunately, this article is beyond the access granted by third-party hosts.

Apache's default DocumentRoot#

In most Linux distributions, Apache's DocumentRoot is located at either /var/www or /var/www/html (enlightened distributions like ArchLinux use /srv/http, a great choice). This setting is okay for the default virtual host. It is wise to have at least a landing page for the default virtual host.

A better location#

Even if the server is only going to host one application, it is a good idea to do away with the package's default location. A logical place for the PHP application is under an appropriate name in the /srv directory:

  • /srv
    • www.MyProject.com
      • www (VirtualHost DocumentRoot)
      • lib
      • etc

One of the other files to save under /srv/www.MyProject.com is the section of the apache.conf configuration that defines the virtual host for this project. In this way, if there are multiple projects, each with their own virtual host, they can be saved side by side under /srv:

  • /srv
    • www.MyProject.com
      • apache.conf
    • cms.MyProject.com
      • apache.conf
    • etc

Installation of apache.conf files#

There are options for "installing" applications saved under a centralized location like /srv. Indeed, the power of this architecture is the flexibility it provides. One possible method is to use symlinks from the Apache configuration directory (usually /etc/httpd/conf) to the apache.conf file under each project's directory. To uninstall, simply remove the symlink.


This organization, when paired with the PHP file structure discussed previously, allows for logical grouping of multiple projects under one host. This logical grouping may be imposed by the third-party hosting company, or it may require the extra work of a savvy system administrator. The advantages offered include:

  • Easier system administration (maintainability)
  • Logical deployment of multiple applications (scalability)
  • Tighter, more consistent control (security).

Continue the discussion on Twitter.