[TOC] #### 1. 环境介绍 --- 本文使用的操作系统及软件介绍: | 名称 | 描述 | 文章 | | ------------ | ------------ | ------------ | | Windows 10 专业版 | 在 Windows 系统上使用虚拟机软件 | | | Oracle VirtualBox | 虚拟机软件(Windows 版本) | [VirtualBox 介绍及安装](https://www.itqaq.com/index/627.html) | | ubuntu-24.04.3-live-server-amd64.iso | Ubuntu24.04 LTS 服务器版的镜像文件 | [Ubuntu 镜像文件下载地址](https://ubuntu.com/download/server) | | Oh My Zsh | Zsh 终端配置管理工具 | [Oh My Zsh 介绍及安装](https://www.itqaq.com/index/362.html) | | zsh-autosuggestions | 根据历史命令自动提示并补全命令 | [Oh My Zsh 第三方插件](https://www.itqaq.com/index/568.html) | | zsh-syntax-highlighting | 检测命令是否存在,命令高亮显示 | [Oh My Zsh 第三方插件](https://www.itqaq.com/index/568.html) | 网络可以设置为 `桥接网卡`,运行以下命令,可以查看局域网 IP: ```bash ip addr hostname -I ``` 系统安装完成后,如果无法连接 SSH,则需要安装 `openssh-server` 服务: ```bash sudo apt install openssh-server -y sudo systemctl start ssh sudo systemctl enable ssh ``` Oh My Zsh、zsh-autosuggestions、zsh-syntax-highlighting 是为了方便命令操作安装的插件,非必须安装 ```bash # 切换为 zsh 终端 sudo apt install zsh -y chsh -s $(which zsh) # 安装 Oh My Zsh sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)" ``` #### 2. 编译安装 --- ##### 准备编译环境 更新软件源并安装必要的编译工具和依赖库,Nginx 需要 PCRE(正则)、Zlib(压缩)、OpenSSL(加密)等库的支持 + build-essential libpcre3-dev zlib1g-dev 编译必需的依赖包 + libssl-dev 是 `--with-http_ssl_module` 所需要的依赖包 + libxml2-dev libxslt1-dev 是 `--with-http_xslt_module` 所需要的依赖包 + libgd-dev 是 `--with-http_image_filter_module` 所需要的依赖包 ```bash sudo apt update sudo apt install -y build-essential libpcre3-dev zlib1g-dev libssl-dev libxml2-dev libxslt1-dev libgd-dev ``` ##### 下载解压源码 前往 [Nginx 官网](https://nginx.org) 获取稳定版(Stable)的下载链接,我下载的是当前的最新稳定版 ```bash cd /usr/local/src sudo wget https://nginx.org/download/nginx-1.28.2.tar.gz sudo tar -zxvf nginx-1.28.2.tar.gz cd nginx-1.28.2 ``` ##### 配置编译选项 + -\-prefix:安装位置 + -\-with-xxx:按需启动模块,生产环境建议开启:ssl、v2、realip 等 ```bash sudo ./configure \ --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-http_xslt_module \ --with-http_image_filter_module \ --with-debug ``` 当看到以下输出结果时,说明编译成功 ```plaintext creating objs/Makefile Configuration 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" ``` ##### 编译与安装 ```bash # 1. 编译(利用多核加速,-j 后跟 CPU 核心数,可以用 nproc 命令查看核心数) sudo make -j$(nproc) # 2. 安装 sudo make install ``` #### 3. 后续配置和验证 --- ##### 验证目录结构 ```bash # 检查 /usr/local/nginx 的结构是否符合预期 ls -l /usr/local/nginx # 预期输出包含: # conf/ (配置文件 nginx.conf 所在) # html/ (默认静态页面) # logs/ (日志目录 access.log, error.log) # sbin/ (可执行文件 nginx 所在) ``` ##### 测试配置文件 ```bash # 测试配置文件语法是否正确 sudo /usr/local/nginx/sbin/nginx -t # 配置文件语法正确时,输出结果: # nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok # nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful ``` ##### Nginx 服务命令 ```bash # 只查看 nginx 版本信息 sudo /usr/local/nginx/sbin/nginx -v # 查看 nginx 版本信息以及编译选项 sudo /usr/local/nginx/sbin/nginx -V # 启动 nginx 服务 sudo /usr/local/nginx/sbin/nginx # 停止 nginx 服务 sudo /usr/local/nginx/sbin/nginx -s stop # 重载配置,不会中断服务 sudo /usr/local/nginx/sbin/nginx -s reload ``` ##### 配置软链接 要让 `nginx` 在任意目录都能直接使用,有以下几种方案: + 创建一个软链接到系统已有的 PATH 目录中 + 将二进制文件的路径添加到系统的环境变量中 ```bash # 创建软链接 sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx # 补充:如果想要删除配置的软链接,只需运行以下命令即可 sudo rm /usr/local/bin/nginx ``` ##### 配置 Nginx 服务 ```bash sudo vim /etc/systemd/system/nginx.service ``` ```plaintext [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid 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 PrivateDevices=true ProtectHome=true ReadWritePaths=/usr/local/nginx/logs /usr/local/nginx/client_body_temp [Install] WantedBy=multi-user.target ``` nginx 服务命令: ```bash # 查看 nginx 服务状态 sudo systemctl status nginx # 启用开机自启 sudo systemctl enable nginx # 禁用开机自启 sudo systemctl disable nginx # 启动 nginx 服务 sudo systemctl start nginx # 关闭 nginx 服务 sudo systemctl stop nginx # 重启 nginx 服务 sudo systemctl restart nginx # 重载 nginx 配置,不会中断服务 sudo systemctl reload nginx ``` #### 4. 遇到的问题 --- 没有安装任何依赖时候,遇到的各种问题整理 直接运行编译命令,不指定任何模块 ```bash sudo ./configure ``` ##### C 编译器 这个错误是因为系统中没有安装 C 编译器,`./configure` 脚本需要编译器来检查系统环境并生成 `Makefile ` ```plaintext ./configure: error: C compiler cc is not found ``` 解决方案:安装 `build-essential` 包,它包含了 `gcc`、`g++` 和 `make` 等核心编译工具 ```bash sudo apt install build-essential -y ``` ##### HTTP rewrite 这个错误是因为编译 `Nginx` 的 `HTTP rewrite` 模块需要 `PCRE` 库的支持,但是当前系统中缺少该依赖库 ```plaintext ./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. ``` 解决方案: ```bash sudo apt install libpcre3-dev -y ``` ##### HTTP gzip 这个错误是因为编译 `Nginx` 的 `HTTP gzip` 模块需要 `zlib` 库的支持,而系统中缺少该依赖库 ```plaintext ./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. ``` 解决方案: ```bash sudo apt install zlib1g-dev -y ``` ##### OpenSSL ```bash sudo ./configure --with-http_ssl_module ``` 这个错误是因为 Nginx 的 SSL/HTTPS 模块需要 OpenSSL 库的支持,而系统中缺少 OpenSSL 的开发文件 ```plaintext ./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. ``` ##### XSLT 模块 `--with-http_xslt_module` 提供 XML 解析支持,比如:如果需要在反向代理时动态转换 XML 数据 ```bash sudo ./configure --with-http_xslt_module ``` 解决方案: ```bash sudo apt install libssl-dev -y ``` ```plaintext ./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries. ``` 解决方案: ```bash sudo apt install -y libxml2-dev libxslt1-dev ``` ##### HTTP Image Filter 图像过滤模块,允许 Nginx 在传输图片时实时进行缩放、裁剪、旋转或格式转换 ```bash sudo ./configure --with-http_image_filter_module ``` 这个错误表明启用了 HTTP Image Filter 模块,但系统中缺少 GD 图形库的开发文件 ```plaintext ./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries. ``` 解决方案: ```bash # 不安装 libjpeg-dev libpng-dev 也可以编译成功 sudo apt install -y libgd-dev libjpeg-dev libpng-dev ``` #### 5. 文件存放位置 --- 思考源码编译安装 Nginx 的整个过程,源码包存放位置、Nginx 安装位置应该放到哪里更合适 ? 为了符合 Linux 文件系统层次结构标准(FHS),并便于后续维护、升级和管理,建议采用以下 “最佳实践” 方案: ##### 源码存放目录 源码包下载与编译目录:`/usr/local/src` 它是 FHS 标准中用于存放本地编译软件源代码的目录,将源码集中在此处,方便版本管理、重新编译或排查问题,且不会污染用户主目录或临时目录 ##### 软件安装位置 推荐路径:`/usr/local/nginx` 安装到该目录的理由: + 规范性:`/usr/local` 是专门用于存放管理员手动安装的软件的标准目录,备份和迁移时只需关注此目录即可 + 隔离性:`apt` 安装的 `nginx` 位于 `/usr/sbin`、`/etc/nginx` 等系统目录,避免冲突,实现共存和独立管理