

Install Nginx With PHP-FPM on Ubuntu 22.04|20.04
source link: https://computingforgeeks.com/install-nginx-with-php-fpm-on-ubuntu-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This guide has been created to help users running Ubuntu 22.04|20.04 server to install Nginx web server and configure PHP-FPM (FastCGI Process Manager). Nginx is a high performance web server that’s free to use. Nginx is designed for speed and scalability with capabilities of reverse proxy and load balancing to a number of backend servers both with HTTP, TCP and UDP protocols. This website is powered by WordPress and Nginx and the performance is really good. Nginx has small memory footprint as compared to Apache, handling same number of concurrent connections.
Features of Nginx Web Server
- Content Cache – Cache static and dynamic content
- Load Balancing – HTTP, TCP, and UDP load balancing with Layer 7 request routing using URI, cookie, args, and more.
- Reverse proxy multiple protocols: HTTP, gRPC, memcached, PHP‑FPM, SCGI, uwsgi
- Handle hundreds of thousands of clients simultaneously
- Stream HTTP video: FLV, HDS, HLS, MP4
- HTTP/2 gateway with HTTP/2 server push support
- Dual‑stack RSA/ECC SSL/TLS offload
- Monitoring plugins: AppDynamics, Datadog, Dynatrace plug‑ins
Step 1: Update Ubuntu
Before you begin, you should have a running Ubuntu server that has been updated and upgraded to the latest available packages.
sudo apt update
sudo apt upgrade
Step 2: Install Nginx on Ubuntu22.04|20.04 Linux
After the system is updated, proceed to install Nginx package on Ubuntu22.04|20.04 Linux system:
sudo apt install nginx
The service should be started automatically after installation.
$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-05-09 19:38:43 UTC; 39s ago
Docs: man:nginx(8)
Main PID: 6449 (nginx)
Tasks: 2 (limit: 2344)
Memory: 3.8M
CGroup: /system.slice/nginx.service
├─6449 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─6451 nginx: worker process
May 09 19:38:43 ubuntu20 systemd[1]: Starting A high performance web server and a reverse proxy server...
May 09 19:38:43 ubuntu20 systemd[1]: Started A high performance web server and a reverse proxy server.
Notice that you cannot run both Apache and Nginx on same port. You’ll need to disable Apache web server or change port of one of them to not http standard port.
sudo systemctl disable --now apache2
sudo systemctl restart nginx
UFW firewall can be configured to allow port 80:
sudo ufw allow proto tcp from any to any port 80,443
Step 3: Install PHP-FPM on Ubuntu 22.04|20.04
If you’re planning on using PHP with Nginx, consider installing PHP-FPM package.
sudo apt update
sudo apt install php php-cli php-fpm php-json php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
PHP-FPM has the service that should be running.
### Ubuntu 20.04 ###
$ systemctl status php*-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 Sat 2020-05-09 19:50:53 UTC; 2min 26s ago
Docs: man:php-fpm7.4(8)
Main PID: 22141 (php-fpm7.4)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 2344)
Memory: 9.3M
CGroup: /system.slice/php7.4-fpm.service
├─22141 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
├─22142 php-fpm: pool www
└─22143 php-fpm: pool www
May 09 19:50:53 ubuntu20 systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
May 09 19:50:53 ubuntu20 systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
### Ubuntu 22.04 ###
$ systemctl status php*-fpm.service
● php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-11-30 22:24:44 EAT; 12s ago
Docs: man:php-fpm8.0(8)
Process: 69709 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.0/fpm/pool.d/www.conf 80 (code=exited, status=0/SUCCESS)
Main PID: 69706 (php-fpm8.0)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 9482)
Memory: 9.1M
CPU: 41ms
CGroup: /system.slice/php8.0-fpm.service
├─69706 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf)
├─69707 php-fpm: pool www
└─69708 php-fpm: pool www
Nov 30 22:24:44 ubuntu22 systemd[1]: Starting The PHP 8.0 FastCGI Process Manager...
Nov 30 22:24:44 ubuntu22 systemd[1]: Started The PHP 8.0 FastCGI Process Manager.
PID and Socket file are located in the directory:
### Ubuntu 20.04 ###
$ ls /run/php/
php-fpm.sock php7.4-fpm.pid php7.4-fpm.sock
### Ubuntu 22.04 ###
$ ls /run/php/
php-fpm.sock php8.0-fpm.pid php8.0-fpm.sock
Step 4: Configure PHP-FPM with Nginx on Ubuntu
Edit your Application Nginx configuration file and set fastcgi_pass section to load through FPM socket. See below snippet.
$ sudo vim /etc/nginx/php_fastcgi.conf
# 404
try_files $fastcgi_script_name =404;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_hide_header X-Powered-By;
fastcgi_hide_header X-CF-Powered-By;
Reload Nginx and open your application on the web to confirm it is working as expected. You have successfully installed Nginx Web server on Ubuntu 22.04|20.04 Linux machine.
PHP Web Programming Video Courses:
Similar guides:
Nginx Books to read
Recommend
-
11
一、改动php.ini 最近使用PhpMyAdmin导入一个1.5M的*.sql.zip文件到MySQL过程中遇到上传文件异常: 413 Request Entity Too Large 搜索了一下,原来是
-
11
PHP 技能精进之 PHP-FPM 多进程模型 仅限深圳|现场揭秘:腾讯云原生数据库架构探索与实践 >> ...
-
12
Fastcgi 协议分析与 PHP-FPM 攻击方法 Fastcgi 协议分析与 PHP-FPM 攻击方法 [toc] 前言?没有前言了…… 早期的Web服务器,只能响应浏览...
-
9
php内核分析-fpm和df的问题思考 阅读量 25365...
-
15
配置 php-fpm 并 安装 ionCube7.2 https://www.digitalocean.com...
-
7
解决 Docker Nginx 与 Php-fpm 偶现 502 问题 2021-10-02 这个标题不太好取,目前环境是 Nginx + Php-fpm,其中之前提过 WordPress 容器优化,使用 php -fpm 可以极大程度的减少容器体积,但那时候遗留了一个问题,就是在后台更新插件或...
-
9
PHP-FPM中backlog参数变更的一些思考 浏览:1516次 出处信息 在前年时,业务中遇到好多次因为PHP-FPM的backlog参数...
-
4
多nginx单php-fpm的配置方法 浏览:3034次 出处信息 用php fastcgi和nginx搭配的形式是很常见的,在很BT的需求下...
-
20
<?xml encoding="utf-8" ??>OpenBSD 7, Nginx, PHP-FPM (FastCGI Process Manager), and MySQL (OEMP) work as a collection to form a web stack that makes it possible to run dynamic applications on a server. Ngi...
-
6
Hardening NGINX and PHP-FPM for Production Arne Blankerts |
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK