在CentOS上编译安装Nginx+实验环境搭建+测试

前端之家收集整理的这篇文章主要介绍了在CentOS上编译安装Nginx+实验环境搭建+测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

0.说明


Nginx作为一款优秀的Web Server软件同时也是一款优秀的负载均衡或前端反向代理、缓存服务软件,很有必要搭建实验环境来对其进行学习。




1.实验环境


本次实验的测试环境使用的宿主机操作系统为Windows 7,在Vmware虚拟机安装CentOS 6.5,说明如下:

  • 宿主机操作系统Windows 7

  • 虚拟机安装的操作系统CentOS 6.5

  • 虚拟机操作系统上网方式NAT

而当使用NAT的方式进行上网时虚拟机、宿主机之间的网络连接关系可如下所示:

wKioL1iw1zWg5yJtAABgyKpZGYA370.png

关于为什么网络拓扑结构是这样的,这里不展开说明,可以参考博主的另一篇博文《在实践中深入理解VMware虚拟机的上网模式NAT模式》,这篇文章深入地分析了VMware虚拟机使用NAT模式上网时的网络结构细节,相信看完这篇文章后,这里搭建Nginx的实验环境也就很容易理解了。

另外需要注意的是这里安装的CentOS 6.5操作系统使用了最小化安装,并且只定制安装了一些常用的开发工具如gcc等,其版本信息如下:

  1. [root@leaf~]#cat/etc/redhat-release
  2. CentOSrelease6.5(Final)
  3. [root@leaf~]#uname-r
  4. 2.6.32-431.el6.x86_64
  5. [root@leaf~]#uname-m
  6. x86_64




2.编译安装Nginx


(1)安装Nginx依赖函数库pcre

pcre为“perl兼容正则表达式”perl compatible regular expresssions,安装其是为了使Nginx支持具备URI重写功能的rewrite模块,如果不安装Nginx将无法使用rewrite模块功能,但是该功能却十分有用和常用。

检查系统中是否有安装:

  1. [root@leaf~]#rpm-qpcrepcre-devel

上面可以看到并没有安装使用yum方式安装如下:

  1. [root@leaf~]#yuminstallpcrepcre-devel-y
  2. ......
  3.  
  4. Installed:
  5. pcre-devel.x86_640:7.8-7.el6
  6.  
  7. Updated:
  8. pcre.x86_640:7.8-7.el6
  9.  
  10. Complete!

安装完后检查一下是否已经成功安装:

  1. [root@leaf~]#rpm-qpcrepcre-devel
  2. pcre-7.8-7.el6.x86_64
  3. pcre-devel-7.8-7.el6.x86_64

可以看到已经安装成功。


(2)安装Nginx依赖函数库openssl-devel

Nginx在使用HTTPS服务的时候要用到此模块,如果不安装openssl相关包,安装过程中是会报错的。

检查系统是否有安装openssl相关包:

  1. [root@leaf~]#rpm-qopensslopenssl-devel
  2. openssl-1.0.1e-15.el6.x86_64
  3. packageopenssl-develisnotinstalled

可以看到只是安装了opensslopenssl-devel还没有安装使用yum安装如下:

  1. [root@leaf~]#yuminstall-yopenssl-devel
  2. ......
  3.  
  4. Complete!

再次检查:

  1. [root@leaf~]#rpm-qopensslopenssl-devel
  2. openssl-1.0.1e-48.el6_8.4.x86_64
  3. openssl-devel-1.0.1e-48.el6_8.4.x86_64

可以看到都已经成功安装上。


(3)下载Nginx软件包

这里使用的Nginx版本为1.6.3,下载方式如下:

  1. [root@leaf~]#pwd
  2. /root
  3. [root@leaf~]#mkdirtools
  4. [root@leaf~]#cdtools/
  5. [root@leaftools]#wgethttp://Nginx.org/download/Nginx-1.6.3.tar.gz
  6. ......
  7. 100%[======================================>]805,253220K/sin3.6s
  8.  
  9. 2017-02-2412:10:26(220KB/s)-aNginx-1.6.3.tar.gzasaved[805253/805253]

查看下载的Nginx软件包:

  1. [root@leaftools]#ll
  2. total788
  3. -rw-r--r--.1rootroot805253Apr82015Nginx-1.6.3.tar.gz

当然上面的方式是使用wget方式直接下载,前提是已经知道了Nginx的下载地址,也可以到官网下载,然后再上传到我们的CentOS操作系统上。


(4)开始安装Nginx

可以先在根目录下创建一个/application文件夹用来存放我们安装的软件:

  1. [root@leaf~]#mkdir/application
  2. [root@leaf~]#ls-d/application/
  3. /application/
  • 解压缩

将我们刚刚下载的Nginx软件包解压缩:

  1. [root@leaftools]#tar-zxvfNginx-1.6.3.tar.gz
  2. ......
  3. [root@leaftools]#ls
  4. Nginx-1.6.3Nginx-1.6.3.tar.gz
  • 使用./configure指定编译参数

先创建一个Nginx用户用来安装完成后运行Nginx使用:

  1. [root@leaftools]#useraddNginx-s/sbin/nologin-M
  2. [root@leaftools]#tail-1/etc/passwd
  3. Nginx:x:500:500::/home/Nginx:/sbin/nologin
  4.  
  5. #-s参数后的/sbin/nologin指定不允许Nginx进行登陆
  6. #-M参数则是在创建该用户时不创建用户家目录

