Apache Installation Notes

Apache is a popular and well known web server available on many platforms.

Configuring Apache for Phalcon

The following are potential configurations you can use to setup Apache with Phalcon. These notes are primarily focused on the configuration of the mod-rewrite module allowing to use friendly urls and the router component. Commonly an application has the following structure:

  1. test/
  2. app/
  3. controllers/
  4. models/
  5. views/
  6. public/
  7. css/
  8. img/
  9. js/
  10. index.php

Directory under the main Document Root

This being the most common case, the application is installed in any directory under the document root. In this case, we use two .htaccess files, the first one to hide the application code forwarding all requests to the application’s document root (public/).

  1. # test/.htaccess
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine on
  4. RewriteRule ^$ public/ [L]
  5. RewriteRule (.*) public/$1 [L]
  6. </IfModule>

Now a second .htaccess file is located in the public/ directory, this re-writes all the URIs to the public/index.php file:

  1. # test/public/.htaccess
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
  7. </IfModule>

If you do not want to use .htaccess files you can move these configurations to the apache’s main configuration file:

  1. <IfModule mod_rewrite.c>
  2. <Directory "/var/www/test">
  3. RewriteEngine on
  4. RewriteRule ^$ public/ [L]
  5. RewriteRule (.*) public/$1 [L]
  6. </Directory>
  7. <Directory "/var/www/test/public">
  8. RewriteEngine On
  9. RewriteCond %{REQUEST_FILENAME} !-d
  10. RewriteCond %{REQUEST_FILENAME} !-f
  11. RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
  12. </Directory>
  13. </IfModule>

Virtual Hosts

And this second configuration allows you to install a Phalcon application in a virtual host:

  1. <VirtualHost *:80>
  2. ServerAdmin admin@example.host
  3. DocumentRoot "/var/vhosts/test/public"
  4. DirectoryIndex index.php
  5. ServerName example.host
  6. ServerAlias www.example.host
  7. <Directory "/var/vhosts/test/public">
  8. Options All
  9. AllowOverride All
  10. Allow from all
  11. </Directory>
  12. </VirtualHost>