首页 > 系统相关 >Ubuntu安装PHP和NGINX环境

Ubuntu安装PHP和NGINX环境

时间:2023-12-08 11:34:48浏览次数:26  
标签:systemd php nginx fpm cby NGINX Ubuntu PHP

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

全网可搜《小陈运维》

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

标签:systemd,php,nginx,fpm,cby,NGINX,Ubuntu,PHP
From: https://www.cnblogs.com/chenby/p/17884814.html

相关文章

  • Ubuntu安装MySQL数据库
    Ubuntu安装MySQL数据库介绍MySQL的定义MySQL是一种开源关系型数据库管理系统。与其他关系型数据库一样,MySQL将数据存储在由行和列组成的表中。用户可以使用结构化查询语言(通常称为SQL)定义、操作、控制和查询数据。由于MySQL是开源的,因此它的大量功能是在超过25年与用户......
  • Ubuntu安装typecho博客
    Ubuntu安装typecho博客简介名称的来历Typecho是由type和echo两个词合成的,来自于开发团队的头脑风暴。Type,有打字的意思,博客这个东西,正是一个让我们通过打字,在网络上表达自己的平台。Echo,意思是回声、反馈、共鸣,也是PHP里最常见、最重要的函数,相信大部分PHP爱好者都是从e......
  • Windows服务器,通过Nginx部署VUE+Django前后端分离项目
    目录基本说明安装Nginx部署VUE前端部署Django后端Djangoadmin静态文件(CSS,JS等)丢失的问题1.基本说明本文介绍了在windows服务器下,通过Nginx部署VUE+Django前后端分离项目。本项目前端运行在80端口,服务器端运行在8000端口。因此本项目使用Django的......
  • VMware17 ubuntu18.04.5安装好后无法访问win11共享文件夹的问题
    1在关闭虚拟机的情况下,点击虚拟机设置,CD/DVD设置使用ISO镜像文件,并设置好镜像路径。2启动虚拟机,此时重新安装VMwaretools按钮变成有效状态,点击该按钮,如果虚拟机进入系统后,该按钮会变成无效状态。3等待虚拟机自动下载VMwaretools,下载后在桌面可以看到VMwaretoolsDVD光盘,......
  • 【环境配置记录】ubuntu用samba共享文件夹给windows
    中文社区真的不太行,英文社区资源丰富很多转载https://askubuntu.com/questions/1462387/trying-to-samba-share-a-folder-always-gives-errors的答案 Pleaseseethefollowinginstallationguideline.Itcaneffectivelysolvetheoutstandingissueof'netusershare'r......
  • Ubuntu下MPICH的安装与配置
    原创直达链接一、MPICH的下载与安装MPI安装文件下载地址:博客下载地址或官网地址可以下载3.4.2版本的,本文就是3.4.2版本1.解压:sudotar-zxvfmpich-3.4.2.tar.gz2.进入mpich-3.4.2文件夹:cdmpich-3.4.23.进行软件配置与检查:先用apt安装gcc,g++sudoaptinstallgcc......
  • Nginx配置nginx.conf文件实现一个IP两个域名
    #安装PCRE库支持yuminstallpcre-develpcre-y#下载Nginx源码包cd/usr/srcwget-chttp://nginx.org/download/nginx-1.12.0.tar.gz #解压Nginx源码包tar-xzfnginx-1.12.0.tar.gz#进入解压目录,然后sed修改Nginx版本信息为JWScdnginx-1.12.0;sed-i-e's/1.12.0//g'-e's......
  • 【Nginx/IIS】解决uniapp/Vue history模式下页面刷新404
    uniapp/Vue开启History模式本地开发:二级页面刷新或者通过链接进入二级页面是正常的打包部署后:二级页面刷新或者通过链接进入二级页面会报错404页面找不到 解决方案:Nginx配置:在nginx.conf的对应location里配置一行代码try_files$uri$uri//index.html;location/h5{......
  • ubuntu 使用systemd systmctl配置服务开机启动,服务包含多个子进程
    背景:需求是这样的,有一个服务,有6个子进程,每次系统重启都要一个一个启动,很繁琐,需要配置到开机启动里而目前系统已经抛弃了chkconfig的配置方式,转而使用systemd来配置开机启动进程了所以需求就变成了把服务配置到systemd开机启动中,服务包含6个子进程配置这个踩了不少坑,特地记录下......
  • ubuntu20.04下搭建EDK2开发环境
    EDK2是UEFI应用程序的官方开发环境。它是由开源的Tianocore项目开发的,英特尔、惠普和微软是该项目的主要贡献者。虽然它可能比GNU-EFI大,但它有更多的功能,因此,一些操作系统开发人员可能更喜欢它而不是GNU-EFI。什么是EDK2?EDK2完全实现了UEFI规范。它包含开放虚拟机固件(OVMF)UE......