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.
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
git clone git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install
nano /etc/php5/apache2/php.ini
extension=phalcon.so
sudo service apache2 restart
<?php phpinfo(); ?>
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/
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
<?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(); }
$url->setBaseUri('/project_name/');
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
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
apache2ctl -M
a2enmod rewrite
sudo service apache2 restart
nano /var/www/project_name/.htaccess
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] </IfModule>
nano /var/www/project_name/public/.htaccess
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] </IfModule>
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>"; } }
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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.
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:
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?