如何在Debian Linux上重新编译nginx web服务器

首先安装包构建工具:

# apt-get install dpkg-dev

安装所有nginx构建依赖项:

# apt-get build-dep nginx

下载nginx源代码:

$mkdir nginx-local
$cd nginx-local/
$apt-get source nginx

上面的命令将下载所有必要的“nginx”源文件,用于构建“*.deb”debian包。

$tree -L 2 
.
├── nginx-1.6.2
│   ├── auto
│   ├── CHANGES
│   ├── CHANGES.ru
│   ├── conf
│   ├── configure
│   ├── contrib
│   ├── debian
│   ├── html
│   ├── LICENSE
│   ├── man
│   ├── README
│   └── src
├── nginx_1.6.2-5.debian.tar.xz
├── nginx_1.6.2-5.dsc
└── nginx_1.6.2.orig.tar.gz
8 directories, 8 files

例如,假设我们要修改Web服务器名称。

编辑nginx-1.6.2/src/http/ngx_http_header_filter_module.c文件

将
static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
修改为
static char ngx_http_server_string[] = "Server: Onitroad Web Server" CRLF;
static char ngx_http_server_full_string[] = "Server: Onitroad Web Server" CRLF;

修改源代码后,
创建一个新的* .deb包:

$cd nginx-1.6.2/
$dpkg-buildpackage -rfakeroot -uc -b
....
        dpkg-deb --build debian/nginx ..
dpkg-deb: building package `nginx' in `../nginx_1.6.2-5_all.deb'.
        dpkg-deb --build debian/nginx-doc ..
dpkg-deb: building package `nginx-doc' in `../nginx-doc_1.6.2-5_all.deb'.
        dpkg-deb --build debian/nginx-common ..
dpkg-deb: building package `nginx-common' in `../nginx-common_1.6.2-5_all.deb'.
        dpkg-deb --build debian/nginx-full ..
dpkg-deb: building package `nginx-full' in `../nginx-full_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-full-dbg ..
dpkg-deb: building package `nginx-full-dbg' in `../nginx-full-dbg_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-light ..
dpkg-deb: building package `nginx-light' in `../nginx-light_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-light-dbg ..
dpkg-deb: building package `nginx-light-dbg' in `../nginx-light-dbg_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-extras ..
dpkg-deb: building package `nginx-extras' in `../nginx-extras_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-extras-dbg ..
dpkg-deb: building package `nginx-extras-dbg' in `../nginx-extras-dbg_1.6.2-5_amd64.deb'.
 dpkg-genchanges -b >../nginx_1.6.2-5_amd64.changes
dpkg-genchanges: binary-only upload (no source code included)
 dpkg-source --after-build nginx-1.6.2
dpkg-buildpackage: binary-only upload (no source included)

现在可以安装新的重新配置包:

$cd ..
$ls
nginx-1.6.2            nginx_1.6.2-5_amd64.changes  nginx_1.6.2-5.dsc        nginx-common_1.6.2-5_all.deb  nginx-extras_1.6.2-5_amd64.deb      nginx-full_1.6.2-5_amd64.deb      nginx-light_1.6.2-5_amd64.deb
nginx_1.6.2-5_all.deb  nginx_1.6.2-5.debian.tar.xz  nginx_1.6.2.orig.tar.gz  nginx-doc_1.6.2-5_all.deb     nginx-extras-dbg_1.6.2-5_amd64.deb  nginx-full-dbg_1.6.2-5_amd64.deb  nginx-light-dbg_1.6.2-5_amd64.deb

从新构建软件包中安装nginx

# dpkg -i nginx_1.6.2-5_all.deb nginx-full_1.6.2-5_amd64.deb nginx-common_1.6.2-5_all.deb nginx-doc_1.6.2-5_all.deb

检查状态Web服务器状态:

systemctl status nginx
   nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
   Active: active (running) since Wed 2014-04-15 09:46:53 AEST; 1min 18s ago
  Process: 3535 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 3534 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 3538 (nginx)
   CGroup: /system.slice/nginx.service
           ├─3538 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─3539 nginx: worker process
           ├─3540 nginx: worker process
           ├─3541 nginx: worker process
           └─3542 nginx: worker process

确认服务器名称更改:

# curl -I http://localhost
HTTP/1.1 200 OK
Server:Onitroad Web Server
Date: Tue, 14 Apr 2014 23:49:37 GMT
Content-Type: text/html
Content-Length: 867
Last-Modified: Tue, 14 Apr 2014 23:45:07 GMT
Connection: keep-alive
ETag: "552da683-363"
Accept-Ranges: bytes
日期:2020-07-07 20:56:29 来源:oir作者:oir