前言
Nginx是一款轻量级的 HTTP 服务器,也可以用于服务端的反向代理和负载均衡。在 Debian/Ubuntu 系统可以通过 apt 进行安装,但是这里主要讲如何通过源码进行编译安装。演示的系统是 Debian,Ubuntu系统尚未测试,可参考。
环境准备
1.查询软件包更新
apt update
2.安装gcc编译器
apt install -y build-essential
3.安装正则库
apt install -y libpcre3 libpcre3-dev
4.安装zlib库
apt install -y zlib1g-dev
5.安装OpenSSL库
apt install -y openssl libssl-dev
6.UFW 防火墙放行服务端口
ufw allow http
ufw allow https
7.下载Nginx源码
wget https://nginx.org/download/nginx-1.24.0.tar.gz
最新版下载地址可以在 Nginx 官网查看并复制
8.解压源码压缩包
tar -xf nginx-1.24.0.tar.gz
9.进入源码文件夹
cd nginx-1.24.0
编译并安装
1.编译前的配置(整块复制粘贴)
./configure \
--prefix=/usr/local/nginx \
--user=www-data \
--group=www-data \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
输出内容:
Configuration summary
+ using threads
+ 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"
nginx configuration file: "/usr/local/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
2.编译并安装
make && make install
系统配置
1.编辑系统变量配置文件,添加 nginx 命令的变量
nano /etc/profile
在文件底部添加下方内容:
# Nginx 环境变量
PATH=$PATH:/usr/local/nginx/sbin
export PATH
2.添加后保存并退出,保存:Ctrl+O,退出:Ctrl+X
3.刷新系统变量
source /etc/profile
4.创建 systemd 守护服务
nano /usr/lib/systemd/system/nginx.service
插入以下内容后保存并退出:
[Unit]
Description=Nginx
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
5.重新加载 systemctl ,使新增的服务生效
systemctl daemon-reload
6.设置 nginx 在开机时启动
systemctl enable nginx
Nginx 初始化配置
1.创建文件夹用于存放缓存文件
mkdir -p /var/cache/nginx/client_temp
2.创建文件夹用于存放虚拟主机配置文件
mkdir -p /usr/local/nginx/sites-available
3.创建文件夹用于存放虚拟主机配置文件的软连接
mkdir -p /usr/local/nginx/sites-enabled
4.创建文件夹用于存放网站文件
mkdir -p /www/wwwroot
5.创建文件夹用于存放网站证书
mkdir -p /www/cert
6.备份Nginx主配置文件
mv /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf.bak
7.新建Nginx主配置文件
nano /usr/local/nginx/nginx.conf
插入以下代码:
# 定义 nginx 运行用户
user www-data;
# pid 文件路径
pid /var/run/nginx.pid;
# 运行进程数(建议设置为等于 CPU 线程数)
worker_processes 2;
# 最大打开文件数
worker_rlimit_nofile 65535;
events {
# Nginx 事件处理模型
use epoll;
# 同时接收多个新连接
multi_accept on;
# 单个进程允许的客户端最大连接数
worker_connections 65535;
}
http {
# 默认编码
charset utf-8;
# 开启文件的高效传输模式
sendfile on;
# 激活 TCP_CORK socket(阻塞住此头部数据,与之后的 sendfile 数据一同发送,优化吞吐性能)
tcp_nopush on;
# 小的数据包不等待直接传输
tcp_nodelay on;
# 保持链接超时设定(实现服务器与客户端之间的长连接,减少系统对TCP连接的建立和销毁的开销)
keepalive_timeout 65;
# 隐藏 Nginx 版本号
server_tokens off;
# 设定文件不存在的错误是否写入日志
log_not_found off;
# 哈希的最大值,影响散列表的冲突率,值越大消耗内存越多,但散列 key 的冲突率会降低。
types_hash_max_size 2048;
# 设置每个散列桶占用的内存大小
types_hash_bucket_size 64;
# 限制上传文件的大小
client_max_body_size 128M;
# MIME 媒体类型配置
include mime.types;
default_type application/octet-stream;
# 日志配置
# 全局访问日志文件
access_log /var/log/nginx/access.log;
# 全局错误日志文件和记录级别设定
error_log /var/log/nginx/error.log warn;
# SSL 会话复用配置(TTPS性能优化)
# 指定客户端可以重用会话参数的时间
ssl_session_timeout 1d;
# SSL session 缓存区大小(1M缓存可容纳4000个会话)
ssl_session_cache shared:SSL:10m;
# 关闭浏览器的 Session Ticket 缓存
ssl_session_tickets off;
# SSL 安全配置
# 允许的 SSL 协议
ssl_protocols TLSv1.2 TLSv1.3;
# SSL 加密套件
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
# 加载其他配置文件
include /etc/nginx/conf.d/*.conf;
# 加载启用的站点配置文件
include /etc/nginx/sites-enabled/*;
}
8.测试Nginx配置文件
nginx -t
输出内容:
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
9.运行Nginx
systemctl start nginx
10.查看 nginx 运行状态
systemctl status nginx
11.nginx 的安装至此已经完成,如果需要新建站点,可以按以下步骤操作
(1)在 /usr/local/nginx/sites-available 目录创建配置文件
nano /usr/local/nginx/sites-available/test.conf
(2)在/usr/local/nginx/sites-enabled 目录创建配置文件的软链接以启用站点
ln -s /usr/local/nginx/sites-available/test.conf /usr/local/nginx/sites-enabled/test.conf
(3)测试配置文件
nginx -t
(4)重新加载 nginx
systemctl reload nginx