Basic Apache2 setup

I just installed Apache 2.2.4 on Windows XP. I needed to set up virtual hosts, so I added the following to C:\WINDOWS\system32\drivers\etc\hosts:
127.0.0.1 server1.localhost server2.localhost
127.0.0.1 www.server1.localhost www.server2.localhost

Now to configure Apache, the virtual host configuration needs to be included in C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf by uncommenting the line containing:
Include conf/extra/httpd-vhosts.conf

Edit C:\Program Files\Apache Software Foundation\Apache2.2\conf\extra\httpd-vhosts.conf to set up your virtual hosts – In my case I used the following:
NameVirtualHost *:80

<Directory />
Order deny,allow
Allow from all
</Directory>

<VirtualHost 192.168.0.102:80>
ServerAdmin webmaster@server1.localhost
DocumentRoot /www/docs/server1.localhost
ServerName server1.localhost
ServerAlias www.server1.localhost
ErrorLog logs/server1.localhost-error_log
CustomLog logs/server1.localhost-access_log common
</VirtualHost>


<VirtualHost 192.168.0.102:80>
ServerAdmin webmaster@server2.localhost
DocumentRoot /www/docs/server2.localhost
ServerName server2.localhost
ServerAlias www.server2.localhost
ErrorLog logs/server2.localhost-error_log
CustomLog logs/server2.localhost-access_log common
</VirtualHost>

Notice the <directory> directive. By default, everything is denied in httpd.conf, so you need this to allow the files in your docs directory. If you don’t, you’ll see 403 not authorized errors in your browser, and you’ll see something like the following in your error log (C:\Program Files\Apache Software Foundation\Apache2.2\logs\server1.localhost-error_log):
client denied by server configuration: C:/www/docs/server1.localhost/

I’ve put the content for my hosts in
C:\www\docs\server1.localhost
C:\www\docs\server2.localhost

Now I can access my content through
http://server1.localhost/
http://server2.localhost/

The next thing I want to do is redirect from www.server1.localhost to server1.localhost – the extra www is unnecessary.

So in httpd.conf uncomment the rewrite module:
LoadModule rewrite_module modules/mod_rewrite.so

Now add the following to the virtual host definition:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^server1\.localhost [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://server1.localhost:%{SERVER_PORT}/$1 [L,R]
RewriteCond %{HTTP_HOST} !^server1\.localhost [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://server1.localhost/$1 [L,R]

Try accessing http://www.server1.localhost/ and see how the browser location changes to http://server1.localhost/

I have a few static resources that I use in several applications on different virtual hosts – things like libraries (Prototype, YUI etc). Rather than uploading them several times, I can put them in a common location and map an alias in each virtual host:
Alias /common /www/docs/common

Now I can access http://server1.localhost/common and http://server2.localhost/common and have them both serving from the same content.

If I put content in versioned directories, then my applications can pick and choose which version they need, and none are forced to upgrade. For example:
/www/docs/common/yui/2.2.0/
/www/docs/common/yui/2.2.2/

Serving this content from Apache in this manner is much more efficient than (for example) including it in my java web applications – when uploading new versions of the applications the WAR is much smaller without all of this static content!

Download Apache from http://httpd.apache.org/download.cgi and view the documentation at http://httpd.apache.org/docs/2.2/

The full vhosts file:
NameVirtualHost *:80

<Directory />
Order deny,allow
Allow from all
</Directory>

<VirtualHost 192.168.0.102:80>
ServerAdmin webmaster@server1.localhost
DocumentRoot /www/docs/server1.localhost
ServerName server1.localhost
ServerAlias www.server1.localhost
ErrorLog logs/server1.localhost-error_log
CustomLog logs/server1.localhost-access_log common

RewriteEngine on
RewriteCond %{HTTP_HOST} !^server1\.localhost [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://server1.localhost:%{SERVER_PORT}/$1 [L,R]
RewriteCond %{HTTP_HOST} !^server1\.localhost [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://server1.localhost/$1 [L,R]

Alias /common /www/docs/common
</VirtualHost>

<VirtualHost 192.168.0.102:80>
ServerAdmin webmaster@server2.localhost
DocumentRoot /www/docs/server2.localhost
ServerName server2.localhost
ServerAlias www.server2.localhost
ErrorLog logs/server2.localhost-error_log
CustomLog logs/server2.localhost-access_log common

RewriteEngine on
RewriteCond %{HTTP_HOST} !^server2\.localhost [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://server2.localhost:%{SERVER_PORT}/$1 [L,R]
RewriteCond %{HTTP_HOST} !^server2\.localhost [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://server2.localhost/$1 [L,R]

Alias /common /www/docs/common
</VirtualHost>

Popular posts from this blog

Slow, Buffering, Stuttering Udemy Video

Intellij tip - Joining multiple lines into one

Looking for IT work in Australia? JobReel.com.au!