前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Debian-12和Ubuntu-20.04编译nginx

Debian-12和Ubuntu-20.04编译nginx

原创
作者头像
峨眉山市雅铭网络
修改2024-06-15 07:45:50
6000
修改2024-06-15 07:45:50
举报
文章被收录于专栏:Linux云服务器Linux云服务器

安装编译依赖环境,本文用Debian12.x和Ubuntu20.04做测试验证。

代码语言:shell
复制
sudo apt install build-essential ca-certificates zlib1g-dev libpcre3 libpcre3-dev tar unzip libssl-dev mercurial libunwind-dev pkg-config make cmake golang gcc git wget
  • Debian如果采用root用户登录则不需要增加sudo来安装,Ubuntu同理。

编译http3/quic需要的依赖,如果不使用,那么可以跳过该步骤。

  • 克隆依赖库我采用自己拉取回gitee的依赖编写
代码语言:shell
复制
git clone https://gitee.com/fenghuolingyun/boringssl.git
  • 原始库地址:git clone https://boringssl.googlesource.com/boringssl
  • 开始编译依赖:
代码语言:shell
复制
cd boringssl
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release .. 
make

注意这里有可能由于网络原因导致golang模块无法拉取而报错,我们进行代理添加,本文使用的阿里云代理:

代码语言:shell
复制
export GO111MODULE=on 
export GOPROXY=https://mirrors.aliyun.com/goproxy/

然后执行make完成返回我们平级目录cd ../../

  • 下载nginx源码,需要其它版本自行指定。
代码语言:shell
复制
wget https://nginx.org/download/nginx-1.25.3.tar.gz
  • 开始编译操作:
代码语言:shell
复制
tar -xf nginx-1.25.3.tar.gz
cd nginx-1.25.3
  • 配置构建参数,这里采用全模块参数:
代码语言:shell
复制
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -ffile-prefix-map=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --with-http_v3_module --with-cc=c++ --with-cc-opt='-I../boringssl/include -x c' --with-ld-opt='-L../boringssl/build/ssl -L../boringssl/build/crypto'
  • 使用早期boringssl的构建参数请使用如下参数:--with-http_v3_module --with-cc-opt=-I../boringssl/include --with-ld-opt='-L../boringssl/build/ssl -L../boringssl/build/crypto'
  • 注意由于在2024年三月boringssl的一个破坏性更新,新的构建参数如下已配置默认:
代码语言:shell
复制
--with-http_v3_module --with-cc=c++ --with-cc-opt='-I../boringssl/include -x c' --with-ld-opt='-L../boringssl/build/ssl -L../boringssl/build/crypto'

执行make && make install 编译后安装参数

  • 创建缺失的目录:
代码语言:shell
复制
mkdir -p /var/cache/nginx/
mkdir -p /var/log/nginx/
mkdir -p /usr/lib/nginx/modules //此目录非必须,方便后续添加动态加载类型模块
  • 添加nginx用户组
代码语言:shell
复制
useradd -m nginx
  • 编写nginx服务,本文采用nano编辑,如没有请使用apt install nano安装
代码语言:shell
复制
nano /usr/lib/systemd/system/nginx.service
  • 输入内容:
代码语言:shell
复制
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target
  • 执行重载服务命令:
代码语言:shell
复制
systemctl daemon-reload
  • 现在来可以直接使用systemctl管理我们编译的nginx

现在来配置开机启动

代码语言:shell
复制
systemctl enable nginx

执行启动

代码语言:shell
复制
systemctl start nginx

停止Nginx服务:

代码语言:shell
复制
systemctl stop nginx

重启Nginx服务:

代码语言:shell
复制
systemctl restart nginx

显示Nginx服务的状态:

代码语言:shell
复制
systemctl status nginx

禁用Nginx服务,使其不会在系统启动时自动启动:

代码语言:shell
复制
systemctl disable nginx

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装编译依赖环境,本文用Debian12.x和Ubuntu20.04做测试验证。
  • 编译http3/quic需要的依赖,如果不使用,那么可以跳过该步骤。
  • 现在来配置开机启动
  • 执行启动
  • 停止Nginx服务:
  • 重启Nginx服务:
  • 显示Nginx服务的状态:
  • 禁用Nginx服务,使其不会在系统启动时自动启动:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档