CentOS 7 配置启动 手动编译的 nginx

1.如何安装请参考之前的编译安装文章

手动编一个的nginx 一般在这个目录下:/usr/local/nginx

2.配置 Nginx 服务

2.1 创建 Systemd 服务文件

  1. 路径:/etc/systemd/system/nginx.service
    sudo vim /etc/systemd/system/nginx.service
  2. 内容:这里是推荐的配置文件:

 

[Unit]
Description=The Nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

2.2 配置文件分析

 

  • [Unit] 部分:
    • After=syslog.target network.target remote-fs.target nss-lookup.target:确保在其他服务启动后再启动 Nginx。
  • [Service] 部分:
    • Type=forking:使用 forking 类型,Nginx 启动后会生成子进程。
    • PIDFile:Nginx 主进程的 PID 文件路径。
    • ExecStartPre:在启动前执行 nginx -t 命令,检查配置文件是否正确。
    • ExecStart:启动 Nginx,指定配置文件路径。
    • ExecReload:重新加载 Nginx 配置。
    • ExecStop:优雅地停止 Nginx。
    • PrivateTmp=true:为 Nginx 提供独立的临时文件夹,提升安全性。

2.3 重新加载 Systemd 服务文件

 

sudo systemctl daemon-reload

3. 配置 Nginx 日志文件

确保你在 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf 中正确设置了日志路径。例如:

error_log /usr/local/nginx/logs/error.log;
access_log /usr/local/nginx/logs/access.log;

4. 启动 Nginx 服务

4.1 启动 Nginx

sudo systemctl start nginx

4.2 检查 Nginx 服务状态

sudo systemctl status nginx

确认 Nginx 是否启动成功。如果出现错误,可以通过查看日志来诊断:

sudo journalctl -u nginx.service

5. 设置 Nginx 开机自启

sudo systemctl enable nginx

这会将 Nginx 添加到系统启动项中,使其在系统重启时自动启动。

6. 调试与常见问题解决

6.1 检查配置文件语法

在启动 Nginx 之前,确保 Nginx 配置文件没有语法错误:

sudo /usr/local/nginx/sbin/nginx -t

如果配置正确,会显示类似以下内容:

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6.2 日志文件

Nginx 错误日志和访问日志位于:

  • 错误日志:/usr/local/nginx/logs/error.log
  • 访问日志:/usr/local/nginx/logs/access.log

通过查看这些日志,可以帮助你定位问题。例如,如果 Nginx 启动失败,可以查看错误日志获取详细的错误信息。

6.3 检查权限问题

  • 如果你使用了 UserGroup 配置项,确保这些用户组存在并且 Nginx 进程有适当的权限访问相关目录和文件。通常,运行 Nginx 的用户不应拥有过高的权限。
  • 如果你的配置文件中没有指定 UserGroup,可以忽略这部分设置,Nginx 将以 root 用户启动并使用默认的用户组。

7. Nginx 配置文件管理

7.1 重新加载配置

当你修改了 Nginx 配置文件后,可以使用以下命令重新加载配置:

sudo systemctl reload nginx

或者使用 Nginx 的命令:

sudo /usr/local/nginx/sbin/nginx -s reload

7.2 停止 Nginx

sudo systemctl stop nginx

或者使用 Nginx 的命令:

sudo /usr/local/nginx/sbin/nginx -s stop

8. 常见错误及解决方案

错误:nginx.pid 文件丢失或权限问题

  • 原因: 如果 nginx.pid 文件丢失或没有正确创建,Nginx 无法正常启动。
  • 解决: 确保 PIDFile 配置项指向正确的路径,且 Nginx 进程有写权限。如果没有 PID 文件,可以手动删除 Nginx 的进程文件并重新启动。

错误:status=216/GROUP 错误

  • 原因: 如果 systemd 无法找到指定的用户或组,可能会出现该错误。
  • 解决: 确保在配置文件中没有指定不存在的用户或组,或者注释掉 UserGroup 行。

9. 附录

9.1 完整的 Nginx 配置文件示例

worker_processes 1;

error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include       /usr/local/nginx/conf/mime.types;
    default_type  application/octet-stream;

    access_log /usr/local/nginx/logs/access.log;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/local/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/nginx/html;
        }
    }
}

9.2 常用命令

  • 启动 Nginx:
sudo systemctl start nginx
  • 停止 Nginx:
sudo systemctl stop nginx
  • 重启 Nginx:
sudo systemctl restart nginx
  • 重新加载配置:
sudo systemctl reload nginx
  • 查看状态:
sudo systemctl status nginx

结语

以上是 Nginx 安装、配置、启动、调试、及自动启动的完整文档,涵盖了你所遇到的常见问题及解决方法。如果你按照这个流程配置 Nginx,应该可以确保 Nginx 能够正常运行并且在重启后自动启动。

 

posted @ 2024-12-04 16:18  取经路上  阅读(104)  评论(0)    收藏  举报