Ubuntu安装PHP和NGINX环境

2023-12-08 11:40:40 浏览数 (2)

Ubuntu安装PHP和NGINX环境

介绍

PHP-FPM

PHP-FPM 是 PHP FastCGI Process Manager 的缩写,是 FastCGI 进程管理器。

PHP-FPM 是基于 master/worker 的多进程架构模式,与 nginx 的设计风格类似。master 进程主要负责 CGI、PHP 环境初始化,事件监听、子进程状态,worker 进程负责处理 PHP 请求。

FPM 的 master 通过共享内存获取 worker 进程的信息,包括 worker 进程当前状态、已处理请求数等,当 master 进程要杀掉一个 worker 进程时则通过发送信号的方式通知 worker 进程。

FPM 的实现首先是创建一个 master 进程,在 master 进程中创建并监听 socket,然后 fork 出多个子进程,这些子进程各自 accept 请求。子进程的处理很简单,它在启动后阻塞在 accept 上,有请求到达后开始读取请求数据,读取完后开始处理然后再返回,在这期间不会接收其它请求。它跟 nginx 的事件驱动模型是不同的,nginx 是非阻塞的模型,如果一个请求数据还未发送完则会处理下一个请求,也就是说一个进程会同时连接多个请求。

优点

  • 提供了更好的 PHP 进程管理方式,支持平滑停止/启动进程;
Nginx 与 PHP-FPM 交互过程

以常见的 LNMP 架构为例,Nginx 在一台服务器上,PHP 在另一台服务器上。

当我们请求一个域名的时候,比如一个测试域名,www.oiox.cn 域名解析到 Nginx 服务器,Nginx 路由到 index.php 文件。此时 Nginx 检测出这不是静态文件,需要找 PHP 解析器来解析,然后加载 Nginx 的 FAST_CGI 模块,并 fastcgi_pass 到 PHP 服务器,比如 10.20.0.1:9000。此时 PHP 服务器上的 PHP-FPM 子进程监听到了请求,然后处理请求。其流程如下:

代码语言:shell复制
浏览器访问 www.oiox.cn
|
域名解析到 Nginx 服务器
|
路由到 www.oiox.cn/index.php
|
Nginx 检测 index.php 不是静态资源,加载 Nginx 的 fast-cgi 模块
|
请求被转发到 PHP 所在的服务器上
|
PHP 服务器上的 fast-cgi 监听 127.0.0.1:9000 地址
|
www.oiox.cn/index.php 请求到达 127.0.0.1:9000
|
PHP-FPM worker 进程执行代码
Nginx 与 PHP-FPM 通信方式

在 Linux 上,Nginx 和 PHP-FPM 通信有两种方式,tcp-socket 和 unix-socket。

当 Nginx 和 PHP-FPM 不在同一台机器上时,只能使用 tcp-socket 这种通信方式。

tcp socket 和 unix socket 对比
  • 效率:理论上,Unix domain socket 不需要经过网络协议栈,不需要打包拆包、计算校验和、维护序列号和应答等,只是将应用层数据从一个进程拷贝到另一个进程。所以其效率比 tcp socket 的方式要高,可减少不必要的 tcp 开销。
  • 跨机器通信:tcp socket 支持跨机器通信,而 unix domain socket 是同一机器进程之间通信。
  • 高并发:实际上,在高并发情况下,两者的性能差距并不明显。但是 tcp socket 能表现出很明显的更高的稳定性

安装PHP

代码语言:shell复制
# 安装php8.1
apt install php8.1-fpm

# 设置开机自启
root@cby:~# systemctl enable  php8.1-fpm
Synchronizing state of php8.1-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable php8.1-fpm
root@cby:~# 

# 查看服务是否正常
root@cby:~# systemctl status php8.1-fpm.service 
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-12-06 15:57:31 CST; 1min 19s ago
       Docs: man:php-fpm8.1(8)
    Process: 22149 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, s>
   Main PID: 22146 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 3943)
     Memory: 7.1M
        CPU: 25ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─22146 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ├─22147 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
             └─22148 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Dec 06 15:57:31 cby systemd[1]: Starting The PHP 8.1 FastCGI Process Manager...
Dec 06 15:57:31 cby systemd[1]: Started The PHP 8.1 FastCGI Process Manager.
root@cby:~# 

安装NGINX

代码语言:shell复制
# 安装NGINX
apt install nginx

# 修改NGINX配置
root@cby:~# cat /etc/nginx/sites-available/default
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	root /var/www/html;
	index index.php index.html index.htm index.nginx-debian.html;
	server_name _;
	location / {
		try_files $uri $uri/ =404;
	}
	location ~ .php$ {
           include snippets/fastcgi-php.conf;
	   fastcgi_pass unix:/run/php/php8.1-fpm.sock;
	}
}
root@cby:~# 

测试页面

代码语言:shell复制
# 写入测试页面
root@cby:~# echo "<?php phpinfo(); ?>" >> /var/www/html/php.php

# 重启NGINX
root@cby:~# systemctl restart nginx
# 设置开机自启NGINX
root@cby:~# systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx
# 查看状态
root@cby:~# 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 Wed 2023-12-06 16:25:33 CST; 36s ago
       Docs: man:nginx(8)
    Process: 30554 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 30555 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 30556 (nginx)
      Tasks: 5 (limit: 3943)
     Memory: 5.2M
        CPU: 24ms
     CGroup: /system.slice/nginx.service
             ├─30556 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─30557 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ├─30559 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ├─30560 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─30561 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Dec 06 16:25:33 cby systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 06 16:25:33 cby systemd[1]: Started A high performance web server and a reverse proxy server.
root@cby:~# 


# 访问测试页面
http://[主机IP]/php.php

关于

https://www.oiox.cn/

https://www.oiox.cn/index.php/start-page.html

CSDN、GitHub、51CTO、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客

全网可搜《小陈运维》

0 人点赞