首页 > 系统相关 >CentOS7-启动|重启|停止|状态服务脚本

CentOS7-启动|重启|停止|状态服务脚本

时间:2023-04-02 18:47:23浏览次数:49  
标签:脚本 1.10 -- 重启 CentOS7 nginx template root 1001

源码编译安装方法

1、上传包nginx-1.10.0.tar.gz至服务端

# 解压到工作目录
[root@template ~]# tar xf nginx-1.10.0.tar.gz -C /usr/local/src/

# 切换至Nginx目录下,找到configure
[root@template ~]# cd /usr/local/src/
[root@template src]# ll
total 0
drwxr-xr-x. 8 1001 1001 158 Apr 26  2016 nginx-1.10.0
[root@template src]# cd nginx-1.10.0/
[root@template nginx-1.10.0]# ll
total 668
drwxr-xr-x. 6 1001 1001   4096 Apr  2 17:25 auto
-rw-r--r--. 1 1001 1001 262619 Apr 26  2016 CHANGES
-rw-r--r--. 1 1001 1001 400302 Apr 26  2016 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 Apr  2 17:25 conf
-rwxr-xr-x. 1 1001 1001   2481 Apr 26  2016 configure
drwxr-xr-x. 4 1001 1001     72 Apr  2 17:25 contrib
drwxr-xr-x. 2 1001 1001     40 Apr  2 17:25 html
-rw-r--r--. 1 1001 1001   1397 Apr 26  2016 LICENSE
drwxr-xr-x. 2 1001 1001     21 Apr  2 17:25 man
-rw-r--r--. 1 1001 1001     49 Apr 26  2016 README
drwxr-xr-x. 9 1001 1001     91 Apr  2 17:25 src

2、安装rpm包,查看他的脚本文件

[root@node01 ~]# yum install nginx-1.10.0-1.el7.ngx.x86_64.rpm -y

3、在rpm包上查看所属组信息

[root@node01 ~]# id nginx
uid=305(nginx) gid=305(nginx) groups=305(nginx)

4、创建用户信息

[root@template ~]# groupadd nginx -r -g 498
[root@template ~]# useradd nginx -r -u 498 -g 498 -c "nginx user" -d /etc/nginx -s /sbin/nologin

5、创建日志文件目录

# 默认情况下是没有的
[root@template nginx-1.10.0]# ll /var/log/nginx
ls: cannot access /var/log/nginx: No such file or directory
[root@template nginx-1.10.0]# mkdir /var/log/nginx

6、可以用此选项查看编译过程需要的参数文件

[root@template nginx-1.10.0]# ./configure --help

7、编译,但是没有装一些环境,在编译中报错中缺什么安装什么包

[root@template nginx-1.10.0]# ./configure \
> --prefix=/etc/nginx \
> --sbin-path=/usr/sbin/nginx \
> --modules-path=/usr/lib64/nginx/modules \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --pid-path=/var/run/nginx.pid \
> --lock-path=/var/run/nginx.lock \
> --user=nginx --group=nginx \
> --with-http_ssl_module \
> --with-threads
checking for OS
 + Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

8、解决第一个报错信息,缺少C环境

[root@template nginx-1.10.0]# yum install gcc gcc-c++ make -y

9、调出命令继续编译,解决报错

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

# 安装一个对应库文件的开发包
[root@template nginx-1.10.0]# yum install pcre-devel -y

10、继续编译

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
[root@template nginx-1.10.0]# yum install openssl-devel -y

11、再次编译,最后make && make install

