Quantcast
Viewing all articles
Browse latest Browse all 2

Linux Slackware 13.0 + Nginx + PHP FastCGI using PHP-FPM

While Apache is a great web server, it has a high memory footprint. If you don’t need all the Apache features or if you have a slow/old/low memory machine Nginx (Engine X) might be perfect.

The typical nginx configuration involves using spawn-fcgi from the LightTPD project to get nginx serving up PHP. There are a few issues with spawn-fcgi which I’m not going to cover here.
In order to avoid these issues I’m going to use PHP-FPM. PHP-FPM stands for PHP FastCGI Process Manager. It is actually a set of patches for the PHP source that bake in FastCGI process management into the PHP binaries.

PHP-FPM requires libevent to be installed. Since Slackware doesn’t include a libevent package it has to be build from sources. In this tutorial I’m using libevent-1.4.12. An attached slackware package of libevent-1.4.12 is available at the end of this post.

cd /usr/local/src
wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
tar -xzvf libevent-1.4.12-stable.tar.gz
./configure --prefix=/usr
make
make install


Now we’re going to get php-fpm package according to the php version we’re going to build. I’m gonna use php-5.3.0 so I need php-fpm-0.6-5.3.0.tar.gz. You can get different versions from php-fpm website.

cd /usr/local/src
wget http://launchpad.net/php-fpm/master/0.6/+download/php-fpm-0.6-5.3.0.tar.gz
tar -xzvf php-fpm-0.6-5.3.0.tar.gz

Php-fpm can be built into php or standalone. According to php-fpm readme it’s recommended to be build into php.
So, we’re going to generate fpm patch for php:

php-fpm-0.6-5.3.0/generate-fpm-patch

That should give you fpm.patch file.

Now we’re going to download php source and patch it with fpm:

wget http://www.php.net/get/php-5.3.0.tar.gz/from/ro2.php.net/mirror
tar -xzvf php-5.3.0.tar.gz
cd php-5.3.0
patch -p1 < ../fpm.patch

We’re going to configure it slackware style, with as many things as possible included:

./buildconf --force

EXTENSION_DIR=/usr/lib/php/extensions \
CFLAGS="-O2 -march=i486 -mtune=i686" \
./configure \
--prefix=/usr \
--sysconfdir=/etc \
--with-fpm \
--with-libevent=/usr \
--enable-fastcgi \
--enable-discard-path \
--enable-force-cgi-redirect \
--disable-safe-mode \
--enable-apc \
--enable-apc-mmap \
--enable-memory-limit \
--enable-suhosin \
--disable-magic-quotes \
--enable-zend-multibyte \
--enable-mbregex \
--enable-tokenizer=shared \
--with-config-file-scan-dir=/etc/php \
--with-config-file-path=/etc/httpd \
--with-mod_charset \
--with-layout=PHP \
--enable-sigchild \
--enable-xml \
--with-libxml-dir=/usr \
--enable-simplexml \
--enable-spl \
--enable-filter \
--disable-debug \
--with-openssl=shared \
--with-pcre-regex=/usr \
--with-zlib=shared,/usr \
--enable-bcmath=shared \
--with-bz2=shared,/usr \
--enable-calendar=shared \
--enable-ctype=shared \
--with-curl=shared \
--with-curlwrappers \
--enable-dba=shared \
--with-gdbm=/usr \
--with-db4=/usr \
--enable-exif=shared \
--enable-ftp=shared \
--with-gd=shared \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-zlib-dir=/usr \
--with-xpm-dir=/usr \
--with-freetype-dir=/usr \
--with-t1lib=/usr \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext=shared,/usr \
--with-gmp=shared,/usr \
--with-iconv=shared \
--with-ldap=shared \
--enable-mbstring=shared \
--enable-hash \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--enable-pdo \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite=shared \
--with-pspell=shared,/usr \
--with-mm=/usr \
--enable-shmop=shared \
--with-snmp=shared,/usr \
--enable-soap=shared \
--enable-sockets \
--with-sqlite=shared \
--enable-sqlite-utf8 \
--with-regex=php \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx=shared \
--with-xsl=shared,/usr \
--enable-zip=shared \
--with-tsrm-pthreads \
--enable-shared=yes \
--enable-static=no \
--with-gnu-ld \
--with-pic \
--with-mcrypt=shared

Notice the following options used:

--with-fpm \
--with-libevent=/usr \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-fastcgi \
--enable-discard-path \
--enable-force-cgi-redirect \
--with-mcrypt=shared

If you don’t have mcrypt installed, you can skip that line.

PHP 5.3.0 is the first version of php which includes mysqlnd.
According to php.net, mysqlnd is a new core library shipped with PHP. It is a PHP-specific replacement for libmysql. mysqlnd will be used to build the mysql, mysqli and PDO_MySQL extensions if libmysql isnt found on the system. It may also be used instead of libmysql even when libmysql is present. mysqlnd is recommended for all PHP installations for performance reasons.

If everything went well, we can proceed to build php-5.3.0:

make
make install

