0
点赞
收藏
分享

微信扫一扫

nginx编译安装

浮游图灵 2024-04-21 阅读 14



在 Ubuntu 上编译安装 Nginx 而非使用包管理器(如 apt)安装,可以让您获得更多的定制选项,比如指定 Nginx 的版本或者添加特定的模块。下面是编译安装 Nginx 的基本步骤:

1. 更新系统并安装依赖

首先,确保系统是最新的,并安装必要的构建工具和依赖库。打开终端并输入以下命令:

sudo apt update
sudo apt upgrade -y
sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev libgd-dev libxml2-dev

这里的依赖项包括:

  • build-essential:提供编译所需的工具,如 gccmake 等。
  • libpcre3-dev:PCRE 库用于正则表达式的解析。
  • zlib1g-dev:用于数据压缩,Nginx 可以用它来gzip压缩响应内容。
  • libssl-dev:OpenSSL 库,用于 SSL/TLS 支持。
  • 其他如 libgd-dev 和 libxml2-dev 根据需要选择安装,它们分别用于图像处理和 XML 处理功能,不是所有场景都需要。

2. 下载 Nginx 源代码

访问 Nginx 官方网站(https://nginx.org/en/download.html)找到最新稳定版的源代码包,或者选择特定版本下载。下载后,解压到适当位置,例如 /usr/local/src/

wget http://nginx.org/download/nginx-1.19.3.tar.gz  # 请替换为实际的版本链接
tar -zxvf nginx-1.19.3.tar.gz
cd nginx-1.19.3

3. 配置 Nginx

接下来,配置 Nginx 以包含所需的模块和指定安装路径。这一步是编译安装的核心,您可以根据需要调整配置选项:

./configure --prefix=/usr/local/nginx \
            --with-pcre \
            --with-openssl=/home/ivan/nginx-1.18.0/openssl \
            --with-http_ssl_module \
            --with-http_v2_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-http_image_filter_module \
            --with-http_slice_module \
            --with-mail \
            --with-threads \
            --with-file-aio \
            --with-stream \
            --with-mail_ssl_module \
            --with-stream_ssl_module 

这里的一些常见选项含义:

  • --with-pcre: 这个选项指定了 PCRE(Perl Compatible Regular Expressions) 库的路径。PCRE 库是 Nginx 处理正则表达式所必需的,用于诸如 location 规则匹配等功能。如果没有指定路径,默认会从系统中查找 PCRE 库。如果您之前已经安装了 PCRE 开发库并且配置了正确的环境,通常不需要指定这个路径。
  • --with-openssl=/home/ivan/nginx-1.18.0/openssl: 这个选项指定了 OpenSSL 库的源代码路径。OpenSSL 是一个强大的安全套接字层密码库,它提供了 SSL 协议和 TLS 协议的支持,使得 Nginx 能够提供 HTTPS 服务。指定这个路径意味着您希望 Nginx 使用特定版本或自定义配置的 OpenSSL 库进行编译。这里提到的路径 /home/ivan/nginx-1.18.0/openssl 应当是指向 OpenSSL 源代码的目录,您需要确保在此路径下已经正确地配置并准备好了 OpenSSL。
  • --with-http_ssl_module: 启用 HTTPS 协议支持,允许 Nginx 服务器处理 SSL/TLS 加密的网页请求。
  • --with-http_v2_module: 启用 HTTP/2 协议支持,提供更高的网页加载速度和效率。
  • --with-http_realip_module: 允许更改客户端请求头中的 $remote_addr 变量,以解决反向代理或负载均衡场景下的 IP 记录问题。
  • --with-http_addition_module--with-http_sub_module: 分别用于在响应内容中添加或替换文本,常用于头部插入、内容修改等。
  • --with-http_dav_module: 支持 WebDAV 协议,允许用户通过 HTTP 协议对远程文件进行增删改查操作。
  • --with-http_flv_module--with-http_mp4_module: 支持 FLV 和 MP4 流式传输,适用于视频点播服务。
  • --with-http_gunzip_module--with-http_gzip_static_module: 分别支持接收和发送 gzip 压缩的内容,提高内容传输效率。
  • --with-http_random_index_module: 当目录索引文件(如index.html)不存在时,随机返回该目录下的一个文件作为索引页。
  • --with-http_secure_link_module: 提供安全链接验证,常用于保护静态资源不被非法访问。
  • --with-http_stub_status_module: 提供简单的服务器状态监控页面,展示活动连接、请求处理等信息。
  • --with-http_auth_request_module: 用于实现基于子请求的外部认证,常与后端认证服务结合使用。
  • --with-http_image_filter_module: 实现基本的图片处理功能,如尺寸调整等,但这个模块性能上可能不如专门的图片处理服务。
  • --with-http_slice_module: 支持将大响应体切片,便于高效传输大数据文件。
  • --with-mail--with-mail_ssl_module: 启用邮件代理服务器功能及 SSL/TLS 支持,适用于搭建邮件服务。
  • --with-threads: 支持 Nginx 使用多线程处理请求,虽然 Nginx 主要设计为异步事件驱动模型,但某些模块或场景可能需要此选项。
  • --with-file-aio: 支持异步I/O,可以提升文件操作的性能。
  • --with-stream--with-stream_ssl_module: 启用 TCP/UDP 流模块及其 SSL/TLS 支持,适用于代理和负载均衡TCP/UDP服务,如HTTPS、SMTPS等。


4. 编译与安装

配置完成后,开始编译并安装 Nginx:

make
sudo make install

5. 创建 Nginx 系统服务

为了方便管理 Nginx 服务,可以创建一个 systemd 服务单元文件:

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

然后,粘贴以下内容并保存:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target

6. 启动并设置开机启动

完成上述步骤后,启动 Nginx 并设置为开机启动:

sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx

至此,您已成功编译安装了 Nginx。可以通过访问 http://your_server_ip 来检查 Nginx 是否正常运行。记得替换 your_server_ip 为您的服务器 IP 地址。

举报

相关推荐

0 条评论