ubuntu 将第三方模块编译到nginx包中
在Ubuntu系统下,我们通常是使用apt-get来搞定一切,简单方便快捷。但是如果要给nginx添加额外的模块就需要重新编译了。从源码编译nginx就要手工补充Debian包提供的一些文件,如启动脚本,logrotate等等。
获取额外的模块
以Pagespeed、upload模块为例。
获取pagespeed模块:
1
2
3
4
5
6
7
8
9
|
sudo
mkdir
/
opt
/
pagespeed
cd
/
opt
/
pagespeed
NPS_VERSION
=
1.9.32.2
sudo
wget
https
:
/
/
github
.com
/
pagespeed
/
ngx_pagespeed
/
archive
/
release
-
$
{
NPS_VERSION
}
-
beta
.zip
sudo
unzip
release
-
$
{
NPS_VERSION
}
-
beta
.zip
cd
ngx_pagespeed
-
release
-
$
{
NPS_VERSION
}
-
beta
/
sudo
wget
https
:
/
/
dl
.google
.com
/
dl
/
page
-
speed
/
psol
/
$
{
NPS_VERSION
}
.tar
.gz
sudo
tar
-
xzvf
$
{
NPS_VERSION
}
.tar
.gz
# extracts to psol/
# Gives us directory /opt/pagespeed/ngx_pagespeed-release-1.9.32.2-beta
|
获取upload模块:
1
2
3
4
5
|
sudo
mkdir
/
opt
/
httpupload
cd
/
opt
/
httpupload
sudo
wget
https
:
/
/
github
.com
/
vkholodkov
/
nginx
-
upload
-
module
/
archive
/
2.2.zip
sudo
unzip
2.2.zip
# Gives us directory: /opt/httpupload/nginx-upload-module-2.2
|
获取nginx源码包和依赖关系
添加nginx源,调整、重建并重新安装nginx。
添加PPA源文件,并确保deb-src指令没被注释掉。
1
2
3
|
$
vim
/
etc
/
apt
/
sources
.list
.d
/
nginx
-
stable
-
trusty
.list
deb
http
:
/
/
ppa
.launchpad
.net
/
nginx
/
stable
/
ubuntu
trusty
main
deb
-
src
http
:
/
/
ppa
.launchpad
.net
/
nginx
/
stable
/
ubuntu
trusty
main
|
更新
1
|
$
sudo
apt
-
get
update
|
获取源码包和依赖包
1
2
3
4
5
6
7
8
9
10
11
|
# Install package creation tools
sudo
apt
-
get
install
-
y
dpkg
-
dev
sudo
mkdir
/
opt
/
rebuildnginx
cd
/
opt
/
rebuildnginx
# Get Nginx (ppa:nginx/stable) source files
sudo
apt
-
get
source
nginx
# Install the build dependencies
sudo
apt
-
get
build
-
dep
nginx
|
1
2
3
4
5
|
$
ls
-
lah
/
opt
/
rebuildnginx
drwxr
-
xr
-
x
10
root
root
4096
Dec
14
16
:
37
nginx
-
1.6.2
/
-
rw
-
r
--
r
--
1
root
root
934244
Dec
14
02
:
10
nginx_1
.
6.2
-
5
+
trusty0
.debian
.tar
.gz
-
rw
-
r
--
r
--
1
root
root
2798
Dec
14
02
:
10
nginx_1
.
6.2
-
5
+
trusty0
.dsc
-
rw
-
r
--
r
--
1
root
root
804164
Sep
18
21
:
31
nginx_1
.
6.2.orig.tar.gz
|
ppa:nginx/stable 提供多个nginx可用包:common, light, full, extras。当执行apt-get install -y nginx时,是安装full版本的。
light 比 common少了 --without-* 的选项包。
编辑/opt/rebuildnginx/nginx-1.6.2/debian/rules文件来添加第三方模块。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
full_configure_flags
:
=
\
$
(
common_configure_flags
)
\
--
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
\
--
add
-
module
=
$
(
MODULESDIR
)
/
nginx
-
auth
-
pam
\
--
add
-
module
=
$
(
MODULESDIR
)
/
nginx
-
dav
-
ext
-
module
\
--
add
-
module
=
$
(
MODULESDIR
)
/
nginx
-
echo
\
--
add
-
module
=
$
(
MODULESDIR
)
/
nginx
-
upstream
-
fair
\
--
add
-
module
=
$
(
MODULESDIR
)
/
ngx_http_substitutions_filter
_module
\
--
add
-
module
=
/
opt
/
httpupload
/
nginx
-
upload
-
module
-
2.2
\
--
add
-
module
=
/
opt
/
pagespeed
/
ngx_pagespeed
-
release
-
1.9.32.2
-
beta
|
重建nginx包
1
2
|
cd
/
opt
/
rebuildnginx
/
nginx
-
1.6.2
sudo
dpkg
-
buildpackage
-
b
|
重建完后,会在/opt/rebuildnginx目录下生产一堆.deb文件,可以使用这些文件来安装nginx。由于我们调整了‘full'包,因此需要使用该包来安装nginx。该包有两个:
1
2
|
nginx
-
full
-
dbg_1
.
6.2
-
5
+
trusty0_amd64
.
deb
nginx
-
full_1
.
6.2
-
5
+
trusty0_amd64
.
deb
|
dbg的是debug版本。
重新安装nginx,可能需要先删除之前安装的nginx。
1
2
3
|
# .deb files appear one level above the `nginx-1.6.2` directory
cd
/
opt
/
rebuildnginx
sudo
dpkg
--
install
nginx
-
full_1
.
6.2
-
5
+
trusty0_amd64
.deb
|
查看已安装的模块
1
2
3
4
5
6
7
8
|
$
nginx
-
V
nginx
version
:
nginx
/
1.6.2
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'
# A bunch of these removed for brevity
--
add
-
module
=
/
opt
/
httpupload
/
nginx
-
upload
-
module
-
2.2
# yay!
--
add
-
module
=
/
opt
/
pagespeed
/
ngx_pagespeed
-
release
-
1.9.32.2
-
beta
# yay!
|
配置pagespeed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
sudo
mkdir
-
p
/
var
/
cache
/
ngx_pagespeed
/
# Stuff up here omitted
server
{
pagespeed
On
;
pagespeed
FileCachePath
"/var/cache/ngx_pagespeed/"
;
pagespeed
EnableFilters
combine_css
,
combine_javascript
;
# Stuff down here omitted
}
$
sudo
service
nginx
configtest
*
Testing
nginx
configuration
[
OK
]
# Restart, assuming it's OK
$
sudo
service
nginx
restart
|
参考资料
PageSpeed Module: https://developers.google.com/speed/pagespeed/module/configuration
Installing optional Nginx modules with apt-get: http://serverfault.com/questions/227480/installing-optional-nginx-modules-with-apt-get
成长的对话版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!