laravel 部署到 nginx

160 阅读4分钟

从源码编译的 PHP 需要做如下配置。

blog.csdn.net/gb4215287/a… blog.csdn.net/have_a_cat/…

Nginx 与 php-fpm 的关系

/var/log/nginx/error.log

2024/07/17 20:02:51 [crit] 455268#0: *104 connect() to unix:/var/run/php/php8.1-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 35.203.210.231, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "122.51.101.83:80"
2024/07/17 20:19:15 [error] 455268#0: *106 access forbidden by rule, client: 180.101.245.246, server: localhost, request: "GET /.env HTTP/1.1", host: "122.51.101.83"
2024/07/17 20:34:06 [crit] 743839#0: *1 connect() to unix:/var/run/php/php8.1-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 183.5.84.82, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "www.embeddedai.site"
2024/07/17 20:34:08 [crit] 743839#0: *1 connect() to unix:/var/run/php/php8.1-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 183.5.84.82, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "www.embeddedai.site"

nginx.conf

            location ~ \.php$ {
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                        include fastcgi_params;
            }

/usr/local/php/etc/php-fpm.d/www.conf

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /usr/local/php/var/run/php-fpm.sock

需注意这里的对应关系:

743839#0: *1 connect() to unix:/var/run/php/php8.1-fpm.sock

fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock;

listen = /usr/local/php/var/run/php-fpm.sock

php-fpm.service 启动超时

php-fpm 启动期间,访问网址,502 Bad Gateway错误消失;过了启动超时时间之后,php-fpm启动失败,错误重新出现。因此,可以确定当前问题是由于php-fpm导致。

查看 apache 服务状态

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: failed (Result: exit-code) since Thu 2024-07-18 10:12:57 CST; 16s ago
     Docs: man:httpd.service(8)
  Process: 1344600 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 1344600 (code=exited, status=1/FAILURE)
   Status: "Reading configuration..."

Jul 18 10:12:57 VM-16-6-opencloudos systemd[1]: Starting The Apache HTTP Server...
Jul 18 10:12:57 VM-16-6-opencloudos httpd[1344600]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' di>
Jul 18 10:12:57 VM-16-6-opencloudos httpd[1344600]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
Jul 18 10:12:57 VM-16-6-opencloudos httpd[1344600]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
Jul 18 10:12:57 VM-16-6-opencloudos httpd[1344600]: no listening sockets available, shutting down
Jul 18 10:12:57 VM-16-6-opencloudos httpd[1344600]: AH00015: Unable to open logs
Jul 18 10:12:57 VM-16-6-opencloudos systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
Jul 18 10:12:57 VM-16-6-opencloudos systemd[1]: httpd.service: Failed with result 'exit-code'.
Jul 18 10:12:57 VM-16-6-opencloudos systemd[1]: Failed to start The Apache HTTP Server.

centos 上的 httpd 和 ubuntu 上的 apache2 相同

Note that your output may be slightly different if you are using an Ubuntu or Debian-derived distribution, where the name of the Apache process is not httpd but is apache2.

80 端口占用

(base) [root@VM-16-6-opencloudos conf]# sudo ss -4 -tlnp | grep 80 LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=826953,fd=6),("nginx",pid=826952,fd=6),("nginx",pid=826951,fd=6))

nginx 和 apache 都是在 web 上通过 80 提供 hppt 服务,搭建网站只需要使用两者之一。也就是说 php-fpm.service 启动超时,和 apache 服务是否开启没有关系。

php-fpm x86_64 7.2.24-1.module+oc8.5.0+124+2b582bc2 @AppStream

将 centos Repository 中的 php-fpm 删除后(该版本与php版本不同),php-fpm 仍然可以使用,但是会提示: Unit php-fpm.service could not be found.

可以确定原系统中的php-fpm.service是由 Repository 中的 php-fpm 提供。为什么通过源码安装的 php-fpm 没有提供对应服务?

www.cnblogs.com/ikai/p/1369… blog.csdn.net/guo_qiangqi…

按照上面两篇指导,手动添加服务,该问题解决!

image.png

网页无法访问

错误提示:File not found.

日志:

2024/07/18 12:05:11 [crit] 1484790#0: *1 stat() "/root/site/magazines/public/" failed (13: Permission denied), client: 183.5.84.82, server: localhost, request: "GET / HTTP/1.1", host: "122.51.101.83"
2024/07/18 12:05:11 [crit] 1484790#0: *1 stat() "/root/site/magazines/public/" failed (13: Permission denied), client: 183.5.84.82, server: localhost, request: "GET / HTTP/1.1", host: "122.51.101.83"
2024/07/18 12:05:11 [error] 1484790#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 183.5.84.82, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "122.51.101.83"
  1. 权限问题把nginx.conf中的user改为root后解决。
  2. stackoverflow.com/questions/2… 也是权限问题。

至此,问题解决!欢迎访问:http://122.51.101.83/