Phalcon PHP is a very impressive PHP MVC Framework that I’ve started to play around with. What differentiates it from the rest of the pack is the fact that it runs in memory on your server in the form of an extension. It’s compiled c code thus it’s executed much faster than a normal PHP framework would be. Unsurprisingly, this results in Phalcon blowing away all of the competition in speed tests.
Build & Install Phalcon Extension
First we need to make sure we have all the pre-requisites
sudo apt-get install php5-dev libpcre3-dev gcc make php5-mysql
Now lets download and build
cd ~/installs git clone --depth=1 git://github.com/phalcon/cphalcon.git cd cphalcon/build sudo ./install
The extension is built, now we need to enable the extension for use in php5-fpm. Create a new file at /etc/php5/mods-available/phalcon.ini and paste in the following
extension=phalcon.so
Symlink the file to the conf.d folder
sudo ln -s /etc/php5/mods-available/phalcon.ini /etc/php5/fpm/conf.d/phalcon.ini
That’s all for the install. Restart php5-fpm and the extension should be active.
Setup your domain
Assuming you are following the directory structure outlined in my previous articles, your domain looks something like this.
/home /[user] /domains /[domain] /backup /dev /logs /private /public
We will be using both the private and the public folder for our Phalcon project. I’m going to re-use the Phalcon tutorial project that is actually on github already. You can find it here.
Most of the code will be the same, with the exception of where the files are placed and some paths in the index.php file.
Lets start with the index.php file. On github it’s inside the public folder. Take that file and place it inside your domains /public folder as well. Replace the contents of the file with the following (change database info to match your server obviously, or comment the whole block out and you can enable it afterwards).
registerDirs( array( '../private/app/controllers/', '../private/app/models/' ) )->register(); // Create a DI $di = new FactoryDefault(); // Set the database service $di['db'] = function() { return new DbAdapter(array( "host" => "localhost", "username" => "root", "password" => "[password]", "dbname" => "tutorial" )); }; // Setting up the view component $di['view'] = function() { $view = new View(); $view->setViewsDir('../private/app/views/'); return $view; }; // Setup a base URI so that all generated URIs include the "tutorial" folder $di['url'] = function() { $url = new Url(); $url->setBaseUri('/'); return $url; }; // Setup the tag helpers $di['tag'] = function() { return new Tag(); }; // Handle the request $application = new Application($di); echo $application->handle()->getContent(); } catch (Exception $e) { echo "Exception: ", $e->getMessage(); }
As you can see by the code above, we’re going to be storing the /app folder from github inside our private folder. This is so none of the app code is publicly accessible. Go ahead and copy the whole folder from github there. You can also download the schema and create a new database and import the schema as well.
When the app folder and the index.php are both in their proper places, we are ready to edit the nginx vhost file for the domain. /etc/nginx/sites-available/[domain]
Replace the contents of your vhost file with the following:
server { listen 80; server_name www.[domain]; return 301 $scheme://[domain]$request_uri; } server { listen 80; server_name [domain]; index index.php index.html index.htm; set $root_path '/home/[user]/domains/[domain]/public/'; root $root_path; try_files $uri $uri/ @rewrite; location @rewrite { rewrite ^/(.*)$ /index.php?_url=/$1; } access_log /home/[user/domains/[domain]/logs/access.log; error_log /home/[user]/domains/[domain]/logs/error.log; location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index /index.php; include /etc/nginx/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; } location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { root $root_path; } location ~ /\.ht { deny all; } }
This should be all you need to get your Phalcon project loading via nginx. Restart nginx and php5-fpm for good measure.
sudo service nginx restart sudo service php5-fpm restart
Load the domain in your browser and you should see a page that has “Hello” on it with a link to a form that you can fill out and submit. If you configured your database and kept the db code in the index.php you should then get a row inserted into your database table. You can verify that command line if you like!
Git (optional)
If you are going to put this code into a Git repository such as bitbucket or github, you can easily accomplish that by creating somewhere new for the git repo to live (I chose to make a directory at ~/domains/[domain]/repo to house it. Move both your public and private folders into that folder after you init an empty git repo. Then symlink your old public and private folder locations to the /repo/public and /repo/private locations. Now you can edit and commit your code straight to your git repo and your old folders will utilize it the same as before.
Leave a Reply