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

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

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

0。描述


Nginx作为一款优秀的Web服务器软件,也是一款优秀的负载均衡或前端反向代理和缓存服务软件。需要搭建一个实验环境来借鉴。



1。实验环境


本实验的测试环境使用的主机 *** 作系统为Windows7,Vmware虚拟机安装CentOS6.5,解释如下:

  • 主机 *** 作系统Windows7

  • 虚拟机安装的 *** 作系统CentOS6.5

    虚拟机 *** 作系统通过NAT访问互联网

    而使用NAT上网时,虚拟机与主机的网络连接关系可以是:

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

    另外需要注意的是,这里安装的CentOS6.5 *** 作系统采用最小安装,只定制安装了gcc等一些常用的开发工具。版本信息如下:

    [root@leaf ~]# cat /etc/redhat-release  CentOS release 6.5 (Final) [root@leaf ~]# uname -r 2.6.32-431.el6.x86_64 [root@leaf ~]# uname -m x86_64



    2。编译并安装Nginx


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

    Pcre是“perl兼容正则表达式”,安装它是为了让Nginx支持具有URI重写功能的重写模块。如果没有安装Nginx,重写模块功能无法使用,但是很有用,很常用。

    检查系统中是否有安装:

    [root@leaf ~]# rpm -q pcre pcre-devel

    从上面可以看出没有安装。使用yum安装它,如下所示:

    [root@leaf ~]# 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@leaf ~]# 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@leaf ~]# rpm -q openssl openssl-devel  openssl-1.0.1e-15.el6.x86_64 package openssl-devel is not installed

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

    [root@leaf ~]# yum install -y openssl-devel ...... Complete!

    再次检查:

    [root@leaf ~]# 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@leaf ~]# pwd /root [root@leaf ~]# mkdir tools [root@leaf ~]# cd tools/ [root@leaf 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@leaf 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@leaf ~]# mkdir /application [root@leaf ~]# ls -d /application/ /application/
  • 使减压

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

    [root@leaf tools]# tar -zxvf nginx-1.6.3.tar.gz ...... [root@leaf tools]# ls nginx-1.6.3  nginx-1.6.3.tar.gz
  • 使用。/configure指定编译参数。

  • 安装后先创建一个nginx用户运行nginx:

    [root@leaf tools]# useradd nginx -s /sbin/nologin -M [root@leaf tools]# tail -1 /etc/passwd nginx:x:500:500::/home/nginx:/sbin/nologin # -s参数后的/sbin/nologin指定不允许nginx进行登陆 # -M参数则是在创建该用户时不创建用户家目录

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

    [root@leaf 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编译

  • [[email protected]]#make ... 检查编译是否成功:

    [root@leaf nginx-1.6.3]# echo $? 0

    返回0表示编译成功。

  • 使用makeinstall来安装

  • [[email protected]]#makeinstall ... 检查安装是否成功:

    [root@leaf nginx-1.6.3]# echo $?      0

    0表示安装成功。

  • 建立到安装目录的软链接。

  • [[email protected]]#ln-s/application/nginx-1.6.3//application/nginx [[email protected]]#ls-l/application/ 共4个 lrwxrwxrwx。1根根25年2月24日12:32nginx->/application/nginx-1.6.3/ drwxr-xr-x.6rootroot4096Feb2412:28nginx-1.6.3

    Nginx的编译安装已经完成,需要验证安装结果,即Nginx是否能正常提供服务。



    3。测试Nginx服务


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

    如下:

    [root@leaf ~]# /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@leaf ~]# /application/nginx/sbin/nginx

    如果启动Nginx服务时出现问题,可以查看Nginx/application/Nginx/logs/error.log的日志,然后根据日志提供的信息解决。


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

  • 检查已打开端口的信息。

  • [root@leaf~]#netstat-LNP|grep80 TCP000.0.0.0:800.0.0:*listEN6772/NGX UNIX2[ACC]streaming91801/init@/com/Ubuntu/Upstart 可以看到Nginx已经在监听80端口了。

  • 查看Nginx流程

  • [root@leaf~]#PSaux|grepnginx root67720.00.1450281140?Ss12:340:00nginx:主进程/应用/nginx/sbin/nginx nginx67730.00.1454601716?S12:340:00nginx:workerprocess root67770.00103256832pts/1s+12:360:00grepnginx 在主机上使用浏览器进行了测试。

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

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

  • 使用wget命令和curl命令进行测试。

  • wget命令:

    [root@leaf 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@leaf 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@leaf tools]# cd /application/nginx/html/ [root@leaf html]# ls 50x.html  index.html [root@leaf html]# mv index.html index.html.source [root@leaf html]# echo "<h1>Hello, I'm xpleaf.</h1>">index.html [root@leaf html]# ls 50x.html  index.html  index.html.source [root@leaf html]# cat index.html <h1>Hello, I'm xpleaf.</h1>

    此时,在主机 *** 作系统上访问http://10.0.0.101/

    你可以看到我们编辑过的页面。



    5。实际场景中的应用


    Nginx无论是用于学习还是生产都是非常重要的,好的开始是成功的一半,所以第一步当然是构建好的Nginx服务。



    6。引用


    向老男孩学习Linux运维Web集群

    http://nginx.org



    欢迎分享,转载请注明来源:内存溢出

    原文地址: http://outofmemory.cn/zz/777747.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-05-03
    下一篇 2022-05-03

    发表评论

    登录后才能评论

    评论列表(0条)

    保存