
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.
Before you begin:
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.
Update the server package index.
Search the default pkg catalog and verify all available PHP versions.
Your output should be similar to the one below:
PHP 8.3 is the latest available version based on the above output you can install on your server.
Install PHP and PHP-FPM based on your desired version. For example, PHP version 8.3.
View the installed PHP version on your server.
Output:
View the installed PHP-FPM version on your server.
Output:
Enable the PHP-FPM service to automatically start at boot time.
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.
Install common PHP extensions. Replace php83 with your desired PHP version.
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.
View all PHP extensions available on your server.
Your output should be similar to the one below:
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.
Open the default PHP-FPM pool configuration /usr/local/etc/php-fpm.d/www.conf using a text editor such as vi.
listen directive and verify the PHP-FPM port to use when connecting to the service.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.
Test the PHP-FPM configuration for syntax errors.
Output:
Restart the PHP-FPM service to apply your configuration changes.
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.
Create a new sample PHP application file test.php in your working directory.
Add the following contents to the file.
Save and close the file.
Run the application file using the PHP CLI utility.
Output:
Install the Nginx web server package to test access to the PHP-FPM service.
Enable the Nginx service to automatically start at boot time.
Open the main Nginx configuration file.
Uncomment the following directives by removing the # sign on the following directives.
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.
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.
Test the Nginx configuration for errors.
Output:
Copy the sample PHP application file you created earlier to the /usr/local/www/nginx web root directory.
Restart the Nginx web server to apply your configuration changes.
Access your server IP address using a web browser such as Chrome and load the /test.php URL path.
Verify that your PHP application loads correctly to confirm that PHP-FPM is successfully processing PHP requests through the Nginx web server.
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.
Install all required compile dependency packages on your server.
Download the FreeBSD ports collection with the 2022Q4 branch that includes older PHP versions such as PHP 7 and above.
Create a new directory /usr/ports to store your ports collection.
Extract the downloaded archive contents to the /usr/ports directory.
Switch to the PHP 7.4 port directory.
Open the /etc/make.conf configuration file.
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.
The above configuration works by modifys the make environment variables to install compiled packages to the /usr/local/php<version> on your server.
Run the following command to compile and install PHP 7.4 with PHP-FPM 7.4.
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.
Back up the original PHP-FPM 7.4 service file.
Create a new PHP-FPM 7.4 service file to enable the service to work with other PHP versions on your server.
Add the following contents to the file.
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.
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.
View the installed PHP 7.4 version on your server.
Your output should be similar to the one below:
View the installed PHP-FPM 7.4 version on your server.
Your output should be similar to the one below:
Open the default PHP-FPM 7.4 pool configuration file www.conf.
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.
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.
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.
0 Comments
Be the first to comment and share your perspective with the community.