在 乌班图 Ubuntu 上离线安装 Nginx
说明
- 离线安装的原理参见:离线安装:导读
- 使用的是 ubuntu-20.04.6-live-server-amd64
- 其中 live-server 代表最小化,无桌面。
- 系统安装时,全称无互联网网络,即:未更新。
- 本文使用虚拟机操作。
- 本文安装时,开启了 SSL、http2
- 本文以 nginx-1.23.4.tar.gz 离线安装为例,会安装两遍
- 第一遍有网,用于下载依赖、备份依赖、安装测试等
- 第二遍断网,使用第一遍备份的依赖,进行安装
安装
查看 Ubuntu 版本号
shelllsb_release -a
shellxuxiaowei@xuxiaowei:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.6 LTS Release: 20.04 Codename: focal xuxiaowei@xuxiaowei:~$
查看 Ubuntu 是否安装了桌面
shelldpkg -l ubuntu-desktop
下载 nginx-1.23.4.tar.gz,上传至 Ubuntu,解压、进入文件夹
shelltar -zxvf nginx-1.23.4.tar.gz cd nginx-1.23.4
在执行之前,给虚拟机新建一个快照,用于回滚测试。
尝试在有网络时的环境下编译(下载依赖,用于在没有网络的环境下使用)
shell./configure --with-http_ssl_module --with-http_v2_module make sudo make install
请查看
/var/cache/apt/archives
文件夹中是否有 .deb 安装文件,如果有,请使用sudo apt-get clean
进行清空。更新软件包索引
shell# 短时间内仅需要运行一次 sudo apt update
执行
./configure --with-http_ssl_module --with-http_v2_module
出现下列错误,说明缺少 gccshellxuxiaowei@xuxiaowei:~/nginx-1.23.4$ ./configure --with-http_ssl_module --with-http_v2_module checking for OS + Linux 5.4.0-144-generic x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found xuxiaowei@xuxiaowei:~/nginx-1.23.4$
下载 gcc、安装 gcc
shell# 下载软件,文件夹:/var/cache/apt/archives sudo apt-get -y install --download-only gcc # 本地安装 sudo apt -y install /var/cache/apt/archives/*.deb
执行
./configure --with-http_ssl_module --with-http_v2_module
出现下列错误,说明缺少 libpcre3-devshell./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.
下载 libpcre3-dev、安装 libpcre3-dev
shell# 下载软件,文件夹:/var/cache/apt/archives sudo apt-get -y install --download-only libpcre3-dev # 本地安装 sudo apt -y install /var/cache/apt/archives/*.deb
执行
./configure --with-http_ssl_module --with-http_v2_module
出现下列错误,说明缺少 libssl-devshell./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option.
下载 libpcre3-dev、安装 libssl-dev
shell# 下载软件,文件夹:/var/cache/apt/archives sudo apt-get -y install --download-only libssl-dev # 本地安装 sudo apt -y install /var/cache/apt/archives/*.deb
执行
./configure --with-http_ssl_module --with-http_v2_module
出现下列错误,说明缺少 zlib1g-devshell./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.
下载 zlib1g-dev、安装 zlib1g-dev
shell# 下载软件,文件夹:/var/cache/apt/archives sudo apt-get -y install --download-only zlib1g-dev # 本地安装 sudo apt -y install /var/cache/apt/archives/*.deb
执行
./configure --with-http_ssl_module --with-http_v2_module
出现下列内容,说明配置正常了。shellConfiguration summary + using system PCRE library + using system OpenSSL library + using system zlib library 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"
执行
make
出现下列错误,说明缺少 makeshellxuxiaowei@xuxiaowei:~/nginx-1.23.4$ make Command 'make' not found, but can be installed with: sudo apt install make # version 4.2.1-1.2, or sudo apt install make-guile # version 4.2.1-1.2 xuxiaowei@xuxiaowei:~/nginx-1.23.4$
下载 make、安装 make
shell# 下载软件,文件夹:/var/cache/apt/archives sudo apt-get -y install --download-only make # 本地安装 sudo apt -y install /var/cache/apt/archives/*.deb
执行
sudo make install
没报错,说明安装成功测试
shell/usr/local/nginx/sbin/nginx -V
显示内容如下,说明安装成功了
shellxuxiaowei@xuxiaowei:~/nginx-1.23.4$ /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.23.4 built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) built with OpenSSL 1.1.1f 31 Mar 2020 TLS SNI support enabled configure arguments: --with-http_ssl_module --with-http_v2_module xuxiaowei@xuxiaowei:~/nginx-1.23.4$
将 /var/cache/apt/archives 文件夹中的 *.deb 文件打包成 nginx-1.23.4-deb.tar.gz、备份 nginx-1.23.4-deb.tar.gz
shellsudo tar czvf nginx-1.23.4-deb.tar.gz /var/cache/apt/archives/*.deb
重置虚拟机状态,并全称断网,进行离线安装
解压 nginx-1.23.4-deb.tar.gz、安装里面的依赖
shelltar -zxvf nginx-1.23.4-deb.tar.gz # 本地安装(注意路径) sudo dpkg -i ./var/cache/apt/archives/*.deb
解压 nginx-1.23.4.tar.gz、安装 Nginx
shelltar -zxvf nginx-1.23.4.tar.gz cd nginx-1.23.4 ./configure --with-http_ssl_module --with-http_v2_module make sudo make install
测试安装结果
shell/usr/local/nginx/sbin/nginx -V
显示内容如下,说明安装成功了
shellxuxiaowei@xuxiaowei:~/nginx-1.23.4$ /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.23.4 built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) built with OpenSSL 1.1.1f 31 Mar 2020 TLS SNI support enabled configure arguments: --with-http_ssl_module --with-http_v2_module xuxiaowei@xuxiaowei:~/nginx-1.23.4$