Home

stefano casazza edited this page Aug 4, 2015 · 4 revisions
Clone this wiki locally

#Running PHP scripts in ULib


Download and install the last release of ULib. I suggest for to have many info to configure ULib with debug (./configure --enable-debug ...). From the downloaded source copy the shell script php.sh, that you can found in ULib-1.4.2/tests/examples, to some working directory, say /var/www/example.


The first time we run ./php.sh inside /var/www/example we must have as result the following directory structure:

./php.sh

./userver.cfg

./err: php?.err

./log: php?.log

./out: php?.out

./www: info.php ./www/cgi-bin: index.php


####1) You can safely run PHP scripts using ULib’s CGI support.

Run ./php.sh with argument 1: (we have as output something like that)

stefano: /var/www/example # ./php.sh 1
PID = 26222
stefano: /var/www/example # curl http://localhost:8080/cgi-bin/index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
...
stefano: /var/www/example # kill 26222

The downside of this approach is the latency caused by the spawn of a new PHP interpreter at each request as we can see in ./log/php1.log:

03/08/15 18:47:51 (pid 18714)> request "/cgi-bin/index.php" run in 219 ms

####2) To improve performance we can rely on a separate PHP processor to handle PHP requests proxed by FastCGI protocol. Most often, this processing is handled with php-fpm.

See the official PHP documentation for fpm for all possible configuration options.

a) TCP socket (IP and port) approach

Edit php-fpm.conf in order to have listen = 127.0.0.1:9000

stefano: /var/www/example # /etc/init.d/php-fpm start
Starting PHP FastCGI Process Manager ...
stefano: /var/www/example # ./php.sh 2a
PID = 26222
stefano: /var/www/example # curl http://localhost:8080/info.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
...
stefano: /var/www/example # kill 26222; /etc/init.d/php-fpm stop
Stopping PHP FastCGI Process Manager ...

Now the latency is much better as we can see in ./log/php2a.log:

03/08/15 20:21:00 (pid 2183)> request "/info.php" run in 8 ms

b) unix domain socket (UDS) approach

Edit php-fpm.conf in order to have listen = /tmp/fcgi.socket

stefano: /var/www/example # /etc/init.d/php-fpm start
Starting PHP FastCGI Process Manager ...
stefano: /var/www/example # ./php.sh 2b
PID = 26222
stefano: /var/www/example # curl http://localhost:8080/info.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
...
stefano: /var/www/example # kill 26222; /etc/init.d/php-fpm stop
Stopping PHP FastCGI Process Manager ...

The latency is not changed as we can see in ./log/php2b.log:

03/08/15 20:21:00 (pid 2183)> request "/info.php" run in 8 ms 

####3) To get superior performance you will want to embed the PHP interpreter in the Ulib, ie to configure ULib with php embedded (./configure --with-php-embedded ...).

A bunch of distros (such as Fedora, Red Hat and CentOS) include a php-embedded package. Install it, along with php-devel and you should be able to build ULib with PHP interpreter embedded (Ubuntu 15.04: apt-get install libphp5-embed php5-dev).

Reinstall ULib and run ./php.sh with argument 3: (we have as output something like that)

stefano: /var/www/example # ./php.sh 3
PID = 26222
stefano: /var/www/example # curl http://localhost:8080/info.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
...
stefano: /var/www/example # kill 26222

The latency is now better as we can see in ./log/php3.log:

03/08/15 20:21:00 (pid 2183)> request "/info.php" run in 7 ms