Nginx網站服務——服務基礎,訪問控制(實戰!)

關於Nginx

一款高性能,輕量級web服務軟件

穩定性高
系統資源消耗低
對HTTP併發鏈接的處理能力高php

  • 單臺物理服務器可支持30000~50000個併發請求

環境

一臺Linux服務器(192.168.13.128)
一臺win10測試機

一,在Windows上將LAMP所需壓縮軟件包共享出來(此處若有問題請看以前的博客相關文章)

Nginx網站服務——服務基礎,訪問控制(實戰!)

二,在Linux上使用遠程共享獲取文件並掛載到mnt目錄下

[root@localhost ~]# smbclient -L //192.168.100.3/   ##遠程共享訪問
Enter SAMBA\root's password: 

                Sharename       Type      Comment
                ---------       ----      -------
                LAMP-C7         Disk       
[root@localhost ~]# mount.cifs //192.168.100.3/LAMP-C7 /mnt  ##掛載到/mnt目錄下

三,編譯安裝Nginx

1,解壓源碼包到/opt下,並查看

[root@localhost ~]# cd /mnt    ##切換到掛載點目錄
[root@localhost mnt]# ls
apr-1.6.2.tar.gz                  Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt
apr-util-1.6.0.tar.gz             error.png                mysql-5.6.26.tar.gz
awstats-7.6.tar.gz                httpd-2.4.29.tar.bz2     nginx-1.12.0.tar.gz
cronolog-1.6.2-14.el7.x86_64.rpm  kali.jpg                 php-5.6.11.tar.bz2
[root@localhost mnt]# tar zxvf nginx-1.12.0.tar.gz -C /opt   ##解壓Nginx源碼包到/opt下
[root@localhost mnt]# cd /opt/    ##切換到解壓的目錄下
[root@localhost opt]# ls
nginx-1.12.0  rh

2,安裝編譯須要的環境組件包

[root@localhost opt]# yum -y install \
gcc \                                       //c語言
gcc-c++ \                        //c++語言
pcre-devel \                     //pcre語言工具
zlib-devel                       //數據壓縮用的函式庫

3,建立程序用戶nginx並編譯Nginx

[root@localhost opt]# useradd -M -s /sbin/nologin nginx  ##建立程序用戶,安全不可登錄狀態
[root@localhost opt]# id nginx
uid=1001(nginx) gid=1001(nginx) 組=1001(nginx)
[root@localhost opt]# cd nginx-1.12.0/                 ##切換到nginx目錄下
[root@localhost nginx-1.12.0]# ./configure \         ##配置nginx
> --prefix=/usr/local/nginx \        ##安裝路徑
> --user=nginx \                         ##用戶名
> --group=nginx \                       ##用戶組
> --with-http_stub_status_module     ##狀態統計模塊

4,編譯和安裝

[root@localhost nginx-1.12.0]# make     ##編譯
...
[root@localhost nginx-1.12.0]# make install   ##安裝
...

5,優化nginx啓動腳本,以便於系統識別

