Linux Apache2 VirtualHosts configuration with NamedVirtualHosts

Okay, so I have been "playing" with Linux servers for a number of years, and I've setup quite a few single-site apache configurations, but I never had much luck with multisite setup... until now. So before I forget what I did I thought I'd put it here in hope that someone else gains some insight from my configuration - perhaps saving a few headaches.

Basically, I was working on a contract that required me to host a number of discrete websites from a single server, I have tried this setup before but always failed, the way I would generally get around it was to setup a single site '/var/www' dir containing multiple folders; and this would generally work. However, I found this to be too cumbersome based on the project requirements and also meant that every service would be have to share the global configuration, not ideal.

NameVirtualHost

Starting with the basics, the machine is an AMD x86_64 with 4 GB RAM running Ubuntu Server 12.04.2 LTS, to get Apache to play ball I needed to setup 'NamedVirtualHost' by adding the following to to '/etc/apache2/conf.d/virtual.conf':

#
#  We're running multiple virtual hosts.
#
NameVirtualHost *

This setting allows us to disguised between the different sites we require by name along with each VirtualHost setup. Basically, when Apache receives a request it checks to see which aliases(CNAME) was used to send the request, and then serves the request by parsing the relevant site.

Apache Host Configuration

We now need to create a host configuration script for each of the sites we wish Apache to parse, these scripts are located in '/etc/apache2/sites-available', and are used to load configuration when Apache starts up. The following is an example script, which, in this case would be called 'swannonline.local' and stored in the above Apache directory:

<VirtualHost *>
 
  ServerAdmin swannonline@googlemail.com
  ServerName swannonline.local
 
  DocumentRoot /var/www/swannonline.local
  <Directory /var/www/swannonline.local>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
  </Directory>
 
  ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  <Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
  </Directory>
 
  ErrorLog /var/log/apache2/error.log
 
  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn
 
  CustomLog /var/log/apache2/access.log combined
 
</VirtualHost>

From this script we have setup a new NamedVirtualHost called 'swannonline.local', we have set a document root for this host, and defined access permissions, along with error reporting and CGI configuration. All that is left to do now is create the 'DocumentRoot' directory and enable the site.

Create DocumentRoot

To be sure the site is working we will also create a simple index.php file in the document root so that we can actually see something being served, and then finally we need to ensure the file permission and ownership are in place.

Create 'DocumentRoot' directory with:

$ sudo mkdir /var/www/swannonline.local

Create a php script that will show the output of phpinfo() - this assumes you have php installed:

$ sudo cat > /var/www/swannonline.local << "EOF"
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
EOF
$ sudo mv /tmp/index.php /var/www/swannonline.local/ 

And finally make sure file permissions are correct:

$ sudo chown -R www-data:www-data /var/www/swannonline.local
$ sudo chmod -R 644 /var/www/swannonline.local

Enable Site

We are now ready to enable the site and check that everything works. When we created the configuration file earlier we created it in '/var/www/sites-available', this is the directory for holding scripts for sites that are, available - obviously. To enable a site we need to create a symbolic link to the script we created in the '/var/www/sites-enabled' directory, however, it is even easier if we use the Apache tools; 'a2ensite' enables a site and 'a2dissite' disables it. We enable 'swannonline.local' as follows:

$ sudo a2ensite swannonline.local
 * Restarting web server apache2                                                                     
 ... waiting 

Now all we need to do is restart the Apache daemon with:

$ sudo service apache2 restart

And that is the server configuration complete.

DNS Configuration

The above setup is fine if you only want to view the named hosts locally, that is, on a browser located on the actual server, however, if you wanted to view them on a different host, on the same network, you wouldn't be able to since the we haven't set up DNS. Basically, if we type 'http://swannonline.local' into a browser, the host wouldn't know how to resolve the address. To overcome this, we need to add a line to the hosts 'etc/hosts' file as follows:

192.168.5.20   swannonline.local   swannonline.local

Once complete, type 'http://swannonline.local' into the host's browser, and we should see the phpinfo output.

Currently, I have no need to share any sites to the outside world, but when I do, it would make sense to have a DNS server configured on the network... watch this space.

Links

Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.