Getting php-cli to match php5-fpm so it behaves consistently under nginx

Nginx is very fast and simple to configure. Getting Python to play nice with it using WSGI is a breeze. PHP is a little tricky.

These notes should apply well to Ubuntu 12.04 upwards. You will need to modify them for other Linux versions.

Most of our web development now favours Python over PHP because of the superior productivity and ease of maintenance but we still have a few legacy PHP systems that need to run under Nginx. Configuring these is straightforward with something along the lines of:

    location /phpapp/ {
        alias /path/to/phpapp/;
        auth_basic "Please log in";
        auth_basic_user_file /path/to/phpapp/htpasswd;
        index index.php;
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;}}

This allows the php application to run in a protected directory. However...

Nginx is using php5-fpm which has its own php.ini files. If the php spawns processes using the command line (WordPerfect does this for example), these will use php-cli which has its own distinct php.ini. This will cause a lot of confusion and problems for anything other than the most trivial of php scripts.

php-cli needs to be modified in order to use the same configuration as the php5-fpm version of php to avoid this problem. According to the documentation, this requires recompilation of the php-cli package:

A simpler method is to replace the existing initialisation files for php-cli with the ones we want to use:

~ $ php -i | grep  'Loaded Configuration File\|additional \.ini files'
Loaded Configuration File => /etc/php5/cli/php.ini
Scan this dir for additional .ini files => /etc/php5/cli/conf.d

Use the output of phpinfo to determine the php-fpm init file and folder and then combine this with the above information as follows:

~ # cd /etc/php5/cli
/etc/php5/cli # mv php.ini php.ini.original
/etc/php5/cli # ln -s /etc/php5/fpm/php.ini
/etc/php5/cli # mv conf.d conf.d.original
/etc/php5/cli # ln -s /etc/php5/fpm/conf.d

All done. There is a possibility that software updates will override the fixes so you may want to put the links into a shell script that can easily be rerun whenever software updates are applied to the server.


Comments

There are currently no comments

New Comment

required

required (not published)

optional

Australia: 07 3103 2894

International: +61 410 545 357