Quantcast
Viewing all articles
Browse latest Browse all 2

Linux Slackware + Apache2 + PHP + mod_fastcgi

This tutorial assumes that you are already familiar with Slackware Linux and Apache2.

Before you start, make sure you have a working httpd.conf of Apache2 because a detailed configuration is not included in this tutorial. Then, remove the installed packages of Apache2 and PHP using Slackware’s pkgtool.

Go to Apache website and get the latest version of Apache2. In this tutorial I’m using Apache-2.2.9:

cd /usr/local/src
wget http://www.eu.apache.org/dist/httpd/httpd-2.2.9.tar.gz

Go to PHP website and get the latest version of PHP5. In this tutorial I’m using PHP-5.2.6

wget http://www.php.net/get/php-5.2.6.tar.gz/from/de.php.net/mirror

Go to FastCGI website and get the latest version of mod_fastcgi. In this tutorial I’m using mod_fastcgi-2.4.6:

wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz

Now, we are going to unzip/configure Apache. In this tutorial we are configuring it Slackware style, with as many things as possible included:

tar -xzvf httpd-2.2.9.tar.gz

./configure \
  --prefix=/usr \
  --sysconfdir=/etc/httpd \
  --libdir=/usr/lib/httpd \
  --libexecdir=/usr/lib/httpd/modules \
  --datadir=/srv/httpd \
  --localstatedir=/var\
  --with-mpm=worker \
  --with-apr=/usr \
  --with-apr-util=/usr \
  --enable-mods-shared=all \
  --enable-so \
  --enable-pie \
  --enable-cgi \
  --with-pcre \
  --enable-ssl \
  --enable-rewrite \
  --enable-vhost-alias \
  --enable-proxy \
  --enable-proxy-http \
  --enable-proxy-ftp \
  --enable-proxy-balancer \
  --enable-cache \
  --enable-mem-cache \
  --enable-file-cache \
  --enable-disk-cache \
  --disable-speling \
  --enable-dav \
  --enable-ldap \
  --enable-authnz-ldap \
  --enable-authn-anon \
  --enable-authn-alias

Please notice that we are going to use Apache mpm worker instead of the default prefork:

--with-mpm=worker \

For additional info about mpm-worker, feel free to visit: http://httpd.apache.org/docs/2.2/mod/worker.html

We are going to build apache:

make
make install

Unzip mod_fastcgi:

cd /usr/local/src
tar -xzvf mod_fastcgi-2.4.6.tar.gz

We are going to build mod_fastcgi according to it’s README:

cd mod_fastcgi-2.4.6
apxs -o mod_fastcgi.so -c *.c

Then, we will copy the binary into Apache’s modules folder:

cp .libs/mod_fastcgi.so /usr/lib/httpd/modules/mod_fastcgi.so

Now, you will have to edit /etc/httpd/httpd.conf and add the following line:

LoadModule fastcgi_module lib/httpd/modules/mod_fastcgi.so

Make sure you have the following files:

mkdir /var/run/httpd
mkdir /tmp/fcgi_ipc
chmod 777 /tmp/fcgi_ipc

Before continue with PHP configuration, make sure you have mcrypt/libmcrypt installed. If you don’t want to install mcrypt, just remove the following line from the php configure command:

--with-mcrypt=shared

Anyway, here there are the links where you can get a slackware packed version of mcrypt:

wget http://repository.slacky.eu/slackware-12.0/libraries/libmcrypt/2.5.8/libmcrypt-2.5.8-i486-1sl.tgz
wget http://darkstar.ist.utl.pt/slackware/addon/slacky/slackware-12.0/security/mcrypt/2.6.7/mcrypt-2.6.7-i486-1sl.tgz

installpkg libmcrypt-2.5.8-i486-1sl.tgz mcrypt-2.6.7-i486-1sl.tgz

Now, we are going to unzip/configure PHP. We are using almost the same configuration as Slackware’s default:

EXTENSION_DIR=/usr/lib/php/extensions \
CFLAGS="-O2 -march=i486 -mtune=i686" \
./configure \
--prefix=/usr \
--sysconfdir=/etc \
--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-dbase=shared \
--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 \
--with-hash \
--with-mhash=shared,/usr \
--with-mysql=shared,/usr \
--with-mysqli=shared,/usr/bin/mysql_config \
--enable-pdo=shared \
--with-pdo-mysql=shared,/usr \
--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

