通常情况下正常安装 Nginx 已经能够满足大部分人的需求,如果有其他需求,可以通过配置参数来定制化安装,以满足您的特定需求在此之前,我们需要了解 Nginx 安装参数

Nginx 编译配置参数及其说明

内置参数默认原则

  • --with 显示加上,默认不内置
  • --without 显示去掉,默认内置
  1. --prefix=PATH: 指定 Nginx 安装的路径,默认为 /usr/local/nginx。您可以修改为其他路径,例如 --prefix=/opt/nginx
  2. --user: 指定运行 Nginx 的 worker 子进程属主
  3. --group: 指定运行 Nginx 的 worker 子进程属组
  4. --pid-path=PATH: 指定 Nginx 进程 ID 文件的路径,默认为 $prefix/logs/nginx.pid
  5. --sbin-path=PATH: 指定 Nginx 可执行文件的路径,默认为 $prefix/sbin/nginx
  6. --conf-path=PATH: 指定 Nginx 配置文件的路径,默认为 $prefix/conf/nginx.conf
  7. --error-log-path=PATH: 指定错误日志文件的路径,默认为 $prefix/logs/error.log
  8. --http-log-path=PATH: 指定 HTTP 访问日志文件的路径,默认为 $prefix/logs/access.log
  9. --with-http_ssl_module: 启用 HTTPS 支持,需要 OpenSSL 库。
  10. --with-http_v2_module: 启用 HTTP/2 支持。
  11. --with-http_gzip_static_module: 启用 gzip 静态文件压缩支持。
  12. --with-http_stub_status_module: 启用 stub 状态模块,用于监控和报告服务器状态。
  13. --with-pcre: 启用 PCRE 正则表达式库支持,用于 HTTP 模块的正则匹配。
  14. --with-zlib: 启用 zlib 压缩库支持。
  15. --with-threads: 启用线程支持。
  16. --with-stream: 启用 Stream 模块,用于 TCP 和 UDP 代理。
  17. --with-mail: 启用 Mail 模块,用于邮件代理。
  18. --add-module=PATH: 添加第三方模块,将模块的源代码路径传递给此参数。
  19. --without-http_rewrite_module: 禁用 HTTP 重写模块。
  20. --without-http_gzip_module: 禁用 HTTP gzip 模块。
  21. --without-http_charset_module: 禁用 HTTP 字符集模块。
  22. --without-http_fastcgi_module: 禁用 HTTP FastCGI 模块。
  23. --without-http_uwsgi_module: 禁用 HTTP uWSGI 模块。
  24. --without-http_scgi_module: 禁用 HTTP SCGI 模块。
  25. --without-mail_pop3_module: 禁用 Mail POP3 模块。
  26. --without-mail_imap_module: 禁用 Mail IMAP 模块。
  27. --without-mail_smtp_module: 禁用 Mail SMTP 模块。

实现 Nginx 定制化安装

安装了必要的开发工具、依赖库和编译器

sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev

下载 Nginx 源代码

前往 Nginx 的官方网站(nginx.org/)下载最新版本的源代码… wget 命令下载

wget http://nginx.org/download/nginx-1.21.3.tar.gz

解压并进入源代码目录:

tar -zxvf nginx-1.21.3.tar.gz
cd nginx-1.21.3

Nginx 配置编译选项

使用 ./configure 命令来配置编译选项。你可以根据你的需求添加各种模块、指定安装目录等。以下是一个示例配置:

./configure --prefix=/usr/local/nginx \
            --with-http_ssl_module \
            --with-http_gzip_static_module \
            --with-http_stub_status_module \
            --with-pcre \
            --with-zlib

在这个示例中,我们启用了 SSL、Gzip 压缩、状态统计等模块。

编译和安装

执行 make 命令编译 Nginx,然后使用 make install 命令安装到指定目录:

make
sudo make install

配置和启动 Nginx

在安装完成后,你可以进入 Nginx 的安装目录(例如 /usr/local/nginx)配置 Nginx,主要配置文件为 conf/nginx.conf。根据你的需求进行配置,然后启动 Nginx:

/usr/local/nginx/sbin/nginx

验证

打开浏览器并访问服务器的 IP 地址或域名,确认 Nginx 是否正常工作。如果配置了 SSL,可以尝试使用 HTTPS 访问。

Nginx 体系化之定制化安装 Nginx_HTTP

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: nginx