FreeBSD: NginX+PHP 5.3 FastCGI (FPM) Installation

NginX (pronounced “engine x”), is a high performance web server and a reverse proxy server. This server is well-known for its low memory footprint rather than Apache. You can refer here for Nginx vs Apache Performance Benchmark result. Nginx is way too better for Apache.

Popular websites that run on Nginx are SourceForge, WordPress, and Hulu. By making Nginx run in FreeBSD, you can deliver light, efficient, powerful, stable and secure web server in a simple way.

What is PHP? I think you all already know and no need to explain further. The PHP handler we will use is FastCGI Process Manager (FPM), is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. This setup will surely deliver high-performance web service with low specs hardware.

OS: FreeBSD 8 64bit
Nginx version: 0.8.54
PHP version: 5.3.6
Website: http://mydomain.net/
Website IP: 192.168.60.2
Web directory: /home/mydomain/public_html

1. Lets start by installing Nginx web server:

cd /usr/ports/www/nginx
make install clean

Once installation start, it will prompt nginx module selection page. You can select any Nginx module you want, but for me, I will select following module to be compiled right away:

[X] HTTP_MODULE               Enable HTTP module
[X] HTTP_ADDITION_MODULE      Enable http_addition module
[X] HTTP_CACHE_MODULE         Enable http_cache module
[X] HTTP_DAV_MODULE           Enable http_webdav module
[X] HTTP_FLV_MODULE           Enable http_flv module
[X] HTTP_GEOIP_MODULE         Enable http_geoip module
[X] HTTP_GZIP_STATIC_MODULE   Enable http_gzip_static module
[X] HTTP_IMAGE_FILTER_MODULE  Enable http_image_filter module
[X] HTTP_PERL_MODULE          Enable http_perl module
[X] HTTP_RANDOM_INDEX_MODULE  Enable http_random_index module
[X] HTTP_REALIP_MODULE        Enable http_realip module
[X] HTTP_REWRITE_MODULE       Enable http_rewrite module
[X] HTTP_SECURE_LINK_MODULE   Enable http_secure_link module
[X] HTTP_SSL_MODULE           Enable http_ssl module
[X] HTTP_STATUS_MODULE        Enable http_stub_status module
[X] HTTP_SUB_MODULE           Enable http_sub module
[X] HTTP_XSLT_MODULE          Enable http_xslt module

If you receive any prompt after that, just accept all values if you want to have a complete setup, or else select what you want, if you know what you are doing.

2. Web server installation done. Make sure Nginx is enabled by adding following line to /etc/rc.conf:

nginx_enable="YES"

3. Before we configure and start the web server, we need to install PCRE,  libtool, PHP with FPM and PHP extensions. Follow these steps:

cd /usr/ports/devel/pcre
make install clean
cd /usr/ports/devel/libtool
make install clean
cd /usr/ports/lang/php5
make install clean

During the selection module page, select FPM (FastCgi Process Manager).

cd /usr/ports/lang/php5-extensions
make install clean

4. Since PHP-FPM is a service, we need to add this in /etc/rc.conf:

php_fpm_enable="YES"

5. By default, there is no php.ini specified. So we need to copy the php.ini which has been prepared during port installation.

cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini

6. Start the PHP-FPM service :

/usr/local/etc/rc.d/php-fpm start

7. Now PHP-FPM already started at localhost port 9000. You can check this using netstat command. Lets create the web and logs directory used to host website (ignore this if you already have the directory):

mkdir /home/mydomain/public_html
mkdir /home/mydomain/logs

8. Lets configure Nginx so it know where is the home directory, what is the virtual host name, how to handle PHP and so on. Open the configuration file using text editor located under /usr/local/etc/nginx/nginx.conf and make sure following values are not commented (#) or you can copy this example and change to the value you want:

user  nobody;
worker_processes  1;
 
error_log  logs/error.log;
 
pid        logs/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"';
 
    sendfile        on;
 
    keepalive_timeout  65;
 
    gzip  on;
 
    # We define the virtual host here
    server {
        listen       192.168.60.2:80;
        server_name  mydomain.net www.mydomain.net;
 
        access_log  /home/mydomain/logs/access.log  main;
 
        location / {
            root   /home/mydomain/public_html;
            index  index.html index.htm index.php;
        }
    # Let nginx know how to handle PHP using fastcgi
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/mydomain/public_html$fastcgi_script_name;
            include        fastcgi_params;
        }
 
        location ~ /\.ht {
            deny  all;
        }
    }
}

9. After that, we good to go. But before start Nginx it is good if we check the configuration first:

nginx -t
/usr/local/etc/rc.d/nginx start

10. Now, Nginx and PHP-FPM should run correctly. If you do any changes on php.ini files or php extension file, you can restart PHP-FPM and reload Nginx:

/usr/local/etc/rc.d/php-fpm restart
/usr/local/etc/rc.d/nginx reload

9 thoughts on “FreeBSD: NginX+PHP 5.3 FastCGI (FPM) Installation

  1. Thanks for this tutorial. it help us much! so surprise by the memory consumption of nginx

    Reply

  2. This web post is awesome. Every step worked perfectly without a single squawk! Awesome job on this content! My server is running like a champ! Keep up the good work!

    Reply

  3. Thank you, but fyi php.ini-production was not /usr/local/etc/php/php.ini-production for my install, it was /usr/local/etc/php.ini-production.

    Reply

  4. Thanks for tutorial, i have installed all clearly. but when i’m trying the start nginx; i’m taking this error “the using of regex “\.php$” requires PCRE library in …nginx.conf”

    Thanks for advice.

    Reply

    1. Hi Selami,

      You need to install pcre library inside the server. Go to /usr/ports/devel/pcre and run following command:
      make install clean

      I have made some correction to the post to include installation of pcre

      Reply

      1. Hi SecaGuy,
        yes i were install pcre, but it’s saying same error,

        i’s take a look nginx configuration (“nginx -V”). it have “–without-pcre” parameter.

        i think this is my problem, but i don’t know; how can i solve. i’m new for freeBSD also Unix. 😀

        i tried /.configure –with-pcre but i can’t do that.

        Thanks

        Reply

    1. Yes, because we have put following line into rc.conf:
      php_fpm_enable=”YES”
      nginx_enable=”YES”

      Reply

  5. Thank you for this simple tutorial that enabled me to get up to speed with nginx and some VM hacking.

    Reply

Leave a Reply

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