一、編譯安裝部分
1. 執行configure, 個別分支可能沒有configure文件, 可去其他分支拷貝一份
./configure \
--prefix=/usr/local/nginx \ --with-openssl=/usr/local/openssl \ --with-pcre \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_perl_module \ --with-stream \ --with-http_flv_module \ --with-http_mp4_module \ --add-module=../nginx-rtmp-module \ --add-module=../nginx_tcp_proxy_module
2.make
可能會報如下錯誤:
# make make -f objs/Makefile make[1]: 進入目錄“/root/code/nginx” cd /usr/local/openssl/ \ && if [ -f Makefile ]; then make clean; fi \ && ./config --prefix=/usr/local/openssl//.openssl no-shared no-threads \ && make \ && make install_sw LIBDIR=lib /bin/sh:行2: ./config: 沒有那個文件或目錄 make[1]: *** [/usr/local/openssl//.openssl/include/openssl/ssl.h] 錯誤 127 make[1]: 離開目錄“/root/code/nginx” make: *** [build] 錯誤 2
則需要修改配置文件,修改如下路徑文件
nginx/code/nginx/auto/lib/openssl
修改conf文件,將其中的 .openssl/ 去掉
然后再重新configure,make
3.make install
4.查看nginx是否運行成功,在瀏覽器輸入nginx服務器IP地址,顯示如下即成功
[root@baijiayun ~]# ps -ef |grep nginx root 8984 1 0 10:56 ? 00:00:00 nginx: master process /usr/bin/nginx nginx 8985 8984 0 10:56 ? 00:00:00 nginx: worker process root 8987 3060 0 10:56 pts/2 00:00:00 grep --color=auto nginx
5.nginx常用命令
啟動 :/usr/local/nginx/sbin/nginx 檢查配置文件:/usr/local/nginx/sbin/nginx -t 重載配置文件:/usr/local/nginx/sbin/nginx -s reload 重啟 : /usr/local/nginx/sbin/nginx -s reopen 停止 :/usr/local/nginx/sbin/nginx -s stop
6.附nginx配置
user root; worker_processes 1; error_log logs/error.log crit; pid logs/nginx.pid; worker_rlimit_nofile 100000; #更改worker進程的最大打開文件數限制 #如果沒設置的話, 這個值為操作系統的限制. #設置后你的操作系統和Nginx可以處理比“ulimit -a”更多的文件 #所以把這個值設高, 這樣nginx就不會有“too many open files”問題了 events { worker_connections 1024; #設置可由一個worker進程同時打開的最大連接數 #如果設置了上面提到的worker_rlimit_nofile, 我們可以將這個值設得很高 } http { include mime.types; 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"'; #json格式日志 log_format logstash_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"agent":"$http_user_agent",' '"status":"$status"}'; access_log logs/access.log logstash_json; sendfile on; #開啟高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來 輸出文件, #對於普通應用設為 on, #如果用來進行下載等應用磁盤IO重負載應用,可設置 為off, #以平衡磁盤與網絡I/O處理速度,降低系統的負載。 #注意:如果圖片顯示不正常 把這個改成off tcp_nopush on; #防止網絡阻塞 tcp_nodelay on; #防止網絡阻塞 #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream mypull{ server 127.30.100.160; } server { listen 80; #server_name localhost; location ~* \.m3u8$ { proxy_pass http://mypull; } location / { root "/soft/cache"; proxy_store on; proxy_store_access user:rw group:rw all:r; proxy_temp_path "/soft/cache"; if ( !-f $request_filename ) { proxy_pass http://mypull; } proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 20m; client_body_buffer_size 128M; proxy_connect_timeout 120; proxy_read_timeout 120; proxy_buffer_size 1M; proxy_buffers 6 128M; proxy_busy_buffers_size 256M; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name localhost; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx-rtmp-module/; } location /hls { #這里也是需要添加的字段。 types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias /tmp/mgclient/hls/; #root /tmp/mgclient; expires -1; add_header Cache-Control no-cache; } location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } rtmp_auto_push on; rtmp { server { listen 1935; #chunk_size 4000; buflen 10s; #設置默認緩沖區長度,通常客戶端set_buflen在播放之前發送RTMP 命令,並重置此設置 #默認是1000 ms application myapp { live on; max_connections 1024; } application mgclient { live on; hls on; hls_fragment 1s; hls_nested on; #on | off 切換HLS嵌套模式. #在此模式下, hls_path為每個流創建一個子目錄 #播放列表和片段在該子目錄中創建. 默認為關閉 wait_key on; #對視頻切片進行保護,這樣就不會產生馬賽克了。 hls_fragment_naming sequential; #設置片段的命名方式 #sequential: 使用增加的整數 #timestamp : 使用流時間戳 #system : 使用系統時間 #hls_playlist_length 30s; #總共可以回看的事件,這里設置的是30s hls_continuous on; #打開HLS連續模式, #在這種模式下,HLS序列號從上次停止的地方開始. #老片段被保存, 默認為關閉。 hls_cleanup on; #on|off 默認是開着的,是否刪除列表中已經沒有的媒體塊 hls_sync 100ms; sync 10ms; meta on; pull rtmp://127.30.100.160; #hls_path /tmp/mgclient/hls/; } application live { live on; drop_idle_publisher 10s; hls on; hls_path /tmp/live; hls_fragment 3s; } application hls{ live on; hls on; hls_path /tmp/mgclient/hls/; hls_nested on; #on | off 切換HLS嵌套模式. #在此模式下, hls_path為每個流創建一個子目錄 #播放列表和片段在該子目錄中創建. 默認為關閉 hls_fragment 1s; wait_key on; #對視頻切片進行保護,這樣就不會產生馬賽克了。 hls_fragment_naming sequential; #設置片段的命名方式 #sequential: 使用增加的整數 #timestamp : 使用流時間戳 #system : 使用系統時間 #hls_playlist_length 30s; #總共可以回看的事件,這里設置的是30s hls_continuous on; #打開HLS連續模式, #在這種模式下,HLS序列號從上次停止的地方開始. #老片段被保存, 默認為關閉。 hls_cleanup on; #on|off 默認是開着的,是否刪除列表中已經沒有的媒體塊 hls_sync 100ms; sync 10ms; meta on; #pull rtmp://127.30.100.160; } } }
7.使用ffmpeg推流到nginx
推一個本地的mp4到上面配置的myapp上:
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.30.100.180:1935/myapp/test1"
流播放地址為(127.30.100.180是我搭建nginx服務器的IP):ffplay rtmp://127.30.100.180:1935/myapp/test1
推一個本地的mp4到hls上:
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.30.100.180:1935/hls/test2"
---------------------------------------------------------------------
二、可能遇到的問題
1.當nginx -s reload時出現如下錯誤:
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
則,需要使用nginx -c的參數指定nginx.conf文件的位置,即:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
此時,重新執行:nginx -s reload即可
查看nginx綁定端口號:
#ss -tunl Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 *:1935 *:* tcp LISTEN 0 128 *:80 *:* tcp LISTEN 0 128 *:22 *:* tcp LISTEN 0 100 127.0.0.1:25 *:* tcp LISTEN 0 128 :::22 :::* tcp LISTEN 0 100 ::1:25 :::*
2.啟動nginx時,可能遇到:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
定位方法
1.先使用ps -e | grep nginx查看是否已經啟動了nginx
2.如果沒有的話則按照提示,查看0.0.0.0:80端口誰占用了,使用netstat -ltunp命令,可以看到0.0.0.0:80端口 被某個進程占用了
執行:kill pid (占用80端口的進程),然后重新啟動nginx即可
3. ./configure: error: C compiler cc is not found缺少gcc編譯器。
解決方法:安裝gcc編譯器
yum -y install gcc-c++ autoconf automake
4. ./configure: error: the HTTP rewrite module requires the PCRE library.確少PCRE庫.
解決辦法:安裝PCRE庫
yum -y install pcre pcre-devel
5. ./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using --without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-http_ssl_module --with-openssl=<path> options. 缺少ssl錯誤。
解決方法:安裝openssl
yum -y install openssl openssl-devel
6. ./configure: error: the HTTP gzip module requires the zlib library. 缺少zlib庫
解決辦法:安裝zlib庫
yum install -y zlib-devel
7. ./configure: error: the HTTP XSLT module requires the libxml2/libxslt 缺少libxml2
yum -y install libxml2 libxml2-dev && yum -y install libxslt-devel
8. ./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
yum -y install gd-devel
9. ./configure: error: perl module ExtUtils::Embed is required 缺少ExtUtils
yum -y install perl-devel perl-ExtUtils-Embed
10. ./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. 缺少GeoIP
yum -y install GeoIP GeoIP-devel GeoIP-data
三、將nginx加入環境變量
直接使用:nginx -v時,可能會報錯:
nginx -v顯示 "-bash: nginx: 未找到命令"
此時,需要在配置文件添加路徑:
vim /etc/profile
添加:
export PATH="/usr/local/nginx/sbin:$PATH"
然后:source /etc/profile 使之生效,此時,nginx -v即可顯示版本號
[root@baijiayun ~]# nginx -v nginx version: nginx/1.15.3