{"id":222,"date":"2014-11-20T11:33:12","date_gmt":"2014-11-20T16:33:12","guid":{"rendered":"http:\/\/devroar.com\/?p=222"},"modified":"2021-05-02T15:23:38","modified_gmt":"2021-05-02T20:23:38","slug":"installing-configuring-phalconphp","status":"publish","type":"post","link":"http:\/\/devroar.com\/index.php\/2014\/11\/20\/installing-configuring-phalconphp\/","title":{"rendered":"Installing &#038; Configuring PhalconPHP"},"content":{"rendered":"<p><a href=\"http:\/\/phalconphp.com\"><img class=\"alignleft\" style=\"width: 225px;\" src=\"http:\/\/devroar.com\/wp-content\/uploads\/2014\/11\/phalcon-php-logo.png\" alt=\"PhalconPHP\"><\/a><\/p>\n<p>Phalcon PHP is a very impressive PHP MVC Framework that I&#8217;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&#8217;s compiled c code thus it&#8217;s executed much faster than a normal PHP framework would be. Unsurprisingly, this results in Phalcon blowing away all of the competition in <a href=\"http:\/\/docs.phalconphp.com\/en\/latest\/reference\/benchmark.html\">speed tests<\/a>.<\/p>\n<p><!--more--><\/p>\n<h3 style=\"clear: left;\">Build &amp; Install Phalcon Extension<\/h3>\n<p>First we need to make sure we have all the pre-requisites<\/p>\n<pre>sudo apt-get install php5-dev libpcre3-dev gcc make php5-mysql\n<\/pre>\n<p>Now lets download and build<\/p>\n<pre>cd ~\/installs\ngit clone --depth=1 git:\/\/github.com\/phalcon\/cphalcon.git\ncd cphalcon\/build\nsudo .\/install\n<\/pre>\n<p>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<\/p>\n<pre>extension=phalcon.so\n<\/pre>\n<p>Symlink the file to the conf.d folder<\/p>\n<pre>sudo ln -s \/etc\/php5\/mods-available\/phalcon.ini \/etc\/php5\/fpm\/conf.d\/phalcon.ini\n<\/pre>\n<p>That&#8217;s all for the install. Restart php5-fpm and the extension should be active.<\/p>\n<h3>Setup your domain<\/h3>\n<p>Assuming you are following the directory structure outlined in my previous articles, your domain looks something like this.<\/p>\n<pre>\/home\n\/[user]\n\/domains\n\/[domain]\n\/backup\n\/dev\n\/logs\n\/private\n\/public\n<\/pre>\n<p>We will be using both the private and the public folder for our Phalcon project. I&#8217;m going to re-use the Phalcon tutorial project that is actually on github already. You can find it <a href=\"https:\/\/github.com\/phalcon\/tutorial\">here<\/a>.<\/p>\n<p>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.<\/p>\n<p>Lets start with the index.php file. On github it&#8217;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).<\/p>\n<pre><!--?php\n\nuse Phalcon\\Loader;\nuse Phalcon\\Tag;\nuse Phalcon\\Mvc\\Url;\nuse Phalcon\\Mvc\\View;\nuse Phalcon\\Mvc\\Application;\nuse Phalcon\\DI\\FactoryDefault;\nuse Phalcon\\Db\\Adapter\\Pdo\\Mysql as DbAdapter;\n\ntry {\n\n    \/\/ Register an autoloader\n    $loader = new Loader();\n    $loader--->registerDirs(\narray(\n'..\/private\/app\/controllers\/',\n'..\/private\/app\/models\/'\n)\n)-&gt;register();\n\n\/\/ Create a DI\n$di = new FactoryDefault();\n\n\/\/ Set the database service\n$di['db'] = function() {\nreturn new DbAdapter(array(\n\"host\" =&gt; \"localhost\",\n\"username\" =&gt; \"root\",\n\"password\" =&gt; \"[password]\",\n\"dbname\" =&gt; \"tutorial\"\n));\n};\n\n\/\/ Setting up the view component\n$di['view'] = function() {\n$view = new View();\n$view-&gt;setViewsDir('..\/private\/app\/views\/');\nreturn $view;\n};\n\n\/\/ Setup a base URI so that all generated URIs include the \"tutorial\" folder\n$di['url'] = function() {\n$url = new Url();\n$url-&gt;setBaseUri('\/');\nreturn $url;\n};\n\n\/\/ Setup the tag helpers\n$di['tag'] = function() {\nreturn new Tag();\n};\n\n\/\/ Handle the request\n$application = new Application($di);\n\necho $application-&gt;handle()-&gt;getContent();\n} catch (Exception $e) {\necho \"Exception: \", $e-&gt;getMessage();\n}\n<\/pre>\n<p>As you can see by the code above, we&#8217;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.<\/p>\n<p>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]<\/p>\n<p>Replace the contents of your vhost file with the following:<\/p>\n<pre>server {\nlisten 80;\nserver_name www.[domain];\nreturn 301 $scheme:\/\/[domain]$request_uri;\n}\n\nserver {\nlisten 80;\nserver_name [domain];\n\nindex index.php index.html index.htm;\nset $root_path '\/home\/[user]\/domains\/[domain]\/public\/';\nroot $root_path;\n\ntry_files $uri $uri\/ @rewrite;\n\nlocation @rewrite {\nrewrite ^\/(.*)$ \/index.php?_url=\/$1;\n}\n\naccess_log \/home\/[user\/domains\/[domain]\/logs\/access.log;\nerror_log \/home\/[user]\/domains\/[domain]\/logs\/error.log;\n\nlocation ~ \\.php$ {\nfastcgi_pass unix:\/var\/run\/php5-fpm.sock;\nfastcgi_index \/index.php;\n\ninclude \/etc\/nginx\/fastcgi_params;\n\nfastcgi_split_path_info ^(.+\\.php)(\/.+)$;\nfastcgi_param PATH_INFO $fastcgi_path_info;\nfastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;\nfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n}\n\nlocation ~* ^\/(css|img|js|flv|swf|download)\/(.+)$ {\nroot $root_path;\n}\n\nlocation ~ \/\\.ht {\ndeny all;\n}\n}\n<\/pre>\n<p>This should be all you need to get your Phalcon project loading via nginx. Restart nginx and php5-fpm for good measure.<\/p>\n<pre>sudo service nginx restart\nsudo service php5-fpm restart\n<\/pre>\n<p>Load the domain in your browser and you should see a page that has &#8220;Hello&#8221; 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!<\/p>\n<h3>Git (optional)<\/h3>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Download article as PDF Phalcon PHP is a very impressive PHP MVC Framework that I&#8217;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<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"http:\/\/devroar.com\/index.php\/2014\/11\/20\/installing-configuring-phalconphp\/\">Read more &#8250;<\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,53],"tags":[85,130,140,136,135,129,20,142,141,133,138,137,134,45,139],"_links":{"self":[{"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/posts\/222"}],"collection":[{"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":15,"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":277,"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions\/277"}],"wp:attachment":[{"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/devroar.com\/index.php\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}