共计 6998 个字符,预计需要花费 18 分钟才能阅读完成。
0. 说明
Nginx 作为一款优秀的 Web Server 软件同时也是一款优秀的负载均衡或前端反向代理、缓存服务软件,很有必要搭建实验环境来对其进行学习。
1. 实验环境
本次实验的测试环境使用的宿主机操作系统为 Windows 7,在 Vmware 虚拟机安装 CentOS 6.5,说明如下:
宿主机操作系统 Windows 7
虚拟机安装的操作系统 CentOS 6.5
虚拟机操作系统上网方式 NAT
而当使用 NAT 的方式进行上网时虚拟机、宿主机之间的网络连接关系可如下所示:

关于为什么网络拓扑结构是这样的,这里不展开说明,可以参考《VMware 虚拟机网络模式详解 -NAT 模式》,这篇文章深入地分析了 VMware 虚拟机使用 NAT 模式上网时的网络结构细节,相信看完这篇文章后,这里搭建 Nginx 的实验环境也就很容易理解了。
另外需要注意的是这里安装的 CentOS 6.5 操作系统使用了最小化安装,并且只定制安装了一些常用的开发工具如 gcc 等,其版本信息如下:
[root@linuxidc ~]# cat /etc/RedHat-release CentOS release 6.5 (Final) [root@linuxidc ~]# uname -r 2.6.32-431.el6.x86_64 [root@linuxidc ~]# uname -m x86_642. 编译安装 Nginx
(1)安装 Nginx 依赖函数库 pcre
pcre 为“perl 兼容正则表达式”perl compatible regular expresssions, 安装其是为了使 Nginx 支持具备 URI 重写功能的 rewrite 模块,如果不安装 Nginx 将无法使用 rewrite 模块功能,但是该功能却十分有用和常用。
检查系统中是否有安装:
[root@linuxidc ~]# rpm -q pcre pcre-devel上面可以看到并没有安装使用 yum 方式安装如下:
[root@linuxidc ~]# yum install pcre pcre-devel -y ...... Installed: pcre-devel.x86_64 0:7.8-7.el6 Updated: pcre.x86_64 0:7.8-7.el6 Complete!安装完后检查一下是否已经成功安装:
[root@linuxidc ~]# rpm -q pcre pcre-devel pcre-7.8-7.el6.x86_64 pcre-devel-7.8-7.el6.x86_64可以看到已经安装成功。
(2)安装 Nginx 依赖函数库 openssl-devel
Nginx 在使用 HTTPS 服务的时候要用到此模块,如果不安装 openssl 相关包,安装过程中是会报错的。
检查系统是否有安装 openssl 相关包:
[root@linuxidc ~]# rpm -q openssl openssl-devel openssl-1.0.1e-15.el6.x86_64 package openssl-devel is not installed可以看到只是安装了 opensslopenssl-devel 还没有安装使用 yum 安装如下:
[root@linuxidc ~]# yum install -y openssl-devel ...... Complete!再次检查:
[root@linuxidc ~]# rpm -q openssl openssl-devel openssl-1.0.1e-48.el6_8.4.x86_64 openssl-devel-1.0.1e-48.el6_8.4.x86_64可以看到都已经成功安装上。
(3)下载 Nginx 软件包
这里使用的 Nginx 版本为 1.6.3,下载方式如下:
[root@linuxidc ~]# pwd /root[root@linuxidc ~]# mkdir tools [root@linuxidc ~]# cd tools/ [root@linuxidc tools]# wget http://nginx.org/download/nginx-1.6.3.tar.gz ...... 100%[======================================>] 805,253 220K/s in 3.6s 2017-02-24 12:10:26 (220 KB/s) - anginx-1.6.3.tar.gza saved [805253/805253]查看下载的 Nginx 软件包:
[root@linuxidc tools]# ll total 788 -rw-r--r--. 1 root root 805253 Apr 8 2015 nginx-1.6.3.tar.gz当然上面的方式是使用 wget 方式直接下载,前提是已经知道了 Nginx 的下载地址,也可以到官网下载,然后再上传到我们的 CentOS 操作系统上。
(4)开始安装 Nginx
可以先在根目录下创建一个 /application 文件夹用来存放我们安装的软件:
[root@linuxidc ~]# mkdir /application [root@linuxidc ~]# ls -d /application/ /application/解压缩
将我们刚刚下载的 Nginx 软件包解压缩:
[root@linuxidc tools]# tar -zxvf nginx-1.6.3.tar.gz ...... [root@linuxidc tools]# ls nginx-1.6.3 nginx-1.6.3.tar.gz使用./configure 指定编译参数
先创建一个 nginx 用户用来安装完成后运行 nginx 使用:
[root@linuxidc tools]# useradd nginx -s /sbin/nologin -M [root@linuxidc tools]# tail -1 /etc/passwd nginx:x:500:500::/home/nginx:/sbin/nologin # - s 参数后的 /sbin/nologin 指定不允许 nginx 进行登陆 # - M 参数则是在创建该用户时不创建用户家目录 使用 configure 命令指定编译参数:
[root@linuxidc nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module对于配置时使用的参数可以通过./configure –help 来进行查询,上面使用的参数解析如下:
--prefix=PATH # 指定安装路径 --user=USER # 设置用户进程权限 --group=GROUP # 设置用户组进程权限 --with-http_stub_status_module # 激活状态信息 --with-http_ssl_module # 激活 ssl 功能 使用 make 进行编译
[root@linuxidc nginx-1.6.3]# make ......检查编译是否成功:
[root@linuxidc nginx-1.6.3]# echo $? 0返回 0 即说明编译成功。
使用 make install 安装
[root@linuxidc nginx-1.6.3]# make install ......检查安装是否成功:
[root@linuxidc nginx-1.6.3]# echo $? 0返回 0 即说明安装成功。
建立安装目录的软链接
[root@linuxidc nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx [root@linuxidc nginx-1.6.3]# ls -l /application/ total 4 lrwxrwxrwx. 1 root root 25 Feb 24 12:32 nginx -> /application/nginx-1.6.3/ drwxr-xr-x. 6 root root 4096 Feb 24 12:28 nginx-1.6.3到此 Nginx 的编译安装工作已经全部完成了,下面就需要对安装结果进行验证了即验证 Nginx 是否可以正常提供服务。
3. 测试 Nginx 服务
(1)启动 Nginx 服务前检查配置文件语法
如下:
[root@linuxidc ~]# /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(2)启动 Nginx 服务
[root@linuxidc ~]# /application/nginx/sbin/nginx如果在启动 Nginx 服务时出现了问题可以查看 Nginx 的日志 /application/nginx/logs/error.log,再根据日志提供的信息来进行解决。
(3)验证 Nginx 服务是否正常
查看已开启的端口信息
[root@linuxidc ~]# netstat -lnp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6772/nginx unix 2 [ACC] STREAM LISTENING 9180 1/init @/com/Ubuntu/upstart可以看到 Nginx 已经在侦听 80 端口。
查看 Nginx 进程
[root@linuxidc ~]# ps aux | grep nginx root 6772 0.0 0.1 45028 1140 ? Ss 12:34 0:00 nginx: master process /application/nginx/sbin/nginxnginx 6773 0.0 0.1 45460 1716 ? S 12:34 0:00 nginx: worker process root 6777 0.0 0.0 103256 832 pts/1 S+ 12:36 0:00 grep nginx在宿主机上使用浏览器进行测试
在我们宿主机的浏览器上输入 http://10.0.0.101/,查看测试结果

可以正常访问,当然前提是 CentOS 上的防火墙功能已经关闭。
使用 wget 命令和 curl 命令测试
wget 命令:
[root@linuxidc tools]# wget 127.0.0.1 --2017-02-24 12:41:05-- http://127.0.0.1/ Connecting to 127.0.0.1:80... connected. HTTP request sent, awaiting response... 200 OK Length: 612 [text/html] Saving to: aindex.htmla 100%[======================================>] 612 --.-K/s in 0s 2017-02-24 12:41:05 (44.1 MB/s) - aindex.htmla saved [612/612]currl 命令:
[root@linuxidc tools]# curl 127.0.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>从上面的结果可以说明 Nginx 已经正常部署并运行。
4. 进一步测试修改 Nginx 显示的页面
通过修改 /application/nginx/html 下的 index.html 文件,我们就可以改变 Nginx 主页显示的内容,操作如下:
[root@linuxidc tools]# cd /application/nginx/html/ [root@linuxidc html]# ls 50x.html index.html [root@linuxidc html]# mv index.html index.html.source [root@linuxidc html]# echo "<h1>Hello, I'm linuxidc.</h1>">index.html [root@linuxidc html]# ls 50x.html index.html index.html.source[root@linuxidc html]# cat index.html <h1>Hello, I'm linuxidc.</h1>这时在宿主机操作系统上访问 http://10.0.0.101/

可以看到已经显示我们编辑的页面了。
5. 在实际场景中的应用
不管是用于学习还是在生产环境中使用,Nginx 都十分重要,而好的开始是成功的一半,所以第一步当然是要把 Nginx 服务搭建好。
CentOS 7 下 Nginx 服务器的安装配置 http://www.linuxidc.com/Linux/2017-04/142986.htm
CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向 http://www.linuxidc.com/Linux/2017-04/142642.htm
CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm
Linux 下安装 PHP 环境并配置 Nginx 支持 php-fpm 模块 http://www.linuxidc.com/Linux/2017-05/144333.htm
Nginx 服务的 SSL 认证和 htpasswd 认证 http://www.linuxidc.com/Linux/2017-04/142478.htm
Linux 中安装配置 Nginx 及参数详解 http://www.linuxidc.com/Linux/2017-05/143853.htm
Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm
CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置 http://www.linuxidc.com/Linux/2017-03/142168.htm
CentOS6.9 编译安装 Nginx1.4.7 http://www.linuxidc.com/Linux/2017-06/144473.htm
Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-06/144795.htm






