1、HTTP協議包含不少功能php
www是http功能之一css
www服務端口默認是80,OSI 第7層 應用層協議html
2、實現WWW服務的經常使用Web軟件mysql
產品:nginx,apache(靜態Web軟件)linux
3、經典的web組合nginx
LAMP(Linux apache mysql php)==》經典web
LNMP(Linux Nginx mysql php)==》國內很是流行sql
4、Nginx介紹apache
Nginx www服務軟件,俄羅斯人開發,開源,性能很高vim
Nginx自己是一款靜態(html,js,css,jpg等)www軟件,靜態小文件(1M),高併發。
同時佔用的資源不多。3萬併發進程,內存150M,不能解析PHP,JSP。
Nginx 使用平臺:unix,linux,windows便可使用。
淘寶更改的nginx:tengine(http://tengine.taobao.org/)
[root@Qinglin-Test1 ~]# curl -I www.taobao.com HTTP/1.1 302 Found Server: Tengine
5、Nginx服務從大的方面的功能
a. www web服務,郵件服務,郵件代理
b. 負載均衡(反向代理proxy)
c. webcache(web)緩存,至關於squid(CDN主要使用Squid)。
6、Nginx特色
一、配置簡單,靈活,輕量。
二、高併發(靜態小文件),靜態幾萬的併發
三、佔用資源少。2W併發,開10個線程服務,內存消耗幾百M
四、功能各種比較多(web,cache,proxy),每個功能都不是特別強的功能
五、支持epool模型。使得Nginix能夠支持高併發。Apache選擇的select模型。
六、Nginx能夠配合動態PHP服務(FastCGI接口)
七、利用Nginx能夠實現IP限制,能夠限制鏈接數。
它所具有 的其餘WWW服務特性以下:
支持基於名字、端口以及IP的多虛擬主機站點。
可進行簡單、方便、靈活的配置和管理。
支持修改Nginx配置,而且在代碼上線時,能夠平滑重啓,不中斷業務訪問。
可自定義訪問日誌格式,臨時緩衝寫日誌操做,快速日誌輪訓及經過syslog處理日誌;
可利用信息控制Nginix進程
支持3xx-5xx http狀態碼重定向
支持rewrite模塊,支持URL重寫及正規表達式匹配
支持基於客戶端IP地址和HTTP基本認證的訪問控制
支持PUT,DELETE,MKCOL,COPY以及MOVE等http請求
方法:
支持FLV流和MP4流技術產品應用;
支持http響應速率限制
支持同一IP地址的併發鏈接或請求數限制
7、Nginx的應用場合
一、提供靜態服務器(圖片、視頻服務),另外一個lighttpd。併發:幾萬併發
二、提供動態服務,Nginx+Fastcgi的方式運行PHP,JSP。動態併發:500-1500
三、提供反向代理,或稱稱爲負載均衡。日PV2000萬如下,併發1-2萬均可以直接用Nginx作理。(至關於Haproxy,suid,F5,A10)
四、提供緩存服務。相似Suid,varnish,ats。
Nginx主機應用場景:
一、Web服務器(首選)
靜態文件,nginx首選
動態文件,配置fastcgi支持php
二、反向代理(負載均衡),1000-2000W PV,併發6000
三、緩存(使用的並很少)
8、Nginx支持虛擬主機
一個Server標籤段就是一個虛擬主機
一、基於域名的虛擬主機,經過域名來區分虛擬主機==》應用:外部網站
二、基於端口的虛擬主機。經過端口來區分虛擬主機==》應用:公司內部網站,網站的後臺。
三、基於IP的虛擬主機。幾乎不用。不支持ifconfig別名,配置文件能夠。
9、Nginx的安裝
Nginx官方地址:http://nginx.org/
一、安裝RCRE
Pcre全稱(Perl Compatible Regular Expressions)中文perl兼容正規表達示。
官方地址:https://sourceforge.net/projects/pcre/files/pcre/
下載的版本:pcre-8.30
下載的地址:http://jaist.dl.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz
可是在生產環境中,咱們通常使用yum安裝。
安裝完成後,nginx會支持僞靜態等perl的正規表達示。注:必裝
檢查工做:檢查本身的系統版本的內核版本,並檢查是否此臺服務器已經安裝pcre了
下面的結果已經顯示出來pcre安裝過了,可是沒有安裝pcre的開發包devel。
[root@Qinglin-Test1 ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@Qinglin-Test1 ~]# uname -r 2.6.32-431.23.3.el6.x86_64 [root@Qinglin-Test1 ~]# rpm -qa pcre pcre-devel pcre-7.8-6.el6.x86_64
yum安裝pcre開發包
[root@Qinglin-Test1 ~]# yum -y install pcre pcre-devel Running Transaction Updating : pcre-7.8-7.el6.x86_64 1/3 Installing : pcre-devel-7.8-7.el6.x86_64 2/3 Cleanup : pcre-7.8-6.el6.x86_64 3/3 Verifying : pcre-7.8-7.el6.x86_64 1/3 Verifying : pcre-devel-7.8-7.el6.x86_64 2/3 Verifying : pcre-7.8-6.el6.x86_64 3/3 Installed: pcre-devel.x86_64 0:7.8-7.el6 Updated: pcre.x86_64 0:7.8-7.el6 Complete!
再次檢查安裝的pcre包,檢查已經發現devel包已經安裝成功,結果以下:
[root@Qinglin-Test1 ~]# rpm -qa pcre pcre-devel pcre-7.8-7.el6.x86_64 pcre-devel-7.8-7.el6.x86_64
二、安裝opnssl開發包
該服務是支持SSL 443的服務,也是必須安裝的
[root@Qinglin-Test1 ~]# yum install -y openssl-devel Installed: openssl-devel.x86_64 0:1.0.1e-48.el6_8.1 Dependency Installed: keyutils-libs-devel.x86_64 0:1.4-5.el6 krb5-devel.x86_64 0:1.10.3-57.el6 libcom_err-devel.x86_64 0:1.41.12-22.el6 libselinux-devel.x86_64 0:2.0.94-7.el6 libsepol-devel.x86_64 0:2.0.41-4.el6 zlib-devel.x86_64 0:1.2.3-29.el6 Dependency Updated: e2fsprogs.x86_64 0:1.41.12-22.el6 e2fsprogs-libs.x86_64 0:1.41.12-22.el6 keyutils-libs.x86_64 0:1.4-5.el6 krb5-libs.x86_64 0:1.10.3-57.el6 libcom_err.x86_64 0:1.41.12-22.el6 libselinux.x86_64 0:2.0.94-7.el6 libselinux-utils.x86_64 0:2.0.94-7.el6 libss.x86_64 0:1.41.12-22.el6 openssl.x86_64 0:1.0.1e-48.el6_8.1 Complete!
查安裝的openssl包,檢查已經發現devel包已經安裝成功,結果以下:
[root@Qinglin-Test1 ~]# rpm -qa openssl* openssl-devel-1.0.1e-48.el6_8.1.x86_64 openssl-1.0.1e-48.el6_8.1.x86_64
三、安裝Nginx服務
檢查是否本臺服務器安裝了nginx,發現並無安裝,以下:
[root@Qinglin-Test1 ~]# rpm -qa nginx [root@Qinglin-Test1 ~]#
本次版本使用1.6.3,stable是穩定版,也可使用1.8.0
開始準備安裝
下載地址:http://nginx.org/download/nginx-1.6.3.tar.gz
在本地建立一個專於用下載的目錄,並進行下載。
[root@Qinglin-Test1 /]# mkdir /home/tools/ -p [root@Qinglin-Test1 /]# cd /home/tools/ [root@Qinglin-Test1 tools]# wget http://nginx.org/download/nginx-1.6.3.tar.gz --2016-06-25 21:34:07-- http://nginx.org/download/nginx-1.6.3.tar.gz Resolving nginx.org... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3 Connecting to nginx.org|95.211.80.227|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 805253 (786K) [application/octet-stream] Saving to: 「nginx-1.6.3.tar.gz」 100%[======================================>] 805,253 515K/s in 1.5s 2016-06-25 21:34:10 (515 KB/s) - 「nginx-1.6.3.tar.gz」 saved [805253/805253] [root@Qinglin-Test1 tools]# ls nginx-1.6.3.tar.gz
建立nginx專用用戶並檢查
-s /sbin/nologin是不容許登錄,-M是不建立家目錄
[root@Qinglin-Test1 nginx-1.6.3]# useradd nginx -s /sbin/nologin -M [root@Qinglin-Test1 nginx-1.6.3]# cat /etc/passwd|grep nginx nginx:x:500:500::/home/nginx:/sbin/nologin
思路:解壓、編譯安裝,並使用echo $?查看返回值是否爲0,非0都是錯誤的。
[root@Qinglin-Test1 tools]# tar -xf nginx-1.6.3.tar.gz [root@Qinglin-Test1 tools]# cd nginx-1.6.3 [root@Qinglin-Test1 nginx-1.6.3]# ls auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README [root@Qinglin-Test1 nginx-1.6.3]# ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module Configuration summary + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library nginx path prefix: "/app/application/nginx-1.6.3" nginx binary file: "/app/application/nginx-1.6.3/sbin/nginx" nginx configuration prefix: "/app/application/nginx-1.6.3/conf" nginx configuration file: "/app/application/nginx-1.6.3/conf/nginx.conf" nginx pid file: "/app/application/nginx-1.6.3/logs/nginx.pid" nginx error log file: "/app/application/nginx-1.6.3/logs/error.log" nginx http access log file: "/app/application/nginx-1.6.3/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@Qinglin-Test1 nginx-1.6.3]# echo $? 0 make[1]: Leaving directory `/home/tools/nginx-1.6.3' [root@Qinglin-Test1 nginx-1.6.3]# echo $? 0 [root@Qinglin-Test1 nginx-1.6.3]# make install make[1]: Leaving directory `/home/tools/nginx-1.6.3'
注:./config是能夠--help的,查看你安裝的模塊
[root@Qinglin-Test1 nginx-1.6.3]# ./configure --help --help print this message --prefix=PATH set installation prefix --sbin-path=PATH set nginx binary pathname --conf-path=PATH set nginx.conf pathname --error-log-path=PATH set error log pathname --pid-path=PATH set nginx.pid pathname --lock-path=PATH set nginx.lock pathname --user=USER set non-privileged user for worker processes --group=GROUP set non-privileged group for worker processes --with-http_ssl_module enable ngx_http_ssl_module --with-http_stub_status_module enable ngx_http_stub_status_module
解釋:
./configure 是生成makefile文件,實際是個配置過程
make 就是編譯生成的配置makefile文件
make install 是真正安裝的過程
參數說明:
--prefix= 指定安裝的目錄
--user= 指定nginx的用戶是什麼,不指定默認nobody
--group= 指定nginx的組是什麼,不指定默認nobody
--with-http_ssl_module 指定nginx使用ssl模塊,支持443端口的https
--with-http_stub_status_module 狀態模塊,好比檢查鏈接數等等,能夠用來監控。
安裝的最後於一步,建立軟鏈接,ln必定要寫全路徑
[root@Qinglin-Test1 nginx-1.6.3]# cd /application/ [root@Qinglin-Test1 application]# ls nginx-1.6.3 [root@Qinglin-Test1 application]# ln -s /application/nginx-1.6.3/ /application/nginx [root@Qinglin-Test1 application]# ll total 4 lrwxrwxrwx 1 root root 25 Jun 25 21:59 nginx -> /application/nginx-1.6.3/ drwxr-xr-x 6 root root 4096 Jun 25 21:59 nginx-1.6.3
四、啓動Nginx,並檢查
執行完不報錯,通常狀況下說明啓動成功了
進程中有一個master能夠理解成領導,實際上功做的是woker
[root@Qinglin-Test1 application]# /application/nginx/sbin/nginx [root@Qinglin-Test1 application]# ps -ef|grep nginx|grep -v grep root 4597 1 0 22:08 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx nginx 4598 4597 0 22:08 ? 00:00:00 nginx: worker process
檢查端口,以下,80已經啓動了
[root@Qinglin-Test1 application]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4597/nginx
本地使用curl命令檢測是否啓動成功
[root@Qinglin-Test1 application]# curl 127.0.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title>
經過瀏覽器訪問,若是不通,檢查一下iptables
五、查看nginx的編譯過的參數
若是到一家新公司,想查看安裝時是如何編譯的,使用-V,方法以下:
[root@Qinglin-Test1 /]# /application/nginx/sbin/nginx -V nginx version: nginx/1.6.3 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) TLS SNI support enabled configure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
六、排錯
錯誤1:多是iptables規則不容許
錯誤2:selinux
排錯日誌:/var/log/messages
/nginx/log/error.log
10、部署一個WEB頁面
一、瞭解Nginx重要目錄
排除掉nginx目錄下的tmp目錄
conf:配置文件目錄
html:默認網站目錄
sbin:啓動命令
logs:錯誤,訪問,pid目錄
[root@Qinglin-Test1 nginx]# cd /application/nginx/ [root@Qinglin-Test1 nginx]# cd / [root@Qinglin-Test1 /]# cd /application/nginx/ [root@Qinglin-Test1 nginx]# ls -v |grep -v temp conf html logs sbin
二、部署一個默認站點(html目錄)
進入到html目錄,將原來的index.html更名,並從新寫一個index.html
[root@Qinglin-Test1 nginx]# cd html/ [root@Qinglin-Test1 html]# ls 50x.html index.html [root@Qinglin-Test1 html]# mv index.html index.html.bak [root@Qinglin-Test1 html]# ls 50x.html index.html.bak [root@Qinglin-Test1 html]# vim index.html <html> <head><title>Qinglin.s Nginx Server Test Page</title></head> <body> Hi,This My Test Page!<br \> My Blog address is <a href="http://www.qinglin.net">Qinglin Blog</a> </body> </html>
改html的內容不須要重啓nginx,如今測試一下頁面是否能夠訪問
11、Nginx模塊彙總
模塊文檔:http://nginx.org/en/docs/
http://tengine.taobao.org/documentation_cn.html
經常使用模塊彙總表:
12、Nginx目錄結構
[root@Qinglin-Test1 application]# tree nginx nginx ├── client_body_temp ==> 客戶端內容的臨時文件 ├── conf ==> 這是Nginx的全部配置文件的目錄 │ ├── fastcgi.conf │ ├── fastcgi.conf.default │ ├── fastcgi_params ==> 配置PHP的動態配置文件 │ ├── fastcgi_params.default ==> 以.default都是nginx自動幫助備份的配置文件 │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf ==> 核心文件,主要配置nginx靜態 │ ├── nginx.conf.default ==> 以.default都是nginx自動幫助備份的配置文件 │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── fastcgi_temp ├── html ==> 默認的站點目錄 │ ├── 50x.html ==> 錯誤頁面替代顯示文件,例如502就會調用該頁面 │ ├── index.html ==> 首頁文件在nginx定義好的默認html歡迎頁 │ └── index.html.bak ├── logs ==> Nginx日誌目錄的默認路徑,包括錯誤日誌及訪問日誌 │ ├── access.log ==> 訪問日誌 │ ├── error.log ==> 錯誤日誌,nginx故障問題會呈現到這裏 │ └── nginx.pid ==> Nginx的pid文件,Nginx進程啓動後,會把PID號寫到這裏 ├── proxy_temp ==> 表明的臨時文件 ├── sbin ==> 這是Nginx命令的目錄,如Nginx的啓動命令 │ └── nginx ==> Nginx的啓動文件命令 ├── scgi_temp ==> 臨時文件 └── uwsgi_temp ==> 臨時文件 9 directories, 22 files
十3、Nginx主配置文件框架(nginx.conf)
[root@Qinglin-Test1 conf]# cat nginx.conf user nobody; ==> 指定nginx使用的用戶 worker_processes 1; ==> 指定幾個worker進程有幾個,填寫時參考CPU核數 error_log logs/error.log; ==> 錯誤日誌路徑 error_log logs/error.log notice; ==> 日誌級別警告 error_log logs/error.log info; ==> 日誌信息 pid logs/nginx.pid; ==> pid(進程標識符) events { ==> 事件區塊開始 worker_connections 1024; ==> 每一個work進程支持最大鏈接數,在這裏設置,能夠理解成併發,算並法是worker數*1024 } http { ==> HTTP區塊開始 include mime.types; ==> 指定mime類型,類型mime.type定義 default_type application/octet-stream; ==> 默認的媒體類型 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; ==> 訪問日誌 sendfile on; ==> 開啓高效文件傳輸模式 tcp_nopush on; ==> 防止網絡阻塞 keepalive_timeout 65; ==> 長鏈接超時時間,單位是秒 gzip on; ==> 開啓gzip壓縮輸出 server { ==> 一個server標籤就是一個虛擬機 listen 80; ==> 服務的端口 server_name localhost; ==> 配置域名 access_log logs/host.access.log main; location / { ==> 以/後開始的匹配的URI,好比/123/index.html root html; ==> 存放html的路徑,默認在配置文件中的html裏 index index.html index.htm; ==> 默認的首頁文件,多個空格分開 }
注:若是location頁面爲/50x.html就優化在=號的location裏找。
十4、基於域名配置虛擬主機
將配置文件最少化,grep排除掉備份使用的nginx.conf.default重定向到nginx.cnf,這樣就沒有註釋的內容。
[root@Qinglin-Test1 conf]# grep -Ev "#|^$" nginx.conf.default >nginx.conf [root@Qinglin-Test1 conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
建立兩個不一樣域名虛擬主機,一個是qinglin.com和guanqinglin.com
下列標紅的爲修改的重點內容,一個是指定域名,一個是指定html目錄
[root@Qinglin-Test1 conf]# vim nginx.conf worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.qinglin.com; location / { root html/qinglin; index index.html index.htm; } } server { listen 80; server_name www.guanqinglin.com; location / { root html/guanqinglin; index index.html index.htm; } }
建立root定義的qinglin和guanqinglin的目錄,並建立index.html文件
[root@Qinglin-Test1 conf]# mkdir /application/nginx/html/{qinglin,guanqinglin} -p [root@Qinglin-Test1 conf]# echo "qinglin" >/application/nginx/html/qinglin/index.html [root@Qinglin-Test1 conf]# echo "guanqinglin" >/application/nginx/html/guanqinglin/index.html [root@Qinglin-Test1 conf]# cat /application/nginx/html/qinglin/index.html qinglin [root@Qinglin-Test1 conf]# cat /application/nginx/html/guanqinglin/index.html guanqinglin
檢查語法並平滑重啓服務
[root@Qinglin-Test1 conf]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful [root@Qinglin-Test1 conf]# /application/nginx/sbin/nginx -s reload [root@Qinglin-Test1 conf]#
修改客戶機hosts來校驗結果
注:雖然指定了不一樣域名不一樣主機,可是使用IP訪問,他會給第一個虛擬主機的配置
配置虛擬主機流程
1)複製一個完整的server標籤段,到結尾,注意:要放到http的結束大括號前,也就是server標籤段放入httpd段落中。
2)更改server_name及對應網頁的root根目錄。
3)檢查配置文件語法,平滑重啓。
4)建立server_name 對應網頁的根目錄,而且創建測試文件,若是沒有index首頁會出現404錯誤。
5)在客戶端對server_name的主機名對host解析或DNS配置,並檢查(ping 域名看返回的IP對不對)。
6)win32 瀏覽器訪問,或者在linux客戶端作host解析,用wget或curl訪問。
十4、基於端口配置虛擬主機
配置虛擬主機監聽的端口
[root@Qinglin-Test1 conf]# vim nginx.conf worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8081; server_name www.qinglin.com; location / { root html/qinglin; index index.html index.htm; } } server { listen 8082; server_name www.guanqinglin.com; location / { root html/guanqinglin; index index.html index.htm; } } "nginx.conf" 26L, 594C written
檢查語法並從新啓動
[root@Qinglin-Test1 conf]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful [root@Qinglin-Test1 conf]# /application/nginx/sbin/nginx -s reload [root@Qinglin-Test1 conf]#
檢查端口並測試
[root@Qinglin-Test1 conf]# netstat -lntup|grep 808* tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 4597/nginx tcp 0 0 0.0.0.0:8082 0.0.0.0:* LISTEN 4597/nginx
十5、基於IP配置虛擬主機
[root@Qinglin-Test1 conf]# vim nginx.conf worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 192.168.1.1:80; server_name www.qinglin.com; location / { root html/qinglin; index index.html index.htm; } } server { listen 192.168.1.1:80; server_name www.guanqinglin.com; location / { root html/guanqinglin; index index.html index.htm; } } "nginx.conf" 26L, 594C written