菜鸟运维笔记:小记编译安装Nginx所遇到的坑


目录(?)[+]

前言

不管是CentOS,或是Debian/Ubuntu,甚至是Windows。Nginx都有已编译好的安装包可用。通常只需要在终端上潇洒地输入apt-get install ...或是yum install ..就可以了。但是对于我这个源码编译狂来说,那样一键安装,显然无法满足我心理需求。

获取Nginx源码

下载

官网已经出现了1.7了。不过我还是选择了目前稳定的1.6版本。打开你的终端,我们开始吧:

<code class="hljs nginx" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="color: rgb(249, 38, 114); margin-top: 0px !important;">wget</span> <span class="hljs-url">http://nginx.org/download/nginx-1.6.2.tar.gz</span>
</code>

解压

<code class="hljs nginx" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="color: rgb(249, 38, 114); margin-top: 0px !important;">tar</span> xvf nginx-<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>.<span class="hljs-number" style="color: rgb(174, 129, 255);">6</span>.<span class="hljs-number" style="color: rgb(174, 129, 255);">2</span>.tar.gz
</code>

编译

进入刚才解压的目录下。你会发现,没有其他软件的源码中所包含的INSTALL文件(该文件通常用于指导如何正确编译源码),有一个README,来我们cat一下。!坑爹啊,里面只有一句话!

<code class="hljs cs" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">Documentation <span class="hljs-keyword" style="color: rgb(249, 38, 114); margin-top: 0px !important;">is</span> available at http:<span class="hljs-comment" style="color: rgb(117, 113, 94);">//nginx.org</span>
</code>

文档在这个网站上。。。
不过不用管他了,看到目录下绿色的configure文件在,那么我们直接上手吧。

configure

<code style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; color: inherit; background-color: transparent; margin-top: 0px !important;">./configure
</code>

然后,你可能会收到一个错误,提示你缺少PCRE。不过,如果你之前已经在这台主机上成功安装了Apache的话,那么现在是不会报错的,因为Apache也是需要PCRE的。(我的是新搞的华为云的主机,几乎裸机)

安装PCRE

PCRE是什么呢?简单说来,就是一个Perl的库。那么我们接下来来编译pcre的源码。哈哈。
到官网去看,当前(截至2014/11/09)最新版本是8.36。

<code class="hljs ruby" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">wget <span class="hljs-symbol" style="color: rgb(249, 38, 114); margin-top: 0px !important;">ftp:</span>/<span class="hljs-regexp" style="color: rgb(174, 129, 255);">/ftp.csx.cam.ac.uk/pub</span><span class="hljs-regexp" style="color: rgb(174, 129, 255);">/software/programming</span><span class="hljs-regexp" style="color: rgb(174, 129, 255);">/pcre/pcre</span>-<span class="hljs-number" style="color: rgb(174, 129, 255);">8.36</span>.tar.gz
</code>
-DHAVE_CONFIG_H

接下来,同样是用tar xvf解压。解压出一个pcre的目录,我们cd进去。不由分说,直接./configure
貌似没出错,接着make,貌似也没出问题,继续make install。咦,有错误!

