添加nginx为系统服务(service nginx start/stop/restart)

1、在/etc/init.d/目录下编写脚本,名为nginx

  1 #!/bin/sh
  2 #
  3 # nginx - this script starts and stops the nginx daemon
  4 #
  5 # chkconfig:   - 85 15
  6 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  7 #               proxy and IMAP/POP3 proxy server
  8 # processname: nginx
  9 # config:      /etc/nginx/nginx.conf
 10 # config:      /etc/sysconfig/nginx
 11 # pidfile:     /var/run/nginx.pid
 12
 13 # Source function library.
 14 . /etc/rc.d/init.d/functions
 15
 16 # Source networking configuration.
 17 . /etc/sysconfig/network
 18
 19 # Check that networking is up.
 20 [ "$NETWORKING" = "no" ] && exit 0
 21
 22 nginx="/usr/local/nginx/sbin/nginx"
 23 prog=$(basename $nginx)
 24
 25 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 26
 27 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 28
 29 lockfile=/var/lock/subsys/nginx
 30
 31 start() {
 32     [ -x $nginx ] || exit 5
 33     [ -f $NGINX_CONF_FILE ] || exit 6
 34     echo -n $"Starting $prog: "
 35     daemon $nginx -c $NGINX_CONF_FILE
 36     retval=$?
 37     echo
 38     [ $retval -eq 0 ] && touch $lockfile
 39     return $retval
 40 }
 41
 42 stop() {
 43     echo -n $"Stopping $prog: "
 44     killproc $prog -QUIT
 45     retval=$?
 46     echo
 47     [ $retval -eq 0 ] && rm -f $lockfile
 48     return $retval
 49 killall -9 nginx
 50 }
 51
 52 restart() {
 53     configtest || return $?
 54     stop
 55     sleep 1
 56     start
 57 }
 58
 59 reload() {
 60     configtest || return $?
 61     echo -n $"Reloading $prog: "
 62     killproc $nginx -HUP
 63 RETVAL=$?
 64     echo
 65 }
 66
 67 force_reload() {
 68     restart
 69 }
 70
 71 configtest() {
 72 $nginx -t -c $NGINX_CONF_FILE
 73 }
 74
 75 rh_status() {
 76     status $prog
 77 }
 78
 79 rh_status_q() {
 80     rh_status >/dev/null 2>&1
 81 }
 82
 83 case "$1" in
 84     start)
 85         rh_status_q && exit 0
 86     $1
 87         ;;
 88     stop)
 89         rh_status_q || exit 0
 90         $1
 91         ;;
 92     restart|configtest)
 93         $1
 94         ;;
 95     reload)
 96         rh_status_q || exit 7
 97         $1
 98         ;;
 99     force-reload)
100         force_reload
101         ;;
102     status)
103         rh_status
104         ;;
105     condrestart|try-restart)
106         rh_status_q || exit 0
107             ;;
108     *)
109       echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
110         exit 2
111
112 esac  

[[email protected] ~]# cp nginx /etc/init.d/
[[email protected] ~]# chmod 755 /etc/init.d/nginx
[[email protected] ~]# chkconfig --add nginx

[[email protected] ~]# service nginx start

[[email protected] ~]# service nginx stop

[[email protected] ~]# service nginx reload

时间: 2024-10-06 21:36:28

添加nginx为系统服务(service nginx start/stop/restart)的相关文章

把Nginx加为系统服务(service nginx start/stop/restart)

1.编写脚本,名为nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig:   - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ #               proxy and IMAP/POP3 proxy server # processname: nginx # config:  

ubuntu下把编译安装的nginx加入到service中

Download Nginx最新稳定版本 可以从这里http://nginx.org/en/download.html 下载最新版本,比如: wget http://nginx.org/download/nginx-1.2.2.tar.gz apt-get install libpcre3 libpcre3-dev 如果需要支持https的话,参考下面的部分: apt-get install libssl-dev apt-get install openssl 生成证书参考文档: http://

RHEL/CentOS 7中Nginx的systemd service

源码安装的nginx ,没有systemd service 管理 nginx 下面教程,告诉你如何设置nginx 的systemd service nginx systemd的服务文件是/usr/lib/systemd/system/nginx.service [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-look

The server of Nginx(二)——Nginx访问控制和虚拟主机

一.Nginx访问控制 (1)基于授权的访问控制 Nginx于Apache一样,可以实现基于用户授权的访问控制,当客户端要访问相应网站或者目录时要求输入用户名密码才能正常访问,配置步骤与Apache基本一致 第一步:生成用户密码认证文件,使用htpasswd生成用户认证文件,如果没有该命令,可使用yum安装httpd-tools软件包,用法与之前讲解Apache认证时一样 ~]#htpasswd -c /usr/local/nginx/passwd.db test #回车后会让输入两次密码 修改

CentOS7添加Nginx为系统服务

1.编辑系统服务 vim /usr/lib/systemd/system/nginx.service  [unit] Description=Web Service After=network.target [Service] PIDFile=/var/run/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecReload=/usr/local/ngin

配置nginx为系统服务(centos6.4)

vi /etc/init.d/nginx 内容添加: 1  #! /bin/bash 2  3 # chkconfig: 35 85 15   4 # description: Nginx is an HTTP(S) server, HTTP(S) reverse 5  6 set -e 7 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 8 DESC="nginx daemon" 9 NAME=ngi

将nginx加入系统服务

编写脚本 1,vi /etc/init.d/nginx #!/bin/sh # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/ngi

linux nginx service nginx restart [fail]

命令:nginx -t 查看失败原因: nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/sites-enabled/default:61 nginx: configuration file /etc/nginx/nginx.conf test failed 然后找到这样的原因的解决的方法 cd /etc/nginx/site-enabaled/default With php5-cgi alone或者

编译安装nginx后service nginx start 启动不了

平时都是yum安装nginx的,今天没事就在虚拟机上编译安装了一回.安装过程很简单,就不一一赘述了. 当我习惯性的用service nginx start启动时,却启动不了.怎么办了,网上看了看,得写脚本.我去,对于 初入门的新手,最怕的就是写脚本了.有什么简单办法没?脚本必须写啊,不写启动不了啊.那怎么办,直接 复制网上的,没意思.思来想去最后决定改造nginx启动脚本.具体思路是这样的,现在另一台虚拟机上yum 安装nginx,安装好后把/etc/init.d/nginx脚本拷贝到另一台编译