<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>personal tech corner</title>
	<atom:link href="http://www.razvan.ws/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.razvan.ws</link>
	<description>docs, how to's and other tech stuff</description>
	<pubDate>Sat, 26 Mar 2011 10:06:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Slackware style init script for nginx web server</title>
		<link>http://www.razvan.ws/slackware-style-init-script-for-nginx-web-server/</link>
		<comments>http://www.razvan.ws/slackware-style-init-script-for-nginx-web-server/#comments</comments>
		<pubDate>Tue, 11 May 2010 18:23:17 +0000</pubDate>
		<dc:creator>Razvan</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[nginx]]></category>

		<category><![CDATA[slackware]]></category>

		<category><![CDATA[web server]]></category>

		<guid isPermaLink="false">http://www.razvan.ws/?p=143</guid>
		<description><![CDATA[Slackware style init script for start/stop/restart nginx web server:

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

test_config() {
  set +e
  RES=`nginx -t 2&#62;&#38;1`
  if [ $? -ne 0 ] ; then
     echo "Error in nginx configuration found, restart aborted!"
     echo $RES
     exit 10
 [...]]]></description>
			<content:encoded><![CDATA[<p>Slackware style init script for start/stop/restart nginx web server:</p>
<pre><code>
#!/bin/sh
#
# /etc/rc.d/rc.nginx
#
# Start/stop/restart nginx web server.

test_config() {
  set +e
  RES=`nginx -t 2&gt;&amp;1`
  if [ $? -ne 0 ] ; then
     echo "Error in nginx configuration found, restart aborted!"
     echo $RES
     exit 10
  fi
  set -e
}

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

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

nginx_restart() {
 test_config
 nginx_stop
 nginx_start
}

case "$1" in
  'start')
    nginx_start
  ;;
  'stop')
    nginx_stop
  ;;
  'restart')
    nginx_restart
  ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
  ;;
