























1.如何安装请参考之前的编译安装文章
手动编一个的nginx 一般在这个目录下:/usr/local/nginx
/etc/systemd/system/nginx.service
sudo vim /etc/systemd/system/nginx.service
[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
After=syslog.target network.target remote-fs.target nss-lookup.target:确保在其他服务启动后再启动 Nginx。Type=forking:使用 forking 类型,Nginx 启动后会生成子进程。PIDFile:Nginx 主进程的 PID 文件路径。ExecStartPre:在启动前执行 nginx -t 命令,检查配置文件是否正确。ExecStart:启动 Nginx,指定配置文件路径。ExecReload:重新加载 Nginx 配置。ExecStop:优雅地停止 Nginx。PrivateTmp=true:为 Nginx 提供独立的临时文件夹,提升安全性。sudo systemctl daemon-reload
确保你在 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf 中正确设置了日志路径。例如:
error_log /usr/local/nginx/logs/error.log;
access_log /usr/local/nginx/logs/access.log;
sudo systemctl start nginx
4.2 检查 Nginx 服务状态
sudo systemctl status nginx
确认 Nginx 是否启动成功。如果出现错误,可以通过查看日志来诊断:
sudo journalctl -u nginx.service
5. 设置 Nginx 开机自启
sudo systemctl enable nginx
这会将 Nginx 添加到系统启动项中,使其在系统重启时自动启动。
在启动 Nginx 之前,确保 Nginx 配置文件没有语法错误:
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。