Wednesday, February 20, 2008

Ubuntu Apache Virtual hosts

Virtual Hosts in Ubuntu. The easiest way to create new virtual host is to create a configuration file in /etc/apache2/sites-enabled. In this directory, you should see a symbolic link to the default virtual host located on /etc/apache2/sites-available/ if default host is enabled.

Usually, you want to do

  1. cd /etc/apache2/sites-available
  2. sudo vim virtualdomain.conf
  3. Write or copy virtual host configuration (see example below)
  4. sudo vim /etc/hosts
  5. Point virtual domain to localhost. For example:
    127.0.0.1 localhost virtualdomain
  6. sudo a2ensite virtualdomain.conf
  7. sudo /etc/init.d/apache2 reload

a2ensite is a script that creates the symlink to /etc/apache2/sites-available/virtualdoain.conf in /etc/apache2/site-enabled directory. This allows to allows quickly disable or enable the virtual host without deleting or moving the configuration file.

Example of virtual host using IP and Port Note that if the server is not listen to port 81, you need to put Listen 81.

Listen 81 NameVirtualHost 192.168.12.2:81 <VirtualHost 192.168.12.2:81> ServerAdmin webmaster@localhost DocumentRoot /home/auser/public_html <Directory /> Options FollowSymLinks AllowOverride None </Directory> ErrorLog /home/auser/log/apache2/error.log LogLevel warn CustomLog /home/auser/log/apache2/access.log combined ServerSignature On </VirtualHost>

No comments: