天天看點

Linux進階運維(一)-搭建NginX 網站伺服器

Web伺服器:(httpd\nginx\lighttpd\tomcat\weblogic\websphere\jboss)

Nginx性能好,免費

Tomcat免費

Nginx(taobao) ---->tengine

建立一個無法登入的使用者useradd -s /sbin/nologin

權限都是針對使用者rwxrwxrwx

程式QQ[發送檔案]

誰運作的這個程式 root ,admin ---->QQ

root----->Nginx[面向全網提供服務]

打指令是root執行指令,以普通使用者的身份啟動

現代化軟體都是子產品化設計

打包100個功能1G

子產品化(預設子產品)

源碼編譯安裝

./config --with–子產品名稱--with-子產品名稱  安裝哪個名稱

./configure —without-子產品名稱  不安裝哪一個子產品

With-http-ssl-module 加密子產品

Compiler 解釋器

Pcre -devel 紅帽的依賴包一般以devel 結尾

netstat指令可以檢視系統中啟動的端口資訊,該指令常用選項如下:

-a顯示所有端口的資訊

-n以數字格式顯示端口号

-t顯示TCP連接配接的端口

-u顯示UDP連接配接的端口

-l顯示服務正在監聽的端口資訊,如httpd啟動後,會一直監聽80端口

-p顯示監聽端口的服務名稱是什麼(也就是程式名稱)

/usr/local/nginx/sbin/nglinx(1.10)

mv 重命名

/root/lnmp_soft/nglinx-1.12.2/objs/nginx(1.12)

服務啟動狀态

make upgrade 關機重新開機 

或者殺死nginx

/usr/local/nginx/sbin/nginx //啟動服務

Killa nginx

/usr/local/nginx/conf配置檔案

/usr/local/nginx/html網頁檔案

/usr/local/nginx/logs日志檔案

/usr/local/nginx/sbin程式

index index.html index.htm; //預設網頁

index 預設首頁

apache

<virtualHost*:80>

Servsername xxx

Documentroot xxx

Nginx

Server{

Listen 80; --預設80

server_name xxx;

root html;

}

  安裝httpd-tools軟體

yum -y install httpd-tools.x86_64

htpasswd -c 建立檔案名

-c create 建立檔案

htpasswd -c /usr/local/nginx/pass tom 建立pass文檔并且建立使用者tom

htpasswd /usr/local/nginx/pass harry 追加使用者harry

虛拟主機:基于域名\基于ip\基于端口

用一台主機一個軟體,實作很多個網站

server {

listen 80;

server_name localhost \域名\IP;

auth_basic “qing shu ru zhang hao ji mi ma:”;

auth_basic_user_file “usr/local/nginx/pass”;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm;

}

編碼0-127 

人:英文

計算機:010101

Asii 編碼

GB2312(中國)

Apple unicode(utf-8) 萬國編碼

Charset utf-8 翻譯為中文

1.源碼安裝

2.使用者認證

3.虛拟主機

http 協定是明文協定

http+ssl=https(秘鑰)

Client----------------server

對稱加密:

123------------123

非對稱加密:()

123--------------t8

一,安裝Nginx

1,首先,确認虛拟機的yum源可用;并設定防火牆為trusted,SELinux為關閉狀态

2,确認lnmp_soft.tar.gz ; nginx-1.12.2.tar.gz這2個壓縮包在虛拟機裡;

3,開始安裝Nginx

#安裝相應的依賴包

[[email protected] ~]# yum -y install gcc pcre-devel openssl-devel

#建立對應的使用者

[[email protected] ~]# useradd -s /sbin/nologin nginx

#解壓軟體包

[[email protected] ~]# tar -xf lnmp_soft.tar.gz

[[email protected] ~]# cd lnmp_soft/

[[email protected] lnmp_soft]# tar -xf nginx-1.10.3.tar.gz

#編譯、安裝

[[email protected] lnmp_soft]# cd nginx-1.10.3/

[[email protected] nginx-1.10.3]# ./configure \

–prefix=/usr/local/nginx \ #指定安裝路徑

–user=nginx \ #指定使用者

–group=nginx \ #指定組

–with-http_ssl_module #開啟SSL加密功能

[[email protected] nginx-1.10.3]# make && make install

二,Nginx常見指令的用法

#啟動服務

[[email protected] nginx-1.10.3]# /usr/local/nginx/sbin/nginx

#關閉服務

[[email protected] nginx-1.10.3]# /usr/local/nginx/sbin/nginx -s stop

#重新加載配置檔案

[[email protected] nginx-1.10.3]# /usr/local/nginx/sbin/nginx -s reload

#檢視軟體資訊

[[email protected] nginx-1.10.3]# /usr/local/nginx/sbin/nginx -V

#建立軟連結,友善後期使用

[[email protected] nginx-1.10.3]# ln -s /usr/local/nginx/sbin/nginx /sbin/

三,用nginx測試指令是否可用

[[email protected] nginx-1.10.3]# nginx

四,用netstat指令檢視nginx服務資訊

[[email protected] nginx-1.10.3]# netstat -anutlp | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6101/nginx:…

補充:netstat的各選項意思

-a 顯示所有端口的資訊

-n 以數字格式顯示端口号

-t 顯示TCP連接配接的端口

-u 顯示UDP連接配接的端口

-l 顯示服務正在監聽的端口資訊,如httpd啟動後,會一直監聽80端口

-p 顯示監聽端口的服務名稱是什麼(也就是程式名稱)

五,用戶端測試

[[email protected] ~]# firefox http://192.168.4.5

案例一,更新Nginx

[[email protected] ~]# tar -xzvf nginx-1.12.2.tar.gz

[[email protected] ~]# cd nginx-1.12.2/

[[email protected] nginx-1.12.2]# ./configure \

–prefix=/usr/local/nginx

–user=nginx

–group=nginx

–with-http_ssl_module

#注意,此處隻能make,不可以make install!!! 切記!切記!切記

[[email protected] nginx-1.12.2]# make

#備份老的配置檔案

