linux 下编译安装 nginx

以下是我的经验,愿与大家分享,不足之处,还望不惜指正。

我用的操作系统是 fedora18,nginx的代码包是从 nginx.org 下载的 nginx-1.2.7.tar.gz。

参考资料是 《nginx http server》

以下是安装步骤:

1.首先要确定安装 nginx 的先决条件,nginx 需要一些其他的工具或库来支持,最基本的四个是:GCC,PCRE library,zlib library,Openssl

   可以用 rpm -qa XXX 来确定你是否已经安装了这些工具和库(rhel , centos , fedora),如果是用 ubuntu 的,应该试试 apt-get 的类似命令。

   如过没有相关的信息,则需要下载安装相应的包,否则安装有可能报错!

   根据 《nginx http server》上的说法是可以使用 yum 或 apt-get 来下载安装,原文如下:

   gcc:

  Here is the typical way to proceed with the download and installation of the GCC package:

  [root@example.com ~]# yum install gcc
  If you use apt-get:
  [root@example.com ~]# apt-get install gcc

   pcre:

   install all PCRE-related packages:

  [root@example.com ~]# yum install pcre*
  If you use apt-get:
  [root@example.com ~]# apt-get install libpcre3 libpcre3-dev

   zlib:

  Using yum:

  [root@example.com ~]# yum install zlib zlib-devel

  Using apt-get:
  [root@example.com ~]# apt-get install zlib1g zlib1g-dev

  openssl:  

  you install openssl and openssl-devel:

  [root@example.com ~]# yum install openssl openssl-devel
  Using apt-get:
  [root@example.com ~]# apt-get install openssl openssl-dev

   关于他们的支持 nginx 的用途我觉得还是应该了解,但我就不写这了,官方推荐了 yum ,但我遇到了一系列问题,可能是我对 yum 还不够了解,

   下面我会提到这些问题和解决。

 2.切换到 root 用户,并解压 nginx-1.2.7.tar.gz。

    我将这个包放到 /opt 这个目录下并解压,一般安装的话貌似会放到 /usr/local 下,不过我为了模拟服务器(我也不知道服务器上是怎么考虑这个目录的),就选这个。

    # tar zxvf nginx-1.2.7.tar.gz

    当前目录下生成子目录 nginx-1.2.7 。

 # cd nginx-1.2.7

   下面在这个目录下开始编译安装,官方手册上还有一条是建立一个用户和用户组,我跳过了,直接将 nginx 的 user 设置为 nobody。

 3.一共就3个命令: 

   # ./configure

   # make 

   # make install

  这是官方手册上介绍的 "the easy way",最后一步需要在 root 的权限下执行,因为默认安装到 /usr/local 这个目录下可能需要 root 权限(我都用root,不管了)。

  但是这样安装有可能导致很多功能使用受阻,并且将来不可预期会执行错误,除非你很了解这个默认配置的各项条款。所以我必须做一些对我来说显式的配置。

  最重要的配置就是第一个命令,总结起来有 3 个方面:

  path option: 配置安装后名文件所在的目录。比如:  --sbin-path=<prefix>/sbin/nginx  把 nginx 的二进制文件安装到 <prefix>/sbin/nginx 目录 

  prerequsites option: 配置先决条件,上面提到的4个是最基本的,如果你希望 nginx 有其他强大功能支持的话,可以配置库或工具来支持。

                                  比如:   --with-pcre=/xxx/xx  指定所需的 pcre 源码包位置。

  module option: nginx 的个个功能模块是否启用。 比如:--with-http_ssl_module 开启https(这个默认是不启用的)。

  了解了这些就可以进行需要的配置了。不过在 path option 中其实只需要一条配置: --prefix= 这是 nginx 根文件的位置,其余所有配置都会以他为基础。我把

  --prefix 设置为 /opt/nginx 那么当设置 --sbin-path=<prefix>/sbin/nginx 时,其实就是 /opt/nginx/sbin/nginx 了。以下是我的安装命令:

  # ./configure  --prefix=/opt/nginx

                        --with-openssl=/usr/local/lib/openssl-1.0.1e

                        --with-zlib=/usr/local/lib/zlib-1.2.7/

                        --with-pcre=/usr/local/lib/pcre-8.21/

                        --with-http_ssl_module

                        --with-http_stub_status_module

                        && make

                        && make install

  可以看到,我手动指定了openssl , zlib, pcre 的源代码位置,之前没指定的时候,在check时就发生了错误:

  make -f objs/Makefile
  make[1]: Entering directory `/opt/nginx-1.2.7'
  cd /usr/lib64/ \
  && if [ -f Makefile ]; then make distclean; fi \
  && CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
  ./configure --disable-shared
  /bin/sh: line 2: ./configure: No such file or directory
  make[1]: *** [/usr/lib64//Makefile] Error 127 
 make[1]: Leaving directory `/opt/nginx-1.2.7'
 make: *** [build] Error 2

  找不到相应的源代码包,官方手册有这么说:

  If you are positive that all prerequisites were installed correctly, perhaps the issue comes from the fact that the configure script is unable to locate the prerequisite

  files. In that case, make sure that you include the switches related to file paths, as described earlier.

  For example, the following switch allows you to specify the location of the OpenSSL
  library files:
  ./configure [...] --with-openssl=/usr/lib64
  The OpenSSL library file will be looked for in the specified folder.

   但是用命令:

   # ./configure --help | grep with-pcre=

   得到的结果是:

    --with-pcre=DIR set path to PCRE library sources

 

   这要的是源码包啊,yum 安装了不就不是源码了吗?于是我对 yum 失去了信心,自己到网上下了 openssl, pcre, zlib的源代码解压:

   http://zlib.net/

   http://www.pcre.org/

   http://www.openssl.org/source/

   并把相应的 prerequisites option 指向对应

   的源码包,就是上面提过的那条命令,然后出现了 make[1]: leaving directory /opt.nginx 

   手册:

  A successful build should result in a final message appearing: make[1]: leaving directory followed by the project source path.

   然后进入 /opt/nginx/sbin 运行:

   # ./nginx

   打开 localhost ,看到了 nginx 的欢迎页面!

   

 

 

 

 

 

 

 

posted @ 2013-02-21 22:15  Ben·lucky  阅读(1158)  评论(0编辑  收藏  举报