AWS EC2实例重启后Nginx服务无法启动求助
问题
我在AWS EC2上运行Laravel应用,之前遇到upload_max_filesize限制问题,想从默认2M改成6M。根据php_info()显示当前使用的php.ini是/etc/php/8.1/fpm/php.ini,修改该文件后重启Nginx,问题没解决。在/etc目录执行grep -r "upload_max_filesize" .发现另外两个文件/etc/php/8.1/apache2/php.ini和/etc/php/8.1/cli/php.ini也有这个参数,于是更新这两个文件后再次重启Nginx。虽然在php artisan tinker里用ini_get()能获取到更新后的值,但应用的php_info()还是显示2M,于是我重启了服务器。
重启后网站无法连接,提示:
无法访问此网站
silkweb.ca 拒绝连接
检查Nginx状态发现服务未运行,但配置测试正常:
ubuntu@ip-172-31-4-36:~$ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful ubuntu@ip-172-31-4-36:~$ sudo systemctl status nginx × nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2023-05-01 17:41:17 UTC; 24min ago Docs: man:nginx(8) CPU: 63ms May 01 17:41:15 ip-172-31-4-36 nginx[522]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error) May 01 17:41:15 ip-172-31-4-36 nginx[522]: nginx: [emerg] bind() to [::]:80 failed (98: Unknown error) May 01 17:41:16 ip-172-31-4-36 nginx[522]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error) May 01 17:41:16 ip-172-31-4-36 nginx[522]: nginx: [emerg] bind() to [::]:80 failed (98: Unknown error) May 01 17:41:16 ip-172-31-4-36 nginx[522]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error) May 01 17:41:16 ip-172-31-4-36 nginx[522]: nginx: [emerg] bind() to [::]:80 failed (98: Unknown error) May 01 17:41:17 ip-172-31-4-36 nginx[522]: nginx: [emerg] still could not bind() May 01 17:41:17 ip-172-31-4-36 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE May 01 17:41:17 ip-172-31-4-36 systemd[1]: nginx.service: Failed with result 'exit-code'. May 01 17:41:17 ip-172-31-4-36 systemd[1]: Failed to start A high performance web server and a reverse proxy server. ubuntu@ip-172-31-4-36:~$
请问Nginx无法启动的原因是什么?(修改upload_max_filesize并重启前一切正常,已多次重启服务器问题依旧。)
原因分析与解决步骤
错误日志里的bind() to 0.0.0.0:80 failed (98: Unknown error)本质是端口80被其他进程占用,导致Nginx无法绑定该端口启动。结合你修改php配置后重启服务器的操作,大概率是你误启动了Apache服务——因为你修改了/etc/php/8.1/apache2/php.ini,重启服务器后Apache可能被自动启用,抢占了80端口。
解决步骤:
- 先确认哪个进程占用了80端口,执行命令:
sudo lsof -i :80 # 或者用netstat sudo netstat -tulpn | grep :80 - 如果确实是Apache(进程名通常是
apache2)占用端口,停止并禁用Apache服务:sudo systemctl stop apache2 sudo systemctl disable apache2 - 重新启动Nginx:
sudo systemctl start nginx - 回到最初的
upload_max_filesize问题:你只需要修改/etc/php/8.1/fpm/php.ini即可,因为Laravel搭配Nginx用的是PHP-FPM,apache2和cli的php.ini不影响Web应用。修改后需要重启的是PHP-FPM服务,不是Nginx:sudo systemctl restart php8.1-fpm
内容的提问来源于stack exchange,提问作者user761100
相关产品推荐
相关产品推荐

