当前位置: 首页 > news >正文

编译安装 Nginx

✅ 一、准备环境

1. 安装依赖包

sudo yum groupinstall -y "Development Tools"
sudo yum install -y pcre-devel zlib-devel openssl-devel
  • pcre-devel:支持正则表达式(用于 locationrewrite
  • zlib-devel:支持 gzip 压缩
  • openssl-devel:支持 HTTPS/SSL
  • Development Tools:包含 gcc、make 等编译工具

✅ 二、下载 Nginx 源码

前往 https://nginx.org/en/download.html 选择稳定版(Stable version)

# 创建工作目录
cd /usr/local/src
sudo mkdir -p /usr/local/src
cd /usr/local/src# 下载 Nginx(以 1.24.0 为例,可替换为最新版)
sudo wget https://nginx.org/download/nginx-1.24.0.tar.gz# 解压
sudo tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

✅ 三、配置编译选项

sudo ./configure \--prefix=/usr/local/nginx \--sbin-path=/usr/sbin/nginx \--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 \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_addition_module \--with-http_sub_module \--with-http_dav_module \--with-http_gunzip_module \--with-http_gzip_static_module \--with-http_auth_request_module \--with-http_random_index_module \--with-http_secure_link_module \--with-http_degradation_module \--with-http_slice_module \--with-mail \--with-mail_ssl_module \--with-stream \--with-stream_ssl_module \--with-stream_realip_module \--with-stream_ssl_preread_module \--with-threads \--with-compat \--user=nginx \--group=nginx

🔍 常用参数说明

  • --prefix:安装路径
  • --with-http_ssl_module:启用 HTTPS
  • --with-http_gzip_static_module:支持静态 Gzip
  • --user=nginx:运行用户

✅ 四、编译并安装

sudo make
sudo make install

⏱️ 编译时间约 2~5 分钟,取决于服务器性能。


✅ 五、创建用户和目录

# 创建 nginx 用户(不用于登录)
sudo useradd -r -s /sbin/nologin nginx# 创建日志目录
sudo mkdir -p /var/log/nginx
sudo chown nginx:nginx /var/log/nginx# 创建配置目录
sudo mkdir -p /etc/nginx

✅ 六、复制默认配置文件

# 将编译生成的配置复制到 /etc/nginx/
sudo cp /usr/local/src/nginx-1.24.0/conf/nginx.conf /etc/nginx/nginx.conf

你可以根据需要修改:

sudo vi /etc/nginx/nginx.conf

✅ 七、创建 systemd 服务文件(开机自启)

创建服务文件:

sudo tee /etc/systemd/system/nginx.service <<-'EOF'
[Unit]
Description=nginx - high performance web 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=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true[Install]
WantedBy=multi-user.target
EOF

✅ 八、启动 Nginx

# 重新加载 systemd
sudo systemctl daemon-reload# 启动 Nginx
sudo systemctl start nginx# 设置开机自启
sudo systemctl enable nginx# 查看状态
sudo systemctl status nginx

✅ 九、验证安装

# 查看版本
nginx -v# 检查配置是否正确
nginx -t# 访问本地
curl -I http://localhost

浏览器访问 http://你的服务器IP,看到 “Welcome to nginx!” 页面即成功 ✅


✅ 十、常用命令

命令说明
nginx -t测试配置文件
nginx -s reload重新加载配置(热重启)
nginx -s stop快速停止
nginx -s quit优雅关闭
systemctl restart nginx重启服务

✅ 十一、(可选)配置防火墙

# 开放 80 和 443 端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

✅ 十二、配置 HTTPS(可选)

如果你有 SSL 证书,可在 nginx.conf 中配置:

server {listen 443 ssl;server_name your-domain.com;ssl_certificate /etc/nginx/ssl/fullchain.pem;ssl_certificate_key /etc/nginx/ssl/privkey.pem;location / {root /usr/share/nginx/html;index index.html;}
}
http://www.lryc.cn/news/626297.html

相关文章:

  • 从AI小智固件到人类智能:计算技术的层级跃迁
  • Linux-----《Linux系统管理速通:界面切换、远程连接、目录权限与用户管理一网打尽》
  • JavaScript 检查给定的四个点是否形成正方形(Check if given four points form a square)
  • [特殊字符] 小豆包 API 聚合平台:让 AI 接入更简单、更高效
  • PyTorch API 7
  • Linux 文件系统权限管理(补充)
  • pinctrl和gpio子系统实验
  • 前后端联合实现文件上传,实现 SQL Server image 类型文件上传
  • LeetCode热题100--101. 对称二叉树--简单
  • 【Kafka】常见简单八股总结
  • 力扣 30 天 JavaScript 挑战 第36天 第8题笔记 深入了解reduce,this
  • Linux Shell 常用操作与脚本示例详解
  • CNN 在故障诊断中的应用:原理、案例与优势
  • DAY 50 预训练模型+CBAM模块
  • 排查Redis数据倾斜引发的性能瓶颈
  • VScode ROS文件相关配置
  • 什么是大数据平台?大数据平台和数据中台有什么关系?
  • 网络间的通用语言TCP/IP-网络中的通用规则3
  • A股大盘数据-20250819 分析
  • 【PyTorch】单对象分割项目
  • Arthas 全面使用指南:离线安装 + Docker/K8s 集成 + 集中管理
  • Python入门第11课:Python网络请求入门,使用requests库轻松获取网页数据
  • Linux的基本操作
  • 浅看架构理论(一)
  • RK3568 Linux驱动学习——Linux设备树
  • 【SQL优化案例】统计信息缺失
  • 51单片机拼接板(开发板积木)
  • oracle官网下载jdk历史版本,jdk历史版本下载
  • Pandas数据预处理中缺失值处理
  • 【数据结构】堆和二叉树详解(下)