At this point you should edit your /etc/httpd/php.ini file as mhash extension is no more provided with php-5.3.0. Also dbase extension have been moved in PECL and it appears to be a bug with pdo_sqlite extension built as shared. So comment out mhash.so, dbase.so and pdo_sqlite.so. It’s recommended to comment out (disable) all the extensions which are not going to be used.

A slackware (13.0) package of php-5.3.0 configured with php-fpm and fastcgi using options above is available at the end of this post.

If everything went well, php -v and php-fpm -v should give you the following output:

root@hopeseekr:/# php -v
PHP 5.3.0 (cli) (built: Oct 27 2009 20:07:27)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

root@hopeseekr:/# php-fpm -v
PHP 5.3.0 (cgi-fcgi) (built: Oct 27 2009 19:31:19)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

root@hopeseekr:/# php --ini
Configuration File (php.ini) Path: /etc/httpd
Loaded Configuration File:         /etc/httpd/php.ini
Scan for additional .ini files in: /etc/php
Additional .ini files parsed:      (none)

Because it’s not such a good practice to run programs as root, create the following user:

groupadd nginx
useradd -g nginx -d /storage/htdocs -s /bin/false nginx

Open up /etc/php-fpm.conf in your editor of choice. Do some searching and change the users who own the processes to nginx. The file is too big to post in here, but the values we want to change are on lines 51, 52, 63 and 66. Should look something like:

<value name="owner">nginx</value>
<value name="group">nginx</value>
<value name="user">nginx</value>
<value name="group">nginx</value>

Now that we’re done with php, we have to move on to Nginx.

Go to Nginx website and get the latest version of Nginx. In this tutorial I’m using nginx 0.7.63:

cd /usr/local/src
wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz

First of all let’s create the following folders just to be sure we’re not going to have errors later.

mkdir /etc/nginx
mkdir /var/log/nginx
mkdir /var/run/nginx

Now, we are going to unzip/configure and build nginx.

tar -xzvf nginx-0.7.63.tar.gz
cd nginx-0.7.63
./configure --prefix=/usr \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid

make
make install

A slackware (13.0) package of nginx-0.7.63 is available at the end of this post.

The next step is to edit /etc/nginx/nginx.conf file. Here it is the file i’m using:

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log;

pid /var/run/nginx/nginx.pid;

events {
    worker_connections  1024;
}

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

  server {
    listen       86.55.39.34:80 default;
    server_name  _;
    access_log /var/log/nginx/access.log main;
    root /storage/htdocs;
    index  index.php index.html index.htm;
    #autoindex on;

    location ~ .php$ {
       fastcgi_pass 127.0.0.1:9000;
       #fastcgi_index index.php;
       include /etc/nginx/fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

  }

}

If you want to find out more about nginx config, check out http://wiki.nginx.org/Main.

Now we have to set up some FastCGI parameters so that PHP doesn’t choke and you’ll avoid random 503 errors from nginx. Open up /etc/nginx/fastcgi_params and add the following:

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’re almost ready. Let’s create a nice slackware-like script to start nginx. Save the following in /etc/rc.d/rc.nginx:

#!/bin/sh
#
# /etc/rc.d/rc.nginx
#
# Start/stop/restart the nginx web server.

nginx_start() {
 /usr/sbin/nginx -c /etc/nginx/nginx.conf
}

nginx_stop() {
 killall nginx
 rm -f /var/run/nginx/nginx.pid
}

nginx_restart() {
 nginx_stop
 nginx_start
}

case "$1" in
  'start')
    nginx_start
  ;;
  'stop')
    nginx_stop
  ;;
  'restart')
    nginx_restart
  ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
  ;;
esac

Don’t forget to make the file executable:

chmod 755 /etc/rc.d/rc.nginx

Now we’re ready to start our new web server:

/etc/rc.d/init.d/php-fpm start
/etc/rc.d/rc.nginx start

If everything went well you should see the following processes:

 2733 ?        Ss     0:00 /usr/bin/php-fpm --fpm-config /etc/php-fpm.conf
 2734 ?        S      0:00  \_ /usr/bin/php-fpm --fpm-config /etc/php-fpm.conf
 2735 ?        S      0:00  \_ /usr/bin/php-fpm --fpm-config /etc/php-fpm.conf
 2736 ?        S      0:00  \_ /usr/bin/php-fpm --fpm-config /etc/php-fpm.conf
 2737 ?        S      0:00  \_ /usr/bin/php-fpm --fpm-config /etc/php-fpm.conf
 2738 ?        S      0:00  \_ /usr/bin/php-fpm --fpm-config /etc/php-fpm.conf
 2756 ?        Ss     0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
 2757 ?        S      0:00  \_ nginx: worker process
 2758 ?        S      0:00  \_ nginx: worker process

It’s a good idea to use a php cacher. Feel free to use the instructions from apache tutorial in order to install APC.

Here there are the Slackware 13.0 packages of the above programs:

libevent-1412-i486-1.txz
nginx-0763-i486-1.txz
php-530-i486-1.txz

Viewing all articles
Browse latest Browse all 2

Trending Articles