FreeBSD install PHP 7.2 with FPM for Nginx

How do I install install PHP 7.2 with FPM for Nginx web server running on FreeBSD operating system?

Introduction: PHP is an acronym for “PHP: Hypertext Preprocessor.” It is an open source general-purpose scripting language. PHP is handy for web development and to run popular software such as MediaWiki, WordPress, and more. PHP is easy to learn and allows you to write dynamically generated web pages on the fly. This page shows how to install PHP 7.2 on a FreeBSD v11.x Unix box along with Nginx web server.

FreeBSD install PHP 7.2 with FPM for Nginx

The procedure for installing PHP 7.2 on FreeBSD is as follows:

  1. Update FreeBSD ports tree
  2. Install PHP 7.2 binary package: pkg install nginx
  3. Install Nginx binary package
  4. Configure Nginx and PHP 7.2
  5. Turn on PHP-fpm service on FreeBSD
  6. Test the setup

Let us see all steps in details.

1. Update FreeBSD ports tree

You must use the portsnap command # portsnap fetch update

2. Install PHP 7.2 on FreeBSD

You can install PHP 7.2 using ports or binary package method. Use any one of the following methods.

The ports method

# cd /usr/ports/lang/php72/
# make install clean

The binary package method

Run the pkg command to install it:
# pkg install php72

For verification purposes, check PHP version, type:
php -v
php --version

3. Install PHP 7.2 extensions on FreeBSD

Again PHP extensions can be installed using the ports or pkg method. Use any one of the following ways.

To install the port, run the following command and choose required extensions:
# cd /usr/ports/lang/php72-extensions/ && make install clean

OR add the binary package:
# pkg install php72-extensions

Another option is installing individual PHP extensions as per your web application or software project needs. One can search PHP 7.2 extensions using the pkg command and grep command/egrep command # pkg search php72 | grep gd
# pkg search php72 | egrep -i --color 'gd|mysqli|openssl'
# pkg search php72 | egrep --color -i -- '-(gd|mysqli|openssl|memcached|opcache|json|mbstring|imagick|xml|zip|composer|igbinary)-'

Simply install it:
# pkg install php72-composer php72-gd php72-json
### OR ###
# pkg install php72-composer-1.6.5 php72-gd-7.2.10 php72-json-7.2.10 php72-mbstring-7.2.10 php72-mysqli-7.2.10 php72-opcache-7.2.10 php72-openssl-7.2.10 php72-pecl-imagick-3.4.3_2 php72-pecl-memcached-3.0.4 php72-xml-7.2.10 php72-zip-7.2.10 php72-pecl-igbinary-2.0.7

4. Configure PHP-FPM

Edit the file /usr/local/etc/php-fpm.d/www.conf
# vi /usr/local/etc/php-fpm.d/www.conf
Find line:
listen =
Update it as follows:
listen = /var/run/php72-fpm.sock
Uncomment the following line:
listen.owner = www = www
listen.mode = 0660

Save and close the file. Enable php-fpm service:
# sysrc php_fpm_enable=YES
Commands to start, stop, restart php-fpm service on FreeBSD server:
# service php-fpm stop
# service php-fpm start
# service php-fpm restart
# service php-fpm status

A note about PHP 7 config

You need to install php.ini file using the php command:
# cp -v /usr/local/etc/php.ini-production /usr/local/etc/php.ini
/usr/local/etc/php.ini-production -> /usr/local/etc/php.ini

Next, we are going to secure PHP and customize it:
# vi /usr/local/etc/php/99-custom.ini
Add the following config:

disable_functions=show_source, system, shell_exec, passthru, proc_open, proc_nice, exec
sendmail_path=/usr/sbin/sendmail -t

Save and close the file. Restart the PHP on FreeBSD:
# service php-fpm restart

5. Configure Nginx to use PHP 7.2 (PHP-FPM)

The final step is connecting Nginx to PHP-FPM service via the FCGI protocol. Edit your Virtual Domain or nginx.conf file as follows:
# cd /usr/local/etc/nginx
# vi nginx.conf

# vi vdomains/http.
Here is an updated sample config file:

server {
    server_name; # use domain name if you have here
    access_log  /var/log/nginx/;
    error_log  /var/log/nginx/;
    root /wwwwiki;
    # php config let nginx talk to php socket
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
                return 404;
        # Mitigate vulnerabilities
         fastcgi_param HTTP_PROXY "";
        fastcgi_pass unix:/var/run/php72-fpm.sock;
        fastcgi_index index.php;
        # include the fastcgi_param setting
        include fastcgi_params;
        # SCRIPT_FILENAME parameter is used for PHP FPM determining
        # the script name.
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;

Reload or restart the Nginx server:
# service nginx reload

6. Test the setup

Create a PHP script called test.php in /wwwwiki/
# vi /wwwwiki/test.php
Append the following code that shows information about PHP’s configuration using phpinfo:


Save and close the file. Set tight file permissions on DocumentRoot (web server root):
# chown -R wwwwiki:wwwwiki /wwwwiki/
# chmod -R 0555 /wwwwiki/

Fire a web browser and test it:

And there you have PHP working as expected and Nginx is talking to PHP-FPM via Unix socket.

How to verify that PHP-fpm running

Use the sockstat command on FreeBSD to list running services, open ports and unix sockets:
# sockstat -46lu | egrep --color 'www|nginx|php'
Also check log files if you get any problems running PHP:
# ls -l /var/log/nginx/
# tail -f /var/log/nginx/

Click to enlarge image


This guide demonstrated how to install PHP FPM securely and configured it using unix socket. Now you have fully working Nginx server along with PHP 7.2 for creating dynamic web pages on demand. Next part of the series shows how to install a database server to build a fully functional web server running on a FreeBSD operating system.

This entry is 2 of 3 in the Nginx, MySQL, and PHP (FEMP) Stack on FreeBSD Tutorial series. Keep reading the rest of the series:
  1. FreeBSD Install Nginx Webserver Tutorial
  2. FreeBSD install PHP 7.2 with FPM for Nginx
  3. Install MariaDB Databases on a FreeBSD 11 Unix Server

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Marc Grondin Sep 11, 2020 @ 23:34

    I know this is 2 years old but i just wanted to say thank you. Got me up and running with NginX+PHP in no time.

  • Peter Boosten Jan 28, 2021 @ 6:27

    Very good description, needed however ‘index index.php’ to avoid a 403.
    Always was a bit hesitant to use nginx for dynamic content, especially because everyone keeps saying it wasn’t built for it, but it’s blazingly fast.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum