nginx没有自启动

查看了相关配置,发现昨天网上找的init.d/nginx居然没有chmod x权限,悲剧啊

另外启动也没有指定配置路径

今天作了相应修改pid判断还没有加进去:

#! /bin/sh
# Author: Enderiose Chris
# Modified: Geoffrey Grosenbach http://www.linuxidc.com
# Modified: Clement NEDELCU
# Reproduced with express authorization from its contributors
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
# config-dir
nginx_config=/usr/local/nginx/conf/nginx.conf
# pidfile


#If the daemon file is not found, terminate the script
test -x $DAEMON || exit 0

d_start(){
        $DAEMON || echo -n "already running"
}
d_stop(){
        $DAEMON -s quit || echo -n "not running"
}

d_reload(){
        $DAEMON -s reload || echo -n "could not reload"
}

case "$1" in
        start)
                echo -n "Starting $DESC: $NAME"
                d_start
                echo "."
        ;;
        stop)
                echo -n "Stopping $DESC: $NAME"
                d_stop
                echo "."
        ;;
        reload)
                echo -n "Reloading $DESC configuration..."
                d_reload
                echo "."
        ;;
        restart)
        echo -n "Restarting $DESC: $NAME"
        d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
        sleep 2
        d_start
        echo "."
        ;;
        *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
        exit 3
        ;;
esac
exit 0

wq之后切忌sudo chmod a+x /etc/init.d/nginx

今天开机没有启动,其实就是因为init.d下的启动文件没有可执行权限,用户组其他(ugo)都没有!!

update-rc.d命令,是用来自动的升级System V类型初始化脚本,简单的讲就是,哪些东西是你想要系统在引导初始化的时候运行的,哪些是希望在关机或重启时停止的,可以用它来帮你设置。

然后输入 以下命令

[root@example.com ~]# update-rc.d –f nginx defaults

好了 从新启动看看NGINX启动了没

如果要取消开机启动可以这样

update-rc.d -f nginx remove

补充资料:

/etc/init.d/里存放了所有的启动脚本,我们平时可以通过/etc/init.d/脚本名 启动模式 来启动某个应用。 例如输入命令“/etc/init.d/smb start”来启动smb服务。 如果是在RedHat系统下,还可以通过service smb start来启动某个服务。

尽管/etc/init.d目录中的脚本可以启动和停止各个服务,但在系统引导时,init并不是直接在/etc/init.d目录下找各个服务的启动脚本,而是在/etc/rc.d/目录下查找,该目录包含rc0.d、rc1.d等分别代表不同的init启动级别的子目录。

你可以用Runlevel命令查看当前你的系统是在那个运行级

#Runlevel
     N  2
注:我当前的系统是2级

接着来看看这些rcx.d的目录下所包含的内容,内容全是一些符号链接,链接到上一级init目录中的脚本上。 这些符号链接名称都以S或K开头,后面跟一个数字以及该脚本所控制的服务名,例如S10network。 当init从低的运行级向高的运行级过渡时,它按照数字递增的顺序运行所有以S开头的脚本,S=start,即启动脚本对应的服务;K=kill,即杀死脚本对应服务。

前面介绍到rcx.d目录下指向启动脚本的符号链接是由K或S+数字+服务名 所组成,中间这个数字十分重要,系统启动时按照这个数字递增执行所有S开头的脚本,系统关闭时按照这个数字递减执行所有K开头的脚本。

如何自定义符号链接? 启动脚本: 因为我刚才的运行级是2级 所以在/etc/rc.d/rc2.d 目录下运行

#ln -s /etc/init.d/nginx S99nginx       //重新定义nginx服务的脚本启动顺序

另外网上找到其他大能写的启动配置:

准备工作,需要先下载pcre库,因为nginx的rewrite模块需要pcre库

这里使用的版本分别为:

pcre:8.12     下载地址: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

nginx:0.8.54    下载地址:http://nginx.org/en/download.html

image

copy压缩包至linux的相应目录,例如:opt下的software,需要确认当前登录用户有权限进行解压和安装。

1)安装pcre库:

tar zxvf pcre-8.12.tar.gz

cd pcre-8.12

./configure<或./config进行编译>

 

在这里可能会遇到出错,显示configure: error: newly created file is older than distributed files!

同步更新一下当前的系统时间即可,操作:

ntpdate 210.72.145.22

ntpdate 0.centos.pool.ntp.org

 

然后进行安装

make && make install

cd ../

 

2)安装Nginx:

tar nginx-0.8.54.tar.gz

cd nginx-0.8.54

 

在这里需要对nginx的源码做一下小的处理,默认nginx是不支持静态文件的POST提交。一般浏览器默认的设置是缓存静态资源的,而有时候却需要对静态文件进行更新,这就需要使用post提交了,而此时nginx却返回405

 

一般处理方法是在配置的时候这样写:

error_page 405 =200 @405;
location @405
{
root /opt/htdocs;
}

重定向了405->200了,并且给405这个错误指定了doc_root,就是正常的doc_root的配置。

 

有兴趣可以参考这里:Nginx的405错误(已解决)

 

也可以对源码进行一些小的改动,使用vim或是copy下来修改都可以。

这里copy下来进行修改的,文件是src/http/modules/ngx_http_static_module.c

image

找到下图中的那一行,并将其注释掉:

image

大致意思是静态资源请求的处理方法中,如果发现请求方法为post提交则拒绝

 

接下来就是安装了

make && make install

Nginx默认被安装在/usr/local/nginx

 

3)开机自启动nginx

这里使用的是编写shell脚本的方式来处理

vi /etc/init.d/nginx  (输入下面的代码)

 

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
#. /etc/rc.d/init.d/functions
# Source networking configuration.
#. /etc/sysconfig/network        //这里需要注意ubuntu和centos是有区别的 ubuntu不能配置这行
# Check that networking is up.
#[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        sleep 2
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL 

 

:wq  保存并退出

设置文件的访问权限

chmod a+x /etc/init.d/nginx   (a+x ==> all user can execute  所有用户可执行)

 

这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…

image

同样的修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

vi /etc/rc.local

加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值