Nginx安装:源代码编译安装
- IT业界
- 2025-08-24 09:30:01

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 ; rewrite ^(.*)$ $server_name$1 permanent; } server { listen 443 ssl; server_name example ; 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/ /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 或 example ,若能成功打开页面,则 HTTPS 代理配置成功。
至此,Nginx 源码编译安装及 HTTPS 配置完成! 🎉
Nginx安装:源代码编译安装由讯客互联IT业界栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Nginx安装:源代码编译安装”
下一篇
动态库和静态库(Linux环境)