在Ubuntu 20.04上安装Nginx,使用PHP-FPM进行PHP-FPM
nginx是一个可自由使用的高性能Web服务器。
Nginx专为速度和可扩展性而设计,具有反向代理的功能,并负载均衡到许多后端服务器,包括HTTP,TCP和UDP协议。
本由WordPress和Nginx提供动力,性能非常好。
与Apache相比,nginx具有小的内存占用空间,处理相同数量的并发连接。
nginx的特征
内容缓存 - 缓存静态和动态ContentLoad Balancing - 使用URI,Cookie,Args和More.reverse Proxy Heque Routing的HTTP,TCP和UDP负载平衡,使用URI,Cookie,Args和More.reverse Proxy多协议:HTTP,GRPC,MEMCACHED,PHP-FPM,SCGI, UWSGIHandle数十万客户同时发生的HTTP视频:FLV,HDS,HLS,MP4HTTP/2网关带HTTP/2服务器推送支持Dual-Stack RSA/ECC SSL/TLS OffloadMonitoring插件:Appdynamics,DataDog,Dynatrace插件
第1步:更新Ubuntu
在开始之前,我们应该拥有已更新和升级到最新可用包的运行Ubuntu Server。
sudo apt update sudo apt upgrade
第2步:在Ubuntu 20.04 Linux上安装nginx
更新系统后,继续在Ubuntu 20.04 Linux上安装nginx包:
sudo apt install nginx
安装后应自动启动服务。
$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 2017-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 Jan 09 19:38:43 ubuntu20 systemd[1]: Starting A high performance web server and a reverse proxy server... Jan 09 19:38:43 ubuntu20 systemd[1]: Started A high performance web server and a reverse proxy server.
请注意,我们无法在同一端口上运行apache和nginx。
我们需要禁用Apache Web服务器或者将其中一个的端口更改为不是HTTP标准端口。
sudo systemctl disable --now apache2
UFW防火墙可以配置为允许端口80:
sudo ufw allow proto tcp from any to any port 80,443
第3步:在Ubuntu 20.04上安装PHP-FPM
如果我们正在计划使用Nginx使用PHP,请考虑安装PHP-FPM包。
sudo apt update sudo apt install php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
PHP-FPM具有应运行的服务。
$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 Sat 2017-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 Jan 09 19:50:53 ubuntu20 systemd[1]: Starting The PHP 7.4 FastCGI Process Manager... Jan 09 19:50:53 ubuntu20 systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
PID和套接字文件位于目录中:
$ls /run/php/ php-fpm.sock php7.4-fpm.pid php7.4-fpm.sock
步骤4:使用Ubuntu上使用nginx配置PHP-FPM
编辑应用程序nginx配置文件并设置FastCGI_Pass部分以加载FPM套接字。
见片段。
$cat /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/php7.4-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;
重新加载nginx并在Web上打开应用程序以确认它按预期工作。