How to Install PHP and PHP-FPM on FreeBSD 14.0

Updated on July 25, 2024
How to Install PHP and PHP-FPM on FreeBSD 14.0 header image

Introduction

PHP (Hypertext Preprocessor) is a popular server-side scripting language that enables the development of dynamic web applications. PHP-FPM (FastCGI Process Manager) is a PHP FastCGI implementation that provides additional features and improved performance over traditional CGI-based methods to process dynamic web application contents on a server.

This article explains how to install PHP and PHP-FPM on a FreeBSD 14.0 server. You will set up a working PHP environment, run multiple PHP versions, and integrate the Nginx web server application to serve dynamic PHP applications on your server.

Prerequisites

Before you begin:

Install PHP and PHP-FPM

PHP is available in the default package repositories on FreeBSD 14.0 with the latest package information. PHP-FPM is bundled with the PHP package depending on your desired version. Follow the steps below to install the latest PHP and PHP-FPM version using the default FreeBSD 14.0 pkg package manager on your server.

  1. Update the server package index.

    console
    $ sudo pkg update
    
  2. Search the default pkg catalog and verify all available PHP versions.

    console
    $ sudo pkg search php | egrep '^php[0-9]+-[0-9]'
    

    Your output should be similar to the one below:

    php81-8.1.29                   PHP Scripting Language (8.1.X branch)
    php82-8.2.18                   PHP Scripting Language (8.2.X branch)
    php83-8.3.6                    PHP Scripting Language (8.3.X branch)

    PHP 8.3 is the latest available version based on the above output you can install on your server.

  3. Install PHP and PHP-FPM based on your desired version. For example, PHP version 8.3.

    console
    $ sudo pkg install -y php83
    
  4. View the installed PHP version on your server.

    console
    $ php -v
    

    Output:

    PHP 8.3.6 (cli) (built: Apr 15 2024 19:21:47) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.3.6, Copyright (c) Zend Technologies
        with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
  5. View the installed PHP-FPM version on your server.

    console
    $ php-fpm -v
    

    Output:

    PHP 8.3.6 (fpm-fcgi) (built: Apr 18 2024 01:11:07)
    Copyright (c) The PHP Group
    Zend Engine v4.3.6, Copyright (c) Zend Technologies
        with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
  6. Enable the PHP-FPM service to automatically start at boot time.

    console
    $ sudo sysrc php_fpm_enable=YES
    

Install PHP Extensions

PHP extensions offer extra functionalities to the core PHP language and enable features such as database integration, support for multiple file formats, and encryption. Follow the steps below to install common PHP extensions required by most dynamic web applications and frameworks such as WordPress.

  1. Install common PHP extensions. Replace php83 with your desired PHP version.

    console
    $ sudo pkg install -y php83-mysqli php83-mbstring php83-bcmath php83-zip php83-gd php83-curl php83-xml
    

    The above command installs the following PHP extensions:

    • php-mysqli: Enables PHP applications to connect and interact with MySQL databases.

    • php-mbstring: Enables support for UTF8 text.

    • php-bcmath: Arbitrary precision mathematics extension for PHP.

    • php-zip: ZIP archive support for PHP.

    • php-gd: GD extension for PHP (image processing).

    • php-curl: cURL extension for PHP (URL transfers).

    • php-xml: XML support for PHP.

    • Run the following command to view additional PHP extensions you can install on your server.

    console
    $ sudo pkg search php | egrep '^php83'
    
  2. View all PHP extensions available on your server.

    console
    $ php -m
    

    Your output should be similar to the one below:

    [PHP Modules]
    bcmath
    Core
    curl
    date
    gd
    hash
    json
    libxml
    mbstring
    
    .....
    
    [Zend Modules]
    Zend OPcache

Configure PHP-FPM

