wsl2 ubuntu22.04 安装 nginx 1.20.2

本文章将介绍怎样在 Windows11 环境下,使用 wsl2 安装的 Ubuntu22.04 系统去安装 nginx。

下载

访问 https://nginx.org/en/download.html 地址,根据需要下载合适版本。笔者这里通过 wget https://nginx.org/download/nginx-1.20.2.tar.gz 命令下载 1.20.2,如下:

hxstrive@hxstrive:~/nginx-1.20.2$ wget https://nginx.org/download/nginx-1.20.2.tar.gz
--2023-02-01 16:56:33--  https://nginx.org/download/nginx-1.20.2.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1062124 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.20.2.tar.gz’

nginx-1.20.2.tar.gz                            100%[====================================================================================================>]   1.01M  50.1KB/s    in 21s

2023-02-01 16:56:55 (49.4 KB/s) - ‘nginx-1.20.2.tar.gz’ saved [1062124/1062124]

解压

下载成功后,使用 ls 命令查看安装包名称,如下:

hxstrive@hxstrive:~$ ls
nginx-1.20.2.tar.gz

再使用 tar -xvzf nginx-1.20.2.tar.gz 命令解压安装包,如下:

hxstrive@hxstrive:~$ tar -xvzf nginx-1.20.2.tar.gz
nginx-1.20.2/
nginx-1.20.2/auto/
nginx-1.20.2/conf/
nginx-1.20.2/contrib/
...

预编译 configure

进入到安装包解压目录,如下:

hxstrive@hxstrive:~$ cd nginx-1.20.2/
hxstrive@hxstrive:~/nginx-1.20.2$ ls
CHANGES  CHANGES.ru  LICENSE  README  auto  conf  configure  contrib  html  man  src

使用 configure 命令预编译 nginx,如下:

hxstrive@hxstrive:~/nginx-1.20.2$ ./configure
checking for OS
 + Linux 5.15.79.1-microsoft-standard-WSL2 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)
checking for gcc -pipe switch ... found
...

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

编译出错了,这是因为 HTTP 重定向模块需要 PCRE 库,通过下面命令安装:

hxstrive@hxstrive:~/nginx-1.20.2$ sudo apt-get install libpcre3 libpcre3-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
libpcre3 is already the newest version (2:8.39-13ubuntu0.22.04.1).
libpcre3 set to manually installed.
...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

继续使用 configure 命令进行预编译,如下:

hxstrive@hxstrive:~/nginx-1.20.2$ ./configure
checking for OS
 + Linux 5.15.79.1-microsoft-standard-WSL2 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)
...
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

编译出错了,这是因为 HTTP gzip 模块需要 zlib 库,通过下面命令安装:

hxstrive@hxstrive:~/nginx-1.20.2$ sudo apt-get install zlib1g-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
...
Unpacking libssl-dev:amd64 (3.0.2-0ubuntu1.7) ...
Setting up libssl-dev:amd64 (3.0.2-0ubuntu1.7) ...

再次尝试使用 configure 进行预编译,如下:

hxstrive@hxstrive:~/nginx-1.20.2$ ./configure
checking for OS
 + Linux 5.15.79.1-microsoft-standard-WSL2 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)
...
  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"

恭喜你,预编译成功了。上面输出信息告诉了我们 nginx 安装的基础路径在 /usr/local/nginx 目录,二进制 nginx 文件位于 /usr/local/nginx/sbin/nginx,配置位于 /usr/local/nginx/conf/nginx.conf

编译 make

预编译通过后,可以使用 make 命令进行正式编译,即将源码编译成可执行的二进制文件。如下:

hxstrive@hxstrive:~/nginx-1.20.2$ make
make -f objs/Makefile
make[1]: Entering directory '/home/hxstrive/nginx-1.20.2'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
        -o objs/src/core/nginx.o 
        src/core/nginx.c
...
objs/ngx_modules.o 
-lcrypt -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 '/home/hxstrive/nginx-1.20.2'

编译成功后,再使用 make install 命令将 nginx 安装到指定位置,如下:

hxstrive@hxstrive:~/nginx-1.20.2$ sudo make install
make -f objs/Makefile install
make[1]: Entering directory '/home/hxstrive/nginx-1.20.2'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
...
make[1]: Leaving directory '/home/hxstrive/nginx-1.20.2'

验证

使用 whereis 命令查看 nginx 安装位置,如下:

hxstrive@hxstrive:~/nginx-1.20.2$ whereis nginx
nginx: /usr/local/nginx

查看 nginx 版本信息,如下:

hxstrive@hxstrive:/usr/local/nginx$ ./sbin/nginx -v
nginx version: nginx/1.20.2
成熟不是人的心变老,而是泪在眼眶里打转还能微笑。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号