1. 程式人生 > >Nginx安裝配置與訪問統計

Nginx安裝配置與訪問統計

ln -s 是否 use ati nginx 服務 hat 代理服務 tex smtp

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

技術分享圖片

二、訪問統計

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安裝配置與訪問統計