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

Nginx安装:源代码编译安装

Nginx安装:源代码编译安装

1. 前置条件

在安装前,确保操作系统已安装以下依赖包:

  • gcc:GNU 编译器,用于编译 C/C++ 代码。
  • zlib & zlib-devel:用于数据压缩(gzip 支持)。
  • pcre-devel:提供正则表达式匹配功能。
  • openssl & openssl-devel:实现 SSL/TLS 加密通信。
1.1 验证依赖是否安装
gcc --version
ldconfig -p | grep libz  # 检查 zlib
ls /usr/include/zlib.h   # 检查 zlib 头文件
ldconfig -p | grep pcre  # 检查 pcre
ls /usr/include/pcre.h   # 检查 pcre 头文件
openssl version          # 检查 openssl
ls /usr/include/openssl/ # 检查 openssl 头文件
1.2 安装依赖(适用于 CentOS/RHEL)

如果外网可用或已配置本地源,可以直接执行以下命令安装:

yum -y install gcc gcc-c++ zlib zlib-devel pcre-devel openssl openssl-devel libtool

2. 编译 & 安装

2.1 上传源码包并解压

nginx-1.26.3.tar.gz 上传至 /opt 目录,并解压:

cd /opt
tar -zxvf nginx-1.26.3.tar.gz
cd nginx-1.26.3
2.2 配置编译参数
# 创建安装目录(需先创建)
mkdir -p /usr/local/nginx

# 执行初始化
./configure \
    --prefix=/usr/local/nginx \
    --with-http_stub_status_module \
    --with-http_ssl_module

配置摘要:

Configuration summary
  + using system PCRE2 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/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

说明:

  • using system PCRE2 library:Nginx 使用系统已安装的 PCRE2 库(用于正则表达式匹配)。
  • using system OpenSSL library:Nginx 使用系统 OpenSSL 库,支持 SSL/TLS 加密。
  • using system zlib library:Nginx 使用系统 zlib 库,支持 gzip 压缩。
2.3 编译 & 安装
make && make install

安装完成后,Nginx 目录结构如下:

/usr/local/nginx/
├── conf    # 配置文件目录(nginx.conf)
├── html    # 默认网页存放目录
├── logs    # 日志目录
├── sbin    # 可执行文件目录(nginx 启动文件)

此时,/opt/nginx-1.26.3 目录及源码包可删除(仅用于安装过程)。

3. 配置 Nginx

3.1 创建专属用户 & 组
groupadd nginx
useradd -g nginx nginx
3.2 设置软连接(方便执行 nginx 命令)
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
nginx -v  # 验证版本

4. 启动 & 管理 Nginx

4.1 启动 Nginx
nginx
4.2 验证是否运行
ps -ef | grep nginx

输出示例:

root     8812  1  0 16:25 ?  00:00:00 nginx: master process nginx
nobody   8813  8812  0 16:25 ?  00:00:00 nginx: worker process
4.3 常用管理命令
nginx -t         # 检查配置文件是否正确
nginx -s reload  # 重新加载配置文件
nginx -s stop    # 停止 Nginx
nginx -s reopen  # 重启 Nginx

5. 配置 HTTPS 代理(可选)

5.1 配置 nginx.conf
server {
    listen 80;
    server_name example.com;
    rewrite ^(.*)$ https://$server_name$1 permanent;
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate      /path/to/cert.pem;
    ssl_certificate_key  /path/to/cert.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}
5.2 注意防火墙设置
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload

6. 访问测试

浏览器访问 http://example.comhttps://example.com,若能成功打开页面,则 HTTPS 代理配置成功。

至此,Nginx 源码编译安装及 HTTPS 配置完成! 🎉

相关文章:

  • vmware虚拟机Ubuntu Desktop系统怎么和我的电脑相互复制文件、内容
  • 硬件岗位是否适合你?
  • Linux环境基础开发工具的使用(三)
  • 用算术右移操作实现整型数的除法
  • 【git】工作流实战:从本地仓库到远程仓库,git pull 与git rebase使用讲解,案例解析
  • C++,设计模式,【工厂方法模式】
  • Openssl之SM2加解密命令
  • 【个人记录】openEuler安装K3S并配置为GPU节点
  • python高效使用06_while_True和while_1哪个效率更高
  • OpenCV形态学操作
  • Windows 10事件查看器
  • PINN求解一维burgers方程
  • 【AB-01】 AUTOSAR Builder软件安装
  • C++:从拷贝构造函数到深浅拷贝
  • 如何修改Windows系统Ollama模型存储位置
  • 第三章 组件(7)- 布局与Sections
  • Java——面向对象编程
  • 使用多态来替换条件语句
  • 【嵌入式Linux应用开发基础】进程间通信(3):共享内存
  • 遗传算法与深度学习实战系列,自动调优深度神经网络和机器学习的超参数
  • 网站制作公司兴田德润简介/用广州seo推广获精准访问量
  • 怎么下载网站所有源码/百度广告点击软件
  • 广东网站建设建站模板/百度一下 你就知道官网
  • 吉林企业网络推广方法/安卓优化大师官方下载
  • 成都的汽车网站建设/搜索引擎推广的优势
  • 北京市建委网站官网/凡科建站客服电话