RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
CentOS上编译安装Nginx+实验环境搭建+测试

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

创新互联公司是一家从事企业网站建设、网站设计制作、成都网站制作、行业门户网站建设、网页设计制作的专业网站建设公司,拥有经验丰富的网站建设工程师和网页设计人员,具备各种规模与类型网站建设的实力,在网站建设领域树立了自己独特的设计风格。自公司成立以来曾独立设计制作的站点千余家。

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

  • 宿主机操作系统Windows 7

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

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

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

    另外需要注意的是这里安装的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_64

(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是否可以正常提供服务。 

(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/nginx
nginx      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


<
head
>
Welcome to nginx!<</code><br /> <code>/title</code><br /> <code>> </code><br /> <code><style> </code><br /> <code>    </code><br /> <code>body { </code><br /> <code>        </code><br /> <code>width: 35em; </code><br /> <code>        </code><br /> <code>margin: 0 auto; </code><br /> <code>        </code><br /> <code>font-family: Tahoma, Verdana, Arial, sans-serif; </code><br /> <code>    </code><br /> <code>} </code><br /> <code><</code><br /> <code>/style</code><br /> <code>> </code><br /> <code><</code><br /> <code>/head</code><br /> <code>> </code><br /> <code><body> </code><br /> <code><h1>Welcome to nginx!<</code><br /> <code>/h1</code><br /> <code>> </code><br /> <code><p>If you see this page, the nginx web server is successfully installed and </code><br /> <code>working. Further configuration is required.<</code><br /> <code>/p</code><br /> <code>> </code><br /> <code> </code> <br /> <code><p>For online documentation and support please refer to </code><br /> <code><a href=</code><br /> <code>"http://nginx.org/"</code><br /> <code>>nginx.org<</code><br /> <code>/a</code><br /> <code>>.<br/> </code><br /> <code>Commercial support is available at </code><br /> <code><a href=</code><br /> <code>"http://nginx.com/"</code><br /> <code>>nginx.com<</code><br /> <code>/a</code><br /> <code>>.<</code><br /> <code>/p</code><br /> <code>> </code><br /> <code> </code> <br /> <code><p><em>Thank you </code><br /> <code>for</code> <br /> <code>using nginx.<</code><br /> <code>/em</code><br /> <code>><</code><br /> <code>/p</code><br /> <code>> </code><br /> <code><</code><br /> <code>/body</code><br /> <code>> </code><br /> <code><</code><br /> <code>/html</code><br /> <code>></code> </p><p>    从上面的结果可以说明Nginx已经正常部署并运行。 </p><p>    通过修改/application/nginx/html下的index.html文件,我们就可以改变Nginx主页显示的内容,操作如下:</p><p><code>[root@linuxidc tools]</code><br /> <code># cd /application/nginx/html/ </code><br /> <code>[root@linuxidc html]</code><br /> <code># ls </code><br /> <code>50x.html  index.html </code><br /> <code>[root@linuxidc html]</code><br /> <code># mv index.html index.html.source </code><br /> <code>[root@linuxidc html]</code><br /> <code># echo "<h1>Hello, I'm linuxidc.</h1>">index.html </code><br /> <code>[root@linuxidc html]</code><br /> <code># ls </code><br /> <code>50x.html  index.html  index.html.</code><br /> <code>source</code><br /> <code>[root@linuxidc html]</code><br /> <code># cat index.html </code><br /> <code><h1>Hello, I'm linuxidc.<</code><br /> <code>/h1</code><br /> <code>></code> </p><p>    这时在宿主机操作系统上访问http://10.0.0.101/</p><p>    可以看到已经显示我们编辑的页面了。</p><p>    不管是用于学习还是在生产环境中使用,Nginx都十分重要,而好的开始是成功的一半,所以第一步当然是要把Nginx服务搭建好。</p> <br> 网站标题:CentOS上编译安装Nginx+实验环境搭建+测试 <br> URL链接:<a href="http://www.jxjierui.cn/article/codghpp.html">http://www.jxjierui.cn/article/codghpp.html</a> </div> <div class="hot_new"> <div class="page_title clearfix"> <h3>其他资讯</h3> </div> <div class="news_list clearfix"> <ul> <li> <a href="/article/dhohpcd.html">PHP函数:getenv</a> </li><li> <a href="/article/dhohpoi.html">了解4GLTE:探究现代通讯技术的奥秘</a> </li><li> <a href="/article/dhohpcc.html">服务器cpu占用过高怎么解决</a> </li><li> <a href="/article/dhohpdj.html">探索Linux系统下分享无线热点(linux系统开热点)</a> </li><li> <a href="/article/dhohpej.html">Linux系统删除:面临灾难怎么办?(删除linux系统)</a> </li> </ul> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> <!-- 底部信息 --> <div class="footer wow fadeInUp"> <div class="rowFluid"> <div class="span12"> <div class="container"> <div class="footer_content"> <div class="span4 col-xm-12"> <div class="footer_list"> <div class="span6"> <div class="bottom_logo"><img src="/Public/Home/images/ewm.jpg" alt="微信服务号二维码" /></div> </div> <div class="span6 col-xm-12"> <div class="quick_navigation"> <div class="quick_navigation_title">快速导航</div> <ul> <li><a href="http://www.cdkjz.cn/fangan/response/" title="响应式网站设计方案" target="_blank">响应式网站设计方案</a></li><li><a href="https://www.cdcxhl.com/qiye.html" title="企业网站建设" target="_blank">企业网站建设</a></li><li><a href="http://www.myfdjzy.com/" title="夏季POLO衫" target="_blank">夏季POLO衫</a></li><li><a href="http://www.cdkjz.cn/fangan/education/" title="教育网站设计方案" target="_blank">教育网站设计方案</a></li><li><a href="http://www.ljwzsj.com/" title="ljwzsj.com" target="_blank">ljwzsj.com</a></li><li><a href="http://www.cdkjz.cn/wangzhan/" title="网站seo优化" target="_blank">网站seo优化</a></li><li><a href="http://m.xwcx.net/phone/" title="手机网站版本" target="_blank">手机网站版本</a></li> </ul> </div> </div> </div> </div> <div class="span4 col-xm-6 col-xs-12"> <div class="footer_list"> <div class="footer_link"> <div class="footer_link_title">友情链接</div> <ul id="frientLinks"> <a href="https://www.cdcxhl.com/" title="网站制作" target="_blank">网站制作</a> <a href="https://www.cdcxhl.com/" title="网站建设" target="_blank">网站建设</a> <a href="https://www.cdxwcx.com/tuiguang/" title="成都网络推广" target="_blank">网络推广</a> <a href="http://seo.cdkjz.cn/" title="成都网站推广" target="_blank">网站推广</a> <a href="https://www.cdcxhl.com/xiaochengx.html" title="成都微信小程序开发" target="_blank">小程序开发</a> <a href="https://www.cdcxhl.com/menu.html" title="创新互联网站栏目导航" target="_blank">网站导航</a> </ul> <div class="footer_link_title">网站建设</div> <ul id="frientLinks"> <li><a href="/">内江浩晨众云建站</a></li> <li><a href="https://www.cdcxhl.com/menu.html" title="创新互联网站栏目导航" target="_blank">网站导航</a></li> </ul> </div> </div> </div> <div class="span4 col-xm-6 col-xs-12"> <div class="footer_list"> <div class="footer_cotact"> <div class="footer_cotact_title">联系方式</div> <ul> <li><span class="footer_cotact_type">企业:</span><span class="footer_cotact_content">青羊区浩晨众云设计工作室(个体工商户)</span></li> <li><span class="footer_cotact_type">地址:</span><span class="footer_cotact_content">成都市青羊区太升南路288号</span></li> <li><span class="footer_cotact_type">电话:</span><span class="footer_cotact_content"><a href="tel:18980820575" class="call">18980820575</a></span></li> <li><span class="footer_cotact_type">网址:</span><span class="footer_cotact_content"><a href="/" title="内江网站建设">www.jxjierui.cn</a></span></li> </ul> </div> </div> </div> </div> </div> <div class="copyright"> <p>公司名称:青羊区浩晨众云设计工作室(个体工商户) 联系电话:18980820575</p> <p><a href="http://beian.miit.gov.cn" target="_blank" rel="nofollow">网站备案号:蜀ICP备2025119795号-2</a></p> <p>内江浩晨众云建站 内江网站建设 内江网站设计 内江网站制作 <a href="http://www.cdxwcx.cn/" target="_blank">成都做网站</a></p> </div> </div> </div> </div> </body> </html> <script> $(".technical_support_box_z_info_box img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>