前言

在生产环境中,很多机器是部署在内网的没有互联网访问权限,难免需要进行本地离线部署,本文详细说明了在不同操作系统中,多种Nginx离线部署方式。

Ubantu 24.04

1、通过下载deb离线包安装

1)使用apt-get download命令

在联网的Ubuntu计算机上(用相同的镜像在虚拟机装一下),你可以使用apt-get download命令来下载指定的软件包(但是没办法下载对应的依赖)。例如,要下载名为package-name的软件包,可以运行以下命令

sudo apt-get download package-name

2.1)使用aptitude的--download-only选项

aptitude是apt的一个高级前端,提供了更多的功能和选项。你可以使用aptitude--download-only选项来下载软件包而不进行安装。例如:

#先安装aptitude
apt install aptitude -y

sudo aptitude --download-only install package-name

下载的软件包将存储在/var/cache/apt/archives目录中。

2.2)使用apt-cache depends和apt-get download命令下载所有依赖项

#使用`apt-cache depends`命令来列出软件包的依赖项。
apt-cache depends package-name
#使用apt-get download命令来下载这些依赖项。
apt-get download $(apt-cache depends package-name | grep Depends | cut -d: -f2 | tr -d "<>")

2.3)使用apt-get install --reinstall -d命令

以使用apt-get install --reinstall -d命令来下载指定的软件包及其依赖关系,而不进行安装。

sudo apt-get install --reinstall -d package-name

这将下载指定的软件包及其依赖关系到/var/cache/apt/archives目录中。

3)将下载的包复制到离线系统

无论你使用哪种方法下载离线包,将这些包复制到没有互联网连接的Ubuntu计算机上。

4)在离线系统上安装包

在离线系统上,你可以使用dpkg -i命令来安装下载的.deb软件包。注意:要是版本低,会把当前版本覆盖导致版本降级出现系统奔溃。

sudo dpkg -i /path/to/package-name_<version>_<architecture>.deb

#建议用以下命令,避免系统奔溃,-E跳过版本与已安装软件版本相同的软件包,-G跳过版本早于已安装软件版本的的软件包。
dpkg -i -E -G *.deb

如果存在依赖问题,你可以使用apt-get install -f命令来修复。

安装完成检查nginx状态

image-20241112220636385

2、编译安装

1)更新软件包

#更新软件包
root@youyou-VMware-Virtual-Platform:~# apt update

2)安装编译依赖项

这边需要通过相同镜像虚拟机把deb安装包下载下来,拷贝到服务器上安装

aptitude --download-only install build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev

这些依赖项包括:

  • build-essential:包含一些必要的软件包,如编译器和make工具等,用于编译安装Nginx。
  • libpcre3:PCRE(Perl兼容正则表达式库),Nginx中默认启用该模块。
  • libpcre3-dev:PCRE开发库,用于编译Nginx PCRE模块。
  • zlib1g-dev:Zlib库提供了压缩和解压缩数据的函数,该库被Nginx使用,并且默认启用gzip模块。
  • libssl-dev:SSL(Secure Sockets Layer)提供了安全的通信渠道,该模块使Nginx能够支持HTTPS协议。

3)下载并解压Nginx源码包

从Nginx官方网站或其他可信的源获取最新版本的源码包。然后,解压源码包并进入解压后的目录,下载地址:http://nginx.org/download/

我们这边下载nginx-1.20.2.tar.gz

下载好后将安装包拷贝到服务器中

解压安装包,并进入到安装包目录

root@youyou-VMware-Virtual-Platform:/home/youyou# tar xf nginx-1.20.2.tar.gz
root@youyou-VMware-Virtual-Platform:/home/youyou# cd nginx-1.20.2/
root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
# ./configure --prefix=/你的安装目录 --add-module=/第三方模块目录 
#运行配置脚本,这里的配置选项可以根据你的需求进行调整。例如,如果你需要启用其他模块,可以在命令末尾添加相应的--with-...选项。
root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

#编译nginx
root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# make

#安装Nginx
root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# make install

#查看nginx版本,能出现说明安装完成
root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.20.2

配置Nginx为系统服务

root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# cat <<eof>/etc/systemd/system/nginx.service
> [Unit]
> Description=nginx - high performance web server
> Documentation=http://nginx.org/en/docs/
> After=network-online.target remote-fs.target nss-lookup.target
> 
> [Service]
> Type=forking
> ExecStartPre=/usr/local/nginx/sbin/nginx -t
> ExecStart=/usr/local/nginx/sbin/nginx
> ExecReload=/usr/local/nginx/sbin/nginx -s reload
> ExecStop=/bin/kill -s QUIT $MAINPID
> PrivateTmp=true
> 
> [Install]
> WantedBy=multi-user.target
> eof

重新加载systemd配置

root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# systemctl daemon-reload

创建nginx运行用户

root@youyou-VMware-Virtual-Platform:/home/youyou/nginx-1.20.2# useradd www

启动并查看nginx运行状态

root@youyou-VMware-Virtual-Platform:/usr/local/nginx# systemctl start nginx
root@youyou-VMware-Virtual-Platform:/usr/local/nginx# systemctl status nginx
● nginx.service - nginx - high performance web server
     Loaded: loaded (/etc/systemd/system/nginx.service; disabled; preset: enabled)
     Active: active (running) since Sun 2024-11-17 17:21:17 CST; 51s ago
       Docs: http://nginx.org/en/docs/
    Process: 9794 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 9795 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 9797 (nginx)
      Tasks: 2 (limit: 4558)
     Memory: 1.8M (peak: 1.9M)
        CPU: 14ms
     CGroup: /system.slice/nginx.service
             ├─9797 "nginx: master process /usr/local/nginx/sbin/nginx"
             └─9798 "nginx: worker process"

