9 June 2008 - 1:35Apache2 VirtualHosts

Apache Chief, what the? Ok, its just a play on words, and I saw it on tv last night during The Family Guy. But why? Well I’ve been playing around with my apache2 webserver. Get it? Apache, see it all makes sense now.

I am soon going to be hosting another site on my webserver for a friend of my brothers until he gets setup with a real hosting company. So I needed to setup apache to handle this additional domain. His site requires some additional options for php which when enabled in the main php.ini break my site. So I had to do some magic with htaccess files aswell.

In order to resolve these issues I decided to use VirtualHosts and an htaccess file. Lets begin with the main httpd.conf file. Here I added this line:

Include conf/vhosts/vhosts.conf

Now I can keep a separate file for the configuration of the domains. The next step is to create the actual directory and configuration file we just mentioned in the link above:

mkdir /etc/httpd/conf/vhosts
touch /etc/httpd/conf/vhosts/vhosts.conf

Now its time to populate the vhosts.conf file with the appropriate settings. To get two different domains setup, we need to have atleast the following:

NameVirtualHost (Servers IP)
 
<VirtualHost (Servers IP)>
  DocumentRoot /var/www/html/site1.com/
  ServerName site1.com
  ServerAlias site1.com www.site1.com
</VirtualHost>
 
<VirtualHost (Servers IP)>
  DocumentRoot /var/www/html/site2.com/
  ServerName site2.com
  ServerAlias site2.com www.site2.com
</VirtualHost>

See that was super easy, but now we have to worry about enabling certain php options for site2.com so it will work as expected. The first thing we need to do is create an .htaccess file:

touch /var/www/html/site2.com/.htaccess

Now add a line for each php setting you need enabled for the site.

php_flag magic_quotes_gpc On
php_flag post_max_size 12M

Once that is complete, its time to modify the vhosts.conf file again. This will allow apache to be able to read the php options we just enabled. In the vhosts.conf file add directory information to the site2.com VirtualHost section.

<VirtualHost (Servers IP)>
  DocumentRoot /var/www/html/site2.com/
  ServerName site2.com
  ServerAlias site2.com www.site2.com
  <Directory /var/www/html/site2.com>
    AllowOverride Options all
    Options Indexes FollowSymLinks
  </Directory>
</VirtualHost>

Of course we still need to restart the httpd service so that the configuration changes we have just made will take effect. Once that is done, you can consider yourself a VirtualHosts expert and blog about the whole experience.

No Comments | Tags: posts

Add a Comment