---
title: "使用nginx做静态文件服务器"
author: "yjph83"
date: "2014-06-20T20:54:56Z"
tags: ["nginx"]
categories: ["互联网"]
slug: "nginx"
draft: false
lastmod: "2016-03-09T11:40:30Z"
id: 2083150
url: "https://yjph83.iteye.com/blog/2083150"
---

### 1.系统环境

操作系统：centos5.2

 

文件服务器只是提供静态文件服务

### 2.安装nginx

需安装[pcre-8.35.tar.gz](ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz) 

这是一种正则表达式的包，一般现在最新的，地址在：

[ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/](ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/)      ；

，使用下面命令下载编译和安装 PCRE 包：

\# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/[pcre-8.35.tar.gz](ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz)  
\# tar -zxvf [pcre-8.35.tar.gz](ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz)  
\# cd pcre-8.35

\# ./configure

\# make

\# make install

）

如果报错：error: no acceptable C compiler found in $PATH  ，则直接  yum install gcc-c++ 安装gcc编译器

 

#wget [http://nginx.org/download/nginx-1.7.2.tar.gz](http://nginx.org/download/nginx-1.7.2.tar.gz)

#tar -zxvf nginx-1.7.2.tar.gz

然后进入到 cd nginx-1.7.2，

shell&gt;./configure --prefix=/usr/local/nginx --with-http\_stub\_status\_module 

shell&gt;make

shell&gt;make install

 

./configure 时报错是：error: the HTTP gzip module requires the zlib library.

用命令查看rpm -qa |grep zlib ，已经安装其实我们需要的是安装这个

用命令 yum -y install zlib-devel

 

其中参数 `--with-http_stub_status_module` 是为了启用 nginx 的 NginxStatus 功能，用来监控 Nginx 的当前状态。

        检查是否安装成功

         #cd  /usr/local/nginx/sbin

         #./nginx -t 

显示：nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

          nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

     则表示安装成功！

       安装成功后 /usr/local/nginx 目录下有四个子目录分别是：conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf，Nginx 只有一个程序文件位于 sbin 目录下的 nginx 文件。确保系统的 80 端口没被其他程序占用，运行 sbin目录下运行./nginx 命令来启动 Nginx，打开浏览器访问此机器的 IP，如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

 

 

注：在64位系统上启动时有可能报如下错误：

Nginx: error while loading shared libraries: libpcre.so.1

解决办法如下：

ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1

查看nginx编译参数：

   nginx安装目录下:

   /usr/local/nginx/sbin/nginx -V     表示查看编译参数；

                                   -v       表示查看版本。

### 3.配置nginx

编辑/usr/local/nginx/conf文件

也可以将conf目录下的nginx.conf 文件拷贝到/nginx/nginx.conf

 

 

全局参数设置：

user  nobody;                               ＃＃使用普通帐号nobody启动服务＃＃

worker\_processes  8;                   ＃＃工作进程设置＃＃

error\_log  logs/error.log;

pid        logs/nginx.pid;

events {

    worker\_connections  5000;        ＃＃每worker\_processes进程的最大连接数＃＃

    use epoll;                                  ＃＃使用epoll＃＃

}

http{ }项参数设置:

server\_names\_hash\_bucket\_size 64;  ＃＃当设置多个虚拟主机sever时，需增大此参数，默认32＃

server\_tokens off;                         ＃＃禁止显示nginx软件的版本号＃＃

sendfile     on;

tcp\_nodelay       on;

keepalive\_timeout  30;

### 4.虚拟主机的设置

 

" www.test.com "的设置：

 server {

        listen       80;

        server\_name  www.test.com ;

        charset utf-8;

        root   /data/www.test.com ;

        index  index.html index.htm;

       }

 

 

### 5.服务的启动停止

设置自启动

nginx的启动脚本可由支持centos版本的nginx的rpm的安装文件取得，拷贝到

/etc/init.d/目录下。修改启动脚本的两个参数项：

nginx="/usr/local/nginx/sbin/nginx"

NGINX\_CONF\_FILE="/usr/local/nginx/conf/nginx.conf"

 

   执行以下命令  
  

   Nginx开启命令：/usr/local/nginx/sbin/./nginx  
   Nginx停止命令: /usr/local/nginx/sbin/./nginx -s stop  
   Nginx的热启动：#usr/local/nginx/sbin/./nginx   -s  reload

 

   chkconfig --add nginx  
   chkconfig nginx on  
   执行/etc/init.d/nginx start   启动  
   /etc/init.d/nginx stop     停止

 

以上基本的nginx就可以使用了。

 

另外可以编写启动脚本如下：

#! /bin/sh

 

\### BEGIN INIT INFO

\# Provides:          nginx

\# Required-Start:    $remote\_fs $syslog

\# Required-Stop:     $remote\_fs $syslog

\# Default-Start:     2 3 4 5

\# Default-Stop:      0 1 6

\# Short-Description: nginx init.d dash script for Redhat or other \*nix.

\# Description:       nginx init.d dash script for Redhat or other \*nix.

\### END INIT INFO

 

#------------------------------------------------------------------------------

\#                               Consts

#------------------------------------------------------------------------------

 

prefix=/usr/local/nginx

exec\_prefix=${prefix}

 

nginx\_BIN=${exec\_prefix}/sbin/nginx

nginx\_CONF=${prefix}/conf/nginx.conf

nginx\_PID=${prefix}/logs/nginx.pid

 

#------------------------------------------------------------------------------

\#                               Simple Tests

#------------------------------------------------------------------------------

 

#test if nginx is a file and executable

