Ubuntu22.04完全离线编译安装Nginx

416 阅读2分钟

一、安装环境&安装包

1.安装环境

查看操作系统的发行版号和操作系统版本

uname -a

2.Nginx安装包

nginx-1.27.3.tar.gz

Nginx官网下载最新版安装包。

二、安装步骤

1.上传安装包后解压

tar -zxvf nginx-1.27.3.tar.gz

2.进入目录

cd nginx-1.27.3

3.添加nginx用户

useradd nginx

4.生成makefile,选择安装目录和配置选项

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--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/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--user=nginx \
--group=nginx \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module

6.编译安装

make && make install

7.验证安装成功

nginx -v

三、systemctl管理Nginx

1.源码编译安装的 nginx 不会自动创建系统服务相关配置文件,需要手动创建 nginx 服务配置文件。

sudo vim /etc/systemd/system/nginx.service

2.在文件中添加以下内容(示例,根据实际安装路径等情况可能需调整):

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存并退出文件(在 vim 中按 Esc 键后输入 :wq 回车)。

3.关闭之前启动的nginx(编译完成后nginx启动的nginx服务程序)

pkill -9  nginx

4.重新加载系统服务配置:

sudo systemctl daemon-reload

5.使用 systemctl 命令启动 nginx

sudo systemctl start nginx

四、踩坑经历

1.Ubuntu22.04缺少libssl.so.1.1

错误信息:

error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

libssl1.1自ubuntu18.04之后就从仓库中移除了,因此在使用Ubuntu22.04时,无论如何sudo apt update也无法安装和更新libss1.1,需手动安装该库。

解决办法:

  1. 在有网的设备中下载libssl1.1对应服务器版本的安装包,libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
  2. 如果上面版本不存在了,在官方库组件库中查找最新的组件,网站链接:ubuntu security openssl

参考

Ubuntu22.04.1 LTS离线编译安装Nginx_ubuntu22.04离线安装nginx-CSDN博客

在Ubuntu上离线安装Nginx的踩坑经历-阿里云开发者社区

解决Ubuntu22.04缺少libssl.so.1.1问题_libssl1.1-CSDN博客

豆包