Ubuntu14.04源码编译安装nginx1.12.2

共9349个字

 
默认apt-get install nginx 安装的版本是1.4.6,需要升级使用。(Ubuntu 其他版本请修改相关参数进行使用,过程一致)
已在本文最后更新 ubuntu16.04 编译 nginx 教程 

安装默认版本

# apt-get install nginx

# nginx -v

nginx version: nginx/1.4.6 (Ubuntu)


下载相关依赖库及安装源码

# cd /opt

# wget http://exim.mirror.fr/pcre/pcre-8.38.tar.gz

wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz
# wget http://zlib.net/zlib-1.2.11.tar.gz

# wget http://nginx.org/download/nginx-1.12.2.tar.gz

# tar -zxf nginx-1.12.2.tar.gz

# tar -zxf zlib-1.2.11.tar.gz
tar -zxf openssl-1.0.2n.tar.gz

tar -zxf pcre-8.38.tar.gz
 


安装编译所需库环境

# apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential build-essential openssl libxslt-dev libssl-dev libperl-dev libgeoip-dev libpam-dev libgd2-xpm-dev


编译安装 nginx
# nginx -V

nginx version: nginx/1.4.6 (Ubuntu)

built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 

TLS SNI support enabled

configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module

# ll

总用量 8848

drwxr-xr-x  6 root root     4096  1月  3 15:43 ./

drwxr-xr-x 22 root root     4096  1月  3 12:28 ../

drwxr-xr-x  9 1001  1001    4096  1月  3 14:58 nginx-1.12.2/

-rw-r--r--  1 root root   981687 10月 20 10:40 nginx-1.12.2.tar.gz

drwxr-xr-x 21 root root     4096  1月  3 14:52 openssl-1.0.2n/

-rw-r--r--  1 root root  5375802 12月  7 21:47 openssl-1.0.2n.tar.gz

drwxr-xr-x  9 1169  1169   12288  1月  3 14:48 pcre-8.38/

-rw-r--r--  1 root root  2053336  1月  3 14:45 pcre-8.38.tar.gz

drwxr-xr-x 14  501 staff    4096  1月  3 14:48 zlib-1.2.11/

-rw-r--r--  1 root root   607698  2月  4  2017 zlib-1.2.11.tar.gz
 

# cd nginx-1.12.2/
# 
./configure --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --with-pcre=/opt/pcre-8.38 --with-zlib=/opt/zlib-1.2.11 --with-openssl=/opt/openssl-1.0.2n

###这里请注意,./configure 后面的配置直接复制上面用 nginx -V 查询到的信息,然后需要修改几个地方--with-http_spdy_module需要修改为--with-http_v2_module,并且删除掉--with-ipv6然后在最后加上--with-pcre=/opt/pcre2-10.30 --with-zlib=/opt/zlib-1.2.11 --with-openssl=/opt/openssl-1.0.2n###


# make


完成后会在objs 目录下生成 nginx 执行文件
#
objs/nginx -v

nginx version: nginx/1.12.2


重命名原来的 nginx

mv /usr/sbin/nginx /usr/sbin/nginx1.4.6


复制新的 nginx 文件到 /usr/sbin/ 目录
cp objs/nginx /usr/sbin/nginx 


编辑 Makefile 文件,输入正确的 nginx 路径进行升级

vim Makefile

default:        build

clean:

        rm -rf Makefile objs

build:

        $(MAKE) -f objs/Makefile

install:

        $(MAKE) -f objs/Makefile install

modules:

        $(MAKE) -f objs/Makefile modules

upgrade:

#       /usr/share/nginx/sbin/nginx -t

#       注释或修改上面这行,输入正确的路径
        
/usr/sbin/nginx -t

        kill -USR2 `cat /run/nginx.pid`

        sleep 1

        test -f /run/nginx.pid.oldbin

        kill -QUIT `cat /run/nginx.pid.oldbin`



保存退出后执行 make upgrade 进行升级
make upgrade

/usr/sbin/nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

kill -USR2 `cat /run/nginx.pid`

sleep 1

test -f /run/nginx.pid.oldbin

kill -QUIT `cat /run/nginx.pid.oldbin`


nginx -v

nginx version: nginx/1.12.2

完成。

2018年1月4日更新Ubuntu16.04编译过程
=====================================================================
=====================================================================
在 Ubuntu16.04下,进行make 时,按照上面的方法会出现错误

# nginx -V

nginx version: nginx/1.10.3 (Ubuntu)

built with OpenSSL 1.0.2g  1 Mar 2016

TLS SNI support enabled

configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

# ./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --with-pcre=/opt/pcre-8.38 --with-zlib=/opt/zlib-1.2.11 --with-openssl=/opt/openssl-1.0.2n
 

# make

...
...

/usr/bin/ld: /opt/pcre-8.38/.libs/libpcre.a(libpcre_la-pcre_compile.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC      

/opt/pcre-8.38/.libs/libpcre.a: error adding symbols: Bad value

collect2: error: ld returned 1 exit status

objs/Makefile:323: recipe for target 'objs/nginx' failed

make[1]: *** [objs/nginx] Error 1

make[1]: Leaving directory '/opt/nginx-1.12.2'

Makefile:8: recipe for target 'build' failed

make: *** [build] Error 2


出现错误,解决这个错误需要把 ./configure 后面我用红色部分标识的参数给删除就好了。--with-ipv6这个参数会报一个 warn,但是不会影响,可去掉也可不理。下面是正确的配置,后面可以按照先前的教程继续即可。
 

# ./configure --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --with-pcre=/opt/pcre-8.38 --with-zlib=/opt/zlib-1.2.11 --with-openssl=/opt/openssl-1.0.2n

# make
...
...
make[1]: Leaving directory '/opt/nginx-1.12.2'

完成后会在objs 目录下生成 nginx 执行文件
objs/nginx -v

nginx version: nginx/1.12.2


重命名原来的 nginx

mv /usr/sbin/nginx /usr/sbin/nginx1.10.3


复制新的 nginx 文件到 /usr/sbin/ 目录
cp objs/nginx /usr/sbin/nginx 


编辑 Makefile 文件,输入正确的 nginx 路径进行升级

vim Makefile

default:        build

clean:

        rm -rf Makefile objs

build:

        $(MAKE) -f objs/Makefile

install:

        $(MAKE) -f objs/Makefile install

modules:

        $(MAKE) -f objs/Makefile modules

upgrade:

#       /usr/share/nginx/sbin/nginx -t

#       注释或修改上面这行,输入正确的路径
        
/usr/sbin/nginx -t

        kill -USR2 `cat /run/nginx.pid`

        sleep 1

        test -f /run/nginx.pid.oldbin

        kill -QUIT `cat /run/nginx.pid.oldbin`



保存退出后执行 make upgrade 进行升级
make upgrade

/usr/sbin/nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

kill -USR2 `cat /run/nginx.pid`

sleep 1

test -f /run/nginx.pid.oldbin

kill -QUIT `cat /run/nginx.pid.oldbin`


nginx -v

nginx version: nginx/1.12.2

完成。


❤ 喜欢 14653

2条回应:“Ubuntu14.04源码编译安装nginx1.12.2”

  1. power up premium说道:

    My spouse and I absolutely love your blog and find most of your post's to be exactly
    I'm looking for. Does one offer guest writers to write content available for you?
    I wouldn't mind creating a post or elaborating on a lot of the subjects you
    write concerning here. Again, awesome web site!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

归档于 Ubuntu