Tuesday, November 20, 2012

Slackware 14 + nginx + FastCGI + PHP (PHP-FPM)

I like the light-weight and fast web server nginx (Engine X).  But it is not included in the default Slackware packages.

First we need to build nginx for Slackware.  There is SlackBuilds script for nginx.  Download the script from SlackBuilds and nginx source code.

As this writing, the stable ngix version is 1.2.5 and SlackBuild script is for version 1.2.2.  You need to modified the script to match the version.  I also like to build with IPv6 support (--with-ipv6) and don't need the select and poll module (--without-poll-module, --without-select-module), we are running Slackware 14, epoll is the best method.  I also remove lines that include perl module support.  Your preferences may not be the same as mime.  I also change the build script to include nginx's default index.html and 50x.html into /var/html.  Th build script diff is here:

--- a/nginx.SlackBuild 2012-09-28 21:34:38.000000000 -0700
+++ b/nginx.SlackBuild 2012-11-19 17:02:38.712164716 -0800
@@ -25,8 +25,8 @@
 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 PRGNAM=nginx
-VERSION=${VERSION:-1.2.2}
-BUILD=${BUILD:-2}
+VERSION=${VERSION:-1.2.5}
+BUILD=${BUILD:-1}
 TAG=${TAG:-_SBo}
 
 if [ -z "$ARCH" ]; then
@@ -85,9 +85,8 @@
   --group=${NGINXGROUP:=nogroup} \
   --error-log-path=/var/log/nginx/error.log \
   --http-log-path=/var/log/nginx/access.log \
-  --with-rtsig_module \
-  --with-select_module \
-  --with-poll_module \
+  --without-select_module \
+  --without-poll_module \
   --with-http_ssl_module \
   --with-http_realip_module \
   --with-http_addition_module \
@@ -99,14 +98,13 @@
   --with-http_random_index_module \
   --with-http_secure_link_module \
   --with-http_stub_status_module \
-  --with-http_perl_module \
-  --with-perl_modules_path=$installvendorlib \
   --http-client-body-temp-path=/var/tmp/nginx_client_body_temp \
   --http-proxy-temp-path=/var/tmp/nginx_proxy_temp \
   --http-fastcgi-temp-path=/dev/shm \
   --without-mail_pop3_module \
   --without-mail_imap_module \
-  --without-mail_smtp_module
+  --without-mail_smtp_module \
+  --with-ipv6
 
 make
 make install DESTDIR=$PKG
@@ -118,9 +116,11 @@
 find $PKG -perm 444 -exec chmod 0644 {} \;
 find $PKG -perm 555 -exec chmod 0755 {} \;
 
+sed --in-place "s/root[ \t]*html/root \/var\/html/" $PKG/etc/nginx/nginx.conf
 # Remove some other empty and/or unnecessary directories from the package
 eval $(perl '-V:archlib')
-rm -rf $PKG/$archlib $PKG/usr/html $PKG/var
+mv $PKG/usr/html  $PKG/var
+#rm -rf $PKG/$archlib $PKG/usr/html $PKG/var
 
 # Add an init script
 mkdir -p $PKG/etc/rc.d
@@ -150,4 +150,4 @@
 cat $CWD/doinst.sh > $PKG/install/doinst.sh
 
 cd $PKG
-/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
+/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

Now build the nginx server by running the script:
./ngix.SlackBuild

You will have a package file after build /tmp/nginx-1.2.5-x86_64-1_SBo.txz (I am compiling it under Slackware64), install it with the installpkg command.  And change mode of the startup script:

chmod +x /etc/rc.d/rc.nginx

One good thing about Slackware 14 is that we have already PHP-FPM built in the stock php package and does not need to go through the hassle to build PHP-FPM ourselves.  You need to make the startup script executable by

chmod +x /etc/rc.d/rc.php-fpm

Edit the file /etc/php-fmp.conf, change catch_workers_output to yes.  This will allow you to get meaningful error message in the log.

Edit the file /etc/nginx/nginx.conf, I have changed the document root to /var/html and move it to the server part:

 server {
        listen       80;
        server_name  localhost;

 root /var/html;
       ...
 }
Uncomment the line on related to fastcgi:
location ~ \.php$ {
 fastcgi_pass  127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include       fastcgi_params;
}
Note original lines
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
has been changed to the above one.

Now, fire up the php-fpm and nginx server:

/etc/rc.d/rc.php-fpm start
/etc/rc.d/rc.nginx start
I have created a small test file /var/html/php.php:
<html>
<body>
<h1>NGINX + FASTCGI + PHP</h1>
<?php
print "<h2>Hello from PHP!</h2>"
?>
</body></html>
When you access http://localhost, you should be able to see:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

When accessing http://localhost/php.php, you should be able to see:

NGINX + FASTCGI + PHP

Hello from PHP!

You may like to add the start up scripts to your rc.local script, so that the services are automatically started when you boot up your machine next time.

Reference:

* Linux Slackware 13.0 + Nginx + PHP FastCGI using PHP-FPM
Post a Comment