PHP-FPM uses pools of worker processes to manage resources and handle dynamic PHP requests on your server efficiently. Follow the steps below to configure the default PHP-FPM pool to enable PHP processing requests using the TCP port 9000 on your server.

  1. Open the default PHP-FPM pool configuration /usr/local/etc/php-fpm.d/www.conf using a text editor such as vi.

    console
    $ sudo vi /usr/local/etc/php-fpm.d/www.conf
    
    • Find the following directive to verify the PHP-FPM pool name.
    ini
    [www]
    
    • Find the following user and group directives to allow PHP-FPM access specific resources on your server.
    ini
    user = www
    group = www
    
    • Find the listen directive and verify the PHP-FPM port to use when connecting to the service.
    ini
    listen = 127.0.0.1:9000
    
    • Find the following process manager options and modify them to match your project requirements.
    ini
    pm = dynamic
    pm.max_children = 50
    pm.start_servers = 2
    pm.min_spare_servers = 2
    pm.max_spare_servers = 35
    

    Within the above options:

    • pm: Sets how PHP-FPM should start and run worker processes on the server. Below are the supported values.
      • static: Enables a fixed number of worker processes to run on the server.
      • dynamic: Enables dynamic changes to the number of worker processes based on the server load.
      • ondemand: Enables spawning of worker process only when needed.
    • pm.max_children: Sets the maximum number of concurrent child processes to create and handle PHP requests.
    • pm.start_servers: Sets the number of child processes to create when PHP-FPM starts.
    • pm.min_spare_servers: Sets the minimum number of idle child processes to run on the server.
    • pm.max_spare_servers: Limits the maximum number of idle processes running on the server.

    Save and close the file.

  2. Test the PHP-FPM configuration for syntax errors.

    console
    $ sudo php-fpm -t
    

    Output:

    ......NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful
  3. Restart the PHP-FPM service to apply your configuration changes.

    console
    $ sudo service php-fpm restart
    

Test PHP and PHP-FPM

PHP is actively running on your server while PHP-FPM accepts processes dynamic requests on the localhost TCP port 9000. Follow the steps below to test access to PHP and install the Nginx web server to forward PHP requests to PHP-FPM for processing.

  1. Create a new sample PHP application file test.php in your working directory.

    console
    $ vi test.php
    
  2. Add the following contents to the file.

    php
    <?php
    
    echo " <h1 align ='center'> Greetings from Vultr! PHP is running on this server </h1>";
    
    ?>
    

    Save and close the file.

  3. Run the application file using the PHP CLI utility.

    console
    $ php test.php
    

    Output:

    Greetings from Vultr! PHP is running on this server
  4. Install the Nginx web server package to test access to the PHP-FPM service.

    console
    $ sudo pkg install -y nginx
    
  5. Enable the Nginx service to automatically start at boot time.

    console
    $ sudo sysrc nginx_enable=yes
    
  6. Open the main Nginx configuration file.

    console
    $ sudo vi /usr/local/etc/nginx/nginx.conf
    
  7. Uncomment the following directives by removing the # sign on the following directives.

    nginx
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    

    Replace the html root value with your web root directory /usr/local/www/nginx. Then, replace /scripts$fastcgi_script_name; with $document_root$fastcgi_script_name; to allow PHP-FPM read the directory files. Your modified Nginc configuration server block should look like the one below.

    nginx
    server {
    listen       80;
    server_name  localhost;
    
    #charset koi8-r;
    
    #access_log  logs/host.access.log  main;
    
    location / {
        root   /usr/local/www/nginx;
        index  index.php index.html index.htm;
    }
    
    #error_page  404              /404.html;
    
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/www/nginx-dist;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/local/www/nginx;
        fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    
    }
    

    Save and close the file.

    The uncommented directives above ensure that Nginx forwards all .php PHP file requests to the PHP-FPM service running on the localhost 127.0.0.1 port 9000 using FastCGI. In addition, Nginx serves web application files from the default web root directory /usr/local/www/nginx.

  8. Test the Nginx configuration for errors.

    console
    $ sudo nginx -t
    

    Output:

    nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
  9. Copy the sample PHP application file you created earlier to the /usr/local/www/nginx web root directory.

    console
    $ sudo cp test.php /usr/local/www/nginx/
    
  10. Restart the Nginx web server to apply your configuration changes.

    console
    $ sudo service nginx restart
    
  11. Access your server IP address using a web browser such as Chrome and load the /test.php URL path.

    http://SERVER-IP/test.php

    Verify that your PHP application loads correctly to confirm that PHP-FPM is successfully processing PHP requests through the Nginx web server.

    Sample PHP application

