草庐IT

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

sre-chan 2023-04-02 原文

源码编译安装方法

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:安装本服务的时候还要安装别的相关服务。

有关CentOS7-启动|重启|停止|状态服务脚本的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  4. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  5. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  6. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby-on-rails - 独立 ruby​​ 脚本的配置文件 - 2

    我有一个在Linux服务器上运行的ruby​​脚本。它不使用rails或任何东西。它基本上是一个命令行ruby​​脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg

  9. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  10. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

随机推荐