Linux下的Nginx安装(开机自启动)

Linux下的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

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

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

大致意思是静态资源请求的处理方法中,如果发现请求方法为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

# 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

        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…

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

vi /etc/rc.local

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

Linux下的Nginx安装(开机自启动)

相关文章:

你感兴趣的文章:

标签云: