## 2022-6-6 更新
在清华大学镜像源里发现了一个给Debian和Ubuntu用的Nginx源:
[https://mirrors.tuna.tsinghua.edu.cn/u.sb/](https://mirrors.tuna.tsinghua.edu.cn/u.sb/)
Ubuntu 22.04的操作步骤如下:
```shell
curl https://n.wtf/public.key -o /usr/share/keyrings/n.wtf.asc
echo "deb [signed-by=/usr/share/keyrings/n.wtf.asc] https://mirrors.tuna.tsinghua.edu.cn/u.sb/ jammy main" > /etc/apt/sources.list.d/n.wtf.list
apt update && apt install nginx
```
![u.sb_1.png](https://s2.loli.net/2022/06/06/uEVCj2H1MmnLDYp.png)
![u.sb_2.png](https://s2.loli.net/2022/06/06/9Oyut7PWxDsUIpK.png)
<br/>
## 前言
Ubuntu 22.04默认源里的Nginx 1.18貌似有问题,我在一台VPS上安装到最后时最后会报一个dpkg的错误。
这里选择自己编译一个1.22.0版本的Nginx。
<br/>
## 构建
### 安装依赖
```shell
apt install make gcc libssl-dev libpcre3-dev zlib1g-dev libxml2-dev libxslt1-dev libgd-dev
```
### 下载源码
```shell
wget https://nginx.org/download/nginx-1.22.0.tar.gz
```
### 解压
```shell
tar -zxf nginx-1.22.0.tar.gz
```
### 构建参数
编译用的参数从另一台ubuntu 20.04安装的ubuntu 1.18复制的
```shell
cd nginx-1.22.0 && ./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' \
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' \
--prefix=/usr/share/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--modules-path=/usr/lib/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-debug \
--with-compat \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-mail=dynamic \
--with-mail_ssl_module
```
### 编译并安装
```shell
make && make install
```
<br/>
## 运行配置
将可执行文件复制到/sbin目录
```shell
cp objs/nginx /sbin/
```
创建运行时文件夹,nginx运行时会往这里写入文件
```shell
mkdir /var/lib/nginx
```
创建并启用service,文件路径: /lib/systemd/system/nginx.service
```text
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
```
```shell
systemctl enable nginx.service && service nginx start
```
[记录,学习,整理] 在Ubuntu22.04上编译Nginx