Ubuntu安装PHP和NGINX环境
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/index.php/start-page.html
CSDN、GitHub、51CTO、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客
全网可搜《小陈运维》
文章主要发布于微信公众号
Ubuntu安装PHP和NGINX环境的更多相关文章
- Ubuntu安装JDK与配置环境变量
Ubuntu14.04安装JDK与配置环境变量 工具/原料 Ubuntu14.04系统 方法/步骤 先从Oracle官网下载JDK.先选择同意按钮,然后根据自己的系统下载相应版本.我的系统 ...
- Ubuntu安装C#语言开发环境
使用Bash自动化安装 先下载Bash脚本(Linux/macOS),运行脚本 ./dotnet-install.sh -c Current 或者使用包管理器安装 wget -q https://pa ...
- mac下 home-brew安装及php,nginx环境安装及配置
Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...
- Ubuntu 安装phpMyAdmin + 配置nginx
0x01 安装phpMyAdmin ``` sudo apt-get install phpmyadmin ``` 0x02 添加链接 ``` sudo ln -s /usr/share/phpMyA ...
- Ubuntu 安装php+mysql+nginx
0x01 安装PHP https://blog.csdn.net/Msmile_my/article/details/73647809 1.添加php的仓库 sudo apt-get instal ...
- ubuntu安装Android Studio开发环境
1.下载 https://developer.android.com/studio/ 2.解压文件,将文件夹copy到/opt/ 3.进入/opt/android-studio/bin,运行./stu ...
- ubuntu安装eclipse配置jdk环境
$ sudo mkdir /usr/local/java //在此目录下新建一个文件夹java $ sudo mv 下载/jdk-8u111-linux-i586.tar.gz /usr/local/ ...
- ubuntu 搭建Mercurial 服务(nginx)
ubuntu 搭建Mercurial 服务(nginx) 环境:ubuntu 12.05 Mercurial 步骤: (1)安装nginx 和 Mercurial: sudo apt-get ins ...
- 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 ...
- 阿里云Ubuntu安装LNMP环境之Nginx
在QQ群很多朋友问阿里云服务器怎么安装LNMP环境,怎么把项目放到服务器上面去,在这里,我就从头开始教大家怎么在阿里云服务器安装LNMP环境. 在这之前,我们先要知道什么是LNMP. L: 表示的是L ...
随机推荐
- Liunx运维(二)-文件与目录操作
文档目录: 一.pwd:显示当前位置 二.cd:切换目录 三.tree:树形结构显示目录 四.mkdir 创建目录 五.touch:创建空文件或改变文件时间戳 六.ls:显示目录下内容相关属性信息 七 ...
- citespace 文献计量工具初探
先放几个教程: 知乎 - CiteSpace 使用教程 - 312 赞同 知乎 - CiteSpace 入门教程 - 949 赞同 简书 - 研究方法 | 用 CiteSpace 进行科学文献可视化分 ...
- 怎样实现WPF Prism Module的国际化和本地化?
怎样实现WPF Prism Module的国际化和本地化? English | 简体中文 上一篇有简单介绍主工程的国际化,使用的资源字典(XAML)实现的. 这几天我添加了几个Prism模块(Modu ...
- 问题--去除CSDN水印
1.问题如上 有时候需要使用其中的图片,但是水印很让人烦恼 确实可以用PS中的修复画笔工具,修复工具等进行处理 但是当水印覆盖到字体时,就会破坏到原有字体 2.解决方式 从CSDN添加水印的方式入手 ...
- 结构体Struct、联合体Union与类Class
结构体Struct.联合体Union与类Class 1. Struct/Class struct能包含成员函数吗? 能! struct能继承吗? 能!! struct能实现多态吗? 能!!! 1.1 ...
- [转帖]TLS 1.2 浏览器兼容性
https://support-splashtopbusiness.splashtop.com/hc/zh-cn/articles/4414002633883-TLS-1-2-%E6%B5%8F%E8 ...
- [转帖]GC日志解读,这次别再说看不懂GC日志了
https://juejin.cn/post/7029130033268555807 测试环境:机器内存16G,JDK 8,12核CPU 测试用例,从网上找的示例,视情况修改即可: java ...
- [转帖]ESXi主机RAID卡_HBA卡_网卡 型号_固件_驱动查询
https://www.cnblogs.com/vincenshen/p/12332142.html 一.RAID卡/HBA卡 型号_固件_驱动查询 1. 查询所有SCSI设备列表 # esxcfg- ...
- [转帖]查看oracle中表的索引
oracle中表的索引信息存在 user_indexes 和 user_ind_columns 两张表里面,其中 user_indexes 系统视图存放是索引的名称以及该索引是否是唯一索引等信息, u ...
- [转帖]Kafka 核心技术与实战学习笔记(八)kafka集群参数配置(下)
一.Topic级别参数 Topic的优先级: 如果同时设置Topic级别参数和全局Broker参数,那么Topic级别优先 消息保存方面: retention.ms:规定Topic消息保存时长.默认是 ...