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

   2023-02-10 学习力0
核心提示: 一、systemctl知识简介从CentOS7 Linux开始,系统里的网络服务启动已经从传统的service改成了systemctl(一个systemd工具,主要负责控制systemd系统和服务管理器。),管理开机自启动的命令也从chkconfig改为了systemctl,由systemctl一个命令代替了CentOS7

 

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

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

 

 vim /usr/lib/systemd/system/nginx.service
 
 [Unit]
 Description=The nginx HTTP and reverse proxy server            #描述说明;
 After=network.target remote-fs.target nss-lookup.target        #服务依赖类别说明;
  
 [Service]
 Type=forking        #服务类型,可选有forking、notify、simple等;
 ExecStartPre=/application/nginx/sbin/nginx -t        #启动前检查配置文件是否正确;
 ExecStart=/application/nginx/sbin/nginx                #启动nginx
 ExecReload=/bin/kill -s HUP $MAINPID                #重载reload
 ExecStop=/bin/kill -s QUIT $MAINPID                    #停止服务
 PrivateTmp=true                #为服务分配独立的空间;
         
 [Install]
 WantedBy=multi-user.target        #多用户级别
 
 说明第一次启动会提示以下报错:
 [root@server nginx-1.8.1]# systemctl restart nginx.service
 Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
 Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
 
 执行下面的命令重新载入 systemd,扫描新的或有变动的单元即可:
 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 五 2018-09-14 00:42:22 CST; 7s ago
 8   Process: 1564 ExecStart=/application/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 9   Process: 1563 ExecStartPre=/application/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
10  Main PID: 1566 (nginx)
11    CGroup: /system.slice/nginx.service
12            ├─1566 nginx: master process /application/nginx/sbin/nginx
13            └─1567 nginx: worker process
14 
15 9月 14 00:42:22 lb01 systemd[1]: Starting The nginx HTTP and reverse proxy server...
16 9月 14 00:42:22 lb01 nginx[1563]: nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
17 9月 14 00:42:22 lb01 nginx[1563]: nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
18 9月 14 00:42:22 lb01 systemd[1]: Started The nginx HTTP and reverse proxy server.
19 [root@lb01 ~]# systemctl restart nginx
20 [root@lb01 ~]# systemctl reload nginx
21 [root@lb01 ~]# systemctl stop nginx
22 [root@lb01 ~]# systemctl restart nginx
23 [root@lb01 ~]# systemctl enable nginx
24 [root@lb01 ~]# systemctl list-unit-files|grep nginx
25 nginx.service                                 enabled 

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • nginx实战(1):宝塔设置反向代理 宝塔反向代理为什么访问不了
    nginx实战(1):宝塔设置反向代理 宝塔反向代
    以下操作适用于默认80端口转其他地址非80端口情况。添加网站注意:因我只是拿来当反向代理来使用,所PHP为纯静态模式。开启反向代理注:目标URL为最终目的地,发送域名为默认,如设置后无效则修改为上图中所示。修改反向代理配置文件说明: 修改反向代理的配置
    03-08
  • nginx1.18.0
    nginx1.18.0
    目录简介安装yum方式安装nginx编译方式安装systemd管理动态添加第三方模块作用提供静态内容简单的代理服务器设置FastCGI代理配置为注释内容使用nginx的命令以及工作机制nginx配置文件nginx_manual查看nginx安装的模块nginx是一个高性能的HTTP和反向代理web服
    03-08
  • Nginx 通过 certbot 为网站自动配置 SSL 证书并续期
    Nginx 通过 certbot 为网站自动配置 SSL 证书并
    1.1、http 和 https 是什么?简单来说,http 是一个传输网页内容的协议,比如你看到的 http 开头的网站 http://www.163.com ,其网页上的文字、图片、 CSS 、 JS 等文件都是通过 http 协议传输到我们的浏览器,然后被我们看到。而 https 可以理解为“ HTTP ov
    03-08
  • 使用漏洞检查器验证 NGINX 安全设置
    使用漏洞检查器验证 NGINX 安全设置
    介绍需要强大的安全设置来保护 Web 服务器免受网络攻击。我们使用漏洞诊断工具验证了 NGINX 安全设置所需的项目。相关术语回顾什么是 NGINX?摘自维基百科 免费和开源的网络服务器开发时专注于处理性能、高并发和小内存使用具有 HTTP、HTTPS、SMTP、POP3 和 I
    03-08
  • Nginx端口占用问题
    错误信息:nginx: [emerg] listen() to 0.0.0.0:80, backlog 511 failed (98: Address already in use)主要是端口被占用,通过如下命令,可以查看该端口被那个应用占用:sudo netstat -ntpl 然后kill -9 PID将其杀死关闭即可解决
    02-13
  • 基于Nginx的网关实现
    基于Nginx的网关实现
    Nginx介绍正向/反向代理 (1)正向代理正向代理,"它代理的是客户端,代客户端发出请求",是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交
    02-13
  • nginx实现unigui群集
    nginx实现unigui群集
    nginx实现unigui群集在笔者写此文的时候,UNIGUI1.50.x的版本已经发布,其提供的HyperServer已经支持群集。有网友还专门为此做了群集方面的测试:从上图可以看出:群集总共开了51个UNIGUI服务程式,总共有13357个客户端(sessions)连接,并且如此多的连接还操
    02-13
  • Linux使用nginx反向代理。可实现域名指向特定端
    在配置80指向域名的时候出现端口占用,使用kill -9无法杀死端口,应使用下面的命令来杀死进程killall -9 nginx(使用完本命令需要再把配置过的配置文件重新启动。命令写在了PS下面)后在root权限下的nginx的sbin下使用./nginx -t(命令重启nginx并检查是否有语
    02-13
  • 装tomcat和nginx心得
    开机启动tomcat1:在/etc/rc.d/init.d目录下生成一个文件tomcat80802:在文件里添加如下内#!/bin/bash#2345 linux运行级别#10开机启动优先级,数值越大越排在前面,最大值100#90关机优先级#chkconfig:2345 10 90#description: tomcat8080 start....start(){ec
    02-13
点击排行