Nginx 为各种 Linux 发行版提供预编译的软件包,方便快速安装和更新。
# 安装依赖
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
# 导入官方签名密钥
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
# 验证密钥
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
输出应包含:
573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
nginx signing key <signing-key@nginx.com>
Stable 版本:
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
Mainline 版本:
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
# 更新软件包索引
sudo apt update
# 安装 Nginx
sudo apt install nginx
# 启动 Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
sudo yum install yum-utils
创建 /etc/yum.repos.d/nginx.repo:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 安装 Stable 版本(默认)
sudo yum install nginx
# 或安装 Mainline 版本
sudo yum-config-manager --enable nginx-mainline
sudo yum install nginx
# 启动 Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 更新软件包索引
sudo apk update
# 安装 Nginx
sudo apk add nginx
# 启动 Nginx
sudo rc-service nginx start
# 设置开机自启
sudo rc-update add nginx
| 文件/目录 | 说明 |
|---|---|
/etc/nginx/nginx.conf | 主配置文件 |
/etc/nginx/conf.d/ | 额外配置文件目录 |
/etc/nginx/mime.types | MIME 类型配置 |
/var/log/nginx/ | 日志文件目录 |
/usr/share/nginx/html/ | 默认网站根目录 |
/usr/sbin/nginx | Nginx 可执行文件 |
官方软件包包含以下模块:
nginx -v
nginx -V
sudo apt update
sudo apt upgrade nginx
sudo yum update nginx
sudo apk update
sudo apk upgrade nginx
# 锁定当前版本
sudo apt-mark hold nginx
# 解锁版本
sudo apt-mark unhold nginx
# 编辑 yum.conf
sudo vim /etc/yum.conf
# 添加以下内容
exclude=nginx-*
/etc/nginx/nginx.conf:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/default.conf:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 启动
sudo systemctl start nginx
# 停止
sudo systemctl stop nginx
# 重启
sudo systemctl restart nginx
# 重新加载配置
sudo systemctl reload nginx
# 查看状态
sudo systemctl status nginx
# 启用开机自启
sudo systemctl enable nginx
# 禁用开机自启
sudo systemctl disable nginx
# 启动
sudo nginx
# 停止
sudo nginx -s stop
# 优雅退出
sudo nginx -s quit
# 重新加载配置
sudo nginx -s reload
# 重新打开日志
sudo nginx -s reopen
# 测试配置
sudo nginx -t
# 允许 HTTP
sudo firewall-cmd --permanent --add-service=http
# 允许 HTTPS
sudo firewall-cmd --permanent --add-service=https
# 重新加载防火墙
sudo firewall-cmd --reload
# 查看规则
sudo firewall-cmd --list-all
# 允许 HTTP 和 HTTPS
sudo ufw allow 'Nginx Full'
# 仅允许 HTTP
sudo ufw allow 'Nginx HTTP'
# 仅允许 HTTPS
sudo ufw allow 'Nginx HTTPS'
# 查看状态
sudo ufw status
# 允许 HTTP
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许 HTTPS
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 保存规则
sudo service iptables save
# 停止服务
sudo systemctl stop nginx
# 卸载软件包
sudo apt remove nginx nginx-common nginx-full
# 删除配置文件
sudo apt purge nginx nginx-common nginx-full
# 删除依赖
sudo apt autoremove
# 手动删除残留文件
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/lib/nginx
# 停止服务
sudo systemctl stop nginx
# 卸载软件包
sudo yum remove nginx
# 删除残留文件
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/lib/nginx
# 停止服务
sudo rc-service nginx stop
# 卸载软件包
sudo apk del nginx
# 删除残留文件
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
# 错误日志
sudo tail -f /var/log/nginx/error.log
# 访问日志
sudo tail -f /var/log/nginx/access.log
# 测试配置
sudo nginx -t
# 显示配置
sudo nginx -T
# 查看 Nginx 进程
ps aux | grep nginx
# 查看端口占用
sudo netstat -tlnp | grep :80
# 或
sudo ss -tlnp | grep :80
# 检查 Nginx 用户
ps aux | grep nginx | grep 'master process'
# 检查文件权限
ls -la /var/log/nginx/
ls -la /etc/nginx/
user nginx;
worker_processes auto; # 自动检测 CPU 核心数
events {
worker_connections 2048; # 增加连接数
}
http {
gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/json application/javascript;
}
# 查看当前限制
ulimit -n
# 临时增加
ulimit -n 65535
# 永久增加
sudo vim /etc/security/limits.conf
# 添加以下内容
* soft nofile 65535
* hard nofile 65535