esac
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.razvan.ws/slackware-style-init-script-for-nginx-web-server/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using Nginx as a reverse proxy for Apache</title>
		<link>http://www.razvan.ws/using-nginx-as-a-reverse-proxy-for-apache/</link>
		<comments>http://www.razvan.ws/using-nginx-as-a-reverse-proxy-for-apache/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 11:44:24 +0000</pubDate>
		<dc:creator>Razvan</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Apache]]></category>

		<category><![CDATA[nginx]]></category>

		<category><![CDATA[reverse proxy]]></category>

		<category><![CDATA[slackware]]></category>

		<guid isPermaLink="false">http://www.razvan.ws/?p=135</guid>
		<description><![CDATA[We all know that Apache is a great software and it cannot be replaced every time. Also, Apache shown great performance serving dynamic php content whether it&#8217;s running mod_php or fastcgi. BoÅ¡tjan Å kufca made an excellent comparison between Apache + mod_php and Nginx + php-fpm.
So, Apache is very good serving dynamic content while nginx is [...]]]></description>
			<content:encoded><![CDATA[<p>We all know that <a href="http://www.apache.org/">Apache</a> is a great software and it cannot be replaced every time. Also, Apache shown great performance serving dynamic php content whether it&#8217;s running mod_php or fastcgi. <a href="http://blog.a2o.si/">BoÅ¡tjan Å kufca</a> made an excellent <a href="http://blog.a2o.si/2009/06/24/apache-mod_php-compared-to-nginx-php-fpm/">comparison between Apache + mod_php and Nginx + php-fpm</a>.</p>
<p>So, Apache is very good serving dynamic content while nginx is the best on serving static content. But is it possible to have the best from both worlds? Is it possible to use all the apache features (like native .htaccess support) while serving the static content of the website with nginx? Luckily yes.</p>
<p>First of all let&#8217;s see the general structure of Apache web server and why it would be a good idea to use a reverse proxy:<br />
1. Client initiates request to the Apache server.<br />
2. His browser connects to the Apache server.<br />
3. The Apache server creates new thread/process to handle the request.<br />
4. If client requested dynamic content, web server spawns CGI process or executes dynamic content handling module (i.e. mod_php) and waits while request will be processed. When it receives resulted web-page, it sends it back to the client.<br />
5. If client asked for some static file, web server sends this file to client<br />
6. Client&#8217;s browser receives answer, closes connection to web server and displays content.</p>
<p><span id="more-135"></span><br />
As we can see, when there are many requests coming to the web server, the server needs to create many parallel threads/processes and keep them running while client will close connection. If client has slow connection, web server process will wait too long and resource consumption will increase very fast. In the same time, if the website has lots of static content, Apache will create lots of parallel processes which will quickly eat up the memory only to serve a static file. If the money are not a problem we can continue to buy more and more RAM and more powerful processors.</p>
<p>However, there is a more efficient solution. We can use a small piece of software (<a href="http://nginx.net/">nginx</a> for example) in front of the Apache web server which will handle all the requests for static content and forward to Apache only the requests for dynamic content. This way Apache will open new threads only to process the dynamic requests and will close them quickly as it won&#8217;t need to wait after the client to close the connection (this is going to be nginx&#8217;s job).</p>
<p>But (there&#8217;s always a but) this approach will bring us the following problems:<br />
1. Each time a vhost is added to apache, we&#8217;ll have to add it also to nginx.<br />
2. Because nginx is a reverse proxy layer on top of Apache, Apache will think that all connections originate from the server running nginx. Every entry in the Apache access logs will appear to come from the IP of the nginx server and securing sessions by checking that a userâ€™s IP address hasnâ€™t changed becomes more difficult.</p>
<p>I believe the first problem doesn&#8217;t bring such great hassle. I&#8217;ll show you how to add vhosts to nginx and you&#8217;ll see it&#8217;s easy.<br />
It can even be created a small bash script which can read apache&#8217;s vhosts and generate the nginx&#8217;s ones. But this is not going to be covered in this post.</p>
<p>As for the second problem we&#8217;ll use the Apache mod_rpaf module to populate the REMOTE_ADDR using a special HTTP header inserted by nginx.</p>
<p>A typical request would work as follows:<br />
- 1.2.3.4 sends HTTP request to nginx server;<br />
- nginx determines that it needs to proxy pass the request to a back-end Apache server (e.g. by looking at the content-type or virtual host).<br />
- nginx adds an HTTP header â€œX-Forwarded-Forâ€ with the clientâ€™s real IP<br />
- nginx forwards (proxy_pass) the request to back-end Apache server<br />
- mod_rpaf in Apache detects that the request is coming from the nginx IP, then substitutes REMOTE_ADDR with the contents of X-Forwarded-For<br />
- Apache handles request as normal. Applications do not need to be aware of the reverse proxy.</p>
<p>Ok, enough with talking, let&#8217;s see how are we going to install nginx as a reverse proxy for a back-end Apache server. Of course we are going to use a <a href="http://www.slackware.com/">Slackware</a> server.</p>
<p>First of all we have to build mod_rpaf and configure apache to use it. Go to <a href="http://stderr.net/apache/rpaf/">mod_rpaf website</a> and get the latest version available. In this tutorial i&#8217;m going to use mod_rpaf 0.6.</p>
<pre><code>cd /usr/local/src
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
tar -xzvf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
</code></pre>
<p>This is going to install mod_rpaf-2.0.so on /usr/lib/httpd/modules/</p>
<p>Now edit /etc/httpd/httpd.conf and add the following:</p>
<pre><code>LoadModule rpaf_module lib/httpd/modules/mod_rpaf-2.0.so

RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1
RPAFheader X-Forwarded-For</code></pre>
<p>Also we have to configure apache to listen only on localhost:</p>
<pre><code>Listen 127.0.0.1:80</code></pre>
<p>Edit /etc/httpd/extra/httpd-vhosts.conf and make sure you use the following directive:</p>
<pre><code>NameVirtualHost *:80</code></pre>
<p>Make sure each of your virtual hosts are defined as:</p>
<pre><code>&lt;VirtualHost *:80&gt;
 ...
&lt;/VirtualHost&gt;</code></pre>
<p>Now we have to build and configure nginx. If you want you can use the following nginx slackware package made by me:</p>
<pre><code><a href="http://www.razvan.ws/wp-content/uploads/2009/10/nginx-0763-i486-1.txz">nginx-0763-i486-1.txz</a></code></pre>
<p>However if you&#8217;re like me and you like to build things manually:</p>
<pre><code>cd /usr/local/src
wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz

mkdir /etc/nginx
mkdir /var/log/nginx
mkdir /var/run/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

groupadd nginx
useradd -g nginx -d /var/www/htdocs -s /bin/false nginx</code></pre>
<p>The next step is to edit /etc/nginx/nginx.conf file. Here it is the file i&#8217;m using:</p>
<pre><code>user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid /var/run/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.9.90:80 default;
    listen       86.55.239.6:80 default;
    server_name  _;
    access_log /var/log/nginx/default.access.log main;

    location / {
        proxy_pass http://127.0.0.1:80;
        include /etc/nginx/proxy.conf;
    }
  }
  include /etc/nginx/vhosts/*[^~];
}</code></pre>
<p>Create /etc/nginx/proxy.conf template file which is going to be used on every vhost:</p>
<pre><code>proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffers           32 4k;</code></pre>
<p>Now you have to set up your virtual hosts. Here it is how /etc/nginx/vhosts/razvan.ws vhost file looks like:</p>
<pre><code>server {
    listen 86.55.239.2:80;
    server_name www.razvan.ws razvan.ws;
    # I'm not going to use nginx to create an access log as this is created by apache anyway
    #access_log /var/log/nginx/razvan.ws.access.log main;

    location / {
        proxy_pass http://127.0.0.1:80;
        include /etc/nginx/proxy.conf;
    }

    location ~* ^.+\.(jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|avi|mp3)$ {
        expires 1d;
        root /var/www/htdocs/razvan.ws;
    }
}</code></pre>
<p>Let&#8217;s create a nice slackware-like /etc/rc.d/rc.nginx script to start/stop/restart nginx:</p>
<pre><code>#!/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.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</code></pre>
<p>Now the only thing left is to restart apache and start nginx:</p>
<pre><code>apachectl restart
/etc/rc.d/rc.nginx start</code></pre>
<p>If everything went well, we&#8217;ll have the following processes:</p>
<pre><code> 4263 ?        Ss     0:00 nginx: master process /usr/bin/nginx -c /etc/nginx/nginx.conf
 4264 ?        S      0:00  \_ nginx: worker process
 4265 ?        S      0:00  \_ nginx: worker process
 4836 ?        Ss     0:00 /usr/bin/httpd -k start
 4837 ?        S      0:00  \_ /usr/bin/fcgi- -k start
 4896 ?        Ss     0:00  |   \_ /usr/bin/php-cgi
 4897 ?        S      0:00  |       \_ /usr/bin/php-cgi
 4898 ?        S      0:00  |       \_ /usr/bin/php-cgi
 4838 ?        Sl     0:00  \_ /usr/bin/httpd -k start
 4866 ?        Sl     0:00  \_ /usr/bin/httpd -k start</code></pre>
<p>Check out your apache access logs to see if everything is ok. Also if you want to make sure nginx is serving the static files, enable nginx access log as well and see if it gets the hits correctly.</p>
<p>Now the server is able to handle more traffic with the less resources. Everything is done transparently for the currently written scripts and for the users.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.razvan.ws/using-nginx-as-a-reverse-proxy-for-apache/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Linux Slackware 13.0 + Nginx + PHP FastCGI using PHP-FPM</title>
		<link>http://www.razvan.ws/linux-slackware-13-nginx-php-fastcgi-using-php-fpm/</link>
		<comments>http://www.razvan.ws/linux-slackware-13-nginx-php-fastcgi-using-php-fpm/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 19:40:41 +0000</pubDate>
		<dc:creator>Razvan</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[apc]]></category>

		<category><![CDATA[nginx]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[php-fpm]]></category>

		<category><![CDATA[slackware]]></category>

		<guid isPermaLink="false">http://www.razvan.ws/?p=104</guid>
		<description><![CDATA[While Apache is a great web server, it has a high memory footprint. If you don&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>While <a href="http://www.apache.org/">Apache</a> is a great web server, it has a high memory footprint. If you don&#8217;t need all the Apache features or if you have a slow/old/low memory machine <a href="http://nginx.net/">Nginx (Engine X)</a> might be perfect.</p>
<p>The typical nginx configuration involves using <em>spawn-fcgi</em> from the <a href="http://www.lighttpd.net/">LightTPD project</a> to get nginx serving up PHP. There are a few issues with <em>spawn-fcgi</em> which I&#8217;m not going to cover here.<br />
In order to avoid these issues I&#8217;m going to use <a href="https://launchpad.net/php-fpm">PHP-FPM</a>. 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.</p>
<p>PHP-FPM requires libevent to be installed. Since Slackware doesn&#8217;t include a libevent package it has to be build from sources. In this tutorial I&#8217;m using libevent-1.4.12. An attached slackware package of libevent-1.4.12 is available at the end of this post.</p>
<pre><code>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</code></pre>
<p><span id="more-104"></span><br />
Now we&#8217;re going to get php-fpm package according to the php version we&#8217;re going to build. I&#8217;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 <a href="https://launchpad.net/php-fpm">php-fpm website</a>.</p>
<pre><code>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</code></pre>
<p>Php-fpm can be built into php or standalone. According to php-fpm readme it&#8217;s recommended to be build into php.<br />
So, we&#8217;re going to generate fpm patch for php:</p>
<pre><code>php-fpm-0.6-5.3.0/generate-fpm-patch</code></pre>
<p>That should give you fpm.patch file.</p>
<p>Now we&#8217;re going to download php source and patch it with fpm:</p>
<pre><code>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 &lt; ../fpm.patch</code></pre>
<p>We&#8217;re going to configure it slackware style, with as many things as possible included:</p>
<pre><code>./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</code></pre>
<p>Notice the following options used:</p>
<pre><code>--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</code></pre>
<p>If you don&#8217;t have mcrypt installed, you can skip that line.</p>
<p>PHP 5.3.0 is the first version of php which includes <a href="http://dev.mysql.com/downloads/connector/php-mysqlnd/">mysqlnd</a>.<br />
According to <a href="http://www.php.net/manual/en/migration53.new-extensions.php">php.net</a>, 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.</p>
<p>If everything went well, we can proceed to build php-5.3.0:</p>
<pre><code>make
make install</code></pre>
<p>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&#8217;s recommended to comment out (disable) all the extensions which are not going to be used.</p>
<p>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.</p>
<p>If everything went well, php -v and php-fpm -v should give you the following output:</p>
<pre><code>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)</code></pre>
<p>Because it&#8217;s not such a good practice to run programs as root, create the following user:</p>
<pre><code>groupadd nginx
useradd -g nginx -d /storage/htdocs -s /bin/false nginx</code></pre>
<p>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:</p>
<pre><code>&lt;value name="owner"&gt;nginx&lt;/value&gt;
&lt;value name="group"&gt;nginx&lt;/value&gt;
&lt;value name="user"&gt;nginx&lt;/value&gt;
&lt;value name="group"&gt;nginx&lt;/value&gt;</code></pre>
<p>Now that we&#8217;re done with php, we have to move on to <a href="http://nginx.net/">Nginx</a>.</p>
<p>Go to <a href="http://nginx.net/">Nginx</a> website and get the latest version of Nginx. In this tutorial Iâ€™m using nginx 0.7.63:</p>
<pre><code>cd /usr/local/src
wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz</code></pre>
<p>First of all let&#8217;s create the following folders just to be sure we&#8217;re not going to have errors later.</p>
<pre><code>mkdir /etc/nginx
mkdir /var/log/nginx
mkdir /var/run/nginx</code></pre>
<p>Now, we are going to unzip/configure and build nginx.</p>
<pre><code>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</code></pre>
<p>A slackware (13.0) package of nginx-0.7.63 is available at the end of this post.</p>
<p>The next step is to edit /etc/nginx/nginx.conf file. Here it is the file i&#8217;m using:</p>
<pre><code>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;
    }

  }

}</code></pre>
<p>If you want to find out more about nginx config, check out <a href="http://wiki.nginx.org/Main">http://wiki.nginx.org/Main</a>.</p>
<p>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:</p>
<pre><code>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;</code></pre>
<p>Now we&#8217;re almost ready. Let&#8217;s create a nice slackware-like script to start nginx. Save the following in /etc/rc.d/rc.nginx:</p>
<pre><code>#!/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</code></pre>
<p>Don&#8217;t forget to make the file executable:</p>
<pre><code>chmod 755 /etc/rc.d/rc.nginx</code></pre>
<p>Now we&#8217;re ready to start our new web server:</p>
<pre><code>/etc/rc.d/init.d/php-fpm start
/etc/rc.d/rc.nginx start</code></pre>
<p>If everything went well you should see the following processes:</p>
<pre><code> 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</code></pre>
<p>It&#8217;s a good idea to use a php cacher. Feel free to use the instructions from <a href="http://www.razvan.ws/linux-slackware-apache2-php-fastcgi/">apache tutorial</a> in order to install <a href="http://www.php.net/apc">APC</a>.</p>
<p>Here there are the Slackware 13.0 packages of the above programs:</p>
<pre><code><a href="http://www.razvan.ws/wp-content/uploads/2009/10/libevent-1412-i486-1.txz">libevent-1412-i486-1.txz</a>
<a href="http://www.razvan.ws/wp-content/uploads/2009/10/nginx-0763-i486-1.txz">nginx-0763-i486-1.txz</a>
<a href="http://www.razvan.ws/wp-content/uploads/2009/10/php-530-i486-1.txz">php-530-i486-1.txz</a></code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.razvan.ws/linux-slackware-13-nginx-php-fastcgi-using-php-fpm/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Linux Slackware + Apache2 + PHP + mod_fastcgi</title>
		<link>http://www.razvan.ws/linux-slackware-apache2-php-fastcgi/</link>
		<comments>http://www.razvan.ws/linux-slackware-apache2-php-fastcgi/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 19:00:37 +0000</pubDate>
		<dc:creator>Razvan</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Apache]]></category>

		<category><![CDATA[apc]]></category>

		<category><![CDATA[fastcgi]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[slackware]]></category>

		<guid isPermaLink="false">http://www.razvan.ws/?p=48</guid>
		<description><![CDATA[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&#8217;s pkgtool.
Go to Apache website and get the latest version of [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial assumes that you are already familiar with Slackware Linux and Apache2.</p>
<p>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&#8217;s pkgtool.</p>
<p>Go to <a href="http://www.apache.org/">Apache</a> website and get the latest version of Apache2. In this tutorial I&#8217;m using Apache-2.2.9:</p>
<pre><code>cd /usr/local/src
wget http://www.eu.apache.org/dist/httpd/httpd-2.2.9.tar.gz</code></pre>
<p>Go to <a href="http://www.php.net/">PHP</a> website and get the latest version of PHP5. In this tutorial I&#8217;m using PHP-5.2.6</p>
<pre><code>wget http://www.php.net/get/php-5.2.6.tar.gz/from/de.php.net/mirror</code></pre>
<p><span id="more-48"></span></p>
<p>Go to <a href="http://www.fastcgi.com/">FastCGI</a> website and get the latest version of mod_fastcgi. In this tutorial I&#8217;m using mod_fastcgi-2.4.6:</p>
<pre><code>wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz</code></pre>
<p>Now, we are going to unzip/configure Apache. In this tutorial we are configuring it Slackware style, with as many things as possible included:</p>
<pre><code>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</code></pre>
<p>Please notice that we are going to use Apache mpm worker instead of the default prefork:</p>
<pre><code>--with-mpm=worker \</code></pre>
<p>For additional info about mpm-worker, feel free to visit: <a href="http://httpd.apache.org/docs/2.2/mod/worker.html">http://httpd.apache.org/docs/2.2/mod/worker.html</a></p>
<p>We are going to build apache:</p>
<pre><code>make
make install</code></pre>
<p>Unzip mod_fastcgi:</p>
<pre><code>cd /usr/local/src
tar -xzvf mod_fastcgi-2.4.6.tar.gz</code></pre>
<p>We are going to build mod_fastcgi according to it&#8217;s README:</p>
<pre><code>cd mod_fastcgi-2.4.6
apxs -o mod_fastcgi.so -c *.c</code></pre>
<p>Then, we will copy the binary into Apache&#8217;s modules folder:</p>
<pre><code>cp .libs/mod_fastcgi.so /usr/lib/httpd/modules/mod_fastcgi.so</code></pre>
<p>Now, you will have to edit /etc/httpd/httpd.conf and add the following line:</p>
<pre><code>LoadModule fastcgi_module lib/httpd/modules/mod_fastcgi.so</code></pre>
<p>Make sure you have the following files:</p>
<pre><code>mkdir /var/run/httpd
mkdir /tmp/fcgi_ipc
chmod 777 /tmp/fcgi_ipc</code></pre>
<p>Before continue with PHP configuration, make sure you have mcrypt/libmcrypt installed. If you don&#8217;t want to install mcrypt, just remove the following line from the php configure command:</p>
<pre><code>--with-mcrypt=shared</code></pre>
<p>Anyway, here there are the links where you can get a slackware packed version of mcrypt:</p>
<pre><code>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</code></pre>
<p>Now, we are going to unzip/configure PHP. We are using almost the same configuration as Slackware&#8217;s default:</p>
<pre><code>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</code></pre>
<p>Edit httpd.conf and add the following (after LoadModule directive):</p>
<pre><code>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</code></pre>
<p>If you want to find more about mod_fastcgi options, visit: <a href="http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html">http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html</a></p>
<p>Create the file /var/www/cgi-bin/php5.fcgi:</p>
<pre><code>#!/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</code></pre>
<p>Make sure the file is own by apache and is executable:</p>
<pre><code>chown apache:apache /var/www/cgi-bin/php5.fcgi
chmod 755 /var/www/cgi-bin/php5.fcgi</code></pre>
<p>Edit /etc/httpd/httpd.conf and make sure the following line is not commented:</p>
<pre><code>Include /etc/httpd/extra/httpd-mpm.conf</code></pre>
<p>Edit /etc/httpd/extra/httpd-mpm.conf and make sure the following lines are not commented:</p>
<pre><code>StartServers          2
MaxClients          150
MinSpareThreads      25
MaxSpareThreads      75
ThreadsPerChild      25
MaxRequestsPerChild   0</code></pre>
<p>At this point you should be ready to start apache:</p>
<pre><code>apachectl start</code></pre>
<p>Check /var/log/httpd/error_log to see if there are any errors.</p>
<pre><code>[notice] FastCGI: process manager initialized (pid 20046)
[notice] Apache/2.2.9 (Unix) mod_fastcgi/2.4.6 configured -- resuming normal operations</code></pre>
<p>If everything is ok, you should see the following processes after you run ps axf:</p>
<pre><code>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</code></pre>
<p>If you want to boost the performance of your PHP scripts, you can use a php cacher. In the example below, I&#8217;m going to use <a href="http://www.php.net/apc">APC</a>.</p>
<p>Get the latest apc version available:</p>
<pre><code>cd /usr/local/src
wget http://pecl.php.net/get/APC-3.0.19.tgz</code></pre>
<p>Unzip/configure APC:</p>
<pre><code>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/</code></pre>
<p>Add the following lines in your /etc/php.ini:</p>
<pre><code>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</code></pre>
<p>Now, you have to restart apache:</p>
<pre><code>apachectl restart</code></pre>
<p>Later edit:<br />
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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.razvan.ws/linux-slackware-apache2-php-fastcgi/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

