CentOS7.5下开发systemctl管理的自定义Nginx启动服务程序

服务器   发布日期:2025年05月14日   浏览次数:195

 

一、systemctl知识简介

从CentOS7 Linux开始,系统里的网络服务启动已经从传统的service改成了systemctl(一个systemd工具,主要负责控制systemd系统和服务管理器。),管理开机自启动的命令也从chkconfig改为了systemctl,由systemctl一个命令代替了CentOS7以前系统中的service和chkconfig两个命令。
系统服务的脚本也从传统的路径的/etc/init.d(/etc/rc.d/init.d/),改到了/usr/lib/systemd(除此之外还有/etc/systemd/system),需要自启动运行的程序,一般存在这个系统服务目录下,即:/usr/lib/systemd/system目录,每一个服务以“服务名.service”结尾,该文件的内容一般分为3部分:即[Unit]、[Service]和[Install]。

二、systemctl管理的sshd服务配置介绍

下面是系统中sshd服务配置及解释说明。

  1. cat /usr/lib/systemd/system/sshd.service
  2. [Unit] #<==对该系统服务描述及说明模块。
  3. Description=OpenSSH server daemon #<==描述性说明。
  4. Documentation=man:sshd() man:sshd_config() #<==文档列表说明。
  5. After=network.target sshd-keygen.service #<==服务依赖类别说明。
  6. Wants=sshd-keygen.service #<==可选的依赖服务。
  7. [Service] #<==系统服务的运行参数设置模块
  8. Type=notify #<==服务类型,可选有forking、notify、simple等。
  9. EnvironmentFile=/etc/sysconfig/sshd #<==环境变量等的配置文件。
  10. ExecStart=/usr/sbin/sshd -D $OPTIONS #<==服务的启动程序。
  11. ExecReload=/bin/kill -HUP $MAINPID #<==重启程序。
  12. KillMode=process
  13. Restart=on-failure
  14. RestartSec=42s
  15. [Install] #<==服务安装的相关设置。
  16. WantedBy=multi-user.target #<==这里为设置多用户级别。可为空格分隔的列表, 表示在使用 systemctl enable 启用此单元时, 将会在对应的目录设置对应文件的软连接。
  17. 更多说明,可参考systemd.unitsystem.service文档,此不细述,都掌握了意义也不大,可以写出启动脚本即可。

三、根据上面的服务配置创建nginx启动脚本

 

  1. vim /usr/lib/systemd/system/nginx.service
  2. [Unit]
  3. Description=The nginx HTTP and reverse proxy server #描述说明;
  4. After=network.target remote-fs.target nss-lookup.target #服务依赖类别说明;
  5. [Service]
  6. Type=forking #服务类型,可选有forking、notify、simple等;
  7. ExecStartPre=/application/nginx/sbin/nginx -t #启动前检查配置文件是否正确;
  8. ExecStart=/application/nginx/sbin/nginx #启动nginx
  9. ExecReload=/bin/kill -s HUP $MAINPID #重载reload
  10. ExecStop=/bin/kill -s QUIT $MAINPID #停止服务
  11. PrivateTmp=true #为服务分配独立的空间;
  12. [Install]
  13. WantedBy=multi-user.target #多用户级别
  14. 说明第一次启动会提示以下报错:
  15. [root@server nginx-1.8.]# systemctl restart nginx.service
  16. Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
  17. Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
  18. 执行下面的命令重新载入 systemd,扫描新的或有变动的单元即可:
  19. systemctl daemon-reload #重新载入 systemd,扫描新的或有变动的单元

 启动nginx:

  1. [root@lb01 ~]# systemctl daemon-reload
  2. [root@lb01 ~]# systemctl stop nginx
  3. [root@lb01 ~]# systemctl start nginx
  4. [root@lb01 ~]# systemctl status nginx
  5. nginx.service - The nginx HTTP and reverse proxy server
  6. Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  7. Active: active (running) since -- :: CST; 7s ago
  8. Process: ExecStart=/application/nginx/sbin/nginx (code=exited, status=/SUCCESS)
  9. Process: ExecStartPre=/application/nginx/sbin/nginx -t (code=exited, status=/SUCCESS)
  10. Main PID: (nginx)
  11. CGroup: /system.slice/nginx.service
  12. ├─ nginx: master process /application/nginx/sbin/nginx
  13. └─ nginx: worker process
  14. 9 :: lb01 systemd[]: Starting The nginx HTTP and reverse proxy server...
  15. 9 :: lb01 nginx[]: nginx: the configuration file /application/nginx-1.8./conf/nginx.conf syntax is ok
  16. 9 :: lb01 nginx[]: nginx: configuration file /application/nginx-1.8./conf/nginx.conf test is successful
  17. 9 :: lb01 systemd[]: Started The nginx HTTP and reverse proxy server.
  18. [root@lb01 ~]# systemctl restart nginx
  19. [root@lb01 ~]# systemctl reload nginx
  20. [root@lb01 ~]# systemctl stop nginx
  21. [root@lb01 ~]# systemctl restart nginx
  22. [root@lb01 ~]# systemctl enable nginx
  23. [root@lb01 ~]# systemctl list-unit-files|grep nginx
  24. nginx.service enabled

 

以上就是CentOS7.5下开发systemctl管理的自定义Nginx启动服务程序的详细内容,更多关于CentOS7.5下开发systemctl管理的自定义Nginx启动服务程序的资料请关注九品源码其它相关文章!