在 Alpine Linux 中编译安装 Nginx

本文演示在基于 Docker 的 Alpine Linux 容器中通过源码安装 Nginx。

启动一个 Alpine 的容器

1
docker run -it --name nginx-base alpine /bin/sh

设置国内源

1
echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.8/main/" > /etc/apk/repositories

更新源

1
apk update && apk upgrade

安装 GCC 环境

1
apk add build-base
1
apk add pcre pcre-dev

PCRE - 全称 Perl Compatible Regular Expressions - Perl 兼容的正则表达式(库)。 Nginx 的 Rewrite 功能依赖于该库。

1
apk add openssl openssl-dev
1
apk add zlib zlib-dev
1
apk add git
1
apk add wget
1
apk add curl

构建 Nginx

获取源代码

1
wget http://nginx.org/download/nginx-1.19.4.tar.gz

解压 源文件

1
tar -zxvf nginx-1.19.4.tar.gz

进入解压后的目录,进行编译前的设置

1
cd nginx-1.19.4
1
./configure

系统显示类似以下的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
.....
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

执行 make, 进行构建

1
make

完成后,可以看到系统有类似以下的显示:

1
2
3
4
5
6
7
8
9
10
11
.....
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/nginx-1.19.4'

安装

1
make install

系统显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
.....
cp conf/uwsgi_params \
'/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/nginx-1.19.4'

可以看到, 编译好的 nginx 被安装在 /usr/local/nginx 目录中。

设置路径

打开 /etc/profile 文件,在最后加入

1
export PATH=$PATH:/usr/local/nginx/sbin

为了让修改生效,执行:

1
source /etc/profile

启动 nginx

在命令行执行:

1
nginx

然后执行:

1
ps -a

如果在系统反馈中看到 nginx, 则说明 nginx 已经编译成功,并能正常启动了。

1
2
3
4
5
PID   USER     TIME  COMMAND
1 root 0:00 /bin/sh
2975 root 0:00 nginx: master process nginx
2976 nobody 0:00 nginx: worker process
2977 root 0:00 ps -a

现在可以使用 curl 来测试一下, 执行:

1
curl http://localhost

可以看到 nginx 返回了nginx的默认网页。

本文标题:在 Alpine Linux 中编译安装 Nginx

文章作者:Morning Star

发布时间:2020年11月24日 - 14:11

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/nginx/nginx-install-from-source-on-alpine/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。