FreeBSD install PHP 7.2 with FPM for Nginx

ADVERTISEMENTS



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?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.

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

Update FreeBSD ports tree Install PHP 7.2 binary package: pkg install nginx Install Nginx binary package Configure Nginx and PHP 7.2 Turn on PHP-fpm service on FreeBSD 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



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



# pkg install php72-extensions

# 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)-'

# 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

OR add the binary package: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 Simply install it:

Edit the file /usr/local/etc/php-fpm.d/www.conf

# vi /usr/local/etc/php-fpm.d/www.conf

Find line:

listen = 127.0.0.1:9000

Update it as follows:

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

Uncomment the following line:

listen.owner = www

listen.group = 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:

display_errors = Off safe_mode = Off safe_mode_exec_dir = safe_mode_allowed_env_vars = PHP_ expose_php = Off log_errors = On error_log = /var/log/nginx/php.scripts.log register_globals = Off cgi.force_redirect = 0 file_uploads = On allow_url_fopen = Off sql.safe_mode = Off disable_functions = show_source, system, shell_exec, passthru, proc_open, proc_nice, exec max_execution_time = 60 memory_limit = 60M upload_max_filesize = 2M post_max_size = 2M cgi.fix_pathinfo = 0 sendmail_path = /usr/sbin/sendmail -fwebmaster@cyberciti.biz -t display_errors=Off safe_mode=Off safe_mode_exec_dir= safe_mode_allowed_env_vars=PHP_ expose_php=Off log_errors=On error_log=/var/log/nginx/php.scripts.log register_globals=Off cgi.force_redirect=0 file_uploads=On allow_url_fopen=Off sql.safe_mode=Off disable_functions=show_source, system, shell_exec, passthru, proc_open, proc_nice, exec max_execution_time=60 memory_limit=60M upload_max_filesize=2M post_max_size=2M cgi.fix_pathinfo=0 sendmail_path=/usr/sbin/sendmail -fwebmaster@cyberciti.biz -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

OR

# vi vdomains/http.192.168.2.31.conf

Here is an updated sample config file:

server { server_name 192.168.2.31; # use domain name if you have here access_log /var/log/nginx/192.168.2.31.access.log; error_log /var/log/nginx/192.168.2.31.error.log; 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 https://httpoxy.org/ 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; } } server { server_name 192.168.2.31; # use domain name if you have here access_log /var/log/nginx/192.168.2.31.access.log; error_log /var/log/nginx/192.168.2.31.error.log; 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 https://httpoxy.org/ 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:

<?php phpinfo ( ) ; ?> <?php 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:

http://192.168.2.31/test.php

http://your-domain-name-here/test.php



How to verify that PHP-fpm running

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

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/192.168.2.31.error.log



Conclusion

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.



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