<\/span><\/h2>\nThe Nginx web server can be installed with the following command:<\/p>\n
sudo apt install nginx -y<\/pre>\nOnce the installation is complete, start and enable the Nginx service:<\/p>\n
sudo systemctl start nginx && sudo systemctl enable nginx<\/pre>\n<\/span>Step 3. Install PHP-FPM<\/span><\/h2>\nNext is to install the php-fpm php extension along with other php extensions.<\/p>\n
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https -y\r\n\r\nLC_ALL=C.UTF-8 add-apt-repository ppa:ondrej\/php\r\n\r\nsudo apt update<\/pre>\nOnce the dependencies are installed, we are ready to install the PHP-FPM.<\/p>\n
sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl<\/pre>\nOnce installed, start and enable the php8.1-fpm.service<\/b><\/p>\nsudo systemctl start php8.1-fpm.service && sudo systemctl enable php8.1-fpm.service<\/pre>\nTo check if everything is OK, execute the following command:<\/p>\n
sudo systemctl status php8.1-fpm.service<\/pre>\nYou should receive the following output:<\/p>\n
root@host:\/var\/www\/html# systemctl status php8.1-fpm\r\n\u25cf php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager\r\n Loaded: loaded (\/lib\/systemd\/system\/php8.1-fpm.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Sat 2022-09-10 15:31:29 CEST; 1min 52s ago\r\n Docs: man:php-fpm8.1(8)\r\n Main PID: 16869 (php-fpm8.1)\r\n Status: \"Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req\/sec\"\r\n Tasks: 3 (limit: 4575)\r\n Memory: 8.9M\r\n CPU: 482ms\r\n CGroup: \/system.slice\/php8.1-fpm.service\r\n<\/pre>\n<\/span>Step 4. Configure PHP-FPM with Nginx<\/span><\/h2>\nThis 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.<\/p>\n
We need to create a virtual host file containing our domain name. Go into the Nginx configuration directory and create the file.<\/p>\n
cd \/etc\/nginx\/conf.d\/ && sudo nano yourdomain.com.conf<\/pre>\nPaste the following lines of code.<\/p>\n
server {\r\n listen 80;\r\n server_name yourdomain.com;\r\n root \/var\/www\/html\/;\r\n\r\n access_log \/var\/log\/nginx\/yourdomain.com-access.log;\r\n error_log \/var\/log\/nginx\/yourdomain.com-error.log error;\r\n index index.html index.htm index.php;\r\n\r\n location \/ {\r\n try_files $uri $uri\/ \/index.php$is_args$args;\r\n }\r\n\r\n location ~ \\.php$ {\r\n fastcgi_split_path_info ^(.+\\.php)(\/.+)$;\r\n fastcgi_pass unix:\/var\/run\/php\/php8.1-fpm.sock;\r\n fastcgi_index index.php;\r\n include fastcgi.conf;\r\n }\r\n }\r\n}<\/pre>\nCheck the Nginx configuration syntax to see if it is OK.<\/p>\n
nginx -t<\/pre>\nYou should receive the following output:<\/p>\n
root@vps:\/etc\/nginx\/conf.d# nginx -t\r\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\r\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful\r\n<\/pre>\nRestart the Nginx service.<\/p>\n
sudo systemctl restart nginx<\/pre>\nNext 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<\/b> The default pool configuration is www.conf<\/b>, but we will not make changes to this file since it is recommended to use separate files for every new configuration and website.<\/p>\n
First, we need to create a group and user, then add the user to the group<\/p>\n
groupadd php_fpm_group\r\n\r\nuseradd -g php_fpm_group php_fpm_user<\/pre>\nNext, we need to create a pool configuration for the website.<\/p>\n
cd \/etc\/php\/8.1\/fpm\/pool.d && touch yourdomain.conf<\/pre>\nOpen the file yourdomain.conf<\/b> with your favorite editor and paste the following lines of code:<\/p>\n[pool_name]\r\nuser = php_fpm_user\r\ngroup = php_fpm_group\r\nlisten = \/var\/run\/php\/php8.1-fpm.sock\r\nlisten.owner = www-data\r\nlisten.group = www-data \r\npm = dynamic \r\npm.max_children = 30 \r\npm.start_servers = 5 \r\npm.process_idle_timeout = 20s\r\n<\/pre>\nLet’s explain the parameters in the file created above:<\/p>\n
The pool name must be specific to every new pool configuration. The user<\/b> and group<\/b> must be the created user and group for our website, while the listen.owner<\/b> and listen.group<\/b> is the user and the group that Nginx is running on. The listen<\/b> is the path of the PHP8.1 sock.<\/p>\n
The pm.max_children<\/b> is for setting the maximum number of children that can be alive at the same time. The pm.start_servers<\/b> is the number of children created on startup. The pm.process_idle_timeout<\/b> is used only if the pm<\/b> is set to dynamic and is the number of desired maximum number idle server processes. The pm=dynamic<\/b> means the number of child processes is set dynamically based on the following directives.<\/p>\n
Remember that this configuration can be different according to your server resource and website needs.<\/p>\n
The last thing is to create the phpinfo.php<\/b> file into the document root of your website.<\/p>\ncd \/var\/www\/html\/ && sudo nano phpinfo.php<\/pre>\nOnce you open the file, paste the following lines of code:<\/p>\n
<? php\r\nphpinfo();\r\n?>\r\n<\/pre>\nSave the file, close it and access the following URL: http:\/\/yourdomain.com\/phpinfo.php<\/b><\/p>\n
In the Environment<\/b> and PHP Variables<\/b> you can notice that the USER<\/b> and HOME<\/b> are set to php_fpm_user<\/b> and \/home\/php_fpm_user<\/b>, respectively. This is proof that the PHP-FPM is successfully configured with Nginx.<\/p>\n
<\/p>\n
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.<\/p>\n
If you liked this about configuring PHP-fpm with Nginx on Ubuntu 22.04<\/a>, please share it with your friends on social networks or simply leave a reply below.<\/p>\n","protected":false},"excerpt":{"rendered":"In this tutorial, we are going to explain how to configure PHP-fpm with Nginx on Ubuntu 22.04. Nginx is a … <\/p>\n
Read More<\/a><\/p>\n","protected":false},"author":4,"featured_media":43649,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,1707],"tags":[49,51,2012],"yoast_head":"\nConfigure PHP-FPM with Nginx on Ubuntu 22.04 - RoseHosting<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n \n \n\t \n\t \n\t \n