1.nginx与php

nginx通过fastcgi来完成交互,nginx转发请求。
FastCGIwebserver每收到一个请求,都会去fork一个cgi进程,请求结束再kill掉这个进程。如果有大量的请求就会造成资源的浪费,于是fastcgi使得请求结束后保留进程,可以一次处理多个请求,避免了每次请求都fork一个进程,很好的提高了效率。
php-fpm(Php-Fastcgi Process Manager)是 FastCGI 的实现,并提供了进程管理的功能,包含 master 主进程和 worker 子进程。master 进程负责解析配置文件,初始化执行环境,然后再启动多个worker进程,这个子进程就是php-cgi.php-fpm 可以根据配置文件动态的控制worker数量,既提高了性能又节约了资源。

2.配置

1.俩种交互方式

通过tcp方式,这种方式可以不同主机。
通过unix方式,这种建立 socket,只能限定于同主机之间。

2.php-fpm

最后一行
tcp:

  1. ; The address on which to accept FastCGI requests.
  2. ; Valid syntaxes are:
  3. ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
  4. ; a specific port;
  5. ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
  6. ; a specific port;
  7. ; 'port' - to listen on a TCP socket to all addresses
  8. ; (IPv6 and IPv4-mapped) on a specific port;
  9. ; '/path/to/unix/socket' - to listen on a unix socket.
  10. ; Note: This value is mandatory.
  11. listen = 127.0.0.1:9000

unix:
通过配置文件我们可以发现 php.fpm 还可以通过 unix socket 连接套接字的方式接收 Nginx 的转发,具体操作如下:新建/dev/shm/php-cgi.sock文件,然后将所有者改为与 nginx 的用户一致。

touch /dev/shm/php-cgi.sock
chown nginx:nginx /dev/shm/php-cgi.sock

将listen = 127.0.0.1:9000 修改为 listen = /dev/shm/php-cgi.sock

3.nginx的配置文件

主要是第二行,nginx的配置取决于php-fpm选择了哪种监听方式
tcp:ip+端口

location ~ .php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}

unix
将fastcgi_pass 127.0.0.1:9000;修改为

fastcgi_pass unix:/dev/shm/php-cgi.sock;

3.重启nginx

linux看哪种方式安装的nginx,百度查查重启方式
我是用brew安装的(brew是一个类似wget的)

brew services restart nginx

当然,我启动失败了,要手动启动

//找到nginx所在的安装目录,和nginx.config所在的目录
//手动启动nginx
sudo 安装的nginx路径 -c config的路径

4.在nginx配置文件统计目录下创建service文件夹

在这里,创建自定义的多个项目的config文件,从而实现配置多项目的作用
小技巧:在nginx.config的http{}内写入:include servers/*,就可以实现项目配置分离出来的目的,便于维护。
后面再在servers内创建项目config文件,大致模板如下:
🔺每个项目配置文件的交互方式都受php-fpm的决定的影响。

server {
    listen 80;
    server_name localadminapmbusi.hnyapu.cn;
    root /home/feixuan/apm.inkegd.com/web;

    #client_max_body_size          128M;
    #client_header_timeout         1m; 
    #client_body_timeout           1m; 
    #proxy_connect_timeout         60s; 
    #proxy_read_timeout            1m;  
    #proxy_send_timeout            1m;
    add_header Access-Control-Allow-Origin $http_origin;
    add_header Access-Control-Allow-Headers X-Requested-With,Origin,Content-Type,token;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    add_header Access-Control-Allow-Credentials true;
    index index.php index.html index.htm;
    access_log /home/feixuan/logs/apm.access.log main;
    error_log /home/feixuan/logs/apm.error.log;
    location / {
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        try_files $uri $uri/ /index.php?$args;
    }
    # deny access hidden file like .git .svn
    location ~ /\. {
        #access_log off;
        #log_not_found off;
        deny all;
    }
    # deny access log file
    location ~* \.(log|pem|ini|bak|sql|htaccess|properties|cfg|conf|config|zip)$ {
        #access_log off;
        #log_not_found off;
        deny all;
    }
    location ~ \.php$ {
        fastcgi_pass        unix:/run/php7.2-fpm.sock;
        fastcgi_index       index.php;
        fastcgi_param       SCRIPT_FILENAME $document_root$fastcgi_script_name;
        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        include             fastcgi_params;
    }
    #error_page 500 502 503 504  /50x.html;
    #location = /50x.html {
    #    root   html;
    #}
}

5.拉取项目

拉去php项目后,可能需要自己重新安装composer并安装项目依赖

curl -sS https://getcomposer.org/installer | php

然后

php composer.phar update

命令行 | Composer 中文文档 | Composer 中文网 (phpcomposer.com)
🔺有空的时候可以看看文档

6.拉取react项目

安装依赖

npm update

启动,后面的可选

npm start -- --proxy=4446

7.配置域名

sudo vi /etc/hosts