
PHP (Hypertext Preprocessor) is a popular server-side scripting language that enables the development of dynamic web applications. PHP-FPM (FastCGI Process Manager) provides additional features and improved performance over traditional CGI-based methods to enable PHP connections with other applications such as web servers to process dynamic web application requests.
This article explains how to install PHP and PHP-FPM on Ubuntu 24.04 and set up a working PHP environment to create dynamic web applications on the server.
Before you begin:
PHP is available in the default APT repositories on Ubuntu 24.04 with the latest package information. Follow the steps below to update the server packages and install the latest PHP version on your server.
Update the server package index.
Install PHP.
View the installed PHP version on your server.
Your output should be similar to the one below:
PHP extensions enable extra PHP functionalities on your server such as database integration, additional file formats, and encryption required by most PHP applications and frameworks. Follow the steps below to install common PHP extensions and enable extra processing functionalities on your server.
Install common PHP extensions.
The above command installs the following PHP extensions:
php-mysql: Enables PHP applications to connect and interact with MySQL databases.
php-mbstring: Enables UTF8 text formats.
php-bcmath: Enables arbitrary precision mathematics functionalities.
php-zip: Enables support for ZIP archives in PHP applications.
php-gd: Enables PHP image processing functionalities.
php-curl: Allows URL transfers and synchronization with PHP applications.
php-xml: Enables XML support in PHP applications.
Run the following command to list all PHP extensions you can install based on your server's PHP version.
View all installed PHP extensions on your server.
Your output should look like the one below:
PHP-FPM is available in the default APT repositories and matches the latest PHP package version installed on your server. Follow the steps below to install PHP-FPM and enable the service to automatically start at boot time.
Install PHP-FPM.
View the installed PHP-FPM version on your server depending on your PHP version. For example, PHP 8.3.
Output:
List all files in the /etc/php/8.3 directory depending on your installed PHP version. Then, verify that the fpm directory is available.
Output:
Based on the above output, PHP-FPM fpm is available and the necessary configurations that control how PHP processes requests on your server.
Switch to the PHP-FPM pool configurations directory /etc/php/8.3/fpm/pool.d/.
Open the default PHP-FPM pool configuration /etc/php/8.3/fpm/pool.d/www.conf using a text editor such as Nano.
[www] option to verify your PHP-FPM pool name.user and group options to verify the user PHP-FPM runs as. When using a web server, verify that PHP-FPM runs as the www-data user.listen directive and verify the socket path to access the PHP-FPM service on your server.pm: Enables the PHP-FPM process manager with any of the following options:static: Sets a fixed number of worker processes using pm.max_children.dynamic: Enables a variable number of worker processes based on demand.ondemand: Enables spawning of worker processes only when needed and disables automatic start.pm.max_children: Sets the maximum number of child processes to create when PHP-FPM starts to control your server memory usage.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 processes to allow on the server.pm.max_spare_servers: Sets the maximum number of idle processes to run on the server.pm.process_idle_timeout: Sets the maximum number of seconds after to wait and stop idle processes.access.log: Enables logging of all PHP-FPM pool connection requests.slowlog: Sets the path to log slow requests.request_slowlog_timeout: Sets the maximum time to wait for a request before it's marked as slow.Save and close the file.
Restart PHP-FPM to apply your configuration changes.
PHP processes dynamic web application contents while PHP-FPM manages the connection to PHP with the necessary resources to enable fast processing of requests on your server. Follow the steps below to test PHP and PHP-FPM by creating a sample PHP application to deliver using the Nginx web server.
Switch to your user home directory.
Create a new sample PHP application file sample.php.
Add the following contents to the file.
Save and close the file.
Run the application using the PHP CLI utility.
Output:
Based on the above output, PHP processes dynamic web application files on your server correctly.
Install the Nginx web server application to test access to the PHP-FPM service.
Back up the default Nginx virtual host configuration.
Create a new default Nginx configuration file.
Add the following configurations to the file.
Save and close the file.
The Nginx configuration above listens for connection requests using all server IP addresses on the default HTTP port 80 and delivers files from the /var/www/html web root directory. Nginx forwards all requests that match the .php file extension to the PHP-FPM socket path /run/php/php-fpm.sock for processing using FastCGI to connect to the default PHP-FPM version service.
Test the Nginx configuration for errors.
Output:
Create a new sample PHP application file test.php in the web root directory /var/www/html specified in your Nginx configuration.
Add the following contents to the file.
Save and close the file.
The above PHP application displays the available PHP version information on your server when accessed in a web browser.
Stop the apache2 service that's pre-installed with PHP to unbind the HTTP port 80.
Allow connections to the HTTP port 80 through the default UFW firewall utility.
Restart Nginx to apply your configuration changes.
Access your server IP address using a web browser such as chrome and load the /test.php URL path to access your PHP application.
Multiple projects require specific PHP versions depending on your needs. PHP supports multiple installed versions you can reference and use with the associated PHP-FPM version. However, the default Ubuntu 24.04 repositories include limited PHP versions. Add a trusted community repository such as ppa:ondrej/php to install older or newer PHP versions on your server such as PHP 7.4 that may not be available in the default repositories. In the following steps, add the ppa:ondrej/php package source to your APT repositories and install multiple PHP versions on your server.
Add the ppa:ondrej/php PPA to your APT repositories.
Install a specific PHP and PHP-FPM version on your server. For example, PHP 7.4.
View the installed PHP version information on your server.
Output:
View the PHP version's PHP-FPM service status and verify that it's running. For example, php7.4-fpm.
Output:
Create a new web root directory to store different PHP application files. For example, /var/www/php74-site.
Copy the test.php PHP application file you created earlier to the directory as index.php.
Create a new Nginx virtual host configuration to test access to your installed PHP-FPM version. For example, php74-site.conf.
Add the following configurations to the file.
Save and close the file.
The above Nginx configuration listens for incoming connections on the custom TCP port 9000 and delivers files from your web root directory /var/www/php74-site. The index directive specifies the default web application files to serve on all requests by default. Then, Nginx forwards all PHP file requests .php to your PHP version's PHP-FPM service php7.4-fpm using the default pool socket path /run/php/php7.4-fpm.sock for processing.
Link the Nginx configuration file to the /etc/nginx/sites-enabled activate the new virtual host.
Test the Nginx configuration for errors.
Output:
Allow network connections to the virtual host TCP port 9000 through the firewall.
Reload the firewall rules to apply changes.
Restart Nginx to apply your configuration changes.
Access your server IP on port 9000 in a new web browser window to open your index.php web application file.
Verify that your PHP 7.4 version information and modules display in your web browser.
/test.php URL path using your server IP in a new web browser tab to verify that your default PHP 8.3 version processes requests separately based on your Nginx configuration.
You have installed PHP and PHP-FPM on your Ubuntu 24.04 server. Then, you installed multiple PHP versions to run different web application projects depending on your specific feature and version needs. You can integrate PHP and PHP-FPM with multiple applications such as Nginx to securely process dynamic web application requests on your server. For more information, visit the official PHP documentation.
0 Comments
Be the first to comment and share your perspective with the community.