本文档描述是从ngnix源代码编译安装,然后升级nginx的过程。为什么从源代码编译呢?因为nginx的官方文档说:
Installing NGINX Open Source from a pre-built package is easier. Installing NGINX from the source files is more flexible: you can compile NGINX Open Source with a particular set of modules including 3rd party ones, or apply latest security patches.
意思就是说预先安装的虽然容易,但是等你想要为最新的版本打补丁的时候,还是需要从源代码进行编译安装的,而且还可以自由选择定制的一些模块进行组装。
进入正文。
1.检查nginx版本
首先,检查centos中已安装的nginx版本,一般在/usr/local/nginx目录下,执行如下命令:
/usr/local/nginx/sbin/nginx -V
,执行完成后会显示nginx版本:
Tengine version: Tengine/2.0.3 (nginx/1.4.7)
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
TLS SNI support enabled
configure arguments:
loaded modules:
ngx_core_module (static)
ngx_errlog_module (static)
ngx_conf_module (static)
ngx_dso_module (static)
ngx_syslog_module (static)
ngx_events_module (static)
ngx_event_core_module (static)
...
可以看到目前的版本是1.47。在这个显示信息中还包括了当前版本的NGINX的编译选项。所以如果编译选蔚项的内容与编译参数不一致,或者为空,那么一定是编译出了问题。
2.查看最新版本
然后去nginx的官方网站,https://www.nginx.com。查看最新的mainline版本的nginx,因为在nignx的文档中管理员说明部分,明确推荐是使用这个版本的。自然有好处,有坏处,相对比,还是好处大于坏处,不然也不会推荐此版本。
结过查看,可以看到目前的最近版本是1.99,比系统使用的版本高了许多,所以应该升级目前的版本了,不过这不是主要的原因,原因是nginx的http2模块在1.90版本后是集成在nginx中的,spdy的功能不希望使用,那么需要进行升级,另外也是出于安全因素的考量。
3.参考安装升级文档
决定了升级,那么就查找nginx的官方文档中,INSTALLING NGINX OPEN SOURCE(http://www.nginx.com/resources/admin-guide/installing-nginx-open-source/),这部分内容在其【NGINX ADMIN GUIDE AND TUTORIAL】中的第一部分。参考这部分的文档内容就可以完成nginx的编译升级了。
4.安装依赖包
第3步中大体是先更新其依赖,再升级。在升级的时候,在
/usr/local
目录下, 直接使用文档中提供的wget命令就可以了,遇到没有找到的包,就去wget后面的URL中找到它的网站,去看看源代码的位置,我遇到的就是openSSL依赖包的版本,现在到了1.02e,没有d版本了,所以将其改为
wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz
这样就可以了。另外,执行对OPENSSL编译的时候,执行如下命令:
./config
./make
./make install
会自动识别操作系统类型,并进行安装。
整个依赖包的安装脚本如下:
Installing NGINX Dependencies
Prior to installing NGINX Open Source, it is necessary to install its dependencies:
the PCRE library – required for the Core and Rewrite modules and provides support for regular expressions:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
tar -zxf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make
sudo make install
the zlib library – required by the Gzip module for headers compression:
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
sudo make install
the OpenSSL library – required the SSL module to support the HTTPS protocol:
wget wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz
tar -zxf openssl-1.0.2e.tar.gz
cd openssl-1.0.2e
./config
make
sudo make install
5.升级到1.99
接下来就可以执行nginx的升级工作了。
执行命令时,要将文档中的版本替换为1.99。
执行wget后,先不急,先备份原来的nignx。这样可以使用原来的配置文件。执行如下命令:
/etc/init.d/nginx stop
mv nginx nginx_BAK
然后,再执行解压,更新安装的操作就行了。
6.解压nginx-1.99
tar -zxf nginx-1.99.tar.gz
cd nginx-1.99
然后关键的来了,就是运行configure,这决定了几件事
1)安装后的目录结构,如配置文件在哪里,执行文件在哪里;
2)依赖的包都哪些
如下代码部分所指,一般为了配置文件的好管理,都放在/usr/local/nginx/conf目录下。
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=../pcre-8.37 \
--with-zlib=../zlib-1.2.8 \
3)其它的编译选项
根据nginx的安装文档,有些默认的编译选项是不需要指定的,只需要把需要加入的模块放入就行了,而文档中列出的需要加入的模块,有些有用有些无用,所以在不知道哪些有用的情况下,默认就全装上去了,可是,这样运行configure步骤的时候会出现一些模块找不到依赖的包的情况,那么就把这些编译选项删除了 ,等到你需要的时候再将其加入进来就可以了。这是值得注意的部分。你全部的编译选项可能是这样的,视操作系统的环境不同:
./configure \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=../pcre-8.37 \
--with-zlib=../zlib-1.2.8 \
--with-threads \
--with-file-aio \
--with-ipv6 \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-cpp_test_module \
--with-debug
4)执行make
5)执行mak install
执行完这步,也许会有链接错误,这时去执行一下命令
/usr/local/nginx/sbin/nginx -V
查看一下编译的模块是否成功,如果成功了,然后直接运行nginx就可以了,这一般都是提示某个文档不可以被重写。应该是上一个版本的nginx留下的旧文件链接。
6)将原来的nginx中的配置文件迁移到新的nginx中。
6.整个更新过程结束
需要指出的是,通过源代码编译后的文档目录结构显然是与预先编译的版本不同,目录中会多出好多与源代码相关的目录。另外Sbin中的文件只有一个nginx,而预编译的有三个。