Slackware

Setting up nginx + php-fpm on Slackware 14.0

For Slackware 14.0, it is now easy to setup an nginx webserver with PHP support via php-fpm. If you do a full install with Slackware 14.0, you can get PHP 5.4.x and php-fpm as this packages are shipped by stock Slackware. However, the default webserver though is still Apache. In this guide, we are going to setup a webserver that can run PHP scripts via php-fpm.

Install nginx

First, we grab a SlackBuild script for nginx via Slackbuilds.org. Extract it, then grab the latest source code for nginx (currently at 1.4.0).

wget http://slackbuilds.org/slackbuilds/14.0/network/nginx.tar.gz
tar xvzf nginx.tar.gz
cd nginx
wget http://nginx.org/download/nginx-1.4.0.tar.gz

Now that the build script and the nginx source code is ready, we need to modify the build script so that it will install the 1.4.0 version instead of whatever version is being hardcoded into it by the build script. In my case, the version used by the script is still at 1.2.6.

Edit nginx.SlackBuild and look for the text VERSION=${VERSION}:-1.2.6 and change it to version 1.4.0. Next, go down the codes and look for the text ./configure and check if --with-http_realip_module is present. If not, add it in the middle of the ./configure code block.

Next, ensure that --with-ipv6 is present as well. If not, add it to the ./configure code block. These two settings allow sniffing real IP address from proxy servers or CDN like CloudFlare. The other option allows the ipv6 syntax that we can use with HttpRealipModule. I needed it on my setup but you may not need it on yours. We’ll never know.

Now that we are finally ready building nginx, let’s build it now (run as root). It may take several minutes to complete.

./nginx.SlackBuild

Finally, we need to install it on our Slackware box. The package filename will be displayed by the build script after it finishes compiling nginx and it is usually put under /tmp directory. (Run as root).

installpkg /tmp/nginx-1.4.0-i486-1_SBo.tgz

Configure nginx

Now that we already have an nginx installed, let’s configure it to be our webserver. First, we set the daemon up and running and be sure we closed/disabled all other web server like Apache. If we have an Apache server running, let’s kill it and make sure it will never run again upon restart.

/etc/rc.d/rc.httpd stop
chmod -x /etc/rc.d/rc.httpd

Next, lets make nginx and php-fpm RC script enabled upon boot.

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

I usually use vhosts for everything, therefore, in this post, I’ll jumpt straight into making virtual hosts for nginx. Let’s start with our default virtual host which is also the localhost. I call my virtual host as darkstar.darkstar.net. The document root will be on /srv/www/htdocs/darkstar.darkstar.net.

Create the directory and put an index file /srv/www/htdocs/darkstar.darkstar.net/index.html and put some content like It works! or any content just for testing. For testing local servers, you may need to add a host file entry for darkstar.darkstar.net like below:

# /etc/hosts
# Other contents
# ...

127.0.0.1       darkstar.darkstar.net

Next, create an nginx conf file. Below is my conf file for /etc/nginx/nginx.conf.

worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  darkstar.darkstar.net;

        #charset koi8-r;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        root /srv/www/htdocs/darkstar.darkstar.net;

        location / {
            root        /srv/www/htdocs/darkstar.darkstar.net;
            index       index.html index.htm;
        }

        access_log  /var/log/nginx/darkstar.darkstar.net-access_log combined;
        error_log   /var/log/nginx/darkstar.darkstar.net-error_log error;
    }

    # Include other config/sites
    include /etc/nginx/sites-enabled/*;
}

As you’ve noticed, we didn’t put any php related configuration here. We will skip that for now and let this vhost serve static files only. At the end of the config file, the line that says include /etc/nginx/sites-elabled/*; means that we can add more vhosts on that directory without messing around with the main config file. Create the directories for now and leave it with no content.

cd /etc/nginx        
mkdir sites-available
mkdir sites-enabled

Now we are ready for our first test. Run the nginx daemon and let’s see what will happen.

/etc/rc.d/rc.nginx start

Now visit http://darkstar.darkstar.net. You should see the text It works! on your browser.

Configure nginx + php-fpm

Now that nginx is ready, lets get PHP working. In this post, we will simply make the PHP run. Nothing fancy. We call our demo site myproject.darkstar.net. You must add this site in your local host file if this is a local virtual host, like what we did on darkstar.darkstar.net. Next, add a config file at /etc/nginx/sites-available/myproject.darkstar.net.conf with below content.

server {
    listen      80;
    server_name  myproject.darkstar.net;
    root        /srv/www/htdocs/myproject.darkstar.net;
    index       index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
        #fastcgi_param   PHP_VALUE "auto_prepend_file=/home/data/scripts/common_functions.php";
    }

    location ~ /\.ht {
        deny  all;
    }

    access_log  /var/log/nginx/myproject.darkstar.net-access_log combined;
    error_log   /var/log/nginx/myproject.darkstar.net-error_log error;
}

Then make sure you sym-link the config file to the sites-enabled directory.

cd /etc/nginx/sites-enabled
ln -s ../sites-available/myproject.darkstar.net.conf myproject.darkstar.net.conf

Next, make a sample PHP script at /srv/www/htdocs/myproject.darkstar.net/index.php with some basic PHP code, ex:

<?php phpinfo() ?>

Launch

Finally, let’s launch the site with a test page. Start php-fpm, restart nginx.

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

Open the site http://myproject.darkstar.net/ and you should be a violet phpinfo() page. By the way, with above nginx config, it allows basic rewrite feature found on frameworks like Zend Framework, Kohana, CodeIgniter and even WordPress.

Enjoy and share.

1 thought on “Setting up nginx + php-fpm on Slackware 14.0”

Leave a reply

Your email address will not be published. Required fields are marked *