Tutorial

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

Published on January 20, 2014
How To Install and Get Started With Phalcon on an Ubuntu 12.04 VPS

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.

Installation


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://github.com/phalcon/cphalcon.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:
extension=phalcon.so

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:

project_name/
 app/
   controllers/
   models/
   views/
 public/
   css/
   img/
   js/

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.

Bootstrapping


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:
<?php

try {

   //Register an autoloader
   $loader = new \Phalcon\Loader();
   $loader->registerDirs(array(
       '../app/controllers/',
       '../app/models/'
   ))->register();

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

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

   //Setup a base URI so that all generated URIs include the "tutorial" folder
   $di->set('url', function(){
       $url = new \Phalcon\Mvc\Url();
       $url->setBaseUri('/project_name/');
       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:
$url->setBaseUri('/project_name/');

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
</Directory>

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]
</IfModule>

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]
</IfModule>

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:

<?php

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.

Conclusion


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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Found:

There must be a file /etc/php5/apache2/conf.d/, you can create a file call 30-phalcon.ini that contains extension=phalcon.so

Thank you very much! I’ve headache on why my phalcon project wouldn’t load correctly. Thanks!

Thanks for the nice tutorial. It helped a lot. :*

a friend of mine told me that while he try to install this, he stuck on memory limit, while his package is 512MB, is that true to install this have some minimum request that may impact on package provided on digitalocean ? I need to make sure before choose pricing plan, kindly explain me to email thanks.

Thank you so much its really helpful for me i hv already did it. but i have stock in some other issue just like one of my controller name is AdminUsersController.php its working fine when its run in wamp but when i deploly it in linux its give me the error, after that i changed the controller name to AdminusersController.php and works fine why this is happening i need know.

I need some help in the mail function how to send mail in phalcon I would like to change the url by using dispatcher->forward() its execute the flow but it does not change the URL which creates a lot of trouble for my project plz help me on this to move out.

My .htaccess file in project/ is

<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] </IfModule>

and in the project/public/

AddDefaultCharset UTF-8 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] </IfModule>

when i point the browser to project folder i can see the folder structure , when i point to project/public then i can see “Hello world.” I am using ubuntu 14.04 LTS os.

I followed all the process like above but still now the same problem is live. Am i Following correctly or is there any error in my installation.

Please help.

This is incorrect:

<Directory /var/www/>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   Order allow,deny
   allow from all
</Directory>

The MultiViews option causes problems with Phalcon. Actually, you don’t have to specify any particular options for Phalcon to work properly aside from AllowOverride: http://docs.phalconphp.com/en/latest/reference/apache.html

Hi,

I am having a problem with editing /etc/apache2/sites-available/default, as described. It doesn’t exist. I found a block that looked similar in /etc/apache2/apache2.conf, so I edited it, thinking that this tutorial might be slightly outdated. However, now I just have a 403 forbidden error. Stupidly, I didn’t write down what the block contained before editing. What virtual host file am I meant to be editing, and what did /etc/apache2/apache2.conf contain originally? Any help is appreciated.

@ozfive - check your phpinfo()

Your section of “Additional .ini files parsed” should contain: /etc/php5/fpm/conf.d/[some-ini-files] … /etc/php5/fpm/conf.d/30-phalcon.ini

Note the path (/etc/php5/fpm/conf.d/) above may be different for your installation (mine is nginx + php-fpm setup, yours may be different).

Your phalcon.ini needs to be in that path!

Because phalcon needs 20-pdo_mysql.ini to go first, hence we use a larger number like 30.

Run in terminal: php -m

You should see “phalcon” in the [PHP Modules] section.

Restart web server and you should see it working.

Tried to run this on Ubuntu 14.04

Stuck on this line for a long time

libtool: compile: gcc -I. -I/root/cphalcon/build/64bits -DPHP_ATOM_INC -I/root/cphalcon/build/64bits/include -I/root/cphalcon/build/64bits/main -I/root/cphalcon/build/64bits -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -DPHALCON_RELEASE -DHAVE_CONFIG_H -march=native -mtune=native -O2 -finline-functions -fomit-frame-pointer -fvisibility=hidden -c /root/cphalcon/build/64bits/phalcon.c -fPIC -DPIC -o .libs/phalcon.o

Then after about 10mins… spits out the following error

gcc: internal compiler error: Killed (program cc1) Please submit a full bug report, with preprocessed source if appropriate. See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions. make: *** [phalcon.lo] Error 1

Any idea what to do?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel