设为首页 收藏本站
查看: 673|回复: 0

[经验分享] linux 下编译安装 nginx

[复制链接]
累计签到:8 天
连续签到:1 天
发表于 2015-7-28 08:06:43 | 显示全部楼层 |阅读模式
  以下是我的经验,愿与大家分享,不足之处,还望不惜指正。
  我用的操作系统是 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:
  [iyunv@example.com ~]# yum install gcc
  If you use apt-get:
  [iyunv@example.com ~]# apt-get install gcc
  pcre:
  install all PCRE-related packages:
  [iyunv@example.com ~]# yum install pcre*
  If you use apt-get:
  [iyunv@example.com ~]# apt-get install libpcre3 libpcre3-dev
  zlib:
  Using yum:
  [iyunv@example.com ~]# yum install zlib zlib-devel
  Using apt-get:
  [iyunv@example.com ~]# apt-get install zlib1g zlib1g-dev
  openssl:  
  you install openssl and openssl-devel:
    [iyunv@example.com ~]# yum install openssl openssl-devel
  Using apt-get:
  [iyunv@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=/sbin/nginx  把 nginx 的二进制文件安装到 /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=/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 的欢迎页面!
  
  
  
  
  
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-91313-1-1.html 上篇帖子: debian7下部署nginx服务器 下篇帖子: 安装配置nginx
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表