This tutorial will describe easy install Nginx with PHP-FPM/FastCGI on Ubuntu 18.04 (Bionic) LTS system. We are using PHP 7.4 and configure with Nginx using PHP-FPM as PHP interpreter.
Nginx is a small, light and fast web server created by Igor Sysoev. It started out as a web server designed for maximum performance and stability. Project open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.
For example, we want to run a web server-based application using native php or frameworks such as Laravel, Code Igniter, even CMS Wordpress, Joomla, and others.
First, Nginx Install
Nginx is already bundled by the default ubuntu repository. We just need to update and install it.
$ sudo apt update
$ sudo apt install nginxSecond, PHP Installation
For the PHP installation we recommend to use ppa:ondrej/php PPA.
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/phpNext step is install PHP 7.4 the latest version available on the day of writing this tutorial. Simply execute follows commands for the installation of PHP, PHP-FPM packages and other PHp module needed.
$ sudo apt update
$ sudo apt install php7.4 php7.4-common php7.4-fpm php7.4-cli php7.4-mcrypt php7.4-mbstring php7.4-mysql php7.4-gd php7.4-curl php7.4-fileinfo php7.4-soap php7.4-zip php7.4-xmlPHP-FPM Config
After install, we adjust php-fpm config.
root@server# vim /etc/php/7.4/fpm/pool.d/www.conf
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data
listen = /run/php/php7.4-fpm.sock
listen.allowed_clients = 127.0.0.1After success install, check running status php-fpm or restart.
root@server# systemctl status php7.4-fpm.service
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-05-19 09:48:26 WIB; 6h ago
Docs: man:php-fpm7.4(8)
Process: 19969 ExecStopPost=/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited, status=0/SUCCESS)
Process: 20009 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited, status=0/SUCCESS)
Main PID: 19970 (php-fpm7.4)
Status: "Processes active: 0, idle: 17, Requests: 3, slow: 0, Traffic: 0req/sec"
Tasks: 18 (limit: 4915)
CGroup: /system.slice/php7.4-fpm.service
├─19970 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
├─19986 php-fpm: pool www-pool
├─19987 php-fpm: pool www-pool
May 19 09:48:26 sbm-web-db systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...Third, Nginx Config
Let’s create a Nginx virtual host to run with FPM/FastCGI.
vim /etc/nginx/sites-available/myweb.comFor this tutorial i am use Laravel Framework, so i copy and paste example document from Laravel website for nginx. With the necessary changes.
server {
listen 80;
server_name example.com;
root /srv/nyweb.com/public;
access_log /var/log/nginx/myweb.access;
error_log /var/log/nginx/myweb.error error;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}See fastcgi_pass, point the php-fpm socket file which is already defined in config in /etc/php/7.4/fpm/pool.d/www.conf.
Save your changes to the configuration file and create a link to site enabled directory.
$ sudo ln -s /etc/nginx/sites-available/myweb.com /etc/nginx/sites-enabled/myweb.com
$ sudo systemctl restart nginx.serviceTest Setup
Create a PHP script with phpinfo() function and place it to your server document root. Use below command to do it.
root@server# echo "<?php phpinfo(); ?>" > /srv/myweb.com/public/index.php
root@server# chown -R www-data:www-data /srv/nyweb.com/publicNow access the info.php using server IP address (for default VirtualHost) for your configured domain in Nginx VirtualHost.
Secure Nginx with Let’s Encrypt
The last, make connection secure with let’s encrypt for free. We can follow this article for install Let’s Encrypt Nginx On Ubuntu.
That is short article about install nginx php-fpm in ubuntu 20.04 server. May be it’s helpful, please feel free to leave a comment if you have any questions and I’ll appreciate it. Thank you.