
PHP is a widely used server-side scripting language for building dynamic web applications. PHP-FPM (FastCGI Process Manager) enhances performance and handles PHP requests efficiently when integrated with web servers.
In this article, you are to install PHP and PHP-FPM on Ubuntu 22.04 and finally set up a working PHP environment to create dynamic web applications on the server.
Before you begin:
PHP is included in Ubuntu 22.04’s default APT repositories. Follow the steps below to update your server and install the latest available PHP version.
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 add functionality to your server, enabling features like database access, encryption, and support for file formats used by many PHP applications and frameworks. Follow the steps below to install common PHP extensions and enhance your server's capabilities.
Install common PHP extensions.
The above command installs the following PHP extensions:
php-mysql – Connects PHP with MySQL databases.
php-mbstring – Supports UTF-8 and multibyte strings.
php-bcmath – Enables high-precision math operations.
php-zip – Adds ZIP archive support.
php-gd – Enables image processing.
php-curl – Allows data transfer via URLs.
php-xml – Provides XML parsing and handling.
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 included in Ubuntu 22.04’s default APT repositories and matches the installed PHP version. Follow the steps below to install PHP-FPM and enable it to start automatically at boot.
Install PHP-FPM.
View the installed PHP-FPM version on your server depending on your PHP version. For example, PHP 8.1.
Output:
List all files in the /etc/php/8.1 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.1/fpm/pool.d/.
Open the default PHP-FPM pool configuration /etc/php/8.1/fpm/pool.d/www.conf using a text editor such as Nano.
Find the [www] option to verify your PHP-FPM pool name.
Find the 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.
Find the listen directive and verify the socket path to access the PHP-FPM service on your server.
Modify the following configuration to match your PHP-FPM processing needs on your server.
pm: Sets the process manager type:
static: Fixed number of child processes.dynamic: Adjusts processes based on demand.ondemand: Spawns processes only when needed.pm.max_children: Max number of child processes.
pm.start_servers: Number of processes to start with (dynamic only).
pm.min_spare_servers: Minimum idle processes.
pm.max_spare_servers: Maximum idle processes.
pm.process_idle_timeout: Time before stopping idle processes.
access.log: Logs PHP-FPM pool requests.
slowlog: Path to log slow requests.
request_slowlog_timeout: Timeout to flag slow requests.
Save and close the file.
Restart PHP-FPM to apply your configuration changes.
PHP handles dynamic content, while PHP-FPM connects PHP to the web server with optimized resource management. Follow the steps below to test PHP and PHP-FPM by serving a sample PHP app through Nginx.
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 config listens on port 80, serves files from /var/www/html, and forwards .php requests to the PHP-FPM socket at /run/php/php8.1-fpm.sock using FastCGI.
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 may need different PHP versions. Ubuntu 22.04's default repositories offer limited PHP versions. To install additional versions like PHP 7.4, add the trusted ppa:ondrej/php repository. Follow the steps below to add the PPA and install multiple PHP versions.
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 Nginx config listens on custom port 9000 and serves files from /var/www/php74-site. It uses the index directive for default pages and forwards .php requests to PHP-FPM (php7.4-fp) via the socket at /run/php/php7.4-fpm.sock.
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.
Access the /test.php URL path using your server IP in a new web browser tab to verify that your default PHP 8.1 version processes requests separately based on your Nginx configuration.
In this article, you’ve installed PHP, PHP-FPM, and multiple PHP versions on your Ubuntu 22.04 server. By integrating PHP and PHP-FPM with applications like Nginx, you can efficiently handle dynamic web requests for various projects. For more information, visit the official PHP documentation.
0 Comments
Be the first to comment and share your perspective with the community.