<code class="hljs http" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="color: rgb(249, 38, 114); margin-top: 0px !important;">libtool</span>: <span class="hljs-string" style="color: rgb(230, 219, 116);">compile: unrecognized option `-DHAVE_CONFIG_H'  </span>
</code>

是我没装libtool么,yum install一下。不对啊,装了啊。后来百度发现是缺少了g++编译器。对哦,我现在是裸机。这个我就不自己编译了(快跑题了),敲命令安装:

<code class="hljs nginx" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(117, 113, 94); margin-top: 0px !important;">#CentOS下面</span>
<span class="hljs-title" style="color: rgb(249, 38, 114);">yum</span> install gcc-c++
<span class="hljs-comment" style="color: rgb(117, 113, 94);">#Ubuntu下面</span>
apt-get install g++
</code>

你应该是root身份,我就不多说了。此时会默认安装一些依赖软件,比如autoconf

zlib

然后我们再重新./configure一下吧,瓦擦嘞,有问题(装完g++,configure都报错了。。),提示缺少zlib。好吧,继续百度,进官网。复制链接,然后继续wget

<code class="hljs nginx" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="color: rgb(249, 38, 114); margin-top: 0px !important;">wget</span> <span class="hljs-url">http://zlib.net/zlib-1.2.8.tar.gz</span>
</code>

继续tar xvfo(╯□╰)o解压出一个目录。我这里目录名是zlib-1.2.8。请注意我所有wget的文件都在同一层目录下面,解压路径也是同一层。
来我ls -F一下:

<code style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; color: inherit; background-color: transparent; margin-top: 0px !important;">nginx-1.6.2/         pcre-8.36/         zlib-1.2.8/
nginx-1.6.2.tar.gz  pcre-8.36.tar.gz  zlib-1.2.8.tar.gz
</code>

接着,我们再进入pcre的目录下面,去configure。加上一个选项。

<code class="hljs sql" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">./configure <span class="hljs-comment" style="color: rgb(117, 113, 94); margin-top: 0px !important;">--with-zlib=../zlib-1.2.8</span>
</code>

注意选项后面应该是,你自己的zlib目录的路径。You Know?
此时,应该不报错了,我们来一路高歌猛进,继续敲两个命令,——————>make——————>make install
好了,没错误安装pcre完毕。深呼一口气,恩,费力不少啊,不过终于安装上了,好吧,教程结束。
咦?慢着,我们的教程不是教你安装pcre的啊。我们是Nginx安装教程啊。那好吧,继续。

make

在pcre安装完毕之后,再次进入nginx的目录下面,去./configure应该不会报错了。
轻轻敲下四个字母make。接着观看滚屏。

make install

滚屏完毕,继续敲make install。然后继续看滚屏。。

开启Nginx

前文之中,我们已经安装好了Nginx,现在我们去开启它。因为刚才nginx源码目录下,我们在configure的时候,没有使用选项--prefix(该选项用来指定nginx的安装位置),所以它默认安装到了/usr/local/nginx/目录中。 我们可以cd进去,也可以不进去。下面开启它:

<code class="hljs perl" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">/usr/<span class="hljs-keyword" style="color: rgb(249, 38, 114); margin-top: 0px !important;">local</span>/nginx/sbin/nginx
</code>

在安装目录下的sbin子目录中有Nginx的可执行文件。不过,很不幸,我的机器上,又报错了:

<code class="hljs perl" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"> error <span class="hljs-keyword" style="color: rgb(249, 38, 114); margin-top: 0px !important;">while</span> loading shared libraries: libpcre.so.<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>: cannot <span class="hljs-keyword" style="color: rgb(249, 38, 114);">open</span> shared object file: No such file <span class="hljs-keyword" style="color: rgb(249, 38, 114);">or</span> directory
</code>

提示缺少libpcre.so.1,但是我们刚才明明装了pcre啊。原来是Nginx默认在/lib64/目录下(我是64位机器,32位应该是搜索/lib/),搜索该库文件。显然,我们自己编译的pcre,它的这个库文件不在这个位置。来我们find / -name 'libpcre.so.1'一下,找找看:

<code class="hljs perl" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">/usr/<span class="hljs-keyword" style="color: rgb(249, 38, 114); margin-top: 0px !important;">local</span>/lib/libpcre.so.<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>
</code>

好吧,在/usr/local/lib/下面。我们来建立以符号链接吧。

<code class="hljs nginx" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="color: rgb(249, 38, 114); margin-top: 0px !important;">ln</span> -s /usr/local/lib/libpcre.so.<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>  /lib64/libpcre.so.<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>
</code>

然后,这次应该真得可以了。。。开启nginx:/usr/local/nginx/sbin/nginx。没报错,那我们来检测一下,看谁在监听80端口。

<code class="hljs nginx" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0.5em; color: rgb(248, 248, 242); display: block; background-color: rgb(35, 36, 31); margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="color: rgb(249, 38, 114); margin-top: 0px !important;">netstat</span> -lpn|grep :<span class="hljs-number" style="color: rgb(174, 129, 255);">80</span>

tcp        <span class="hljs-number" style="color: rgb(174, 129, 255);">0</span>      <span class="hljs-number" style="color: rgb(174, 129, 255);">0</span> <span class="hljs-number" style="color: rgb(174, 129, 255);">0.0.0.0:80</span>                  <span class="hljs-number" style="color: rgb(174, 129, 255);">0.0.0.0</span>:*                   LISTEN      <span class="hljs-number" style="color: rgb(174, 129, 255);">28126</span>/nginx  
</code>

好了,打完收工。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值