Default VirtualHost for Apache2 Zend Framework

December 18th 2012

Virtual Hosts in Apache can be a pain. Here's the one I use when I'm setting up a quick development environment.

First, edit the vhost file. On Ubuntu (using Apache2) you'll find this in /etc/apache2/sites-enabled/000-default

Text Snippet:
sudo su
nano /etc/apache2/sites-enabled/000-default

I generally replace all the content of this file with:

Text Snippet:
<VirtualHost *:80>
        ServerAdmin webmaster@myserver
        DocumentRoot /var/www/public
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory "/var/www/public">
            Order Deny,Allow
            Allow from all
            AllowOverride None
            SetEnv APPLICATION_ENV development
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} -s [OR]
            RewriteCond %{REQUEST_FILENAME} -l [OR]
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule ^.*$ - [NC,L]
            RewriteRule ^.*$ index.php [NC,L]
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After this, restart Apache2 by issuing:

Text Snippet:
/etc/init.d/apache2 restart

If Apache fails to restart, there are a couple things to check, first make sure that the web root exists. In my example I've used '/var/www/public'

If that directory exists, then also make sure that the rewrite mod is enabled. You can do this by issuing:

Text Snippet:
a2enmod rewrite

Finally, restart Apache, again we issue:

Text Snippet:
/etc/init.d/apache2 restart

Hopefully this will work for you without a hitch.