[[email protected] nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold

#拷貝新版本

[[email protected] nginx-1.12.2]# cp objs/nginx /usr/local/nginx/sbin/

#檢視更新後的版本

[[email protected] nginx-1.12.2]# nginx -V

nginx version: nginx/1.12.2

案例二,使用者認證

目的:通路WEB頁面需要進行使用者認證;使用者名為tom,密碼為123456

#備份主配置檔案,防止在修改出錯時又找不到錯誤原因,可及時的恢複原配置檔案

[[email protected] nginx-1.12.2]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

#修改主配置檔案 (添加auth_basic開頭的這兩行)

[[email protected] nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name localhost;

auth_basic “shuru:”; #認證提示符(也就是在web頁面上顯示的)

auth_basic_user_file “/usr/local/nginx/pass”; #認證密碼檔案

location / {

root html;

index index.html index.htm;

}

生成密碼檔案前,先確定系統已安裝httpd-tools

[[email protected] nginx-1.12.2]# yum -y install httpd-tools

#建立密碼檔案,并設定tom,密碼123456

[[email protected] nginx-1.12.2]# htpasswd -c /usr/local/nginx/pass tom

New password:

Re-type new password:

Adding password for user tom

#如果是追加使用者,則不需要-c選項

[[email protected] nginx-1.12.2]# htpasswd /usr/local/nginx/pass jerry

New password:

Re-type new password:

Adding password for user jerry

#驗證密碼檔案是否成功

[[email protected] nginx-1.12.2]# cat /usr/local/nginx/pass

tom: a p r 1 apr1 apr1R.D1Zjr9 d 4 z Q M m F Q V V L v c N T S L r N s 11 j e r r y : d4zQMmFQVVLvcNTSLrNs11 jerry: d4zQMmFQVVLvcNTSLrNs11jerry:apr1 R j 3 M a Y d q Rj3MaYdq Rj3MaYdqXj/UHwaDopyCpHi.DcQmc/

#重新開機Nginx服務。(重新開機前確定Nginx是運作狀态,否則會報錯)

[[email protected] nginx-1.12.2]# nginx -s reload

#用戶端驗證

[[email protected] nginx-1.12.2]#firefox http://192.168.4.5

案例三,基于域名的虛拟主機

目标:

1,實作2個基于域名的虛拟主機,域名分别為www.a.com和www.b.com

2,對域名為www.a.com的站點進行使用者認證,使用者名稱為tom,密碼為123456

步驟一:

#修改主配置檔案

#一個server就是一個虛拟主機

#root 為網頁根目錄

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name www.a.com; #把原來的修改為www.a.com

auth_basic “shuru:”;

auth_basic_user_file “/usr/local/nginx/pass”;

location / {

root html;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

#新增一個server,可以把上面的複制、粘貼下來,并做相應的修改

server {

listen 80;

server_name www.b.com; #把原來的修改為www.b.com

location / {

root www;

index index.html index.htm;

}

}

步驟二:

#建立網頁根目錄以及對應的首頁檔案

[[email protected] ~]# mkdir /usr/local/nginx/www

[[email protected] ~]# echo “wwww” > /usr/local/nginx/www/index.html

步驟三:

#重新開機nginx服務,用戶端測試

1,先修改client用戶端的/etc/hosts檔案,進行域名解析

[[email protected] ~]# vim /etc/hosts

192.168.4.5 www.a.com www.b.com

2,client測試

[[email protected] ~]# firefox http://www.a.com

[[email protected] ~]# firefox http://www.b.com

案例四,拓展内容

一,基于端口的虛拟主機

步驟一:

#基于上一個實驗,修改主配置檔案

#在前面實驗的基礎上,新增2個新的server

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

server {

listen 8080;

server_name web1.example.com;

location / {

root web1;

index index.html index.htm;

}

}

server {

listen 8000;

server_name web1.example.com;

location / {

root web2;

index index.html index.htm;

}

}

步驟二:

#建立對應的網頁根目錄,以及首頁檔案

[[email protected] ~]# mkdir /usr/local/nginx/web1 /usr/local/nginx/web2

[[email protected] ~]# echo “web1” > /usr/local/nginx/web1/index.html

[[email protected] ~]# echo “web2” > /usr/local/nginx/web2/index.html

步驟三:

#重新開機nginx服務,并在用戶端client測試

[[email protected] ~]# nginx -s reload

#在client上,/etc/hosts檔案裡添加一個域名解析

[[email protected] ~]# vim /etc/hosts

192.168.4.5 web1.example.com

[[email protected] ~]# firefox http://web1.example.com:8080

[[email protected] ~]# firefox http://web1.example.com:8000

二,基于IP的虛拟主機

步驟一:

#要配置基于IP的虛拟主機,必須要先給web伺服器proxy的網卡裝置添加2個IP别名(也就是一個網卡綁定多個IP)

#首先,執行ifconfig,檢視網卡裝置名稱

[[email protected] ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.5 netmask 255.255.255.0 broadcast 192.168.4.255

#我們可以看到網卡裝置名稱為 eth0

#下面,我們給該裝置添加IP别名

#添加IP别名1

[[email protected] ~]# ifconfig eth0:1 192.168.4.6 broadcast 192.168.4.255 netmask 255.255.255.0 up

[[email protected] ~]# route add -host 192.168.4.6 dev eth0:1

[[email protected] ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.5 netmask 255.255.255.0 broadcast 192.168.4.255

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.6 netmask 255.255.255.0 broadcast 192.168.4.255

#添加IP别名2

[[email protected] ~]# ifconfig eth0:2 192.168.4.7 broadcast 192.168.4.255 netmask 255.255.255.0 up

[[email protected] ~]# route add -host 192.168.4.7 dev eth0:2

[[email protected] ~]# ifconfig

[[email protected] ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.5 netmask 255.255.255.0 broadcast 192.168.4.255

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.6 netmask 255.255.255.0 broadcast 192.168.4.255

eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.7 netmask 255.255.255.0 broadcast 192.168.4.255

#到此,配置完成!!!

步驟二:

#修改主配置檔案,在前面的基礎之上新增2個server

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

server {

listen 192.168.4.6:80;

server_name 192.168.4.6;

location / {

root web3;

index index.html index.htm;

}

}

server {

listen 192.168.4.7:80;

server_name 192.168.4.7;

location / {

root web4;

index index.html index.htm;

}

}

步驟三:

#建立對應的網頁根目錄,以及首頁檔案

[[email protected] ~]# mkdir /usr/local/nginx/web3 /usr/local/nginx/web4

[[email protected] ~]# echo “web3” > /usr/local/nginx/web3/index.html

[[email protected] ~]# echo “web4” > /usr/local/nginx/web4/index.html

步驟四:

#重新開機nginx服務,并在用戶端測試

[[email protected] ~]# nginx -s reload

[[email protected] ~]# firefox http://192.168.4.6

[[email protected] ~]# firefox http://192.168.4.7

思考題:

在配置基于IP的虛拟主機時,如果2個server_name後面寫的是同一個域名,用戶端的/etc/hosts裡也寫了對應的域名解析。

那麼,我們在測試時,輸入的不是IP位址,而是域名時,會出現什麼問題?原因是什麼??

案例五,SSL虛拟主機

沿用基于域名的虛拟主機,配置加密網站,目标為:

1,域名為www.a.com

2,該站點通過https通路

3,通過私鑰、證書對該站點所有資料加密

步驟一:

#生成私鑰與證書

[[email protected] ~]# cd /usr/local/nginx/conf/

#生成私鑰匙

[[email protected] conf]# openssl genrsa > cert.key

Generating RSA private key, 2048 bit long modulus

…+++

…+++

e is 65537 (0x10001)

#生成證書

[[email protected] conf]# openssl req -new -x509 -key cert.key > cert.pem

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter ‘.’, the field will be left blank.

Country Name (2 letter code) [XX]:CN #CN為國家

State or Province Name (full name) []:Guangdong #Guangdong 為省份

Locality Name (eg, city) [Default City]:GZ #GZ 為城市

Organization Name (eg, company) [Default Company Ltd]:tedu #tedu為公司名

Organizational Unit Name (eg, section) []:operation #operation為部門名

Common Name (eg, your name or your server’s hostname) []:proxy #proxy為主機名

Email Address []:[email protected] #[email protected] 為郵箱

步驟二:

#修改主配置檔案,設定加密網站的虛拟主機

#如果主配置檔案有操作上面的虛拟主機,為了防止看着太亂,可以先把上面拓展的2個虛拟主機(端口、IP)的配置都注釋掉

#vim批量操作的技巧:

#ctrl+v 一直按着選中要修改的行;再用shift+i 執行要做的操作(我們這邊為加#);然後再按esc鍵;就一下子全部加了#啦

#把配置最後一個被#注釋掉的server開啟,并做相應的修改

[[email protected] conf]# vim /usr/local/nginx/conf/nginx.conf

server {

listen 443 ssl;

server_name www.c.com;

ssl_certificate      cert.pem;
    ssl_certificate_key  cert.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   html;
        index  index.html index.htm;
    }
}
           

步驟三:

#重新開機nginx服務

[[email protected] conf]# nginx -s reload

#用戶端修改/etc/hosts檔案,添加www.c.com

[[email protected] ~]# vim /etc/hosts

192.168.4.5 www.a.com www.b.com web1.example.com www.c.com

#通路測試

#注意:需要添加信任證書後,才可以正常通路

[[email protected] ~]# firefox https://www.c.com

拓展知識點:

nginx的虛拟主機的域名含有通配符*這種情況時,nginx是如何确定通路的順序??

比如:第一台虛拟主機的域名是www.a.com *.a.com;第二台的是uc.a.com。那麼我通路uc.a.com時,是傳回第一台還是第二台呢?

答案是第二台的頁面内容!!

原因如下:(官網文檔裡有說明,翻譯如下)

在按名稱搜尋虛拟伺服器期間,如果名稱與多個指定的變量比對(例如,通配符名稱和正規表達式比對),将按以下優先級順序選擇第一個比對:

1、确切的名字

2、以星号開頭的最長通配符名稱,例如“ .example.com”

3、最長的通配符名稱以星号結尾,例如“ mail.”

4、第一個比對的正規表達式(按配置檔案中的出現順序)

繼續閱讀