Web Server: Using FastCGIProxy to Reverse Proxy to Native PHP

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cookbook}} {{Integration}}
+
{{WebServer}} {{Integration}} {{Cookbook}}
  
 
This cookbook will show you how to execute local CGI scripts through Resin using [http://en.wikipedia.org/wiki/FastCGI FastCGI].  We'll use php-fpm as the FastCGI process manager and map *.php to Resin's included FastCGI servlet.  Finally, it will provide instruction on setting up WordPress to run in Resin using FastCGI.
 
This cookbook will show you how to execute local CGI scripts through Resin using [http://en.wikipedia.org/wiki/FastCGI FastCGI].  We'll use php-fpm as the FastCGI process manager and map *.php to Resin's included FastCGI servlet.  Finally, it will provide instruction on setting up WordPress to run in Resin using FastCGI.
  
 
For standard CGI, see [[Application Server: Native PHP With CGIServlet]].
 
For standard CGI, see [[Application Server: Native PHP With CGIServlet]].
 +
 +
''Keep in mind that Resin comes with [[http://quercus.caucho.com/ Quercus]], Caucho's 100% Java implementation of PHP5.  You may not need native PHP if Quercus works for your application.
  
 
== Executing PHP with FastCGI ==
 
== Executing PHP with FastCGI ==
Line 14: Line 16:
 
php-fpm will startup by default listening on the local address on port 9000, which works fine for us.
 
php-fpm will startup by default listening on the local address on port 9000, which works fine for us.
  
The example below demonstrates how to configure Resin to execute PHP via FastCGI.  Keep in bind that Resin comes with [[http://quercus.caucho.com/ Quercus]], Caucho's 100% Java implementation of PHP5.  You may not need native PHP if Quercus works for your application.
 
  
''web-inf/resin-web.xml:''
+
The example below provides a simple Resin configuration with /var/www as the root (htdocs) directory where PHP content is located. 
  <web-app xmlns="http://caucho.com/ns/resin"
+
 
        xmlns:ee="urn:java:ee"
+
(This is an "Apache" style configuration, where all documents located in /var/www are web content.  You can also configure FastCgi in a single Java web-application by adding <resin:FastCgiProxy>to WEB-INF/resin-web.xml.)
        xmlns:resin="urn:java:com.caucho.resin">
+
 
 +
''conf/resin.xml:
 +
  <resin xmlns="http://caucho.com/ns/resin"
 +
      xmlns:resin="urn:java:com.caucho.resin">
 
   
 
   
   <servlet servlet-name='php-fcgi-servlet' servlet-class='com.caucho.servlets.FastCGIServlet'>
+
   <log-handler name="" level="all" path="stdout:"
  <init server-address="localhost:9000"/>
+
              timestamp="[%y-%m-%d %H:%M:%S.%s]"
   </servlet>
+
              format=" {${thread}} ${log.message}"/>
 +
               
 +
   <logger name="" level="info"/>
 
   
 
   
   <servlet-mapping url-pattern='/*.php' servlet-name='php-fcgi-servlet'/>
+
   <cluster-default>
 +
    <resin:import path="classpath:META-INF/caucho/app-default.xml"/>
 +
  </cluster-default>
 
   
 
   
</web-app>
+
  <cluster id="">
 +
    <server id="">
 +
      <http port="8080"/>
 +
    </server>
 +
    <host id="" root-directory=".">
 +
      <web-app id="/" root-directory="/var/www">
 +
        <resin:FastCgiProxy regexp="\.php">
 +
          <address>localhost:9000</address>
 +
        </resin:FastCgiProxy>
 +
      </web-app>
 +
    </host>
 +
  </cluster>
 +
 
 +
</resin>
  
Create a simple test php file in your webapp's root directory, as follows:
+
Create a simple test php file in /var/www, as follows:
  
 
''test.php
 
''test.php
 
  <?php phpinfo(); ?>
 
  <?php phpinfo(); ?>
  
And then access the URL http://127.0.0.1:8080/yourwebapp/test.php and you will see something like this:
+
And then access the URL http://127.0.0.1:8080/test.php and you will see something like this:
  
 
[[File:Php-info-fpm.png]]
 
[[File:Php-info-fpm.png]]
Line 40: Line 61:
 
Notice "Server API" shows "FPM/FastCGI".     
 
Notice "Server API" shows "FPM/FastCGI".     
  
(As an interesting exercise, try commenting out the servlet-mapping in resin-web.xml, and access the page again.  It should still work, but the page now show the Quercus PHP info display instead:)
+
As an interesting exercise, try commenting out <resin:FastCgiProxy> in resin.xml, and access the page again.  It should still work, but the page now show the Quercus PHP info display instead.  All *.php is mapped to QuercusServlet by default in app-default.xml.  Your resin-web.xml overrides this mapping, but the webapp will fallback to QuercusServlet if not set.
  
 
[[File:Php-info-quercus.png]]
 
[[File:Php-info-quercus.png]]
 
All *.php is mapped to QuercusServlet by default in app-default.xml.  Your resin-web.xml overrides this mapping, but the webapp will fallback to QuercusServlet if not set.
 
  
 
== WordPress on Resin with FastCGI and php-fpm ==
 
== WordPress on Resin with FastCGI and php-fpm ==
Line 53: Line 72:
 
  # PHP requires the php-mysql extension
 
  # PHP requires the php-mysql extension
 
  # WordPress requires sendmail
 
  # WordPress requires sendmail
 
+
 
  # Startup php-fpm
 
  # Startup php-fpm
 
  php-fpm
 
  php-fpm
Line 82: Line 101:
 
  # Startup Resin
 
  # Startup Resin
 
  resinctl start
 
  resinctl start
 
# Access the URL http://127.0.0.1:8080/wp-admin/install.php
 
  
You can now setup WordPress using the install page:
+
 
 +
Access the URL http://127.0.0.1:8080/wp-admin/install.php :
  
 
[[File:Wordpress-install.png]]
 
[[File:Wordpress-install.png]]

Latest revision as of 00:00, 7 February 2012

Web-48.pngShare-48.pngCookbook-48.png

This cookbook will show you how to execute local CGI scripts through Resin using FastCGI. We'll use php-fpm as the FastCGI process manager and map *.php to Resin's included FastCGI servlet. Finally, it will provide instruction on setting up WordPress to run in Resin using FastCGI.

For standard CGI, see Application Server: Native PHP With CGIServlet.

Keep in mind that Resin comes with [Quercus], Caucho's 100% Java implementation of PHP5. You may not need native PHP if Quercus works for your application.

Executing PHP with FastCGI

Setup of FastCGI is sightly more complex than basic CGI, since you need to install php-fpm. php-fpm is PHP's FastCGI process manager. It DOES NOT come with PHP by default. It must be enabled as a compile time option using the flag "--enable-fpm". See PHP-FPM Install documentation for more information. (The IUS Community Project has a php-fpm RPM available.)

# Startup php-fpm
php-fpm

php-fpm will startup by default listening on the local address on port 9000, which works fine for us.


The example below provides a simple Resin configuration with /var/www as the root (htdocs) directory where PHP content is located.

(This is an "Apache" style configuration, where all documents located in /var/www are web content. You can also configure FastCgi in a single Java web-application by adding <resin:FastCgiProxy>to WEB-INF/resin-web.xml.)

conf/resin.xml:

<resin xmlns="http://caucho.com/ns/resin"
      xmlns:resin="urn:java:com.caucho.resin">

 <log-handler name="" level="all" path="stdout:"
              timestamp="[%y-%m-%d %H:%M:%S.%s]"
              format=" {${thread}} ${log.message}"/>
               
 <logger name="" level="info"/>

 <cluster-default>
   <resin:import path="classpath:META-INF/caucho/app-default.xml"/>
 </cluster-default>

 <cluster id="">
   <server id="">
     <http port="8080"/>
   </server>
   <host id="" root-directory=".">
     <web-app id="/" root-directory="/var/www">
       <resin:FastCgiProxy regexp="\.php">
         <address>localhost:9000</address>
       </resin:FastCgiProxy>
     </web-app>
   </host>
 </cluster>
  
</resin>

Create a simple test php file in /var/www, as follows:

test.php

<?php phpinfo(); ?>

And then access the URL http://127.0.0.1:8080/test.php and you will see something like this:

Php-info-fpm.png

Notice "Server API" shows "FPM/FastCGI".

As an interesting exercise, try commenting out <resin:FastCgiProxy> in resin.xml, and access the page again. It should still work, but the page now show the Quercus PHP info display instead. All *.php is mapped to QuercusServlet by default in app-default.xml. Your resin-web.xml overrides this mapping, but the webapp will fallback to QuercusServlet if not set.

Php-info-quercus.png

WordPress on Resin with FastCGI and php-fpm

These instructions were tested with Resin 4.0.25 Professional, PHP 5.3.10, and WordPress 3.3.1.

# Before starting, create an empty mysql database for your WordPress tables
# PHP requires the php-mysql extension
# WordPress requires sendmail

# Startup php-fpm
php-fpm

# Shutdown resin 
resinctl stop

# Backup the packaged ROOT webapp directory: 
cd /var/www/webapps 
mv ROOT ROOT.bak

# Download and extract Wordpress:
wget http://wordpress.org/latest.zip
unzip latest.zip

# Rename wordpress to ROOT so that it is served as the ROOT webapp (/ context)
mv wordpress ROOT

# Create resin-web.xml
cd ROOT
mkdir WEB-INF
vi WEB-INF/resin-web.xml (copy/paste from the example above)

# Configure WordPress
cp wp-config-sample.php wp-config.php
vi wp-config.php (modify DB_NAME, DB_USER, DB_PASSWORD, DB_HOST)

# Startup Resin
resinctl start


Access the URL http://127.0.0.1:8080/wp-admin/install.php :

Wordpress-install.png

Personal tools
TOOLBOX
LANGUAGES