Running PHP as CGI in OpenSolaris 2009.06
- 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=workersriramn@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.)
|
|
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.


Copying the PHP binary can be avoided, resolving any concerns with future pkg updates.
Here's an example:
$ cat /etc/apache2/2.2/conf.d/php-as-cgi.conf
Alias /phpapp /export/home/trawick/phpapp
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>
Posted by Jeff Trawick on July 17, 2009 at 07:00 AM PDT #
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
Susan
http://8080proxy.com
Posted by Susan on July 20, 2009 at 07:46 AM PDT #