Book Image

Learning Phalcon PHP

By : Calin Rada, Ioan C Rada
Book Image

Learning Phalcon PHP

By: Calin Rada, Ioan C Rada

Overview of this book

Table of Contents (17 chapters)
Learning Phalcon PHP
About the Author
About the Reviewers

The Apache and Nginx configuration files

We will use /var/www/learning-phalcon.localhost as the default directory for our project, and we will refer to it as the root folder. Please create this folder:

$ sudo mkdir -p /var/www/learning-phalcon.localhost/public

Of course, if you want, you can use another folder. Let's create a test file in our public folder under the root directory with some PHP content:

$ cd /var/www/learning-phalcon.localhost/public
$ echo "<?php date();" > index.php


Let's switch to the default directory where Apache holds the configuration files for the available websites, using the command line: $ cd /etc/apache2/sites-available/. After that, perform the following set of steps:

  1. Using your favorite editor, create a file named learning-phalcon.localhost for apache version < 2.4 or learning-phalcon.localhost.conf for apache version >= 2.4:

    $ vim learning-phalcon.localhost.conf
  2. Now, paste the following content to this file:

    <VirtualHost *:80>
        DocumentRoot "/var/www/learning-phalcon.localhost"
        DirectoryIndex index.php
        ServerName learning-phalcon.localhost
        ServerAlias www.learning-phalcon.localhost
        <Directory "/var/www/learning-phalcon.localhost/public">
            Options All
            AllowOverride All
            Allow from all
  3. Then, switch to the public folder and add a file named .htaccess to it:

    $ cd /var/www/learning-phalcon.localhost/public
    $ vim .htaccess
  4. Then, add the following content to the .htaccess file:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
  5. This will not work unless you have enabled mod_rewrite. To do so, execute this command:

    $ sudo a2enmod rewrite
  6. Now that we have configured our virtual host, let's enable it:

    $ sudo a2ensite learning-phalcon.localhost
    $ sudo service apache2 reload

The host file

If you open a browser and type http://www.learning-phalcon.localhost/, you'll receive a host not found or connection error. This is because there is no name resolver for this TLD (short for Top Level Domain). To fix this, we edit our host file and add this name:

$ echo " learning-phalcon.localhost www.learning-phalcon.localhost" | sudo tee /etc/hosts

Restart your browser and type the address http://www.learning-phalcon.localhost/ again. If everything goes well, you should see the current date/time.


If you choose to use Nginx (which I recommend, especially because it can serve more concurrent clients with higher throughput, and it serves static content more efficiently) instead of Apache, here is what you need to do:

Locate the config folder of Nginx (in Ubuntu, it is installed under /etc/nginx/). Create a file named learning-phalcon.localhost in your sites-available folder (by navigating to /etc/nginx/sites-available):

$ cd /etc/nginx/sites-available
$ vim learning-phalcon.localhost

Now, add the following content to it:

server {
    listen 80;
    server_name learning-phalcon.localhost;

    index index.php;
    set $root_path "/var/www/learning-phalcon.localhost/public";
    root $root_path;

    client_max_body_size 10M;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;

    location ~ \.php {
        fastcgi_index /index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_intercept_errors on;
        include fastcgi_params;

        fastcgi_split_path_info ^(.+\.php)(/.*)$;

        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;

    location ~ /\.ht {
        deny all;


In some environments, you might need to edit your php.ini file and set cgi.fix_pathinfo = 0.

Then, save the file and restart Nginx:

$ sudo service nginx restart

Please edit and save your host file (check The host file section), then open your browser and type http://www.learning-phalcon.localhost/. At this point, you should see a page that shows the current date/time.

There are many possible methods to install and configure PHP and Apache/Nginx. Feel free to do a simple Google search and choose one that fits you better, if my method is not the optimal one for your needs.

Assuming that everything went well until now, we will go further by learning a little bit about Phalcon's internals.