当前位置 : 主页 > 操作系统 > centos >

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

来源:互联网 收集:自由互联 发布时间:2023-07-30
源码编译安装方法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
源码编译安装方法 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:安装本服务的时候还要安装别的相关服务。

原文链接:https://www.cnblogs.com/sre-chan/p/17280849.html

自由互联热门推荐:PDF电子发票识别软件,一键识别电子发票并导入到Excel中!10大顶级数据挖掘软件!人工智能的十大作用!

上一篇:如何在Linux系统上升级软件包?
下一篇:没有了
网友评论