① Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特色是佔有內存少,併發能力強。nginx
② Nginx做爲Http服務器,有如下幾項基本特徵:git
b.1 處理靜態文件,索引文件以及自動索引,打開文件描述符緩衝。github
b.2 無緩存的反向代理加速,簡單的負載均衡和容錯正則表達式
b.3 模塊化的結構,包括gzipping,byte ranges,chunked responses以及SSI-filter等filter,若是由FastCGI或其它代理服務器處理蛋液中存在的多個SSI,則這項處理能夠並行運行,而不須要相互等待。算法
b.4 支持SSL和TLSSNI。緩存
③ nginx官方網址:http://nginx.org/安全
若是須要搭建直播流媒體服務器,則能夠在安裝nginx的同時指定一下rtmp-module,這樣安裝好的nginx服務器就具有直播的功能。服務器
nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module。併發
使用git命令進行下載:負載均衡
git clone https://github.com/arut/nginx-rtmp-module.git
注: 若是沒有安裝git,能夠經過命令 yum groupinstall "Development Tools" 安裝相關的軟件
nginx擁有ssl功能,gzip模塊和rewrite模塊,所以安裝nginx的時候須要先安裝openssl庫,zlib庫,pcre庫。
說明: OpenSSL 是一個安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。
官網地址:https://www.openssl.org/source/
安裝步驟:
1 wget https://www.openssl.org/source/openssl-1.0.2m.tar.gz 2 tar -zxvf openssl-1.0.2m.tar.gz 3 cd openssl-1.0.2m 4 ./config 5 make 6 make install
說明:zlib是提供數據壓縮用的函式庫,由Jean-loup Gailly與Mark Adler所開發,第一版0.9版在1995年5月1日發表。zlib使用DEFLATE算法,最初是爲libpng函式庫所寫的,後來廣泛爲許多軟件所使用。此函式庫爲自由軟件,使用zlib受權。
官方網址:http://www.zlib.net/
安裝步驟:
1 wget http://www.zlib.net/zlib-1.2.11.tar.gz 2 tar -zxvf zlib-1.2.11.tar.gz 3 cd zlib-1.2.11 4 ./configure 5 make 6 make install
說明: PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。這些在執行正規表達式模式匹配時用與Perl 5一樣的語法和語義是頗有用的。Boost太龐大了,使用boost regex後,程序的編譯速度明顯變慢。測試了一下,一樣一個程序,使用boost::regex編譯時須要3秒,而使用pcre不到1秒。所以改用pcre來解決C語言中使用正則表達式的問題。
官方網址:http://www.pcre.org/
安裝步驟:
1 wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.21.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
2 tar -xzvf pcre2-10.21.tar.gz 3 cd pcre2-10.21 4 ./configure 5 make 6 make install
安裝完成nginx前置要求事後,就能夠開始安裝nginx了。
安裝步驟:
1 wget http://nginx.org/download/nginx-1.13.6.tar.gz 2 tar -xzvf nginx-1.13.6.tar.gz 3 cd nginx-1.13.6 4 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_flv_module --add-module=/usr/local/nginx/buildpath/nginx-rtmp-module 5 make 6 make install
注: 編譯時指定了用戶,目錄,若是不須要直播,能夠將最後面的--add-module去掉
1 cd /usr/local/nginx/ 2 sbin/nginx -t
3 sbin/nginx
若是啓動nginx時出現錯誤:
這個是因爲沒有建立nginx的用戶,建立用戶:
1 useradd -s /sbin/nologin -M nginx 2 id nginx
① 使用命令 vi /etc/init.d/nginx,開發編輯器,輸入內容:
1 #!/bin/sh 2 # chkconfig: 2345 85 15 3 # Startup script for the nginx Web Server 4 # description: nginx is a World Wide Web server. 5 # It is used to serve HTML files and CGI. 6 # processname: nginx 7 # pidfile: /usr/local/nginx/logs/nginx.pid 8 # config: /usr/local/nginx/conf/nginx.conf 9 10 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 11 DESC="nginx deamon" 12 NAME=nginx 13 DAEMON=/usr/local/nginx/sbin/$NAME 14 SCRIPTNAME=/etc/init.d/$NAME 15 16 test -x $DAEMON || exit 0 17 18 d_start(){ 19 $DAEMON || echo -n "already running" 20 } 21 22 d_stop(){ 23 $DAEMON -s quit || echo -n "not running" 24 } 25 26 27 d_reload(){ 28 $DAEMON -s reload || echo -n "can not reload" 29 } 30 31 case "$1" in 32 start) 33 echo -n "Starting $DESC: $NAME" 34 d_start 35 echo "." 36 ;; 37 stop) 38 echo -n "Stopping $DESC: $NAME" 39 d_stop 40 echo "." 41 ;; 42 reload) 43 echo -n "Reloading $DESC conf..." 44 d_reload 45 echo "reload ." 46 ;; 47 restart) 48 echo -n "Restarting $DESC: $NAME" 49 d_stop 50 sleep 2 51 d_start 52 echo "." 53 ;; 54 *) 55 echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2 56 exit 3 57 ;; 58 esac 59 60 exit 0
② 接着使用命令:
chmod +x /etc/init.d/nginx chkconfig --add nginx chkconfig nginx on
③ 使用命令查看是否啓動正常
service nginx start # 啓動
service nginx stop # 關閉
開通防火牆,運行nginx端口經過。
而後經過ip進行訪問,若是出現,則證實配置成功。