Nginx 编译安装

Nginx 编译安装

以 Ubuntu 20.04 为例, 编译安装 Nginx

版本选择

各版本源码下载地址

  • Stable version 稳定版本
  • Mainline version 主线版本
  • Legacy versions 旧版本

编译错误

PCRE

./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.

pcre 下载地址

wget https://ftp.pcre.org/pub/pcre/pcre-8.44.zip
unzip pcre-8.44.zip

编译安装

下载源码并解压

wget http://nginx.org/download/nginx-1.20.0.tar.gz
tar -xvf nginx-1.20.0.tar.gz

编译 、安装

cd nginx-1.20.0
./configure --with-pcre=/home/wyj/Downloads/pcre-8.44
make
sudo make install
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin

以下是 configure 命令输出信息

  1. Nginx 安装目录 /usr/local/nginx
  2. Nginx 可执行文件路径 /usr/local/nginx/sbin/nginx
  3. Nginx 模块目录 /usr/local/nginx/modules
  4. Nginx 配置文件目录 /usr/local/nginx/conf
  5. Nginx 配置文件路径 /usr/local/nginx/conf/nginx.conf
  6. Nginx PID文件路径 /usr/local/nginx/logs/nginx.pid
  7. Nginx 访问日志路径 /usr/local/nginx/logs/access.log
  8. Nginx 错误日志路径 /usr/local/nginx/logs/error.log
Configuration summary
  + using PCRE library: /home/wyj/Downloads/pcre-8.44
  + OpenSSL library is not used
  + 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"

服务管理

Nginx 监听 80 端口, 所以需要 root 权限

启动 Nginx

sudo nginx

使用 curl localhost 命令检查服务是否启动成功

如果看到有 Welcome to nginx! 表示启动成功

停止服务

快速关闭

sudo nginx -s stop

优雅关闭

sudo nginx -s quit

如何优雅 ? ? ?

处理完当前未处理完的任务后再关闭 Nginx 服务

重启服务

优雅重启

sudo nginx -s reload

日志分割

确保在 Nginx 服务运行的情况下执行

sudo nginx -s reopen

配置管理

单站点

如果只有一个 站点 需要解析可以直接修改 nginx.conf

多站点

如果将多个站点配置放在同一个文件管理不方便, nginx 支持 include 命令来加载多个配位文件; 具体操作如下

  1. 在 nginx.conf 同级目录创建 sites-availablesites-enabled 目录
  2. 将配置存放在 sites-available 目录, 通过域名来命名,如: democode.vip.conf
  3. sites-available 目录下的文件 软链接 到 sites-enabled 目录下
  4. 在 nginx.conf 配置文件尾部追加 include ./sites-enabled/*.conf

如此管理优点

  1. 如果需要关闭某一个站点, 只需删除 sites-enabled 目录下对应站点的软链接即可, 而不必删除真正的配置文件

include 支持正则表达式, 可以通过 * 加载所有的配置;也可以将一个一个加载配置文件(比较繁琐)

include ./sites-enabled/www.democode.vip.conf
include ./sites-enabled/image.democode.vip.conf
include ./sites-enabled/code.democode.vip.conf

configure

./configure --help 可以查看帮助文档

主要分为以下几类

  1. --without 以 without 开头表示, 默认是开启的, 例如: --without-http
  2. --with 以 with 开头,表示默认是关闭的,例如: --with-mail
  3. 基础配置命令, 如:--prefix=PATH
  4. 依赖位置配置,如: --with-pcre=DIR

对于默认开的的模块, 如果不需要该模块, 需要手动关闭

./configure --without-http

对于默认关闭的模块, 如果需要该模块, 需要手动开启

./configure --with-mail

对于基础配置,一般都有默认值,如 --prefix 默认值 /usr/local/nginx (在 ./configure 命令执行成功之后,终端输出信息中可以看到)

对于依赖,如果已经安装并添加到 PATH 环境变量可以忽略配置,否则需要手动指定依赖位置, 否则会报错; 例如:

./configure --with-pcre=/home/wyj/Downloads/pcre-8.44

如何查看默认安装的模块

通过查看源码目录下的的 ngx_modules.c 文件, 命令 cat objs/ngx_modules.c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值