
PHP is a widely-used server-side scripting language for building dynamic web applications. PHP-FPM enhances performance by efficiently managing PHP processes.
This article covers installing PHP and PHP-FPM on Ubuntu 20.04 to set up a PHP environment.
Before you begin:
Have an Ubuntu 20.04 server.
Set up a new A record for your domain that points to the server IP address.
Access the server using SSH as a non-root user with sudo privileges.
Ubuntu 20.04 includes PHP in its default APT repositories. Update your server packages and install PHP using the latest available 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 like database support, file handling, and encryption—commonly required by PHP apps and frameworks. Install them to enhance your server’s PHP capabilities.
Install common PHP extensions.
The above command installs the following PHP extensions:
Common PHP extensions include:
php-mysql: Connects PHP apps with MySQL.
php-mbstring: Handles UTF-8 text.
php-bcmath: Supports precision math operations.
php-zip: Enables ZIP file support.
php-gd: Adds image processing features.
php-curl: Supports data transfer via URLs.
php-xml: Enables XML handling in PHP.
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's APT repositories and aligns with your installed PHP version. Install it to handle PHP requests efficiently and ensure it starts automatically on boot.
Install PHP-FPM.
View the installed PHP-FPM version on your server depending on your PHP version. For example, PHP 7.4.
Output:
List all files in the /etc/php/7.4 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/7.4/fpm/pool.d/.
Open the default PHP-FPM pool configuration /etc/php/7.4/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.
PHP-FPM pool settings control how PHP processes handle requests:
pm: Defines the process manager type (static, dynamic, or ondemand).pm.max_children: Limits total PHP child processes to manage memory.pm.start_servers, pm.min_spare_servers, pm.max_spare_servers: Control how many PHP processes run or stay idle.pm.process_idle_timeout: Stops idle processes after a set time.access.log: Logs all incoming requests.slowlog and request_slowlog_timeout: Track and log slow PHP requests for performance tuning.Save and close the file.
Restart PHP-FPM to apply your configuration changes.
PHP handles dynamic content, while PHP-FPM manages and optimizes request processing. To verify they're working, you can deploy a sample PHP file 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 config listens on port 80 for all IPs and serves files from /var/www/html. PHP files are handled by FastCGI via the PHP-FPM socket at /run/php/php7.4-fpm.sock.
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.
To use multiple PHP versions, add the ppa:ondrej/php repository. This allows installing PHP versions like 8.1 that aren't available in Ubuntu 20.04's default repositories.
Add the ppa:ondrej/php PPA to your APT repositories.
Install a specific PHP and PHP-FPM version on your server. For example, PHP 8.1.
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, php8.1-fpm.
Output:
Create a new web root directory to store different PHP application files. For example, /var/www/php81-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, php81-site.conf.
Add the following configurations to the file.
Save and close the file.
This Nginx setup listens on port 9000, serves files from /var/www/php81-site, and uses the index directive for default files. It forwards .php requests to the PHP 8.1 FPM socket at /run/php/php8.1-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 8.1 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 7.4 version processes requests separately based on your Nginx configuration.
You have installed PHP and PHP-FPM on your Ubuntu 20.04 server, enabling multiple PHP versions for different web applications based on project needs. PHP and PHP-FPM can be integrated with applications like Nginx to handle dynamic web requests securely.
0 Comments
Be the first to comment and share your perspective with the community.