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 子进程监听到了请求,然后处理请求。其流程如下:

浏览器访问 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

# 安装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

# 安装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:~#

测试页面

# 写入测试页面
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、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客

全网可搜《小陈运维》

文章主要发布于微信公众号

Ubuntu安装PHP和NGINX环境的更多相关文章

  1. Ubuntu安装JDK与配置环境变量

    Ubuntu14.04安装JDK与配置环境变量 工具/原料   Ubuntu14.04系统 方法/步骤     先从Oracle官网下载JDK.先选择同意按钮,然后根据自己的系统下载相应版本.我的系统 ...

  2. Ubuntu安装C#语言开发环境

    使用Bash自动化安装 先下载Bash脚本(Linux/macOS),运行脚本 ./dotnet-install.sh -c Current 或者使用包管理器安装 wget -q https://pa ...

  3. mac下 home-brew安装及php,nginx环境安装及配置

    Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...

  4. Ubuntu 安装phpMyAdmin + 配置nginx

    0x01 安装phpMyAdmin ``` sudo apt-get install phpmyadmin ``` 0x02 添加链接 ``` sudo ln -s /usr/share/phpMyA ...

  5. Ubuntu 安装php+mysql+nginx

    0x01 安装PHP https://blog.csdn.net/Msmile_my/article/details/73647809   1.添加php的仓库 sudo apt-get instal ...

  6. ubuntu安装Android Studio开发环境

    1.下载 https://developer.android.com/studio/ 2.解压文件,将文件夹copy到/opt/ 3.进入/opt/android-studio/bin,运行./stu ...

  7. ubuntu安装eclipse配置jdk环境

    $ sudo mkdir /usr/local/java //在此目录下新建一个文件夹java $ sudo mv 下载/jdk-8u111-linux-i586.tar.gz /usr/local/ ...

  8. ubuntu 搭建Mercurial 服务(nginx)

    ubuntu 搭建Mercurial 服务(nginx) 环境:ubuntu 12.05  Mercurial 步骤: (1)安装nginx 和 Mercurial: sudo apt-get ins ...

  9. Ubuntu 14.04 安装LNMP(nginx/1.12.1+php7.1.9+mysql5.7.19)环境

    这篇教程中,我们将讨论怎样在Ubuntu 14.04搭建LNMP环境 1 安装Nginx 首先我们要更新apt源 sudo add-apt-repository ppa:nginx/stable  s ...

  10. 阿里云Ubuntu安装LNMP环境之Nginx

    在QQ群很多朋友问阿里云服务器怎么安装LNMP环境,怎么把项目放到服务器上面去,在这里,我就从头开始教大家怎么在阿里云服务器安装LNMP环境. 在这之前,我们先要知道什么是LNMP. L: 表示的是L ...

随机推荐

  1. 如何实现 Excel 表格转置(行列互换)

    直接上经验贴: https://baijiahao.baidu.com/s?id=1690475581736550777&wfr=spider&for=pc 大概就是 先复制粘贴,在粘 ...

  2. 在线photoshop网页版工具开发

    基于javascript开发的在线ps工具,打包方式webpack 在线预览 在线ps网页版 源码地址 https://github.com/geeeeeeeek 功能介绍 在线图像编辑器允许您使用H ...

  3. 最近遇到的问题记录:UrlEncode、UrlDecode

    本文阅读前了解知识:什么时候需要使用UrlEncode和UrlDecode函数 作者使用谷歌浏览器,通过按下F12对第三方网站http协议的接口抓包进行分析操作. 场景 运维小哥哥偶尔使用某某外包公司 ...

  4. AHB-SRAMC Design-02

    AHB-SRAMC Design SRAMC(另外一种代码风格)解析 SRAM集成,顶层模块尽量不要写交互逻辑 module ahb_slave_if( input hclk, input hrest ...

  5. text, data and bss: Code and Data Size Explained

    [来源]

  6. Linux-进程管理-ps-kill

  7. [转帖]在麒麟Linux安装Postgis

    https://jimolonely.github.io/tech/linux/install-postgis-kylin/ 接着上一篇在麒麟linux上安装Postgresql12.5 ,我们来安装 ...

  8. [转帖]centos7 firewall-cmd主机之间端口转发

    目录 1. firewalld 1.1 firewalld守护进程 1.2 控制端口/服务 1.3 伪装IP 1.4 端口转发 2. 案例 2.1 配置ServerA 2.2 安装nginx测试 (可 ...

  9. Oracle Linux7 与Oracle Linux8 下载rpm包的位置

    Oracle Linux 7 http://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/index.html 索引页 http://yum.or ...

  10. java浅拷贝BeanUtils.copyProperties引发的RPC异常 | 京东物流技术团队

    背景 近期参与了一个攻坚项目,前期因为其他流程原因,测试时间已经耽搁了好几天了,本以为已经解决了卡点,后续流程应该顺顺利利的,没想到 人在地铁上,bug从咚咚来~ 没有任何修改的服务接口,抛出异常: ...