In this tutorial, we are going to explain how to configure PHP-fpm with Nginx on Ubuntu 22.04.
Nginx is a web server used for serving web requests on our website. It can be used as a reverse proxy, mail proxy, HTTP cache, or load balancer. Nginx is a free and open source software that can be configured with the PHP module PHP-fpm and improve the website loading and handle a huge amount of traffic in no time.
Configuring PHP-fpm with Nginx on Ubuntu 22.04 is a very easy and straightforward process that can take up to 15 minutes. Let’s get started!
Table of Contents
Prerequisites
- Fresh install of Ubuntu 22.04
- User privileges: root or non-root user with sudo privileges
Step 1. Update the System
A fresh installation of Ubuntu 22.04 needs a system update, and that’s why we need to execute the following commands.
Step 2. Install Nginx webserver
The Nginx web server can be installed with the following command:
Once the installation is complete, start and enable the Nginx service:
Step 3. Install PHP-FPM
Next is to install the php-fpm php extension along with other php extensions.
Once the dependencies are installed, we are ready to install the PHP-FPM.
Once installed, start and enable the php8.1-fpm.service
To check if everything is OK, execute the following command:
You should receive the following output:
Step 4. Configure PHP-FPM with Nginx
This is the last and the most important step since, in this paragraph, we are going to explain how to configure the PHP-fpm with previously installed Nginx.
We need to create a virtual host file containing our domain name. Go into the Nginx configuration directory and create the file.
Paste the following lines of code.
Check the Nginx configuration syntax to see if it is OK.
You should receive the following output:
Restart the Nginx service.
Next is to create a custom pool in the configuration of the PHP8.1-FPM. The location of the pool configuration is /etc/php/8.1/fpm/pool.d The default pool configuration is www.conf, but we will not make changes to this file since it is recommended to use separate files for every new configuration and website.
First, we need to create a group and user, then add the user to the group
Next, we need to create a pool configuration for the website.
Open the file yourdomain.conf with your favorite editor and paste the following lines of code:
Let’s explain the parameters in the file created above:
The pool name must be specific to every new pool configuration. The user and group must be the created user and group for our website, while the listen.owner and listen.group is the user and the group that Nginx is running on. The listen is the path of the PHP8.1 sock.
The pm.max_children is for setting the maximum number of children that can be alive at the same time. The pm.start_servers is the number of children created on startup. The pm.process_idle_timeout is used only if the pm is set to dynamic and is the number of desired maximum number idle server processes. The pm=dynamic means the number of child processes is set dynamically based on the following directives.
Remember that this configuration can be different according to your server resource and website needs.
The last thing is to create the phpinfo.php file into the document root of your website.
Once you open the file, paste the following lines of code:
Save the file, close it and access the following URL: http://yourdomain.com/phpinfo.php
In the Environment and PHP Variables you can notice that the USER and HOME are set to php_fpm_user and /home/php_fpm_user, respectively. This is proof that the PHP-FPM is successfully configured with Nginx.
That’s it. If you find any difficulties while configuring this, you can simply contact our technical support by submitting a support ticket, and our admins will help you immediately. We are available 24/7.
If you liked this about configuring PHP-fpm with Nginx on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below.
Once you create the new pool file for php-fpm, you need to restart php-fpm.