
PHP (Hypertext Preprocessor) is a widely used server-side scripting language designed for building dynamic web applications. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation that manages worker processes efficiently, offering features such as adaptive process spawning, graceful restarts, and improved resource handling compared to traditional CGI methods.
This article explains how to install PHP and PHP-FPM on an Ubuntu 26.04 server. It covers installing common PHP extensions, configuring the PHP-FPM pool settings, testing PHP with Nginx, and running multiple PHP versions side by side.
Before you begin, you need to:
The default Ubuntu 26.04 APT repositories include the latest PHP package. The following steps update the package index and install the PHP command-line interpreter.
Update the APT package index.
Install PHP.
Confirm the installed PHP version.
Your output should be similar to the one below:
PHP extensions add capabilities such as database connectivity, image processing, and encryption support that most web applications and frameworks require. The following steps install commonly used extensions and verify the active modules.
Install common PHP extensions.
The command installs the following extensions:
php-mysql: Provides connectivity to MySQL and MariaDB databases.php-mbstring: Adds support for multibyte string encoding such as UTF-8.php-bcmath: Enables arbitrary-precision arithmetic operations.php-zip: Adds support for reading and writing ZIP archives.php-gd: Provides image creation and manipulation functions.php-curl: Enables HTTP requests and URL transfers from PHP.php-xml: Adds XML parsing and generation support.Run the following command to list all available PHP extensions for your installed version.
View all active PHP modules.
Your output should be similar to the one below:
PHP-FPM is available in the default APT repositories and matches the installed PHP version. The following steps install PHP-FPM, verify the installation, and configure the worker pool settings.
Install PHP-FPM.
Confirm the installed PHP-FPM version.
Your output should be similar to the one below:
List the PHP configuration directories to verify that the fpm directory exists.
Your output should be similar to the one below:
Open the default PHP-FPM pool configuration file using a text editor such as Nano.
Find the [www] directive to verify the pool name.
Find the user and group directives to confirm that PHP-FPM runs as the www-data user. Web servers such as Nginx require this user for proper file access.
Find the listen directive to verify the Unix socket path used to connect to the PHP-FPM service.
Adjust the following process manager directives based on your server resources and expected traffic.
Within the configuration:
pm: Sets the process manager mode. Options include static (fixed number of workers), dynamic (scales workers based on demand), and ondemand (spawns workers only when requests arrive).pm.max_children: Defines the maximum number of child processes that can run simultaneously.pm.start_servers: Sets how many child processes to create at startup.pm.min_spare_servers: Maintains a minimum number of idle worker processes.pm.max_spare_servers: Limits the maximum number of idle worker processes.Save and close the file.
Restart PHP-FPM to apply the configuration changes.
PHP processes scripts through the command-line interpreter, while PHP-FPM handles requests forwarded by a web server. The following steps verify both by running a PHP script from the terminal and then configuring Nginx to serve a PHP application through PHP-FPM.
Create a sample PHP script.
Add the following content to the file.
Save and close the file.
Run the script using the PHP CLI.
Your output should be similar to the one below:
Stop and disable the Apache service that is pre-installed with PHP.
Install the Nginx web server to test PHP-FPM integration.
Back up the default Nginx virtual host configuration.
Create a new default Nginx configuration file.
Add the following configuration to the file.
Save and close the file.
This configuration listens on port 80 and serves files from the /var/www/html directory. Nginx forwards all .php requests to the PHP-FPM socket at /run/php/php-fpm.sock for processing through FastCGI.
Test the Nginx configuration for syntax errors.
Create a PHP test file in the Nginx web root directory.
Add the following content to the file.
Save and close the file.
This script displays detailed PHP version and configuration information when accessed through a web browser.
Allow HTTP traffic on port 80 through the firewall.
Restart Nginx to apply the configuration.
Access the PHP test page in a web browser. Replace SERVER-IP with your server's public IP address.
The browser displays the PHP information page, confirming that Nginx processes PHP requests through PHP-FPM.
You have installed PHP and PHP-FPM on an Ubuntu 26.04 server, configured the FPM worker pool, and verified PHP processing through Nginx. PHP-FPM integrates with web servers such as Nginx and Apache to handle dynamic application requests efficiently. For more information, refer to the official PHP documentation.
0 Comments
Be the first to comment and share your perspective with the community.