搭建nginx服務器和直播流媒體服務器
1、nginx簡單說明
① Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特點是占有內存少,並發能力強。
② Nginx作為Http服務器,有以下幾項基本特征:
b.1 處理靜態文件,索引文件以及自動索引,打開文件描述符緩沖。
b.2 無緩存的反向代理加速,簡單的負載均衡和容錯
b.3 模塊化的結構,包括gzipping,byte ranges,chunked responses以及SSI-filter等filter,如果由FastCGI或其它代理服務器處理蛋液中存在的多個SSI,則這項處理可以並行運行,而不需要相互等待。
b.4 支持SSL和TLSSNI。
③ nginx官方網址:http://nginx.org/
2、下載nginx-rtmp-module
如果需要搭建直播流媒體服務器,則可以在安裝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庫。
3、安裝openssl庫
說明: 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.2m4 ./config 5 make 6 make install
4、安裝zlib庫
說明: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
5、安裝pcre庫
說明: 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 2 tar -xzvf pcre2-10.21.tar.gz 3 cd pcre2-10.21 4 ./configure 5 make 6 make install
6、下載nginx
安裝完成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去掉
7、啟動查看nginx是否正常
1 cd /usr/local/nginx/ 2 sbin/nginx -s 3 sbin/nginx
如果啟動nginx時出現錯誤:
這個是由於沒有創建nginx的用戶,創建用戶:
1 useradd -s /sbin/nologin -M nginx 2 id nginx
8、將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 # 關閉
9、外網訪問nginx
開通防火墻,運行nginx端口通過。
然後通過ip進行訪問,如果出現,則證明配置成功。
搭建nginx服務器和直播流媒體服務器