一、安装依赖包

yum -y install pcre-devel zlib-devel

useradd -M -s /sbin/nologin nginx          //新建nginx用户,并不指定宿主目录

tar xzvf /media/nginx-1.2.8.tar.gz -C /opt    //解压源码包

二、编译安装

cd /opt/nginx-1.2.8/

./configure \

--prefix=/usr/local/nginx \     //指定nginx安装目录

--user=nginx --group=nginx \

--with-http_stub_status_module

make

make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/   //建立软连接

三、制作管理脚本

vi /etc/init.d/nginx

#!/bin/bash

# chkconfig: 35 99 20

# description: Nginx Service Control Script

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

PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in

  start)

    $PROG

    ;;

  stop)

    kill -s QUIT $(cat $PIDF)

    ;;

  restart)

    $0 stop

    $0 start

    ;;

  reload)

    kill -s HUP $(cat $PIDF)

    ;;

  *)

        echo "Usage: $0 {start|stop|restart|reload}"

        exit 1

esac

exit 0

chmod +x /etc/init.d/nginx   //增加执行权限

chkconfig --add nginx        //nginx服务加入到service管理

用以下命令管理nginx的启动

nginx -t//检查

nginx//启动

killall -1 nginx//重启

killall -3 nginx//停止

四、配置统计页面

cd /usr/local/nginx/conf     //切换到主配置文件目录

cp nginx.conf nginx.conf.back     //备份主配置文件,以防万一嘛!

grep -v "#" nginx.conf > 123

mv -f 123 nginx.conf

vi /usr/local/nginx/conf/nginx.conf               //修改主配置文件

 

server {

        listen       80;

        server_name  localhost;

charset utf-8;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

插入下面四行

        location ~ /status {//访问位置为/status

        stub_status   on;//打开状态统计功能

        access_log off;//关闭此位置的日志记录

        }

 

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

}

 

在游览器输入192.168.1.10即可访问nginx的默认首页,加/status即可访问统计页面

nginx 编译安装_软连接

/usr/local/nginx/html/index.html   默认主页路径,配置虚拟主机,默认主页依然会生效,而apache配置虚拟主机后,默认主页不在生效

六、配置虚拟主机

server {

        server_name  www.benet.com;

        location / {

            root   /var/www/benet;

            index  index.html index.php;

        }

    }

    server {

        server_name  www.accp.com;

        location / {

            root   /var/www/accp;

            index  index.html index.php;

       }

}

在游览器输入www.benet.comwww.accp.com即可访问对应的网站