Setup PHP 5.6 with Nginx

PHP-FPM (FastCGI Process Manager) can be used together with Nginx to host PHP websites on the FreeBSD 10 system. We need to install PHP and configure Nginx to make these things work together.

Install PHP 5.6 (don’t forget to enable FPM option to build php-fpm executable):

make -C /usr/ports/lang/php56 install clean

Install some PHP extensions (e.g. enable MYSQL, MYSQLI or PDO_MYSQL if the website is working with MySQL database):

make -C /usr/ports/lang/php56-extensions install clean

Enable PHP-FPM in /etc/rc.conf:

php_fpm_enable="YES"

Start PHP-FPM:

service php-fpm start

It’s possible to tune some PHP-FPM configuration later in /usr/local/etc/php-fpm.conf.

Then configure Nginx to pass requests through FastCGI:

server {
  listen 80;
  server_name example.com;
  root /usr/local/www/example;
  index index.php;

  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
      return 404;
    }
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

And reload Nginx configuration:

service nginx reload
comments powered by Disqus