Nginx简介与安装
Nginx简介
1. 什么是Nginx
Nginx(发音为"engine-x")是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP服务器。它由俄罗斯程序员Igor Sysoev于2004年开发,最初是为了解决C10K问题(即如何在一台物理机上同时服务10,000个用户)。
2. Nginx的特点
高性能
# Nginx采用事件驱动的异步非阻塞架构
# 单个worker进程可以处理数千个并发连接
# 内存占用低,CPU消耗少
高可靠性
# 主进程+工作进程的架构设计
# 工作进程崩溃不影响服务
# 支持热重载配置
功能丰富
# HTTP服务器
# 反向代理服务器
# 负载均衡器
# HTTP缓存
# SSL/TLS终端
3. Nginx vs Apache
特性 | Nginx | Apache |
---|---|---|
架构 | 事件驱动 | 进程/线程驱动 |
内存使用 | 低 | 高 |
并发处理 | 优秀 | 良好 |
静态文件 | 优秀 | 良好 |
动态内容 | 需要代理 | 原生支持 |
配置 | 简洁 | 复杂 |
模块 | 编译时确定 | 动态加载 |
Nginx安装
1. Ubuntu/Debian系统安装
使用包管理器安装
# 更新包列表
sudo apt update
# 安装Nginx
sudo apt install nginx -y
# 启动Nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 检查服务状态
sudo systemctl status nginx
添加官方仓库安装最新版
# 安装必要的包
sudo apt install curl gnupg2 ca-certificates lsb-release
# 添加Nginx签名密钥
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
# 添加稳定版仓库
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
# 更新包列表
sudo apt update
# 安装Nginx
sudo apt install nginx -y
2. CentOS/RHEL系统安装
使用包管理器安装
# CentOS 7
sudo yum install epel-release -y
sudo yum install nginx -y
# CentOS 8/RHEL 8
sudo dnf install nginx -y
# 启动服务
sudo systemctl start nginx
sudo systemctl enable nginx
# 配置防火墙
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
添加官方仓库
# 创建仓库文件
sudo tee /etc/yum.repos.d/nginx.repo << EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF
# 安装Nginx
sudo yum install nginx -y
3. 源码编译安装
下载源码
# 下载Nginx源码
cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.24.0.tar.gz
sudo tar -xzf nginx-1.24.0.tar.gz
cd nginx-1.24.0
安装编译依赖
# Ubuntu/Debian
sudo apt install build-essential libpcre3-dev libssl-dev zlib1g-dev
# CentOS/RHEL
sudo yum groupinstall "Development Tools"
sudo yum install pcre-devel openssl-devel zlib-devel
配置编译选项
# 配置编译参数
sudo ./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/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-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-http_slice_module \
--with-http_v2_module
编译和安装
# 编译
sudo make -j$(nproc)
# 安装
sudo make install
# 创建nginx用户
sudo useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
# 创建必要目录
sudo mkdir -p /var/cache/nginx
sudo chown nginx:nginx /var/cache/nginx
创建systemd服务文件
sudo tee /etc/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 重新加载systemd
sudo systemctl daemon-reload
# 启动并启用服务
sudo systemctl start nginx
sudo systemctl enable nginx
验证安装
1. 检查Nginx状态
# 检查服务状态
sudo systemctl status nginx
# 检查进程
ps aux | grep nginx
# 检查端口
sudo netstat -tlnp | grep :80
sudo ss -tlnp | grep :80
2. 测试Web访问
# 本地测试
curl http://localhost
curl -I http://localhost
# 浏览器访问
# 打开浏览器访问 http://服务器IP
# 应该看到Nginx默认欢迎页面
3. 检查Nginx版本和配置
# 查看版本信息
nginx -v
nginx -V
# 测试配置文件语法
sudo nginx -t
# 查看配置文件位置
sudo nginx -T
基本操作
1. 服务管理
# 启动服务
sudo systemctl start nginx
# 停止服务
sudo systemctl stop nginx
# 重启服务
sudo systemctl restart nginx
# 重新加载配置(不中断服务)
sudo systemctl reload nginx
sudo nginx -s reload
# 查看服务状态
sudo systemctl status nginx
# 设置开机自启
sudo systemctl enable nginx
# 取消开机自启
sudo systemctl disable nginx
2. 配置文件位置
# 主配置文件
/etc/nginx/nginx.conf
# 站点配置目录
/etc/nginx/conf.d/ # CentOS/RHEL
/etc/nginx/sites-available/ # Ubuntu/Debian
/etc/nginx/sites-enabled/ # Ubuntu/Debian
# 日志文件
/var/log/nginx/access.log # 访问日志
/var/log/nginx/error.log # 错误日志
# 默认网站根目录
/usr/share/nginx/html/ # CentOS/RHEL
/var/www/html/ # Ubuntu/Debian
3. 基本命令
# 测试配置文件
sudo nginx -t
# 重新加载配置
sudo nginx -s reload
# 停止服务
sudo nginx -s stop
# 优雅停止(处理完当前请求后停止)
sudo nginx -s quit
# 重新打开日志文件
sudo nginx -s reopen
防火墙配置
1. Ubuntu/Debian (ufw)
# 允许HTTP
sudo ufw allow 'Nginx HTTP'
# 允许HTTPS
sudo ufw allow 'Nginx HTTPS'
# 允许HTTP和HTTPS
sudo ufw allow 'Nginx Full'
# 查看状态
sudo ufw status
2. CentOS/RHEL (firewalld)
# 允许HTTP服务
sudo firewall-cmd --permanent --add-service=http
# 允许HTTPS服务
sudo firewall-cmd --permanent --add-service=https
# 重新加载防火墙
sudo firewall-cmd --reload
# 查看已开放的服务
sudo firewall-cmd --list-services
故障排除
1. 常见问题
端口被占用
# 检查80端口占用
sudo netstat -tlnp | grep :80
sudo lsof -i :80
# 如果Apache占用了80端口
sudo systemctl stop apache2
sudo systemctl disable apache2
权限问题
# 检查nginx用户
ps aux | grep nginx
# 修改文件权限
sudo chown -R nginx:nginx /var/www/html/
sudo chmod -R 755 /var/www/html/
SELinux问题(CentOS/RHEL)
# 检查SELinux状态
sestatus
# 临时禁用SELinux
sudo setenforce 0
# 永久禁用SELinux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
2. 日志查看
# 查看错误日志
sudo tail -f /var/log/nginx/error.log
# 查看访问日志
sudo tail -f /var/log/nginx/access.log
# 查看systemd日志
sudo journalctl -u nginx -f
总结
本课程介绍了Nginx的基本概念、特点和安装方法:
- Nginx特点:高性能、高可靠性、功能丰富
- 安装方式:包管理器安装、源码编译安装
- 基本操作:服务管理、配置测试、日志查看
- 故障排除:常见问题的解决方法
下一课预告
在下一课中,我们将详细学习Nginx配置文件的结构和语法,包括:
- 配置文件层次结构
- 核心指令详解
- 配置语法规则
- 配置文件优化
💡 小贴士:Nginx安装完成后,建议先熟悉基本的启动、停止、重载等操作,这些是日常运维的基础技能。
📚 文章对你有帮助?请关注我的公众号,万分感谢!
获取更多优质技术文章,第一时间掌握最新技术动态

关注公众号
第一时间获取最新技术文章

添加微信
技术交流 · 问题答疑 · 学习指导
评论讨论
欢迎留下你的想法和建议