新安装lighttpd出现gw_backend.c错误,PHP无法正常运行求助
Lighttpd无法运行PHP,反复出现FastCGI Socket连接报错
问题现象
- 服务器可正常处理HTML文件,但PHP程序无法运行
- 日志反复输出两条报错:
(gw_backend.c.324) child exited: 0 unix:/var/run/lighttpd/php.socket-0 (gw_backend.c.468) unlink /var/run/lighttpd/php.socket-0 after connect failed: Connection refused - 使用命令
sudo lighttpd -D -f /etc/lighttpd/lighttpd.conf启动服务无报错
配置信息
lighttpd.conf 文件内容
server.modules = ( "mod_indexfile", "mod_access", "mod_alias", "mod_redirect", "mod_compress", "mod_dirlisting", "mod_staticfile", ) server.document-root = "/var/www/html" server.upload-dirs = ( "/var/cache/lighttpd/uploads" ) server.errorlog = "/var/log/lighttpd/error.log" server.pid-file = "/run/lighttpd/lighttpd.pid" server.username = "www-data" server.groupname = "www-data" server.port = 8078 # strict parsing and normalization of URL for consistency and security # https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_http-parseoptsDetails # (might need to explicitly set "url-path-2f-decode" = "disable" # if a specific application is encoding URLs inside url-path) server.http-parseopts = ( "header-strict" => "enable",# default "host-strict" => "enable",# default "host-normalize" => "enable",# default "url-normalize-unreserved"=> "enable",# recommended highly "url-normalize-required" => "enable",# recommended "url-ctrls-reject" => "enable",# recommended "url-path-2f-decode" => "enable",# recommended highly (unless breaks app) #"url-path-2f-reject" => "enable", "url-path-dotseg-remove" => "enable",# recommended highly (unless breaks app) #"url-path-dotseg-reject" => "enable", #"url-query-20-plus" => "enable",# consistency in query string ) index-file.names = ( "index.php", "index.html" ) url.access-deny = ( "~", ".inc" ) static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" ) compress.cache-dir = "/var/cache/lighttpd/compress/" compress.filetype = ( "application/javascript", "text/css", "text/html", "text/plain" ) # default listening port for IPv6 falls back to the IPv4 port ## Use ipv6 if available #include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port include_shell "/usr/share/lighttpd/create-mime.conf.pl" include "/etc/lighttpd/conf-enabled/*.conf" #server.compat-module-load = "disable" fastcgi.server = ( ".php" => (( "bin-path" => "/bin/php7.4", "socket" => "/var/run/lighttpd/php.socket", "max-procs" => 1, "bin-environment" => ( "PHP_FCGI_CHILDREN" => "16", "PHP_FCGI_MAX_REQUESTS" => "10000" ), "broken-scriptfilename" => "enable" )))
conf-enabled 目录内容
ls conf-enabled/ 10-fastcgi.conf 90-javascript-alias.conf
10-fastcgi.conf 文件内容
cat conf-enabled/10-fastcgi.conf # /usr/share/doc/lighttpd/fastcgi.txt.gz # http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi server.modules += ( "mod_fastcgi" )
Socket文件权限信息
ls -l /var/run/lighttpd/php.socket-0 srwxr-xr-x 1 www-data www-data 0 Nov 1 13:36 /var/run/lighttpd/php.socket-0
排查与解决步骤
检查PHP-FPM安装状态
配置中使用的/bin/php7.4是CLI版本PHP,无法作为FastCGI后端处理请求。先确认系统是否安装PHP-FPM:dpkg -l | grep php7.4-fpm未安装则执行:
apt update && apt install php7.4-fpm安装后启动并设置开机自启:
systemctl start php7.4-fpm systemctl enable php7.4-fpm修正FastCGI配置
修改lighttpd.conf中的fastcgi.server配置,指向PHP-FPM默认socket路径(可通过/etc/php/7.4/fpm/pool.d/www.conf中的listen参数确认):fastcgi.server = ( ".php" => (( "socket" => "/run/php/php7.4-fpm.sock", "broken-scriptfilename" => "enable" )))验证权限匹配
确保www-data用户有权访问PHP-FPM socket:- 打开
/etc/php/7.4/fpm/pool.d/www.conf,确认listen.owner和listen.group均为www-data - 检查socket权限:
权限应为ls -l /run/php/php7.4-fpm.socksrw-rw---- 1 www-data www-data ...,不符合则修改配置后重启php7.4-fpm
- 打开
清理无效Socket并重启服务
rm /var/run/lighttpd/php.socket-0 systemctl restart lighttpd测试PHP运行
在/var/www/html下创建test.php:<?php phpinfo(); ?>通过浏览器访问
http://服务器IP:8078/test.php,若正常显示PHP信息则问题解决。
内容的提问来源于stack exchange,提问作者user1424074
相关产品推荐
相关产品推荐

