{"id":48,"date":"2016-05-06T08:59:22","date_gmt":"2016-05-06T08:59:22","guid":{"rendered":"http:\/\/symbioticindia.in\/docu\/?p=48"},"modified":"2016-05-06T08:59:22","modified_gmt":"2016-05-06T08:59:22","slug":"run-your-php-applications-under-resin","status":"publish","type":"post","link":"http:\/\/symbioticindia.in\/docu\/2016\/05\/06\/run-your-php-applications-under-resin\/","title":{"rendered":"Run your PHP applications under Resin"},"content":{"rendered":"<p>I love the power of Java and of backend products like OpenJMS, Hibernate, Spring, JMX and so on. And I love the simple, immediate and effective approach of PHP when developing the web presentation layer. I always use both languages together, usually exposing Java services to the PHP applications thru Soap or Xml-Rpc.<br \/>\nBut how nice would it be to instantiate Java objects directly and transparently inside a php script?<\/p>\n<p>During the last years I&#8217;ve tried a number of times to use the Java extension released with PHP, but for different reasons I&#8217;ve never been fully satisfied with it.<br \/>\nRecently I&#8217;ve discovered Quercus, a 100% Java implementation of PHP integrated into <a href=\"http:\/\/www.caucho.com\/\">Resin<\/a>, the deservedly well known open source application server from Caucho. It&#8217;s immediately been a very productive experience and I&#8217;ve already moved some of my PHP applications to Resin\/Quercus quite smoothly.<br \/>\nHere I would like to explain step by step how to set up an environment as similar as possible to the original native PHP one.<\/p>\n<p><strong>How to<\/strong><br \/>\nI take for granted that you already have a proper JDK installed. Now you can download the open source version of Resin from <a href=\"http:\/\/www.caucho.com\/download\/\">Caucho<\/a> and uncompress it. From now on I&#8217;ll refer to resin&#8217;s main directory as &#8216;%RESIN%&#8217; for convenience.<br \/>\nThe directories <em>bin<\/em>,<em>conf<\/em> and <em>webapps<\/em> will contain what expected.<\/p>\n<p>Resin comes with a default web application, located in <em>webapps\/ROOT<\/em>. We&#8217;re going to use it for simplicity.<br \/>\nPHP files in resin are handled by QuercusServlet, so we need to instruct resin. Let&#8217;s create a file <em>web.xml<\/em> under <em>webapps\/ROOT\/WEB-INF<\/em> with the following content:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"><span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">web-app<\/span> <span style=\"color: #00aae2;\">xmlns<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">http:\/\/caucho.com\/ns\/resin<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n  <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">servlet-mapping<\/span> <span style=\"color: #00aae2;\">url-pattern<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">*.php<\/span><span style=\"color: #00cd00;\">\"<\/span>\r\n       <span style=\"color: #00aae2;\">servlet-class<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">com.caucho.quercus.servlet.QuercusServlet<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n       <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">init<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n               <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">compile<\/span><span style=\"color: #00aae2;\">&gt;<\/span>false<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">compile<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n       <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">init<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n  <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">servlet-mapping<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">web-app<\/span><span style=\"color: #00aae2;\">&gt;<\/span> <\/span><\/pre>\n<\/div>\n<p>This is all you need to run simple php scripts. Start\/restart Resin, put in <em>webapps\/ROOT\/<\/em> a, for instance, <em>test.php<\/em> with:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"><span style=\"color: #00aae2;\">&lt;?php<\/span>\r\n        <span style=\"color: #00cc99;\">phpinfo<\/span>()<span style=\"color: #00ffff;\">;<\/span>\r\n<span style=\"color: #00aae2;\">?&gt;<\/span>\r\n\r\n<\/span><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>and on your browser point to <em>http:\/\/127.0.0.1:8080\/test.php<\/em> to see the result of phpinfo(). Ok, this is quite simple, but not a &#8220;real world&#8221; example.<br \/>\nWhen working on php web applications, I usually have at least the following directory structure:<\/p>\n<ul>\n<li><em>site<\/em> which is the one publicly accessible(the document root)<\/li>\n<li><em>php<\/em> which contains accessory classes and libraries<\/li>\n<\/ul>\n<p>(I&#8217;m ignoring logs, templates etc. for now) and usually have a <em>prepend.php<\/em> file that runs before each script and is usually located in my <em>php<\/em> directory with my libraries.<br \/>\nMy <em>site<\/em> directory under resin is the document root (in this case <em>webapps\/ROOT\/<\/em>), so I don&#8217;t have to deal with it, but I need to set <em>include_path<\/em> and <em>auto_prepend<\/em>, normally defined in php.ini(or .htaccess) under Apache. Here we don&#8217;t have php.ini, but we can put them in web.xml. Our new web.xml will be:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"><span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">web-app<\/span> <span style=\"color: #00aae2;\">xmlns<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">http:\/\/caucho.com\/ns\/resin<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n  <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">servlet-mapping<\/span> <span style=\"color: #00aae2;\">url-pattern<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">*.php<\/span><span style=\"color: #00cd00;\">\"<\/span>\r\n       <span style=\"color: #00aae2;\">servlet-class<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">com.caucho.quercus.servlet.QuercusServlet<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n       <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">init<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n               <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">compile<\/span><span style=\"color: #00aae2;\">&gt;<\/span>false<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">compile<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n               <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">php-ini<\/span> \r\n                 <span style=\"color: #00aae2;\">include_path<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">.:%RESIN\/webapps\/ROOT\/WEB-INF\/php<\/span><span style=\"color: #00cd00;\">\"<\/span> \r\n                 <span style=\"color: #00aae2;\">auto_prepend_file<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">prepend.php<\/span><span style=\"color: #00cd00;\">\"<\/span> \r\n               <span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n       <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">init<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n  <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">servlet-mapping<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">web-app<\/span><span style=\"color: #00aae2;\">&gt;<\/span> <\/span><\/pre>\n<\/div>\n<p>In this case the search order for an <em>include() or require()<\/em> will be the current dir first and then <em>webapps\/ROOT\/WEB-INF\/php<\/em> (to be created). If you use PEAR or other classes, here you&#8217;ll add their paths. With <em>auto_prepend<\/em> we set a prepend file (to be created) to be searched in <em>include_path<\/em>.<br \/>\nNow our environment is much more similar to the one we are used with native PHP, but still a fundamental feature is missing: database connectivity.<br \/>\nDon&#8217;t worry, it&#8217;s again quite simple.<br \/>\nFirst, open <em>%RESIN%\/conf\/resin.conf<\/em> and find the database section. A sample pool configuration will be there commented. You should uncomment it and, if your db engine is for example Mysql, edit the configuration to something like this:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"> <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">database<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n   <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">jndi-name<\/span><span style=\"color: #00aae2;\">&gt;<\/span>jdbc\/mysql<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">jndi-name<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n   <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">driver<\/span> <span style=\"color: #00aae2;\">type<\/span><span style=\"color: #00aae2;\">=<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n     <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">url<\/span><span style=\"color: #00aae2;\">&gt;<\/span>jdbc:mysql:\/\/localhost:3306\/your_db_name<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">url<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n     <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">user<\/span><span style=\"color: #00aae2;\">&gt;<\/span>your_username<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">user<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n     <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">password<\/span><span style=\"color: #00aae2;\">&gt;<\/span>your_password<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">password<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n    <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">driver<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n    <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">prepared-statement-cache-size<\/span><span style=\"color: #00aae2;\">&gt;<\/span>8<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">prepared-statement-cache-size<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n    <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">max-connections<\/span><span style=\"color: #00aae2;\">&gt;<\/span>20<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">max-connections<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n    <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">max-idle-time<\/span><span style=\"color: #00aae2;\">&gt;<\/span>30s<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">max-idle-time<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n  <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">database<\/span><span style=\"color: #00aae2;\">&gt;<\/span>\r\n\r\n<\/span><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>and save. Don&#8217;t forget to download mysql <a href=\"http:\/\/www.mysql.com\/products\/connector\/j\/\">Connector\/J<\/a> and put the jar under <em>%RESIN%\/lib<\/em>.<br \/>\nThe last change to do is in our web.xml, add the following line inside the &#8220;&lt;init&gt;&#8221; section(where is also &lt;php-ini&gt;):<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"> <span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">database<\/span><span style=\"color: #00aae2;\">&gt;<\/span>jdbc\/mysql<span style=\"color: #00aae2;\">&lt;<\/span><span style=\"color: #00aae2;\">\/<\/span><span style=\"color: #00aae2;\">database<\/span><span style=\"color: #00aae2;\">&gt;<\/span> <\/span><\/pre>\n<\/div>\n<p>and save. That&#8217;s all. Now, provided that you have put the PEAR&#8217;s path in <em>include_path<\/em> and that PEAR DB module is installed, you can write a script like this:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"><span style=\"color: #00aae2;\">&lt;?php<\/span>\r\n        <span style=\"color: #ffa500;\">require_once<\/span> <span style=\"color: #00cd00;\">'<\/span><span style=\"color: #00cd00;\">DB.php<\/span><span style=\"color: #00cd00;\">'<\/span><span style=\"color: #00ffff;\">;<\/span>\r\n        <span style=\"color: #cc6600;\">$<\/span><span style=\"color: #cc6600;\">dbh<\/span> <span style=\"color: #00ffff;\">=<\/span> DB<span style=\"color: #00ffff;\">:<\/span><span style=\"color: #00ffff;\">:<\/span><span style=\"color: #00cc99;\">connect<\/span>(<span style=\"color: #00cd00;\">'<\/span><span style=\"color: #00cd00;\">mysql<\/span><span style=\"color: #00cd00;\">'<\/span><span style=\"color: #00ffff;\">,<\/span> <span style=\"color: #ffa500;\">false<\/span>)<span style=\"color: #00ffff;\">;<\/span>\r\n        <span style=\"color: #ffa500;\">if<\/span> (PEAR<span style=\"color: #00ffff;\">:<\/span><span style=\"color: #00ffff;\">:<\/span><span style=\"color: #b2dfee;\">isError<\/span>(<span style=\"color: #cc6600;\">$<\/span><span style=\"color: #cc6600;\">dbh<\/span>)) <span style=\"color: #00ffff;\">{<\/span>\r\n            <span style=\"color: #ffa500;\">die<\/span>(<span style=\"color: #66ccff;\"><strong>$dbh<\/strong><\/span><span style=\"color: #b2dfee;\">-&gt;getMessage<\/span>())<span style=\"color: #00ffff;\">;<\/span>\r\n        <span style=\"color: #00ffff;\">}<\/span>\r\n        <span style=\"color: #cc6600;\">$<\/span><span style=\"color: #cc6600;\">results<\/span><span style=\"color: #00ffff;\">=<\/span><span style=\"color: #00ffff;\">&amp;<\/span> <span style=\"color: #66ccff;\"><strong>$dbh<\/strong><\/span><span style=\"color: #b2dfee;\">-&gt;query<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">select<\/span> <span style=\"color: #00cd00;\">*<\/span> <span style=\"color: #00cd00;\">from<\/span> <span style=\"color: #00cd00;\">your_table<\/span><span style=\"color: #00cd00;\">\"<\/span>)<span style=\"color: #00ffff;\">;<\/span>\r\n        <span style=\"color: #ffa500;\">while<\/span>(<span style=\"color: #66ccff;\"><strong>$results<\/strong><\/span><span style=\"color: #b2dfee;\">-&gt;fetchInto<\/span>(<span style=\"color: #cc6600;\">$<\/span><span style=\"color: #cc6600;\">row<\/span><span style=\"color: #00ffff;\">,<\/span> DB_FETCHMODE_ASSOC))<span style=\"color: #00ffff;\">{<\/span>\r\n                <span style=\"color: #ffa500;\">echo<\/span> <span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">&lt;<\/span><span style=\"color: #00cd00;\">pre<\/span><span style=\"color: #00cd00;\">&gt;<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00ffff;\">;<\/span>\r\n                <span style=\"color: #00cc99;\">print_r<\/span>(<span style=\"color: #cc6600;\">$<\/span><span style=\"color: #cc6600;\">row<\/span>)<span style=\"color: #00ffff;\">;<\/span>\r\n                <span style=\"color: #ffa500;\">echo<\/span> <span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">&lt;<\/span><span style=\"color: #00cd00;\">\/<\/span><span style=\"color: #00cd00;\">pre<\/span><span style=\"color: #00cd00;\">&gt;<\/span><span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00ffff;\">;<\/span>\r\n        <span style=\"color: #00ffff;\">}<\/span>\r\n\r\n<span style=\"color: #00aae2;\">?&gt;<\/span> <\/span><\/pre>\n<\/div>\n<p>This is only the first half of the picture, as the most interesting part is the integration between Java and PHP, but this is another story&#8230;&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love the power of Java and of backend products like OpenJMS, Hibernate, Spring, JMX and so on. And I love the simple, immediate and effective approach of PHP when developing the web presentation layer. I always use both languages together, usually exposing Java services to the PHP applications thru Soap or Xml-Rpc. But how ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"http:\/\/symbioticindia.in\/docu\/2016\/05\/06\/run-your-php-applications-under-resin\/\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6,10],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-java","category-php","category-resin"],"_links":{"self":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":49,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/48\/revisions\/49"}],"wp:attachment":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}