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.
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
命令输出信息
- Nginx 安装目录
/usr/local/nginx
- Nginx 可执行文件路径
/usr/local/nginx/sbin/nginx
- Nginx 模块目录
/usr/local/nginx/modules
- Nginx 配置文件目录
/usr/local/nginx/conf
- Nginx 配置文件路径
/usr/local/nginx/conf/nginx.conf
- Nginx PID文件路径
/usr/local/nginx/logs/nginx.pid
- Nginx 访问日志路径
/usr/local/nginx/logs/access.log
- 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 命令来加载多个配位文件; 具体操作如下
- 在 nginx.conf 同级目录创建
sites-available
和sites-enabled
目录 - 将配置存放在
sites-available
目录, 通过域名来命名,如:democode.vip.conf
- 将
sites-available
目录下的文件 软链接 到sites-enabled
目录下 - 在 nginx.conf 配置文件尾部追加
include ./sites-enabled/*.conf
如此管理优点
- 如果需要关闭某一个站点, 只需删除
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
可以查看帮助文档
主要分为以下几类
--without
以 without 开头表示, 默认是开启的, 例如:--without-http
--with
以 with 开头,表示默认是关闭的,例如:--with-mail
- 基础配置命令, 如:
--prefix=PATH
- 依赖位置配置,如:
--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