您的位置 首页 php

记一次项目迁移升级 nginx 与php分离部署

LNMP 分离部署

模拟环境

在本地测试,原理差不多,这里只开2台服务器

  • ubuntu环境
  • 2台服务器
  • 一台IP 10.10.100.136 的 服务器安装openresty
  • 一台IP 为 10.10.100.200 的 服务器安装php7.4

在10.10.100.136服务器上安装openresty

openresty 是一款基于 NGINX 和 LuaJIT 的 Web 平台,有好多实用的组件,做网关,缓存啥的,都是不错的选择,推荐可以看看

安装步骤

步骤一:安装导入 GPG 公钥时所需的几个依赖包(整个安装过程完成后可以随时删除它们):

  sudo  apt-get  -y install --no-install-recommends wget gnupg ca-certificates  

步骤二:导入我们的 GPG 密钥:

   wget  -O -  | sudo apt-key add -  

步骤三:添加我们官方 APT 仓库。

对于 x86_64 amd64 系统,可以使用下面的命令:

  echo "deb  $(lsb_release -sc) main" \
     | sudo tee /etc/apt/sources.list.d/openresty.list  

步骤四:更新 APT 索引:

  sudo apt-get update  

然后就可以像下面这样安装 软件包 ,比如 openresty

  sudo apt-get -y install openresty  

安装目录

默认安装目录 /usr/local/openresty

运行命令

  • 查看状态
  • systemctl status openresty.service
  • 启动
  • systemctl start openresty.service
  • 停止
  • systemctl stop openresty.service
  • 重载
  • systemctl reload openresty.service

在10.10.100.200服务器上安装PHP7.4

在正式环境下,最好还是用源码或是yum安装吧,自定义比较方便,这里主要是为了方便而已

设置软件源并更新(可选 )

如果是新服务器,需要做以上的步骤,具体操作这 里不写了

安装php7.4

  apt-get install php7.4  

安装php7.4-fpm

  apt-get install php7.4-fpm  

运行命令

  • 查看状态
  • systemctl status php7.4-fpm.service
  • 启动
  • systemctl start php7.4-fpm.service
  • 停止
  • systemctl stop php7.4-fpm.service

openresty 和php的配置文件调整

openresty配置文件如下

配置文件目录

  /usr/local/openresty/nginx/conf  

nginx.conf 配置文件

说明:这里由于 是本地环境,并没有做任何的优化设置,直接在nginx.conf里修改的,正式环境上自行调整吧

  {
 
 worker_processes  1;
 
 error_log  logs/error.log  info;
 
 
 
 events {
     worker_connections  1024;
 }
 
 
 http {
      include        mime.types;
     default_type  application/octet-stream;
 
     log_format  main  '$remote_addr - $remote_user [$time_local] "$ request " '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  logs/access.log  main;
 
     sendfile        on;
 
     keepalive_timeout  65;
     #反向代理池,这里可以设置多个参数,这里省略了
     upstream phppools {
     server 10.10.100.200:9000;
     }
     server {
         listen       80;
         server_name  localhost;
 
         location / {
              root    /wwwdata;#这个目录也必须要 在php服务器上有才可以
             index  index.php index.html index.htm;
         }
 
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   /wwwdata;
         }
 
         location ~ \.php$ {
             root           /wwwdata;#这个目录也必须要 在php服务器上有才可以
             fastcgi_pass   phppools; #如果php服务器有多个,可以使用upstream来实现
             #fastcgi_pass   10.10.100.200:9000; #如果php服务器只有一台,可以这样设置
             fastcgi_index  index.php;
             fastcgi_param   script _FILENAME  $document_root$fastcgi_script_name;#如果访问出现404时,试试改下这里
             include        fastcgi_ params ;
         }
 
     }
 
 
 }
   

php7.4-fpm配置修改如下

配置目录

  /etc/php/7.4/fpm/pool.d  

www.conf 配置文件

需要修改监听 地址 ,需要把 127.0.0.1 socket 的监听方式改成监听本地ip地址的 方式

  listen=10.10.100.200:9000  

php 服务器上也需要有与openrestry服务器上有同样的目录,就是 这个/wwwdatap目录,需要与openresty配置文件中的root参数中的目录保持一 样,这样以后你的php项目都是放在这个php服务器上的/wwwdata目录里就可以正常访问了

文章来源:智云一二三科技

文章标题:记一次项目迁移升级 nginx 与php分离部署

文章地址:https://www.zhihuclub.com/151576.shtml

关于作者: 智云科技

热门文章

网站地图