Install Multiple PHP Versions

PHP supports multiple versions on FreeBSD 14.0. However, specific PHP versions are not available in the default repositories on your server. Follow the steps below to install specific PHP versions such as PHP 7.4 and enable multiple versions using a FreeBSD ports collection on your server.

  1. Install all required compile dependency packages on your server.

    console
    $ sudo pkg install -y autoconf automake libtool re2c bison pkgconf sqlite3 libxml2
    
  2. Download the FreeBSD ports collection with the 2022Q4 branch that includes older PHP versions such as PHP 7 and above.

    console
    $ wget -c "https://cgit.freebsd.org/ports/snapshot/ports-2022Q4.tar.gz"
    
  3. Create a new directory /usr/ports to store your ports collection.

    console
    $ sudo mkdir /usr/ports
    
  4. Extract the downloaded archive contents to the /usr/ports directory.

    console
    $ sudo tar -xzf ports-2022Q4.tar.gz -C /usr/ports
    
  5. Switch to the PHP 7.4 port directory.

    console
    $ cd /usr/ports/ports-2022Q4/lang/php74
    
  6. Open the /etc/make.conf configuration file.

    console
    $ sudo vi /etc/make.conf
    
  7. Add the following configurations to the file to install PHP 7.4 with necessary dependency packages to work with other PHP versions on your server.

    make
    p_PHP_FLAVORS=php71 php72 php73 php74
    p_GETFLAVOR=echo $$FLAVOR
    p_FLAVOR=${p_GETFLAVOR:sh}
    
    .for port in ${p_PHP_FLAVORS}
    
    .if ${.CURDIR:M*/ports/*/${port}*}
    PREFIX=/usr/local/${port}
    PHPBASE=/usr/local/${port}
    DISABLE_VULNERABILITIES=yes
    .info set by CURDIR: ${.CURDIR} -- sets PREFIX: ${PREFIX} and PHPBASE: ${PHPBASE}
    .endif
    
    .if ${p_FLAVOR} == ${port}
    PREFIX=/usr/local/${port}
    PHPBASE=/usr/local/${port}
    DISABLE_VULNERABILITIES=yes
    .info set by FLAVOR ${p_FLAVOR} -- sets PREFIX: ${PREFIX} and PHPBASE: ${PHPBASE}
    .endif
    
    .endfor
    

    The above configuration works by modifys the make environment variables to install compiled packages to the /usr/local/php<version> on your server.

  8. Run the following command to compile and install PHP 7.4 with PHP-FPM 7.4.

    console
    $ sudo make -DBATCH install clean
    

    Wait at least 3 minutes for the build process to complete and install PHP 7.4. Your output should be similar to the one below when successful.

    make[1]: "/etc/make.conf" line 11: set by CURDIR: /usr/ports/ports-2022Q4/lang/php74 -- sets PREFIX: /usr/local/php74 and PHPBASE: /usr/local/php74
    make[2]: "/etc/make.conf" line 11: set by CURDIR: /usr/ports/ports-2022Q4/lang/php74 -- sets PREFIX: /usr/local/php74 and PHPBASE: /usr/local/php74
    make[2]: "/etc/make.conf" line 11: set by CURDIR: /usr/ports/ports-2022Q4/lang/php74 -- sets PREFIX: /usr/local/php74 and PHPBASE: /usr/local/php74
    make[1]: "/etc/make.conf" line 11: set by CURDIR: /usr/ports/ports-2022Q4/lang/php74 -- sets PREFIX: /usr/local/php74 and PHPBASE: /usr/local/php74
    ===>  Cleaning for php74-7.4.32
    make[1]: "/etc/make.conf" line 11: set by CURDIR: /usr/ports/ports-2022Q4/lang/php74 -- sets PREFIX: /usr/local/php74 and PHPBASE: /usr/local/php74
  9. Back up the original PHP-FPM 7.4 service file.

    console
    $ sudo mv /usr/local/php74/etc/rc.d/php-fpm  /usr/local/php74/etc/rc.d/php-fpm.ORIG
    
  10. Create a new PHP-FPM 7.4 service file to enable the service to work with other PHP versions on your server.

    console
    $ sudo vi /usr/local/php74/etc/rc.d/php-fpm
    
  11. Add the following contents to the file.

    sh
    #!/bin/sh
    
    # PROVIDE: php74-fpm
    # REQUIRE: LOGIN
    # KEYWORD: shutdown
    
    #
    # Add the following line to /etc/rc.conf to enable php74-fpm:
    # php74_fpm_enable="YES"
    #
    
    . /etc/rc.subr
    
    name="php74_fpm"
    rcvar=php74_fpm_enable
    
    start_precmd="php74_fpm_prestart"
    restart_precmd="php74_fpm_checkconfig"
    reload_precmd="php74_fpm_checkconfig"
    configtest_cmd="php74_fpm_checkconfig"
    
    load_rc_config "$name"
    
    : ${php74_fpm_enable="NO"}
    : ${php74_fpm_umask=""}
    
    extra_commands="reload configtest logrotate"
    
    command="/usr/local/php74/sbin/php-fpm"
    pidfile="/var/run/php74-fpm.pid"
    sig_stop="QUIT"
    sig_reload="USR2"
    logrotate_cmd="php74_fpm_logrotate"
    
    required_files="/usr/local/php74/etc/php-fpm.conf"
    
    php74_fpm_logrotate() {
            if [ -z "$rc_pid" ]; then
                    _run_rc_notrunning
                    return 1
            fi
            echo "Rotating logs $name."
            kill -USR1 $rc_pid
    }
    
    php74_fpm_checkconfig()
    {
            echo "Performing sanity check on php74-fpm configuration:"
            eval ${command} -t
    }
    
    php74_fpm_prestart()
    {
            php74_fpm_checkconfig
            checkconfig=$?
            if [ $checkconfig -ne 0  ]; then
                    return $checkconfig
            fi
    
            if [ ! -z "$php74_fpm_umask"  ]; then
                    echo "Setting umask to: ${php74_fpm_umask}"
                    umask $php74_fpm_umask
            fi
    }
    
    run_rc_command "$1"
    

    Save and close the file.

    Within the above configuration, the pidfile="/var/run/php74-fpm.pid" directive enables the PHP-FPM 7.4 service to use a different process ID name on your server to avoid conflicts with other PHP versions.

  12. Create soft links for the PHP 7.4 and PHP-FPM 7.4 binaries, and the service file to run PHP using php74 and php74-fpm commands on your server.

    console
    $ sudo ln -s /usr/local/php74/bin/php /usr/local/bin/php74 && \
      sudo ln -s /usr/local/php74/sbin/php-fpm /usr/local/bin/php74-fpm && \
      sudo ln -s /usr/local/php74/etc/rc.d/php-fpm /etc/rc.d/php74-fpm
    
  13. View the installed PHP 7.4 version on your server.

    console
    $ php74 -v
    

    Your output should be similar to the one below:

    PHP 7.4.32 (cli) (built: Jun 15 2024 01:17:48) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
  14. View the installed PHP-FPM 7.4 version on your server.

    console
    $ php74-fpm -v
    

    Your output should be similar to the one below:

    PHP 7.4.32 (fpm-fcgi) (built: Jun 15 2024 01:17:53)
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
  15. Open the default PHP-FPM 7.4 pool configuration file www.conf.

    console
    $ sudo vi /usr/local/php74/etc/php-fpm.d/www.conf
    
  16. Find the listen directive and change the default TCP port 9000 to a value such as 9001 to access the PHP-FPM 7.4 service on your server.

    ini
    listen = 127.0.0.1:9001
    

    Save and close the file.

    You can manage and install multiple versions by following the above port collection steps depending on your desired PHP version. Change the PHP-FPM listening port for each version to limit service conflicts on the server. When using a specific version, reference the associated TCP port such as 9001 to successfully process PHP applications on your server.

Conclusion

You have installed PHP and PHP-FPM on a FreeBSD 14.0 server, configured the default FPM pools and enabled multiple versions on your server. You can use PHP with multiple applications such as web servers and securely process dynamic web application contents on your server. For more information, visit the PHP documentation.