CentOS8.0编译源码安装nginx和防火墙使用
📅 2026/7/4 3:59:18
👁️ 阅读次数
📝 编程学习
注意:编译源码和采用yum等方式安装nginx,默认的存放路径可能不同。
当make安装完nginx后,查看进程可见master等进程存在,
但systemctl status命令不能用,找不到nginx.service,也找不到/var/log/nginx/error.log
这是因为源代码手动编译安装的(./configure && make && make install),默认安装路径是/usr/local/nginx。只能用/usr/local/nginx/sbin/nginx -s reload/stop或者去/usr/local/nginx/logs/access.log
systemctl管理服务依赖于/etc/systemd/system/目录下的.service配置文件,需要创建/etc/systemd/system/nginx.service文件
[Unit] Description=The NGINX HTTP and reverse proxy server After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID [Install] WantedBy=multi-user.target然后就可以使用systemctl这些常用命令了
sudo systemctl daemon-reload sudo systemctl enable nginx # 设置开机自启 sudo systemctl start nginx # 启动服务 sudo systemctl status nginx # 现在就能正常查看状态了验证nginx
| 方法 | 命令示例 | 主要用途 |
|---|---|---|
| 服务状态 | sudo systemctl status nginx | 最全面,查看服务整体运行状态 |
| 进程检查 | ps aux | grep nginx | 确认Nginx进程是否存在 |
| 端口检查 | sudo ss -tulpn | grep nginx | 确认Nginx是否在监听预期端口 |
| 配置测试 | sudo nginx -t | 测试配置文件语法是否正确 |
| 日志查看 | sudo tail -f /var/log/nginx/error.log | 排查启动或运行时的错误 |
curl -I http://localhost HTTP/1.1 200 OK Server: nginx/1.20.2 ss -tulpn | grep :80 tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=210461,fd=6),("nginx",pid=210460,fd=6))除了安装成功后,以后可能需要修改配置文件,
每次修改完后,都尽量测试和重新加载,查看修改是否正确和生效
nginx的生效与否可能涉及服务器防火墙的端口保护规则
firewall-cmd --list-all # 查看当前开放端口 public (active) interfaces: eth0 ports: 20/tcp 21/tcp 22/tcp 80/tcp 443/tcp 8888/tcp 20916/tcp 39000-40000/tcp firewall-cmd --add-port=80/tcp --permanent # 开放80端口 firewall-cmd --reload这两个网站用来在公网查看是否有开放端口
Open Port Check Tool - Test Port Forwarding on Your Router
编程学习
技术分享
实战经验