Codeigniter, Nginx and Postgres for noobies like me.

Install PHP-FPM and Codeigniter on top of Nginx

April 20, 2009 in Codeigniter, Nginx by ElToro

This post handles the PHP installation and compilation part, you need to have Nginx already up and running. PHP-FRM performs really as with Nginx so this is really good choise for highly responsive Web Site. First you need to get PHP sources.  The PHP version you want is 5.2.8 because there is PHP-FRM patch matching with that version.

Requirement

  1. Nginx installed
  2. Postgres installed (not really needed)
  3. Linux, I am using Debian based Kubuntu, but any debian based distros should be fine
  4. Build tools

 

1. Get Sources

This is easy part just look for closest repository to get PHP 5.2.8 version sources. Download using browser or wget.  Today there is newer PHP release available but because there is not matching PHP-FPM patch available so we need to get the matching pair.

 

wget http://fi2.php.net/get/php-5.2.8.tar.gz/from/fi.php.net/mirror

 

Download the matching PHP-FPM software / patch and today it is the php-5.2.8-fpm-0.5.10.diff.gz

 

wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz

 

2. Untar and Patch PHP sources with PHP-FPM

Now you need to use some shell commands to UnTar and Gunzip and patch your packages. Patching is breezing easy when your software versions match. Just do the following command in folder where you dowloaded the PHP and PHP-FPM packages. You may also want store you sources in /usr/src folder but you do not need to. Any folder will do.

 

tar xzvf php-5.2.8.tar.gz 
gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.8 -p1

 

If you did not get any errors as you should not. You now have php-5.2.8 folder which source files are patched with matching php-fpm codes and fixes.

3. Build PHP binaries with PHP-FPM included

This is also pretty easy to do, but there is some choises to make, which makes this thing actually a bit nasty. You need to configure you PHP build to match your need, but my advise is to use some know setup for your configuration. Eg. Check what debian is doing in the default build and it should be really good starting point. You should use the configuration flags what are good for your setups. Here is some basic setup to get you started.

 

 cd php-5.2.8
./configure --enable-fastcgi --enable-fpm --with-pgsql --with-mcrypt --with-zlib --enable-mbstring --with-openssl --with-gd --with-jpeg-dir=/usr/lib --enable-gd-native-ttf --without-sqlite --disable-pdo --disable-reflection
make all
make install
strip /usr/local/bin/php-cgi

 

 

4. Configure the settings

Settings file for the PHP-FPM can be found from /usr/local/etc/php-fpm.conf Edit the file and change atleast the Unix user of processes and group to mach your server setup. You can use eg the www-user, www-data, but I am using these.

nano /usr/local/etc/php-fpm.conf
Unix user of processes
nginx
Unix group of processes
nginx

Copy the php.ini in to your PHP inlclude_path: /usr/local/lib/php. You will find the base file from your php-5.2.8 folder names as php.ini-recommended. Copy it to your PHP include folder.

 

cp php.ini-recommended /usr/local/lib/php/php.ini

 

Edit php.ini to match your needs. Php.ini-recommended file can be found from the php-5.2.8 folder.

5. Codeigniter get it running

Download the latest CodeIgniter framework 1.7.1 and unzip it in to your public web forder. Eg: in to /var/www/codetest. Then you need to configure CodeIgniter framework and Nginx configs to get things running.

 

unzip CodeIgniter_1.7.1.zip /var/www/
mv /var/www/CodeIgniter_1.7.1 /var/www/codetest

 

Only changes you need to do is, to remove index.php from CodeIgniter configs. This default stuff will work with out this change, but you will need this in some point

Change this line in /var/www/codetest/system/application/config/config.php file

 

//$config['index_page'] = "index.php";
$config['index_page'] = "";

 

6. Finish PHP-FPM and CodeIgniter install

You also want / need to configure your Nginx scripts to make Nginx forward your queries to PHP-FPM parser.

 

nano nginx.conf
worker_processes  2;

events { worker_connections  1024; }

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

server {
    listen       80;
    server_name  localhost;
 root   /var/www;

//CodeIgniter framework is unzipped in /var/www/codetest folder
//If URI is not found fall back to @codeigniter definition
location /codetest {
    try_files $uri @codeigniter
    }

//Here we know that file matching URI did not found, 
//so we want to use index.php as front end and pass those to php-fpm
location @codeigniter {
 fastcgi_split_path_info ^(/codetest)(.*)$;
 include /etc/nginx/fastcgi_params;
 fastcgi_pass   127.0.0.1:9000;
 //Override some default parametr from fatcgi_params file
 fastcgi_param  SCRIPT_FILENAME    $document_root/codetest/index.php;
 fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
 fastcgi_param  PATH_INFO          $fastcgi_path_info;
}}}

 

Start the PHP-FPM from command line.

 

php-fpm start

 

After testing the PHP-FPM and if you are happy remember to add those to your init scripts.

After changing the nginx.conf file reload those changes by using, the nginx init.d script, or by manually restarting nginx.

sudo /etc/init.d/nginx reload

7. Test your changes

Browse to http://localhost/codetest and you should see CodeIngiter welcome page. If you see all done, you can continue to study Nginx.conf or CodeIniter further.

X. Possible errors during the process

You may get some depencier error during the configuration / compilation process but do not panic!

configure: error: XML configuration could not be found

apt-get install libxml2-dev

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

apt-get install libmcrypt-dev

configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path

apt-get install postgresql-server-dev-8.3

 

 

Leave a reply

Feel free to share your thoughts about this entry.

Nickname
Email
Website
Confirmation image
Confirmation code
Comment