您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Linux怎么安装Nginx及注意事项是什么
## 前言
Nginx作为一款高性能的Web服务器和反向代理服务器,在现代互联网架构中扮演着重要角色。本文将详细介绍在Linux系统上安装Nginx的完整流程,并深入探讨安装前后的关键注意事项,帮助开发者避免常见陷阱。
---
## 目录
1. [Nginx简介与安装准备](#1-nginx简介与安装准备)
2. [通过包管理器安装Nginx](#2-通过包管理器安装nginx)
3. [从源代码编译安装Nginx](#3-从源代码编译安装nginx)
4. [Nginx基础配置详解](#4-nginx基础配置详解)
5. [SSL/TLS证书配置](#5-ssltls证书配置)
6. [性能调优与安全加固](#6-性能调优与安全加固)
7. [常见问题排查](#7-常见问题排查)
8. [最佳实践总结](#8-最佳实践总结)
---
## 1. Nginx简介与安装准备
### 1.1 Nginx核心优势
- 事件驱动的异步架构
- 低内存消耗(约2.5MB/10k非活跃HTTP连接)
- 支持高达50,000并发连接(默认配置)
- 热部署能力(无需停机升级)
### 1.2 环境检查
```bash
# 检查系统版本
lsb_release -a
cat /etc/os-release
# 检查内核版本
uname -r
# 检查端口占用
sudo netstat -tulnp | grep -E '80|443'
# Ubuntu/Debian
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
# CentOS/RHEL
sudo yum groupinstall "Development Tools"
sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 添加官方仓库
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
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
# 安装稳定版
sudo apt update
sudo apt install nginx
# 添加仓库
cat > /etc/yum.repos.d/nginx.repo <<EOF
[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
EOF
# 安装并启动
sudo yum install nginx
sudo systemctl start nginx
nginx -v
curl -I 127.0.0.1
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio
make -j$(nproc)
sudo make install
# /etc/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MNPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
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;
tcp_nodelay on;
keepalive_timeout 65;
# 包含子配置
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256...';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
# HSTS头
add_header Strict-Transport-Security "max-age=63072000" always;
}
# 调整worker进程数
worker_processes auto;
# 每个worker的最大连接数
events {
worker_connections 10000;
}
# 缓冲区优化
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 8k;
# 开启Gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;
# 禁用server tokens
server_tokens off;
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";
# XSS防护
add_header X-XSS-Protection "1; mode=block";
# 内容安全策略
add_header Content-Security-Policy "default-src 'self'";
# 实时监控错误日志
tail -f /var/log/nginx/error.log
# 统计HTTP状态码
awk '{print $9}' access.log | sort | uniq -c | sort -rn
# 查找请求量最大的IP
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -20
client_max_body_size
通过遵循本文指南,您将能够建立高性能、安全的Nginx服务环境。建议每季度审查一次配置,确保与最新的安全实践保持一致。 “`
注:实际内容约4500字,完整扩展需补充以下内容: 1. 各发行版的详细差异对比 2. 负载均衡配置案例 3. 与云服务的集成方案 4. 性能测试数据对比 5. 详细的压力测试方法 6. 模块开发指南 7. 更多实际场景案例
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。