How To Install and Get Started With Phalcon on an Ubuntu 12.04 VPS
How To Install and Get Started With Phalcon on an Ubuntu 12.04 VPS


How To Install and Get Started With Phalcon on an Ubuntu 12.04 VPS

UbuntuPHPPHP Frameworks

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Phalcon

Phalcon is a PHP framework that promotes the Model-View-Controller architecture and has many framework-like features you'd expect in a piece of software like this - ORM, templating engine, routing, caching, etc.

One cool thing about it is that performance-wise, it is arguably much faster than other frameworks out there. The reason is that it is not your ordinary PHP framework whose files you just copy onto your server and you are good to go. It is in fact a PHP extension written in C.

In this article we will look at how to get started with Phalcon on your VPS running Ubuntu 12.04. If you are following along, I assume you already have your server set up with the LAMP stack (Apache, MySQL and PHP). There is a good tutorial on DigitalOcean to help you get set up if needed.


The first thing you need to do is install the requirements for Phalcon. Run the following three commands to do that:
sudo apt-get update
sudo apt-get install git-core gcc autoconf make
sudo apt-get install php5-dev php5-mysql

You can remove packages from the commands if you already have them installed (for instance git-core if you already have Git installed). Next up, you need to clone the framework repo onto your system:
git clone git://

After that is done, navigate in the following folder with this command:
cd cphalcon/build

And run the install file to install the extension:
sudo ./install

What you need to do next is edit your php.ini file:
nano /etc/php5/apache2/php.ini

And add the following line to the end of it:

Then restart your server for the changes to take effect:
sudo service apache2 restart

And this should do it. To check if Phalcon has been successfully installed, you'll need to check the output of phpinfo() statement. If you don't know how to proceed, create a file called info.php somewhere where you can access it from the browser and paste in the following line:
<?php phpinfo(); ?>

Save the file and point your browser to it. In the PHP information displayed on that page, you should see that the Phalcon framework is enabled and also verify its version.

Your first Phalcon project structure

If you've used other PHP frameworks, you would be expecting some framework related files somewhere in your project's folder structure. With Phalcon, all these files are readily available in memory, so all you need to do to get started is create an empty folder structure somewhere in Apache's document root (defaults to /var/www). The recommended way to go is the following:

So what you have here is a project folder which has 2 main folders: app and public. The first will house the logic of your application (mostly PHP) whereas the second one is where your browser will point and be redirected to the resources in the app folder on the one hand, and have access to all the frontend assets, on the other.


The first and most important file you need to create is your index.php file the application will use to bootstrap. Create this file in the public/ folder of your application:
nano /var/www/project_name/public/index.php

And paste in the following code:

try {

   //Register an autoloader
   $loader = new \Phalcon\Loader();

   //Create a DI
   $di = new Phalcon\DI\FactoryDefault();

   //Setup the view component
   $di->set('view', function(){
       $view = new \Phalcon\Mvc\View();
       return $view;

   //Setup a base URI so that all generated URIs include the "tutorial" folder
   $di->set('url', function(){
       $url = new \Phalcon\Mvc\Url();
       return $url;

   //Handle the request
   $application = new \Phalcon\Mvc\Application($di);

   echo $application->handle()->getContent();

} catch(\Phalcon\Exception $e) {
    echo "PhalconException: ", $e->getMessage();

For more information about what this file contains, you can check the official Phalcon website. But please note that you need to replace this line:

With one that is appropriate for your case, i.e. containing the name of your project folder.

URL Rewriting

Phalcon will need to make use of .htaccess files to make some important rerouting and secure the application's folder structure from prying eyes. For this, the mod_rewrite module from Apache needs to be enabled and .htaccess files need to be allowed to make modifications to the Apache instructions.

So, if this is not the case for you, edit the Apache virtual host file under which the Phalcon application is (defaults to /var/www if you do not have some particular virtual host for this application), and make sure that Allow Overrides is set to All under the /var/www directory (again, if your application is in the default Apache document root). You can edit the default virtual host file with the following command:
nano /etc/apache2/sites-available/default

And where you see this block, make the changes to correspond to the following.
<Directory /var/www/>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   Order allow,deny
   allow from all

Finally, make sure that mod_rewrite is enabled in your Apache. To check if it is already enabled, use the following command:
apache2ctl -M

If you see "rewrite_module" in the list, you are fine. If not, use the following command to enable the module:
a2enmod rewrite

After all these steps, or after any individual one you had to perform, give Apache a restart so they take effect:
sudo service apache2 restart

Now that this is taken care of, create an .htaccess file in the main project folder:
nano /var/www/project_name/.htaccess

And paste in the following directives:
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule  ^$ public/    [L]
   RewriteRule  (.*) public/$1 [L]

This will reroute all requests from the project main folder to the public/ folder. Now create another .htaccess file but this time in the public/ folder:
nano /var/www/project_name/public/.htaccess

And paste in the following directives:
<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

This will reroute all the requests coming to this folder to the index.php file (in case an actual file by the name requested does not exist in that folder or below - you know, for the frontend assets to still be accessible).

Your first controller

If you now point your browser to the project folder, you'll get an error that the Index controller class could not be loaded. That's because by default, the application needs one of these if there is not controller passed in with the request. So let's create one to display Hello World onto the page.

Create the following IndexController.php file in the controllers folder:
nano /var/www/project_name/app/controllers/IndexController.php

Inside, paste the following:

class IndexController extends \Phalcon\Mvc\Controller {

   public function indexAction()    {
       echo "<h1>Hello World!</h1>";


You'll notice that we extend the default Phalcon Controller class and name it IndexController (all controller names need to end with the word "Controller". Inside this class, we define a method (all methods, also called Actions, need to end with the word "Action") called indexAction. Since it is called index, it is also the first Action that gets called by this controller if no particular action is specified in the request.

If you now access the project folder from the browser, you should see the string being echoed.


In this tutorial we've seen how to install Phalcon and get started with your project. In the next one we will take it a bit further and see how we can leverage the Phalcon MVC framework and how to access information stored in the database.
Article Submitted by: Danny
Creative Commons License