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:///arut/nginx-rtmp-module。
使用git命令进行下载:
git clone https:///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.2m
4 ./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:///pub/software/programming/pcre/pcre2-10.21.tar.gz
wget ftp:///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
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 -t
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进行访问,如果出现,则证明配置成功。

















