Running PHP on Nginx

I’ve been running Apache since 1998. Nginx didn’t exist then. FreshPorts runs on Apache mostly because that’s what I was using. There was no selection process. I use Nginx in a few locations, but I’ve never run FreshPorts on it.

I am configuring a new server (x8dtu) for FreshPorts. So far, I have two jails:

  1. nginx01
  2. pg01

My next task: get PHP running on nginx01.

Name confusion

While I was researching, I became confused and thought there were two different solutions I could try:

  1. PHP-FPM
  2. FastCGI

It turns out, I was wrong, and I discovered the truth as soon as I got to the PHP-FPM website. PHP-FPM is FastCGI Process Manager.

The DO Way

I’m starting with the Digital Ocean tutorial. Unlike their tutorial, I am using PostgreSQL.

My first thought while reading: where did they install php-fpm? I expected it to be a separate package. No, it’s not. I discovered this when I saw a reference to /usr/local/etc/rc.d/php-fpm in their tutorial. I tried this:

$ pkg which /usr/local/etc/rc.d/php-fpm
/usr/local/etc/rc.d/php-fpm was installed by package php56-5.6.31

Ahh, I already have it installed. Good. Let’s enable it:

$ sudo sysrc php_fpm_enable="YES"
php_fpm_enable:  -> YES

php-fpm configuration

The only change I made to the default /usr/local/etc/php-fpm.conf was to use this listen command instead:

listen = /var/run/php-fpm.sock

NOTE: 2019.12.01- for more recent versions of PHP, you want to modify /usr/local/etc/php-fpm.d/www.conf.

I also uncommented these lines:

listen.owner = www
listen.group = www
listen.mode = 0660

PHP configuration

Next, I altered /usr/local/etc/php.ini so it contained this line:

; for php-fpm
cgi.fix_pathinfo=0

Starting php-fpm

This part was easy:

$ sudo service php-fpm start
Performing sanity check on php-fpm configuration:
[11-Sep-2017 22:14:10] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.

Let me show you what was running:

$ ps auwwx | grep php
root    59086  0.0  0.0 206468 17000  -  SsJ  22:14   0:00.00 php-fpm: master process (/usr/local/etc/php-fpm.conf) (php-fpm)
www     59087  0.0  0.0 206468 17012  -  SJ   22:14   0:00.00 php-fpm: pool www (php-fpm)
www     59088  0.0  0.0 206468 17012  -  SJ   22:14   0:00.00 php-fpm: pool www (php-fpm)
dan     59177  0.0  0.0  10732  1840  0  R+J  22:14   0:00.00 grep php

Profit!

nginx configuration

Oh wait, there is some nginx configuration to do.

These are the settings I added/changed:

user  www;
worker_processes  8;

I chose 8 because I have 16 CPUs:

$ sysctl hw.ncpu
hw.ncpu: 16

I’ll leave the other 8 for other purposes, such as PostgreSQL.

Other details

The other thing I did was use their default nginx.conf configuration file. It looks like this:

user  www;
worker_processes  8;
error_log /var/log/nginx/error.log info;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log /var/log/nginx/access.log;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  x8dtu-nginx01.example.org;
        root /usr/local/www/nginx;
        index index.php index.html index.htm;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                include fastcgi_params;
        }
    }

    include /usr/local/etc/nginx/includes/*.conf;

}

It just worked. Thanks Digital Ocean.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Comment

Scroll to Top