找回密码
 立即注册
首页 业界区 业界 Failed to start nginx.service: Unit nginx.service no ...

Failed to start nginx.service: Unit nginx.service not found.

崔和美 3 天前
分享一篇nginx安装后,出现:Failed to start nginx.service: Unit nginx.service not found. 我们需要如何解决这个问题
在使用 Nginx 1.26.3 时遇到了 nginx.service找不到的问题。这是因为通过源码编译安装 Nginx 后,systemd 没有对应的服务单元文件。别担心,我们可以手动创建一个
1.创建服务文件
使用vi编辑器在 systemd 的系统目录中创建服务文件。通常建议放在 /etc/systemd/system/目录下
  1. sudo vi /etc/systemd/system/nginx.service
复制代码
2.编写服务配置
将以下内容复制到文件中。​请务必将路径 /usr/local/nginx/全部替换为您实际的 Nginx 安装路径 /usr/local/nginx-1.26.3/
  1. [Unit]
  2. Description=The NGINX HTTP and reverse proxy server
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/usr/local/nginx-1.26.3/logs/nginx.pid
  7. ExecStartPre=/usr/local/nginx-1.26.3/sbin/nginx -t
  8. ExecStart=/usr/local/nginx-1.26.3/sbin/nginx
  9. ExecReload=/usr/local/nginx-1.26.3/sbin/nginx -s reload
  10. ExecStop=/usr/local/nginx-1.26.3/sbin/nginx -s stop
  11. PrivateTmp=true
  12. [Install]
  13. WantedBy=multi-user.target
复制代码
关键提示​:请仔细核对并确保 PIDFile、ExecStartPre、ExecStart、ExecReload和 ExecStop的路径都正确指向您的 Nginx 安装路径 /usr/local/nginx-1.26.3
ExecStartPre参数用于在启动前测试配置文件语法,能帮助提前发现配置错误
3.重新加载systemd配置
创建文件后,必须让 systemd 重新加载配置,以识别这个新服务
  1. sudo systemctl daemon-reload
复制代码
4.启动并验证Nginx服务
  1. sudo systemctl start nginx
复制代码
检查服务状态
  1. sudo systemctl status nginx
复制代码
1.png

5.设置开机自启动
  1. sudo systemctl enable nginx
复制代码
检查确认开机自启是否已成功启用
  1. sudo systemctl is-enabled nginx
复制代码
2.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册