[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  ##建立軟鏈接讓系統識別nginx啓動腳本
[root@localhost nginx]# 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
[root@localhost nginx]# nginx      ##開啓ngnix
[root@localhost nginx]# netstat -ntap | grep 80     ##查看端口,nginx已經開啓
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      39620/nginx: master 
[root@localhost nginx]# systemctl stop firewalld.service    ##關閉防火牆
[root@localhost nginx]# setenforce 0

6,安裝elinks網頁測試工具,並進行測試

[root@localhost nginx]# yum install elinks -y    ##安裝elinks軟件
[root@localhost nginx]# elinks http://localhost  ##測試nginx網頁

Nginx網站服務——服務基礎,訪問控制(實戰!)

7,服務開啓重載以及關閉

[root@localhost nginx]# killall -s QUIT nginx   ##中止 或者使用killall -3 nginx
[root@localhost nginx]# killall -s HUP nginx    ##重啓 或者使用killall -1 nginx
[root@localhost nginx]# nginx                         ##開啓

8,製做管理腳本,便於使用service管理使用

[root@localhost nginx]# cd /etc/init.d/   ##切換到啓動配置文件目錄
[root@localhost init.d]# ls
functions  netconsole  network  README
[root@localhost init.d]# vim nginx         ##編輯啓動腳本文件

#!/bin/bash
# chkconfig: - 99 20                                    ##註釋信息
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"           ##設置變量爲nginx命令文件
PIDF="/usr/local/nginx/logs/nginx.pid"       ##設置變量PID文件 進程號爲5346
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
[root@localhost init.d]# chmod +x /etc/init.d/nginx    ##給啓動腳本執行權限
[root@localhost init.d]# chkconfig --add nginx          ##添加到service管理器中
[root@localhost init.d]# service nginx stop                ##就能夠使用service控制nginx
[root@localhost init.d]# service nginx start

Nginx的訪問狀態統計

  • 啓用HTTP_STUB_STATUS狀態統計模塊
  • nginx -V能夠查看已安裝的Nginx是否包含統計模塊

一,修改nginx配置文件

[root@localhost ~]# cd /usr/local/nginx/conf     ##切換到配置文件目錄
[root@localhost conf]# vim nginx.conf              ##修改Nginx配置文件

server {
    listen       80;
    server_name  www.kgc.com;        ##指明一個域名

    charset utf-8;                                 ##中文字符集

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }
    location /status {                           ##添加狀態統計
    stub_status on;
    access_log off;
    }

二,安裝DNS服務器作域名解析

1,安裝bind服務

[root@localhost conf]# yum install bind -y   ##安裝DNS服務

2,配置主配置文件/etc/named.conf

[root@localhost conf]# vim /etc/named.conf   ##主配置文件

options {
                listen-on port 53 { any; };      ##將本機監聽爲全部
                listen-on-v6 port 53 { ::1; };
                directory       "/var/named";
                dump-file       "/var/named/data/cache_dump.db";
                statistics-file "/var/named/data/named_stats.txt";
                memstatistics-file "/var/named/data/named_mem_stats.txt";
                recursing-file  "/var/named/data/named.recursing";
                secroots-file   "/var/named/data/named.secroots";
                allow-query     { any; };      ##容許全部

3,配置區域配置文件(etc/named.rfc1912.zones)

[root@localhost conf]# vim /etc/named.rfc1912.zones  ##配置區域配置文件
zone "localhost" IN {             ##複製模板到下面
                                type master;
                                file "named.localhost";
                                allow-update { none; };
};

zone "kgc.com" IN {              ##修改localhost爲kgc.com
                                type master;
                                file "kgc.com.zone";      ##建立區域數據配置文件
                                allow-update { none; };
};

4,編輯區域數據配置文件( kgc.com.zone)

[root@localhost conf]# cd /var/named
[root@localhost named]# cp -p named.localhost kgc.com.zone   
##複製模板爲kgc.com.zone
[root@localhost named]# vim kgc.com.zone  ##編輯區域數據配置文件

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                                                        0       ; serial
                                                                        1D      ; refresh
                                                                        1H      ; retry
                                                                        1W      ; expire
                                                                        3H )    ; minimum
                                NS      @
                                A       127.0.0.1
www IN  A       192.168.13.128   ##刪除ipv6 添加域名解析地址爲本機

5,關閉防火牆並開啓服務

[root@localhost named]# systemctl start named   ##開啓dns服務
[root@localhost named]# systemctl stop firewalld.service    ##關閉防火牆
[root@localhost named]# setenforce 0   ##關閉加強功能

6,利用win10測試機來測試

Nginx網站服務——服務基礎,訪問控制(實戰!)
Nginx網站服務——服務基礎,訪問控制(實戰!)

基於受權的訪問控制

配置步驟與Apache基本一致

  • 生成用戶密碼認證文件
  • 修改主配置文件對相應目錄,添加認證配置項
  • 重啓服務,訪問測試

一,修改主配置文件

[root@localhost ~]# cd /usr/local/nginx/conf     ##切換到配置文件目錄
[root@localhost conf]# vim nginx.conf              ##修改Nginx配置文件

    location / {
        auth_basic "secret";                                ##驗證類型
        auth_basic_user_file /usr/local/nginx/passwd.db;      ##驗證文件路徑
        root   html;
        index  index.html index.htm;
    }

二,安裝httpd-tools工具包,設置密碼認證文件

[root@localhost conf]# yum install httpd-tools -y    ##安裝工具包
[root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db test    ##設置密碼認證文件
New password:          ##輸入密碼
Re-type new password:        ##確認密碼
Adding password for user test
[root@localhost conf]# cat /usr/local/nginx/passwd.db      ##查看密碼認證文件
test:$apr1$LqqHZeX3$24E7/HeacTVRzKA7nvSgY/
[root@localhost conf]# service nginx stop      ##關閉服務
[root@localhost conf]# service nginx start      ##開啓服務

三,使用win10測試機測試

Nginx網站服務——服務基礎,訪問控制(實戰!)
Nginx網站服務——服務基礎,訪問控制(實戰!)

謝謝閱讀!!!

相關文章
相關標籤/搜索