飞腾芯片 + 银河麒麟OS是目前国产自主可控市场上的主流基础平台。飞腾芯片是aarch64架构,是支持64位的ARM芯片。银河麒麟是基于Ubuntu的发行版。因此可以认为飞腾芯片 + 银河麒麟OS相当于 ARM64 + Ubuntu。
本文介绍在飞腾平台上编译安装nginx的步骤。
从http://nginx.org/en/download.html下载当前稳定版本的源码,例如
| 1
 | wget http://nginx.org/download/nginx-1.14.2.tar.gz
 | 
 
解压nginx源码:
| 1
 | tar -zxvf nginx-1.14.2.tar.gz
 | 
 
将nginx源码目录下contrib/vim/的所有内容,copy到用户的$HOME/.vim目录,可以实现nginx配置文件在vim中的语法高亮。
| 1
2
 | mkdir ~/.vim
cp -r contrib/vim/*  ~/.vim/
 | 
 
再使用vim打开nginx.conf,可以看到配置文件已经可以语法高亮。
查看编译配置支持的参数
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 | $ ./configure --help | more
--help                             print this message
  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname
  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes
  --build=NAME                       set build name
  --builddir=DIR                     set build directory
  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module
  ......
  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources
  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL
  --with-debug                       enable debug logging
 | 
 
--with开头的模块缺省不包括在编译结果中,如果想使用需要在编译配置时显示的指定。--without开头的模块则相反,如果不想包含在编译结果中需要显示设定。
例如我们可以这样进行编译前设置:
| 1
 | ./configure --prefix=/home/adp/nginx  --with-http_ssl_module
 | 
 
设置了nginx的安装目录,需要http_ssl模块。
如果报错缺少OpenSSL,需要先安装libssl。在/etc/apt/sources.list.d目录下增加支持ARM64的apt源,例如国内的清华,创建tsinghua.list,内容如下:
| 1
2
3
4
5
6
7
8
 | deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
 | 
 
执行命令安装OpenSSL:
| 1
2
 | $ sudo apt-get update
$ sudo apt-get install libssl-dev
 | 
 
configure命令执行完后,会生成中间文件,放在目录objs下。其中最重要的是ngx_modules.c文件,它决定了最终那些模块会编译进nginx。
在nginx目录下执行make编译:
编译成功的nginx二进制文件在objs目录下。如果是做nginx的升级,可以直接将这个二进制文件copy到nginx的安装目录中。
在nginx目录下执行make install进行安装:
安装完成后,我们到--prefix指定的目录中查看安装结果:
| 1
2
3
4
5
6
7
 | $ tree -L 1 /home/adp/nginx
nginx/
├── conf
├── html
├── logs
└── sbin
 | 
 
编辑nginx/conf/nginx.conf文件,设置监听端口为8080:
| 1
2
3
4
5
6
7
 | http {
    ...
    server {
        listen       8080;
        server_name  localhost;
...
 | 
 
启动nginx
访问默认首页:
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 | $ curl -I http://localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 02 Apr 2019 08:38:02 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 02 Apr 2019 08:30:04 GMT
Connection: keep-alive
ETag: "5ca31d8c-264"
Accept-Ranges: bytes
 | 
 
其他常用命令:
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 | # 查看帮助
$ ./sbin/nginx  -?
# 重新加载配置
$ ./sbin/nginx -s reload
# 立即停止服务
$ ./sbin/nginx -s stop
# 优雅停止服务
$ ./sbin/nginx -s quit
# 测试配置文件是否有语法错误
$ ./sbin/nginx  -t/-T
# 打印nginx版本、编译信息
$ ./sbin/nginx  -v/-V
 |