關于Nginx一款高性能,輕量級web服務軟件
穩定性高
單臺物理服務器可支持30000~50000個并發請求
系統資源消耗低
對HTTP并發連接的處理能力高
環境
- 一臺Linux服務器(192.168.13.128)
- 一臺win10測試機
一,在Windows上將LAMP所需壓縮軟件包共享出來(此處如有問題請看之前的博客相關文章)
二,在Linux上使用遠程共享獲取文件并掛載到mnt目錄下
1
2
3
4
5
6
7
|
[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下,并查看
1
2
3
4
5
6
7
8
9
10
|
[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,安裝編譯需要的環境組件包
1
2
3
4
5
|
[root@localhost opt] # yum -y install \ gcc \ //c 語言 gcc-c++ \ //c ++語言 pcre-devel \ //pcre 語言工具 zlib-devel // 數據壓縮用的函式庫 |
3,創建程序用戶nginx并編譯Nginx
1
2
3
4
5
6
7
8
9
|
[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,編譯和安裝
1
2
3
4
|
[root@localhost nginx-1.12.0] # make ##編譯 ... [root@localhost nginx-1.12.0] # make install ##安裝 ... |
5,優化nginx啟動腳本,以便于系統識別
1
2
3
4
5
6
7
8
9
|
[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網頁測試工具,并進行測試
1
2
|
[root@localhost nginx] # yum install elinks -y ##安裝elinks軟件 [root@localhost nginx] # elinks http://localhost ##測試nginx網頁 |
7,服務開啟重載以及關閉
1
2
3
|
[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管理使用
1
2
3
|
[root@localhost nginx] # killall -s QUIT nginx ##停止 或者使用killall -3 nginx [root@localhost nginx] # killall -s HUP nginx ##重啟 或者使用killall -1 nginx [root@localhost nginx] # nginx ##開啟 |
Nginx的訪問狀態統計
啟用HTTP_STUB_STATUS狀態統計模塊
nginx -V可以查看已安裝的Nginx是否包含統計模塊
一,修改nginx配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[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服務
1
|
[root@localhost conf] # yum install bind -y ##安裝DNS服務 |
2,配置主配置文件/etc/named.conf
1
2
3
4
5
6
7
8
9
10
11
12
|
[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)
1
2
3
4
5
6
7
8
9
10
11
12
|
[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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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,關閉防火墻并開啟服務
1
2
3
|
[root@localhost named] # systemctl start named ##開啟dns服務 [root@localhost named] # systemctl stop firewalld.service ##關閉防火墻 [root@localhost named] # setenforce 0 ##關閉增強功能 |
6,利用win10測試機來測試
基于授權的訪問控制
配置步驟與Apache基本一致
生成用戶密碼認證文件
修改主配置文件對相應目錄,添加認證配置項
重啟服務,訪問測試
一,修改主配置文件
1
2
3
4
5
6
7
8
9
|
[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工具包,設置密碼認證文件
1
2
3
4
5
6
7
8
9
|
[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測試機測試
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.51cto.com/14469918/2451655