Running PHP as CGI in OpenSolaris 2009.06

As part of Web Stack project, we deliver an integrated AMP stack within OpenSolaris where Apache 2.2.x, PHP 5.2.x and MySQL 5.1 is integrated together to work seamlessly. In this environment,
  • Apache is configured to run in pre-fork model
  • PHP is loaded as a module (mod_php5) within Apache (pre-fork) MPM.
  • Apache (worker) MPM model is not supported with PHP.

This particular scenario will work great for majority of users.

However, if you are one of those minority folks, who is interested in running PHP as CGI say with Apache 2.2 (worker) MPM, then here is what you need to do :

Configure Apache Server to start in worker (MPM) mode. This should provide you with better performance. This is how you can run Apache 2.2 in worker mode within OpenSolaris.

sriramn@opensolaris:~$ svcadm disable -s http:apache22

sriramn@opensolaris:~$ svccfg -s setprop http:apache22 httpd/server_type=worker

sriramn@opensolaris:~$ svcadm refresh http:apache22

sriramn@opensolaris:~$ svcadm enable -s http:apache22

Af course, to actually see any performance improvement, you will need to copy mpm.conf from samples directory(/etc/apache2/2.2/samples-conf.d/mpm.conf) to  /etc/apache2/2.2/conf.d/mpm.conf  and then tune this file accordingly.

Now that we have got Apache 2.2 worker MPM configured, if you try to run any PHP script within your document root directory, you will notice that they don't work. This is because, PHP module is not loaded if Apache is configured to run in worker MPM. (Remember, PHP modules and different extensions are not completely thread safe.) .

Now to get PHP scripts working again, here is what I did :

Copy PHP CGI script to the Apache's cgi-bin directory

sriramn@opensolaris:~$ pfexec cp /usr/php/bin/php-cgi  /var/apache2/2.2/cgi-bin/

sriramn@opensolaris:~$ pfexec chown webservd:webservd /var/apache2/2.2/cgi-bin/php-cgi

Now, you might want to create a file under /etc/apache2/2.2/conf.d/php-cgi.conf with the below content

#Create a alias for every location where you want to keep your PHP script.
ScriptAlias /contents/ "/var/www/contents/"
ScriptAlias /drupal/  "/var/www/drupal/"
AddHandler  php-cgi  .php
Action          php-cgi  /cgi-bin/php-cgi
<Directory /var/www/contents/>    
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/drupal/>
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>

(updated above content to reflect the feedback received from Jeff's comments to my earlier blog. Thanks a lot, Jeff.)

Here's a better example:

# Create the Alias for every php application that you want to deploy

Alias /phpapp /var/www/contents/

Alias /drupal /var/www/drupal/

ScriptAlias /php-cgi /usr/php/5.2/bin/php-cgi

<‚ĀěDirectory /usr/php/5.2/bin/>
 Order allow,deny
 Allow from all
</Directory>

<Location "/phpapp">
 Order allow,deny
 Allow from all
 AddHandler php-cgi .php
 Action php-cgi /php-cgi
</Location>

<Location "/drupal">
 Order allow,deny
 Allow from all
 AddHandler php-cgi .php
 Action php-cgi /php-cgi
</Location>

Finally, you will need to restart the server to allow this above configuration to be loaded. Now, server should be ready to process any php files as 'cgi' script under /var/www/contents directory

Hope you find this useful.

Comments:

Post a Comment:
Comments are closed for this entry.
About

Sharing what little I know

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today