Nginx安装配置与访问统计

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

Nginx是一款轻量级的Web 服务器,其特点是占有内存少,并发能力强,它具有强大的高性能Web和反向代理服务。

部署环境:

  • redhat6.5系统
  • ip地址192.168.100.101
  • 相关软件包百度云

一、nginx配置

1.安装环境包

# yum install pcre pcre-devel zlib-devel gcc gcc-c++ -y

2.创建一个不指定用户家目录 不允许shell登录nginx用户

# useradd -M -s /sbin/nologin nginx

3.解压压缩包到opt

# tar zxvf nginx-1.6.0.tar.gz -C /opt

4.配置编译安装

# cd /opt/nginx-1.60
 ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

注释

 ./configure \
--prefix=/usr/local/nginx \    #ngnix存放位置
--user=nginx \                 #Nginx管理用户
--group=nginx \                #Nginx管理组
--with-http_stub_status_module             #开启stub_status状态统计模块

5.编译安装

# make && make install

6.用命令创建一个快捷方式

# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

7.检查、启动、重启、停止

# nginx -t         //查看是否安装成功
# nginx           //启动Nginx 服务
# netstat -anpt | grep nginx        //查看端口
# killall -1 nginx     //重启Nginx服务
#killall -3 nginx     //关闭Nginx服务

8.制作Nginx管理脚本 方便chkconfig和service工具管理

# vim /usr/local/nginx/conf/nginx.conf
    pid        logs/nginx.pid;   //去#号  使下面脚本中路径文件生成

#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"      #赋值  Nginx 启动命令
PIDF="/usr/local/nginx/logs/nginx.pid"  #赋值  Nginx 进程号
case "$1" in
  start)
    $PROG
    ;;  
  stop)
    kill -s QUIT $(cat $PIDF)  #指定杀死进程号
    ;;
  restart)
    $0 stop   $0 当前脚本
    $0 start
    ;;
  reload)
    kill -s HUP $(cat $PIDF)  #指定重载
    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1   #异常退出
esac
exit 0  #正常退出

9.添加权限,关闭防火墙

# chmod +x /etc/init.d/nginx  #添加执行权限
# chkconfig --add nginx       #把服务添加到service
# chkconfig --level 35 nginx on

10.开启服务,关闭防火墙

# service nginx start
# service iptables stop
# setenforce 0

11.测试访问192.168.100.101

Nginx安装配置与访问统计

二、访问统计

1.配置统计页面

# cd /usr/local/nginx/conf
# mv nginx.conf nginx.conf.back   #备份配置文件
# grep -v "#" nginx.conf.back > nginx.conf   #过滤注释行生成配置文件
# vim /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;                     #关闭此位置的日志记录
        }               //在"server"这里插入的这4行的

        error_page   500 502 503 504  /50x.html;  #页面错误反馈的页面是 50x.html
        location = /50x.html {              #错误首页位置
            root   html;                    #网站根目录的位置
        }

       }
    }

2.重启nginx服务

service nginx restart

3.测试访问192.168.100.101/status

Nginx安装配置与访问统计

猜你喜欢

转载自blog.51cto.com/13777111/2165737