11月 17 17:21:17 youyou-VMware-Virtual-Platform systemd[1]: Starting nginx.service - nginx - high performance web server...
11月 17 17:21:17 youyou-VMware-Virtual-Platform nginx[9794]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax>
11月 17 17:21:17 youyou-VMware-Virtual-Platform nginx[9794]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is su>
11月 17 17:21:17 youyou-VMware-Virtual-Platform systemd[1]: Started nginx.service - nginx - high performance web server.

配置nginx配置文件软连接

root@youyou-VMware-Virtual-Platform:/usr/local/nginx# ln -s /usr/local/nginx/conf /etc/nginx

到此ubantu下nginx的编译安装完成,感谢支持!!!

Rocky Linux release 8.8

1、通过下载rpm离线包安装

1)使用repotrack命令

替换yum源,浙江大学yum源非常不错:https://mirrors.zju.edu.cn

[root@nginx-proxy ~]# sed -e 's|^mirrorlist=|#mirrorlist=|g' \
>     -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.zju.edu.cn/rocky|g' \
>     -i.bak \
>     /etc/yum.repos.d/Rocky-AppStream.repo \
>     /etc/yum.repos.d/Rocky-BaseOS.repo \
>     /etc/yum.repos.d/Rocky-Extras.repo \
>     /etc/yum.repos.d/Rocky-PowerTools.repo
[root@nginx-proxy ~]# yum clean all
32 个文件已删除
[root@nginx-proxy ~]# yum makecache

开始安装插件,repotrack是yum-utils包中的一个工具,它可以用来下载软件包及其全部依赖项


#安装使用yum install yum-utils
[root@nginx-proxy ~]# yum install yum-utils -y

2)使用repotrack命令下载依赖包

#创建存放nginx安装包的目录
[root@nginx-proxy ~]# mkdir nginx_rpm
[root@nginx-proxy ~]# cd nginx_rpm/
[root@nginx-proxy nginx_rpm]# pwd
/root/nginx_rpm

#开始下载nginx依赖包
[root@nginx-proxy nginx_rpm]# repotrack nginx

#下载完成,229个依赖包
[root@nginx-proxy nginx_rpm]# ls -l|wc -l
229

3)将依赖包拷贝到服务器上进行安装

#进入到rpm目录,安装所有包
[root@p2b nginx_rpm]# yum install --skip-broken *

#安装成功,验证nginx状态
[root@p2b nginx_rpm]# systemctl start nginx
[root@p2b nginx_rpm]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2024-11-17 06:28:08 EST; 8s ago
  Process: 7319 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 7317 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 7316 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 7321 (nginx)
    Tasks: 2 (limit: 4497)
   Memory: 3.7M
   CGroup: /system.slice/nginx.service
           ├─7321 nginx: master process /usr/sbin/nginx
           └─7322 nginx: worker process

11月 17 06:28:07 p2b systemd[1]: Starting The nginx HTTP and reverse proxy server...
11月 17 06:28:07 p2b nginx[7317]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
11月 17 06:28:07 p2b nginx[7317]: nginx: configuration file /etc/nginx/nginx.conf test is successful
11月 17 06:28:07 p2b systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
11月 17 06:28:08 p2b systemd[1]: Started The nginx HTTP and reverse proxy server.

2、编译安装

1、安装依赖库

#通过方法1准备好编译环境依赖包
#安装编译环境
[root@nginx-proxy gcc_yl]# yum install *

将安装包上传到服务器

[root@nginx-proxy ~]# tar xf nginx-1.20.2.tar.gz
[root@nginx-proxy ~]# cd nginx-1.20.2/
[root@nginx-proxy nginx-1.20.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
# ./configure --prefix=/你的安装目录 --add-module=/第三方模块目录 
#运行配置脚本,这里的配置选项可以根据你的需求进行调整。例如,如果你需要启用其他模块,可以在命令末尾添加相应的--with-...选项。
[root@nginx-proxy nginx-1.20.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

#编译nginx
[root@nginx-proxy nginx-1.20.2]# make

#安装Nginx
[root@nginx-proxy nginx-1.20.2]# make install

#查看nginx版本,能出现说明安装完成
[root@nginx-proxy nginx-1.20.2]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.20.2

配置Nginx为系统服务

[root@nginx-proxy nginx-1.20.2]# cat <<eof>/etc/systemd/system/nginx.service
> [Unit]
> Description=nginx - high performance web server
> Documentation=http://nginx.org/en/docs/
> After=network-online.target remote-fs.target nss-lookup.target
> 
> [Service]
> Type=forking
> ExecStartPre=/usr/local/nginx/sbin/nginx -t
> ExecStart=/usr/local/nginx/sbin/nginx
> ExecReload=/usr/local/nginx/sbin/nginx -s reload
> ExecStop=/bin/kill -s QUIT $MAINPID
> PrivateTmp=true
> 
> [Install]
> WantedBy=multi-user.target
> eof

剩下的步骤就和Ubantu 24.04安装一样了,我就不在赘述。

结语

在生产环境中,我们难免需进行离线软件部署或者将提前准备好的软件包批量上传到不同的服务器进行批量配置部署,这都离不开软件包的下载。本文核心就是通过yum或apt命令,先将软件依赖包保存到本地,再进行打包处理使得能够在无网络的主机中进行离线安装。通过此方法大部分软件离线安装问题皆可解决。

标签: linux