安装LNMP(Nginx、MySQL、PHP)时,常因缺少依赖包导致失败(如编译Nginx需gcc
、make
,PHP需oniguruma
、rpcgen
)。
解决方法:
提前通过yum
安装必要依赖,例如:
yum install -y gcc automake autoconf libtool make wget git cmake ncurses-devel perl-module-install oniguruma rpcsvc-proto
若安装中途失败,清理残留临时文件(如rm -rf /tmp/lnmp*
),修复依赖后重新执行安装脚本。
此错误多因Nginx无法连接PHP-FPM进程,常见原因包括:
fastcgi_pass
指令指向的PHP-FPM监听地址/端口错误;systemctl status php-fpm
,未启动则执行systemctl start php-fpm
;fastcgi_pass
参数(如fastcgi_pass 127.0.0.1:9000
)与PHP-FPM配置文件(/etc/php-fpm.d/www.conf
)中的listen
指令一致;/etc/nginx/nginx.conf
中user
指令)与PHP-FPM(www.conf
中user
/group
)的运行用户(如均为www-data
)。权限设置不当会导致网站无法访问或文件上传失败,常见场景:
www-data
):chown -R www-data:www-data /var/www/html
;755
(chmod 755 /var/www/html
)、文件644
(chmod 644 /var/www/html/*
);setenforce 0
,或修改/etc/selinux/config
中SELINUX=permissive
;.user.ini
文件(防跨目录访问),需先解除锁定:chattr -i /var/www/html/.user.ini
,修改后再锁定:chattr +i /var/www/html/.user.ini
。Nginx、MySQL、PHP-FPM的配置文件语法错误会导致服务无法启动,常见于自定义配置时。
解决方法:
nginx -t
(提示“syntax is ok”则表示正确);mysql --help
(或检查/etc/my.cnf
是否有无效参数);php-fpm -t
(检查www.conf
中的listen
、user
等参数)。systemctl restart nginx
)。Nginx、MySQL、PHP-FPM服务启动失败,需通过日志分析定位原因:
/var/log/nginx/error.log
(常见原因:端口被占用、配置文件错误);/var/log/mysqld.log
(常见原因:root密码错误、数据目录权限问题);/var/log/php-fpm.log
(常见原因:listen
端口冲突、user
/group
不存在)。systemctl status nginx
(替换为对应服务名);systemctl stop httpd
);systemctl daemon-reload && systemctl restart 服务名
。即使服务正常运行,防火墙或SELinux可能拦截HTTP(80)/HTTPS(443)流量,导致网站无法访问。
解决方法:
firewalld
):firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload
setsebool -P httpd_can_network_connect 1
(针对PHP-FPM连接)。PHP-FPM进程数不足、innodb_buffer_pool_size
设置过小会导致网站响应慢。
解决方法:
/etc/php-fpm.d/www.conf
):pm.max_children = 50 # 根据服务器内存调整(如1GB内存设为20-30) pm.start_servers = 10 pm.min_spare_servers = 5 pm.max_spare_servers = 20
/etc/my.cnf
):innodb_buffer_pool_size = 512M # 设为服务器内存的50%-80%(如1GB内存设为512M) query_cache_size = 64M
systemctl restart php-fpm mysql
。PHP无法连接MySQL数据库,常见原因:
systemctl start mysql
;USE mysql; SELECT User, Host FROM user; # 确认用户存在且Host为'%'(允许远程)或'localhost' GRANT ALL PRIVILEGES ON 数据库名.* TO '用户名'@'localhost' IDENTIFIED BY '密码'; FLUSH PRIVILEGES;
/etc/my.cnf
)注释bind-address = 127.0.0.1
,并重启MySQL。