[root@template nginx-1.10.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-threads
[root@template nginx-1.10.0]# make && make install

12、修改pid

logs/nginx.pid

13、切换到/etc/init.d/脚本目录下编写Nginx脚本

[root@template ~]# cd /etc/init.d/
[root@template init.d]# cat nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /www/wdlinux/nginx/conf/nginx.conf
# pidfile:     /www/wdlinux/nginx/logs/nginx.pid
# Url http://www.wdlinux.cn
# Last Updated 2010.06.01
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    #service php-fpm start
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    $nginx -s stop
    echo_success
    retval=$?
    echo
    #service php-fpm stop
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    stop
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    $nginx -s reload
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

14、增加脚本你的执行权限

[root@template init.d]# chmod +x nginx

15、添加到启动服务中

[root@template init.d]# chkconfig --add nginx
[root@template init.d]# chkconfig nginx  on

16、测试

方式二:通过rpm包的脚本修改

[root@node01 system]# cat nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target
[root@node01 system]# pwd
/usr/lib/systemd/system

参数说明:

Unit:
Description:描述信息
After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反;
Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit即无法激活;
Wants:依赖到的其它units,弱依赖;
Conflicts:定义units间的冲突关系。

Service:
Type:定义影响ExecStart及相关参数功能的unit进程启动类型;

simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中;
forking:由ExacStart启动的程序透过spawns延伸出其它子程序来作为此deamon的主要服务。原生父程序在启动结束后就会终止。
oneshot:与simple类似,不过这个程序在完成工作后就结束,不常驻内存;
dbus:与simple类似,但这个daemon必须要在取得一个D-Bus的名称后,才会继续运作。因此通常也要同时设定BusName=才行;
notify:在启动完成后会发送一个通知消息。还需要配合NotifyAccess来让Systemd接收消息;
idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行。这类的daemon通常是开机到最后才执行即可的服务。

EnvironmentFile:环境配置文件;
ExecStart:指明启动unit要运行命令或脚本的绝对路径;
ExecStartPre:在ExecStart之前运行的绝对路径;
ExecStartPost:在ExecStart之后运行的绝对路径;
ExecStop:指明停止unit要运行的命令或脚本的绝对路径;
Restart:当设定Restart=1时,则当次daemon服务意外终止后,会再次自动启动。

PrivateTmp:true/false表示是否给服务分配独立的临时空间


Install:
Alias:别名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依赖,强依赖;
WantedBy:被哪些units所依赖,弱依赖;
Also:安装本服务的时候还要安装别的相关服务。

标签:脚本,1.10,--,重启,CentOS7,nginx,template,root,1001
From: https://www.cnblogs.com/sre-chan/p/17280849.html

相关文章

  • CentOS7.2T以上磁盘挂载
    CentOS7.服务器挂载大于2T磁盘目录CentOS7.服务器挂载大于2T磁盘0.环境信息Linux原理1.详细挂载步骤1.1.查看当前系统磁盘使用情况1.2.查看是否有未知硬盘未挂载1.3.挂载硬盘-使用parted1.3.1.使用parted命令1.3.2.创建labelgptl执行两次1.3.3.依次录入yes/mkpart......
  • 脚本批量备份交换机路由器配置研究
    交换机路由器配置文件备份批量脚本powershellrouterswitchbackupsshpowershell传教士原创文章。始于2023-04-02允许转载,但必须保留名字和出处,否则追究法律责任 ---【前言】---最近我看了一篇文章。讲的是:【目的】:以5分钟为循环周期,经ssh,用密码,用py批量备份交换......
  • Centos7 中 关于 tcp_syn_retries
    设置地址:/proc/sys/net/ipv4/tcp_syn_retries默认设置成6,代表在syn请求超时的情况下重发6次,每次的等待时间为2^times ,即2s,4s,8s,16s,32s,64s(不计最初的请求的1s)所以syn的超时时间为2^retries+1  ......
  • 解决VSCode终端中禁止运行脚本问题的一种方式
    1.右击VSCode图标,选择以管理员身份运行;2.在终端中执行get-ExecutionPolicy,显示Restricted,表示状态是禁止的;3.这时执行set-ExecutionPolicyRemoteSigned;4.此时再执行get-ExecutionPolicy,显示RemoteSigned,则表示状态解禁,可以运行......
  • 一个循环采集CPU的etl日志的脚本
    一个循环采集CPU的etl日志的脚本mdD:\\tempsetTargetDriveEtl=D:\\temp@echooffSET/A"index=1"SET/A"count=10":whileif%index%leq%count%(echoThevalueofindexis%index%wmicprocesswherename="wprui.exe"......
  • springboot和redis执行lua脚本——踩坑
    问题:原先想使用redis执行lua脚本作为项目限流基础,lua脚本后写完后执行一直报错如下图:  卡了几天了,没看明白咋回事,一次偶然试了一下解决了,传递lua参数需要时String类型难怪说报错强转String类型异常  灵感来源参考文章:踩坑之RedisTemplate执行Lua脚本-知乎(zhihu.c......
  • shell 脚本之一键部署安装 Nginx
    今天咸鱼给大家分享个源码编译安装Nginx的shell脚本 这个shell脚本可重复执行 完整源码放在最后 定义一个变量来存放nginx版本号version=1.15.4 nginx下载地址:http://nginx.org/download/ 下列函数功能则是判断当前步骤是否执行成功,并将结果输出出......
  • centos7获取IP地址的两种方法
    一、centos7获取IP地址的两种方法动态获取IP设置静态IP地址二、动态获取IP(不推荐使用)1、使用ipaddr命令查看查看网卡名和是否有网络,获知网卡名为ens33。2、输入vi/etc/sysconfig/network-scripts/ifcfg-ens33,修改ifcfg-ens33配置文件。BOOTPROTO=dhcpONBOOT=yes3......
  • 关闭 Ubuntu 中的关机/重启确认的小技巧
    导读对于Ubuntu新手来说,有很多新东西要学,但是网上很多教程不是针对新手的。在这里,我们不走寻常路。不能说全部的教程都是为初学者准备,但至少大部分是。关闭Ubuntu中的关机/重启确认这篇文章也是一篇新手教程,并且展示如何在每次执行关机、重启、注销时禁用确定框。Ubu......
  • zabbix监控tcp连接数脚本
    1、添加脚本[root@localhost]#vim/etc/zabbix/zabbix_agent2.d/plugins.d/check_tcp.sh#!/bin/bashNAME=$1functionLISTEN{netstat-an|grep'LISTEN'|greptcp|wc-l}functionESTABLISHED{netstat-an|grep'ESTABLISHED'|greptcp|wc-l}......