test -x $nginx\_BIN || exit 0

 

\# Include nginx defaults if available

if \[ -f /etc/default/nginx ] ; then

    . /etc/default/nginx

fi

 

#set exit condition

#set -e

 

#------------------------------------------------------------------------------

\#                               Functions

#------------------------------------------------------------------------------

 

setFilePerms(){

    if \[ -f $nginx\_PID ]; then

        chmod 400 $nginx\_PID

    fi

}

 

configtest() {

    $nginx\_BIN -t -c $nginx\_CONF

}

 

#courtesy of php-fpm

wait\_for\_pid () {

    try=0

 

    while test $try -lt 35 ; do

        case "$1" in

            'created')

            if \[ -f "$2" ] ; then

                try=''

                break

            fi

            ;;

 

            'removed')

            if \[ ! -f "$2" ] ; then

                try=''

                break

            fi

            ;;

        esac

 

        try=\`expr $try + 1\`

        sleep 1

    done

}

 

status(){

if \[ ! -r $nginx\_PID ] ; then

echo "nginx is stopped"

exit 0

fi

 

PID=\`cat $nginx\_PID\`

if ps -p $PID | grep -q $PID; then

echo "nginx (pid $PID) is running..."

else

echo "nginx dead but pid file exists"

fi

}

 

removePIDFile(){

    if \[ $1 ]; then

        if \[ -f $1 ]; then

            rm -f $1

        fi

    else

        #Do default removal

        if \[ -f nginx\_PID ]; then

            rm -f nginx\_PID

        fi

    fi

}

 

start() {

echo -n "Starting nginx "

 

$nginx\_BIN -c $nginx\_CONF

 

if \[ "$?" != 0 ] ; then

echo " failed"

exit 1

fi

 

wait\_for\_pid created $nginx\_PID

 

if \[ -n "$try" ] ; then

echo " failed"

exit 1

else

echo " done"

fi

}

 

stop() {

echo -n "Gracefully shutting down nginx "

 

if \[ ! -r $nginx\_PID ] ; then

echo "warning, no pid file found - nginx is not running ?"

exit 1

fi

 

kill -INT \`cat $nginx\_PID\`

 

wait\_for\_pid removed $nginx\_PID

 

if \[ -n "$try" ] ; then

echo " failed. Use force-quit"

exit 1

else

echo " done"

fi

}

 

reload() {

    echo -n "Reload service nginx"

 

if \[ ! -r $nginx\_PID ] ; then

echo "warning, no pid file found - nginx is not running ?"

exit 1

fi

 

kill -HUP \`cat $nginx\_PID\`

 

echo " done"

}

 

quietupgrade() {

    echo -n "Peforming Quiet Upgrade nginx"

 

if \[ ! -r $nginx\_PID ] ; then

echo "warning, no pid file found - nginx is not running ?"

exit 1

fi

 

kill -USR2 \`cat nginx\_PID\`

kill -WINCH \`cat nginx\_PID.oldbin\`

 

if \[ ! -r $nginx\_PID ] ; then

kill -QUIT \`cat nginx\_PID.oldbin\`

wait\_for\_pid removed nginx\_PID.oldbin

removePIDFile nginx\_PID.oldbin

 

echo " done"

else

echo " failed"

exit 1

 

echo -n "ERROR! Reverting back to original nginx"

 

kill -HUP \`cat nginx\_PID\`

kill -TERM \`cat nginx\_PID.oldbin\`

kill -QUIT \`cat nginx\_PID.oldbin\`

 

wait\_for\_pid removed nginx\_PID.oldbin

removePIDFile nginx\_PID.oldbin

 

echo " done"

fi

}

 

terminate() {

    echo -n "Force terminating (via KILL) nginx"

 

if \[ ! -r $nginx\_PID ] ; then

echo "warning, no pid file found - nginx is not running ?"

exit 1

fi

 

kill -TERM \`cat $nginx\_PID\`

 

wait\_for\_pid removed $nginx\_PID

 

if \[ -n "$try" ] ; then

echo " failed"

exit 1

else

echo " done"

fi

}

 

destroy() {

    echo -n "Force terminating and may include self (via KILLALL) nginx"

    killall nginx -q &gt;&gt; /dev/null 2&gt;&1

    echo " done"

}

 

case "$1" in

    start)

        start

        ;;

    stop)

        stop

        ;;

force-quit)

terminate

;;

    restart)

        stop

        sleep 1

        start

        ;;

    reload)

        $1

        ;;

    status)

        status

        ;;

    configtest)

        $1

        ;;

    quietupgrade)

        $1

        ;;

    destroy)

        $1

        ;;

    \*)

        echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest|quietupgrade|destroy}"

        echo "       The 'destroy' command should only be used as a last resort." 

        exit 1

        ;;

esac

 

exit 0

 

将以上脚本在 /etc/init.d/ 目录下创建文件   nginx ，并粘贴上以上的脚本保存；

设置nginx的权限：

\# chmod 777 /etc/init.d/nginx

将nginx启动命令脚本设置为服务：

#chkconfig --add  nginx

设置nginx开机启动，启动级别

\# chkconfig --level 2345 nginx on

查看开机启动配置信息

\# chkconfig --list  nginx

 

OK,  到此，脚本启动相关完成！

 

 

[http://www.oschina.net/question/8676\_7661](http://www.oschina.net/question/8676_7661)   这个文章不错

 

[http://www.cnblogs.com/tommyli/archive/2013/02/27/2935213.html](http://www.cnblogs.com/tommyli/archive/2013/02/27/2935213.html)   参考的