nginx + PHP-FPM on

Debian 5.0 / Ubuntu 9.04

 

nginx-logo.png

Prologue

This guide will show you step by step how to get nginx with php-fpm up and running. This guide doesn’t require any deep-knowledge in Linux. I made this how to on Debian 5.0 Lenny but have been tested on Ubuntu 9.04 Jaunty without any problems.

In this guide I use nginx 0.7.62, php 5.3.0 and php-fpm 0.5.12. You will find links to both the packages and the configuration files in the text and in the right menu.

If you are using an earlier version of php 5.3.0 you can find suitable php-fpm patch here. And don’t forget to compile php with the “—enable-fastcgi” option.

If you have any questions feel free to email me on robin [at] nginx.se .

I’m currently working on a web-based tool which will generate a custom config file for nginx including v-hosts. You will soon be able to find this guide for FreeBSD.

 

 

 

Grab the latest updates for you system.

sudo apt update

sudo apt upgrade

 

Make sure your server is time accurate.

sudo apt-get install ntp ntpdate

 

Install some required tool for compiling and running nginx and php.

sudo apt-get install make bison flex gcc patch autoconf subversion locate unzip gettext libgd2-xpm

sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libjpeg-dev libpng-dev

sudo apt-get install mysql-client libmysqlclient15-dev

 

If you want to run MySQL database server on the same system execute these lines. This is optional.

sudo apt-get install mysql-server

You might want your MySQL server to accept incoming connections from any adress not just localhost(127.0.0.1).
Use your favorite editor and edit /etc/mysql/my.cnf

sudo nano /etc/mysql/my.cnf

Find the line containing bind-address = 127.0.0.1. Disable this line by putting # before the syntax.

#bind-address = 127.0.0.1

After you made any change in my.cnf you must restart MySQL.

sudo /etc/init.d/mysql restart

 

Go to the directory where you want to compile nginx and php. Usually /usr/local/src/ .

cd /usr/local/src/

Grab the source code for php and php-fpm. You can grab the packages used in this tutorial in the menu to the right, or by click on the links. I'm using php 5.3.0 (click here to download) and php-fpm 0.5.12 (click here to download).

sudo wget http://cdn2.rwl.se/nginx/php-5.3.0.tar.gz

sudo wget http://cdn2.rwl.se/nginx/php-5.3.0-fpm-0.5.12.diff.gz

Extract the tar.gz file containing php 5.3.0 source code.

sudo tar zvxf php-5.3.0.tar.gz

Patch php with php-fpm.

sudo gzip -cd php-5.3.0-fpm-0.5.12.diff.gz | sudo patch -d php-5.3.0 -p1

Go to the php directory and start compile.

cd php-5.3.0

sudo ./configure --enable-fpm --with-mysql --with-mysqli --with-gd --with-jpeg-dir --without-sqlite --with-gettext --with-mcrypt --with-zlib --enable-mbstring --without-pgsql --with-curl --disable-debug --enable-pic --disable-pdo --disable-rpath --enable-inline-optimization --with-bz2 --with-xml --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-xslt --enable-memcache --enable-zip --with-pcre-regex --disable-tokenizer --disable-filter --disable-reflection --without-unixODBC --without-odbc --disable-hash

sudo make all install

Not to some further modifications. Install memcache, apc etc etc.

sudo strip /usr/local/bin/php-cgi

sudo pecl install memcache

Question

Enable memcache session handler support? [yes] : *Press enter*

sudo pecl install apc-beta

Question

Enable per request file info about files used from the APC cache [no] : *Press enter*

Question

Enable spin locks (EXPERIMENTAL) [no] : *Press enter*

Prepare configuration file for php. You can either use php.ini-development or php.ini-production.

sudo cp /usr/local/src/php-5.3.0/php.ini-production /usr/local/lib/php/php.ini

sudo mkdir /etc/php/

sudo ln -s /usr/local/lib/php/php.ini /etc/php/php.ini

sudo ln -s /usr/local/etc/php-fpm.conf /etc/php/php-fpm.conf

Change some values in php-fpm.conf. Line 52,53. Click here to download prepped php-fpm.conf

sudo nano /etc/php/php-fpm.conf

www-data

www-data

You need to uncomment and edit line 63 and 66 by remove the surrounding

www-data

www-data

Enable APC in php.ini. Click here to download prepped php.ini

sudo nano /etc/php/php.ini

Add these lines at the end of the file.

[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 30
apc.max_file_size = 10M
apc.stat=1
extension=apc.so
extension=memcache.so
apc.filters = wp-cache-config

 

If you need to recompile php for some reason eg. add functionality.

cd /usr/local/src/php-5.3.0

sudo make clean

sudo rm config.cache

sudo ./configure .........

sudo make all install

 

Grab source code for nginx. You can download nginx 0.7.62 (click here to download).

cd /usr/local/src

sudo wget http://cdn2.rwl.se/nginx/nginx-0.7.62.tar.gz

Extract source code and enter directory.

sudo tar zxvf nginx-0.7.62.tar.gz && cd nginx-0.7.62

Compile nginx. Click here to read more about modules for nginx.

sudo ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module

sudo make && sudo make install

sudo ln -s /usr/local/nginx/conf /etc/nginx

Download/copy nginx configuration

sudo nano /etc/nginx/nginx.conf

*Delete all lines and paste new configuration*

Add the following lines to /etc/nginx/fastcgi_params

sudo nano /etc/nginx/fastcgi_params

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

Now we must create a initd script for start and stop nginx. Download/view init.d script here

sudo nano /etc/init.d/nginx

*paste code*

Create init.d script for php-fpm. Download/view init.d script here

sudo nano /etc/init.d/php-fpm

*paste code*

sudo chmod +x /etc/init.d/nginx /etc/init.d/php-fpm

Create directory for your www data (this is where you put websites). Usually /var/www

sudo mkdir /var/www

Create the directory for your sites configuration

sudo mkdir /usr/local/nginx/sites-enabled

sudo ln -s /usr/local/nginx/sites-enabled /etc/sites

Create your first site configuration. Download/view configuration here

*paste configuration*

Create a simple test file for your new site.

sudo echo "" > /var/www/index.php

Now everything should be ready and you just have to start php-fpm and nginx!

sudo /etc/init.d/php-fpm start

sudo /etc/init.d/nginx start

If you want to make nginx and php-fpm start on boot execute these lines

sudo update-rc.d nginx defaults

sudo update-rc.d php-fpm defaults

Now test your new nginx server http://ipaddr . You should be able to se some info about your php config. Enjoy!

Feel free to email me if you have any questions or encounter any problems. robin [at] nginx.se