How To Configure PHP-FPM With NGINX - DigitalOcean
How To Configure PHP-FPM With NGINX - DigitalOcean
1. Install PHP�FPM
Summary
1 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
Meghna Gangwar
• You can open a SSH session to your Ubuntu 18.04 system using root or a sudo
enabled user.
• You have already installed NGINX and PHP in your Ubuntu 18.04 system.
2 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
• Install PHP�FPM
• Configure PHP�FPM Pool
• Configure NGINX for PHP�FPM
• Test NGINX PHP�FPM Configuration
Nginx doesnʼt know how to run a PHP script of its own. It needs a PHP module like
PHP�FPM to efficiently manage PHP scripts. PHP�FPM, on the other hand, runs
outside the NGINX environment by creating its own process. Therefore when a user
requests a PHP page the nginx server will pass the request to PHP�FPM service using
FastCGI. The installation of in Ubuntu 18.04 depends on PHP and its version.
Check the documentation of installed PHP before proceeding with installing FPM in
your server. Assuming you have already installed the latest PHP 7.3, then you can
install FPM using the following apt-get command.
The FPM service will start automatically, once the installation is over. You can verify
that using the following systemd command:
The php-fpm service creates a default pool, the configuration (www.conf) for which
can be found in /etc/php/7.3/fpm/pool.d folder. You can customize the default pool
3 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
# groupadd wordpress_user
# useradd -g wordpress_user wordpress_user
Now navigate to the FPM configuration directory and create a configuration file using
your favorite text editor like vi:
# cd /etc/php/7.3/fpm/pool.d
# vi wordpress_pool.conf
[wordpress_site]
user = wordpress_user
group = wordpress_user
listen = /var/run/php7.2-fpm-wordpress-site.sock
listen.owner = www-data
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
; Choose how the process manager will control the number of child processes.
pm = dynamic
pm.max_children = 75
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.process_idle_timeout = 10s
The above FPM configuration options and their values are described below.
• : The name of the pool and must be unique across all pool
names.
• : The user and group under which the pool will run.
• : The name of the socket file for this pool.
• : Must match to the user and group on which
NGINX is running. In our case it is www-data.
• : Allows to set custom php configuration values.
• : Allows to set PHP boolean flags.
• : The process manager settings and the value is Dynamic means the number
4 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
...
...
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
...
...
Also, the process managers settings in the above pool configuration file are set to
dynamic. Choose a setting that best suits your requirement. The other configuration
options for process manager are:- : A fixed number of PHP processes will be
maintained.
Once you are done with creating the above configuration file, restart fpm service to
apply new settings:
The FPM pool will be created immediately to serve php pages. Remember, you can
create a separate systemd service by specifying the above FPM configuration file
thereby enabling you to start/stop this pool without affecting other pools.
5 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
Now create an NGINX server block that will make use of the above FPM pool. To do
that, edit your NGINX configuration file and pass the path of poolʼs socket file using
the option fastcgi_pass inside location block for php.
server {
listen 80;
server_name example.journaldev.com;
root /var/www/html/wordpress;
access_log /var/log/nginx/example.journaldev.com-access.log;
error_log /var/log/nginx/example.journaldev.com-error.log error;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php7.2-fpm-wordpress-site.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
6 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
Make sure the above configuration setting is syntactically correct and restart NGINX.
# nginx-t
# systemctl restart nginx
To test if the above NGINX configuration file is indeed using the newly created FPM
pool, create a php info file inside the web root. I have used /var/www/html/wordpress
as a web root in the above NGINX configuration file. Adjust this value according to
your environment.
# cd /var/www/html/wordpress
# echo "<?php echo phpinfo();?>" > info.php
Once you are done with creating the PHP info page, point your favorite web browser
to it. You will notice that the value of $_SERVER['USER'] and $_SERVER['HOME']
variable are pointing to wordpress_user and /home/wordpress_user respectively that
we set in the FPM configuration file previously and thus confirms that the NGINX is
serving the php pages using our desired FPM pool.
In this article, we learned how to install php-fpm and configure separate pools for
different users and applications. We also learned how to configure an NGINX server
block to connect to a PHP�FPM service. PHP�FPM provides reliability, security,
scalability, and speed along with a lot of performance tuning options. You can now
split the default PHP�FPM pool into multiple resource pools to serve different
applications. This will not only enhance your server security but also enable you to
allocate server resources optimally!
7 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
Thanks for learning with the DigitalOcean Community. Check out our
offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly
reviewed it. If you have any suggestions for improvements, please let us know by
clicking the “report an issue“ button at the bottom of the tutorial.
8 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
- Piotr
• September 2, 2020
I have deployed a web app to AWS and php-fpm is occupied most of the
CPU cores. sometimes it makes the server crash. Can there be something
wrong with the configuration?
- vishal
• April 2, 2020
when ever i m trying to execute php file from browser it gets download
- aditya
• November 6, 2019
Thank you for your tutorial. I am plan to install Nginx server separately with
PHP server. Meaning Nginx in one server and PHP is in another server…And
the NGINX server will located at DMZ zone and will face the internet. My
question is: 1� Which server we need to install PHP�FPM? In Nginx server or
in php server? 2� And how these two server will communicate? Which
confuguration files need to alter? Sorry if my question is sound silly :). I am
new to php-fpm and still in the midst of understanding it. Thank you.
- Paklah
9 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
Click below to sign up and get to try our products over 60 days!
10 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
11 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
12 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
13 of 14 05/08/24, 11:18
How to Configure PHP-FPM with NGINX | DigitalOcean https://www.digitalocean.com/community/tutorials/ph...
14 of 14 05/08/24, 11:18