使用configure命令指定编译参数:

  1. [root@leafNginx-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来进行查询,上面使用的参数解析如下:

  1. --prefix=PATH#指定安装路径
  2. --user=USER#设置用户进程权限
  3. --group=GROUP#设置用户组进程权限
  4. --with-http_stub_status_module#激活状态信息
  5. --with-http_ssl_module#激活ssl功能
  • 使用make进行编译

  1. [root@leafNginx-1.6.3]#make
  2. ......

检查编译是否成功:

  1. [root@leafNginx-1.6.3]#echo$?
  2. 0

返回0即说明编译成功。

  • 使用make install安装

  1. [root@leafNginx-1.6.3]#makeinstall
  2. ......

检查安装是否成功:

  1. [root@leafNginx-1.6.3]#echo$?
  2. 0

返回0即说明安装成功。

  • 建立安装目录的软链接

  1. [root@leafNginx-1.6.3]#ln-s/application/Nginx-1.6.3//application/Nginx
  2. [root@leafNginx-1.6.3]#ls-l/application/
  3. total4
  4. lrwxrwxrwx.1rootroot25Feb2412:32Nginx->/application/Nginx-1.6.3/
  5. drwxr-xr-x.6rootroot4096Feb2412:28Nginx-1.6.3


到此Nginx的编译安装工作已经全部完成了,下面就需要对安装结果进行验证了即验证Nginx是否可以正常提供服务。




3.测试Nginx服务


(1)启动Nginx服务前检查配置文件语法

如下:

  1. [root@leaf~]#/application/Nginx/sbin/Nginx-t
  2. Nginx:theconfigurationfile/application/Nginx-1.6.3//conf/Nginx.confSyntaxisok
  3. Nginx:configurationfile/application/Nginx-1.6.3//conf/Nginx.conftestissuccessful


(2)启动Nginx服务

  1. [root@leaf~]#/application/Nginx/sbin/Nginx

如果在启动Nginx服务时出现了问题可以查看Nginx的日志/application/Nginx/logs/error.log,再根据日志提供的信息来进行解决


(3)验证Nginx服务是否正常

  • 查看已开启的端口信息

  1. [root@leaf~]#netstat-lnp|grep80
  2. tcp000.0.0.0:800.0.0.0:*LISTEN6772/Nginx
  3. unix2[ACC]STREAMLISTENING91801/init@/com/ubuntu/upstart

可以看到Nginx已经在侦听80端口。

  1. [root@leaf~]#psaux|grepNginx
  2. root67720.00.1450281140?Ss12:340:00Nginx:masterprocess/application/Nginx/sbin/Nginx
  3. Nginx67730.00.1454601716?S12:340:00Nginx:workerprocess
  4. root67770.00.0103256832pts/1S+12:360:00grepNginx
  • 在宿主机上使用浏览器进行测试

在我们宿主机的浏览器上输入http://10.0.0.101/,查看测试结果

wKioL1iw4rCToOMpAAAsJBgtzOA400.png

可以正常访问,当然前提是CentOS上的防火墙功能已经关闭

  • 使用wget命令和curl命令测试

wget命令:

  1. [root@leaftools]#wget127.0.0.1
  2. --2017-02-2412:41:05--http://127.0.0.1/
  3. Connectingto127.0.0.1:80...connected.
  4. HTTPrequestsent,awaitingresponse...200OK
  5. Length:612[text/html]
  6. Savingto:aindex.htmla
  7.  
  8. 100%[======================================>]612--.-K/sin0s
  9.  
  10. 2017-02-2412:41:05(44.1MB/s)-aindex.htmlasaved[612/612]

currl命令:

  1. [root@leaftools]#curl127.0.0.1
  2. <!DOCTYPEhtml>
  3. <html>
  4. <head>
  5. <title>WelcometoNginx!</title>
  6. <style>
  7. body{
  8. width:35em;
  9. margin:0auto;
  10. font-family:Tahoma,Verdana,Arial,sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>WelcometoNginx!</h1>
  16. <p>Ifyouseethispage,theNginxwebserverissuccessfullyinstalledand
  17. working.Furtherconfigurationisrequired.</p>
  18.  
  19. <p>Foronlinedocumentationandsupportpleasereferto
  20. <ahref="http://Nginx.org/">Nginx.org</a>.<br/>
  21. Commercialsupportisavailableat
  22. <ahref="http://Nginx.com/">Nginx.com</a>.</p>
  23.  
  24. <p><em>ThankyouforusingNginx.</em></p>
  25. </body>
  26. </html>

从上面的结果可以说明Nginx已经正常部署并运行。




4.进一步测试修改Nginx显示页面


通过修改/application/Nginx/html下的index.html文件,我们就可以改变Nginx主页显示内容,操作如下:

  1. [root@leaftools]#cd/application/Nginx/html/
  2. [root@leafhtml]#ls
  3. 50x.htmlindex.html
  4. [root@leafhtml]#mvindex.htmlindex.html.source
  5. [root@leafhtml]#echo"<h1>Hello,I'mxpleaf.</h1>">index.html
  6. [root@leafhtml]#ls
  7. 50x.htmlindex.htmlindex.html.source
  8. [root@leafhtml]#catindex.html
  9. <h1>Hello,I'mxpleaf.</h1>

这时在宿主机操作系统上访问http://10.0.0.101/

wKioL1iw5QmR_kwsAAAHDYS1ylo571.png

可以看到已经显示我们编辑的页面了。




5.在实际场景中的应用


不管是用于学习还是在生产环境中使用,Nginx都十分重要,而好的开始是成功的一半,所以第一步当然是要把Nginx服务搭建好。




6.参考资料


《跟老男孩学Linux运维Web集群实战》

http://Nginx.org

猜你在找的CentOS相关文章