make
make install

Edit httpd.conf and add the following (after LoadModule directive):

FastCgiIpcDir /tmp/fcgi_ipc
AddHandler fastcgi-script .fcgi .fcg .fpl
FastCgiConfig -singleThreshold 100 -killInterval 300 -autoUpdate -idle-timeout 240 -pass-header HTTP_AUTHORIZATION

ScriptAlias /php-fcgi "/var/www/cgi-bin/php5.fcgi"

AddHandler application/x-httpd-php .php
Action application/x-httpd-php /php-fcgi

If you want to find more about mod_fastcgi options, visit: http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html

Create the file /var/www/cgi-bin/php5.fcgi:

#!/bin/sh
PHP_FCGI_CHILDREN=2
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=500
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php-cgi

Make sure the file is own by apache and is executable:

chown apache:apache /var/www/cgi-bin/php5.fcgi
chmod 755 /var/www/cgi-bin/php5.fcgi

Edit /etc/httpd/httpd.conf and make sure the following line is not commented:

Include /etc/httpd/extra/httpd-mpm.conf

Edit /etc/httpd/extra/httpd-mpm.conf and make sure the following lines are not commented:

StartServers          2
MaxClients          150
MinSpareThreads      25
MaxSpareThreads      75
ThreadsPerChild      25
MaxRequestsPerChild   0

At this point you should be ready to start apache:

apachectl start

Check /var/log/httpd/error_log to see if there are any errors.

[notice] FastCGI: process manager initialized (pid 20046)
[notice] Apache/2.2.9 (Unix) mod_fastcgi/2.4.6 configured -- resuming normal operations

If everything is ok, you should see the following processes after you run ps axf:

20511 ?        Ss     0:00 /usr/bin/httpd -k start
20512 ?        S      0:00  \_ /usr/bin/fcgi- -k start
20571 ?        Ss     0:00  |   \_ /usr/bin/php-cgi
20572 ?        S      0:00  |       \_ /usr/bin/php-cgi
20573 ?        S      0:00  |       \_ /usr/bin/php-cgi
20574 ?        S      0:00  |       \_ /usr/bin/php-cgi
20575 ?        S      0:00  |       \_ /usr/bin/php-cgi
20576 ?        S      0:00  |       \_ /usr/bin/php-cgi
20577 ?        S      0:00  |       \_ /usr/bin/php-cgi
20578 ?        S      0:00  |       \_ /usr/bin/php-cgi
20579 ?        S      0:00  |       \_ /usr/bin/php-cgi
20580 ?        S      0:00  |       \_ /usr/bin/php-cgi
20581 ?        S      0:00  |       \_ /usr/bin/php-cgi
20513 ?        Sl     0:00  \_ /usr/bin/httpd -k start
20541 ?        Sl     0:00  \_ /usr/bin/httpd -k start

If you want to boost the performance of your PHP scripts, you can use a php cacher. In the example below, I’m going to use APC.

Get the latest apc version available:

cd /usr/local/src
wget http://pecl.php.net/get/APC-3.0.19.tgz

Unzip/configure APC:

tar -xvf APC-3.0.19.tgz
cd APC-3.0.19
phpize
EXTENSION_DIR=/usr/lib/php/extensions \
CFLAGS="-O2 -march=i486 -mtune=i686" \
./configure \
--prefix=/usr \
--sysconfdir=/etc \
--enable-apc-mmap \
--with-php-config=/usr/bin/php-config

make
cp modules/apc.* /usr/lib/php/extensions/

Add the following lines in your /etc/php.ini:

extension=apc.so
apc.enabled=1
apc.shm_segments=1
apc.shm_size=128
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX

Now, you have to restart apache:

apachectl restart

Later edit:
After successfully running above setup on a box with medium/high traffic for a time, I saw that changing the number of PHP_FCGI_CHILDREN to 2 helped getting better performance and better loading times under high load.


Viewing all articles
Browse latest Browse all 2

Trending Articles