添加Nginx为系统服务(设置开机启动)
在本节中,我们将创建一个脚本,将Nginx守护进程转换为实际的系统服务。 这有两个作用:守护程序可以使用标准命令控制,更重要的是,它可以在系统启动时自动启动,并在系统关闭时停止。
System V scripts
<br\>
大多数基于Linux的操作系统使用System-V风格的init守护进程。 换句话说,他们的启动过程由一个称为init的守护进程管理。
这个守护进程基于运行级别的原理运行,它代表计算机的状态。 这里是一个表表示各种运行级别:
| 运行级别 | 状态 |
| 0 | 系统关闭 |
| 1 | 单用户模式(救援模式) |
| 2 | 多用户模式,不支持NFS |
| 3 | 完全多用户模式 |
| 4 | 未使用 |
| 5 | 图形界面模式 |
| 6 | 系统重新启动 |
关于init脚本
<br\>
init脚本(也称为服务启动脚本或甚至sysv脚本)是一个符合某种标准的shell脚本。 该脚本通过诸如开始,停止和其他等命令来控制守护进程应用程序。首先,当计算机启动时,init守护程序将使用start参数运行脚本。 另一种可能性是通过从shell手动执行脚本:
- [root@example.com ~]# service nginx start
或者如果您的系统没有service命令:
- [root@example.com ~]# /etc/init.d/nginx start
Nginx init脚本
<br\>
/etc/init.d/nginx:
基于Debian的发行版本
- #! /bin/sh
- ### BEGIN INIT INFO
- # Provides: nginx
- # Required-Start: $all
- # Required-Stop: $all
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: starts the nginx web server
- # Description: starts nginx using start-stop-daemon
- ### END INIT INFO
- PATH=/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DAEMON=/opt/sbin/nginx
- NAME=nginx
- DESC=nginx
- test -x $DAEMON || exit 0
- # Include nginx defaults if available
- if [ -f /etc/default/nginx ] ; then
- . /etc/default/nginx
- fi
- set -e
- case "$1" in
- start)
- echo -n "Starting $DESC: "
- start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid \
- --exec $DAEMON -- $DAEMON_OPTS
- echo "$NAME."
- ;;
- stop)
- echo -n "Stopping $DESC: "
- start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid \
- --exec $DAEMON
- echo "$NAME."
- ;;
- restart|force-reload)
- echo -n "Restarting $DESC: "
- start-stop-daemon --stop --quiet --pidfile \
- /var/run/nginx.pid --exec $DAEMON
- sleep 1
- start-stop-daemon --start --quiet --pidfile \
- /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
- echo "$NAME."
- ;;
- reload)
- echo -n "Reloading $DESC configuration: "
- start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/nginx.pid \
- --exec $DAEMON
- echo "$NAME."
- ;;
- *)
- N=/etc/init.d/$NAME
- echo "Usage: $N {start|stop|restart|force-reload}" >&2
- exit 1
- ;;
- esac
- exit 0
基于Red Hat的发行版本
- #!/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/nginx/nginx.conf
- # config: /etc/sysconfig/nginx
- # pidfile: /var/run/nginx.pid
- # 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/sbin/nginx"
- prog=$(basename $nginx)
- NGINX_CONF_FILE="/etc/nginx/nginx.conf"
- [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
- lockfile=/var/lock/subsys/nginx
- make_dirs() {
- # make required directories
- user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
- if [ -z "`grep $user /etc/passwd`" ]; then
- useradd -M -s /bin/nologin $user
- fi
- options=`$nginx -V 2>&1 | grep 'configure arguments:'`
- for opt in $options; do
- if [ `echo $opt | grep '.*-temp-path'` ]; then
- value=`echo $opt | cut -d "=" -f 2`
- if [ ! -d "$value" ]; then
- # echo "creating" $value
- mkdir -p $value && chown -R $user $value
- fi
- fi
- done
- }
- start() {
- [ -x $nginx ] || exit 5
- [ -f $NGINX_CONF_FILE ] || exit 6
- make_dirs
- echo -n $"Starting $prog: "
- daemon $nginx -c $NGINX_CONF_FILE
- retval=$?
- echo
- [ $retval -eq 0 ] && touch $lockfile
- return $retval
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc $prog -QUIT
- retval=$?
- echo
- [ $retval -eq 0 ] && rm -f $lockfile
- return $retval
- }
- restart() {
- configtest || return $?
- stop
- sleep 1
- start
- }
- reload() {
- configtest || return $?
- echo -n $"Reloading $prog: "
- killproc $nginx -HUP
- 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
注意修改脚本中的路径。
安装Nginx init脚本
<br\>
添加执行权限:
- [root@example.com ~]# chmod +x /etc/init.d/nginx
Debian-based发行版本:
- [root@example.com ~]# update-rc.d -f nginx defaults
Red Hat–based发行版本:
- [root@example.com ~]# chkconfig nginx on
添加Nginx为系统服务(设置开机启动)的更多相关文章
- 源码编译安装nginx及设置开机启动项
1.上传nginx文档:解压到/data目录下,并安装依赖包tar xf nginx-1.20.1.tar.gz -C /data/cd /data/nginx-1.20.1/ && ...
- CentOS7 nginx 最简单的安装以及设置开机启动
1. 下载tar包. 2. 解压缩tar包 3. 安装必须的部分 yum包 yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd ...
- Centos设置开机启动Apache和Mysql[总结]
1.前言 最近学习搭建wordpress,需要用到apahce和mysql.我是下载源代码进行安装的,安装在/url/local目录下,每次开机都需要手动启动,有点麻烦.如是想设置开机启动,从网上查了 ...
- ubuntu下设置开机启动服务
原文:http://blog.csdn.net/dante_k7/article/details/7213151 在ubuntu10.04之前的版本都是使用chkconfig来进行管理,而在之后的版本 ...
- centos7安装docker并设置开机启动
版本要求:查看内核版本,需大于3.10 [root@localhost ~]# uname -r -.el7.x86_64 更新内核:如果是生产机器务必慎重更新内核,避免出现不必要的问题. sudo ...
- openerp 7 在ubuntu上设置开机启动
我们要让openerp开机运行起来. 第一步,先进入系统目录: cd /etc/init.d 第二步,创建文件.命名为openerp-server sudo vi openepr-server 第三步 ...
- [转载]C#设置开机启动
原理就是在注册表启动项里添加一项.路径:SOFTWARE\Microsoft\Windows\CurrentVersion\Run或者直接:运行->regedit找到这个路径添加一项. usin ...
- ubuntu-18.04 设置开机启动脚本-亲测有效
ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用. 2.将下列内容复制进rc-local.servi ...
- ubuntu-18.04 设置开机启动脚本
ubuntu-18.04 设置开机启动脚本 参阅下列链接 https://askubuntu.com/questions/886620/how-can-i-execute-command-on-sta ...
随机推荐
- linux搭建sftp服务器
转自:http://blog.csdn.net/superswordsman/article/details/49331539 最近工作需要用到sftp服务器,被网上各种方法尤其是权限设置问题搞得晕头 ...
- tar命令的使用方法
tar [-cxtzjvfpPN] 文件与目录参数说明:-c :建立一个打包文件:-x :解开一个打包文件:-t :查看 tar包里面的文件:-z :打包后用gzip压缩,生成.tar.gz文件:-j ...
- js获取带#号链接后的参数
现在许多的主流网站都将'#'大规模用于重要URL中,我们通过正则表达式和window.location.search获取参数已经行不通了. 一.'#'号是什么 1.#代表网页中的一个位置.其后面的字符 ...
- PHP算法之快速排序、冒泡排序
快速排序 <?php Class Sort { //快速排序 public function quickly($array) { //判断排序的数组是否大于1 if (count($array) ...
- org.apache.catalina.LifecycleException错误解决方案
1.org.apache.catalina.LifecycleException错误 一般是由于在tomcat中运行web应用时为所在的jvm分配的堆空间过小,具体错误截图如下所示: 2.为特定程序分 ...
- Java中的集合迭代器
集合的迭代器 任何集合都有迭代器. 任何集合类,都必须能以某种方式存取元素,否则这个集合容器就没有任何意义. 迭代器,也是一种模式(也叫迭代器模式).在java中它是一个对象,其目的是遍历并选中其中的 ...
- CSS如何让不相等的字符上下对齐
最后效果: <div class="main"> <span style="font-size:12px;"><dl class= ...
- HDU4287
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- linux学习笔记-conky配置开机启动方法
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.常用桌面的配置方法 创建启动文件并加入以下配置 ~/.config/autostart/conky.desktop [Des ...
- MVVM 和 VUE
一,使用jquery和使用vue的区别 二,对MVVM的理解 三,vue中如何实现响应式 四,vue如何解析模版 五,vue整个实现流程 一,使用jquery和使用vue的区别 jquery实现t ...