CentOS 7下源码编译安装Nginx 1.3.15全指南

📅 2026/8/11 11:23:24
CentOS 7下源码编译安装Nginx 1.3.15全指南
1. 项目概述在CentOS 7环境下从源码编译安装nginx-1.3.15.tar.gz是一个典型的服务器环境配置任务。与直接使用yum安装预编译版本相比源码编译方式能获得更精细的控制权允许我们根据实际需求定制模块和功能。这个版本虽然较旧但在某些特定业务场景下仍有使用需求。我最近在一个需要兼容老系统的项目中就遇到了这样的需求。通过源码编译我们不仅解决了依赖库版本冲突问题还针对性地优化了某些性能参数。下面就把完整的操作流程和踩坑经验分享给大家。2. 环境准备与依赖安装2.1 系统基础环境检查在开始前建议先执行以下命令更新系统并检查基础环境yum update -y uname -a # 确认内核版本 cat /etc/redhat-release # 确认系统版本CentOS 7默认的gcc版本是4.8.5这个版本对于编译nginx-1.3.15已经足够。但如果你的系统是minimal安装可能需要先安装开发工具链yum groupinstall Development Tools -y yum install -y gcc gcc-c make2.2 必需依赖库安装nginx的编译依赖以下几个关键库yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel这里需要特别注意pcre用于支持正则表达式zlib用于gzip压缩openssl如果需要HTTPS支持则必须安装提示如果服务器无法连接外网需要提前下载好这些依赖包的rpm文件进行离线安装。3. 源码编译安装过程3.1 获取并解压源码包建议在/usr/local/src目录下进行操作cd /usr/local/src wget http://nginx.org/download/nginx-1.3.15.tar.gz tar zxvf nginx-1.3.15.tar.gz cd nginx-1.3.15如果wget不可用也可以先下载到本地再上传到服务器。解压后目录结构如下auto/: 自动检测系统特性的脚本conf/: 默认配置文件src/: 核心源码man/: 手册页3.2 配置编译选项执行configure脚本进行编译前配置./configure \ --prefix/usr/local/nginx \ --usernginx \ --groupnginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-pcre关键参数说明--prefix指定安装目录--user/--group指定运行用户with-http_ssl_module启用SSL支持with-http_realip_module获取真实客户端IP注意如果出现checking for ... not found错误通常是缺少对应的开发包需要yum安装对应的-devel包。3.3 编译与安装配置完成后执行编译和安装make make install编译过程大约需要3-10分钟取决于服务器性能。完成后可以检查安装目录ls -l /usr/local/nginx应该能看到conf、html、logs、sbin等目录。4. 系统配置与优化4.1 创建nginx系统用户为了安全考虑建议创建专用用户运行nginxuseradd -M -s /sbin/nologin nginx chown -R nginx:nginx /usr/local/nginx4.2 配置环境变量为了方便使用可以将nginx添加到PATHecho export PATH$PATH:/usr/local/nginx/sbin /etc/profile source /etc/profile现在可以直接执行nginx命令了。4.3 配置systemd服务创建服务文件/etc/systemd/system/nginx.service[Unit] Descriptionnginx - high performance web server Afternetwork.target [Service] Typeforking PIDFile/usr/local/nginx/logs/nginx.pid ExecStartPre/usr/local/nginx/sbin/nginx -t ExecStart/usr/local/nginx/sbin/nginx ExecReload/bin/kill -s HUP $MAINPID ExecStop/bin/kill -s QUIT $MAINPID PrivateTmptrue Usernginx Groupnginx [Install] WantedBymulti-user.target然后启用服务systemctl daemon-reload systemctl enable nginx systemctl start nginx5. 基本配置与测试5.1 验证安装检查nginx是否正常运行ps aux | grep nginx netstat -tunlp | grep nginx curl -I http://localhost应该能看到nginx进程和监听的80端口。5.2 基础配置调整编辑/usr/local/nginx/conf/nginx.confworker_processes auto; # 自动设置worker数量 events { worker_connections 1024; # 每个worker的连接数 } http { server_tokens off; # 隐藏版本号 # 其他配置... }5.3 防火墙设置如果启用了firewalld需要开放80端口firewall-cmd --permanent --add-servicehttp firewall-cmd --reload6. 常见问题排查6.1 启动时报错排查如果启动失败首先检查错误日志tail -n 50 /usr/local/nginx/logs/error.log常见问题端口冲突Address already in use权限问题Permission denied配置错误syntax error6.2 命令找不到问题如果出现-bash: nginx: command not found检查PATH是否包含nginx路径是否有执行权限6.3 性能调优建议对于生产环境建议调整以下参数worker_processes设置为CPU核心数worker_connections根据内存调整keepalive_timeout适当延长7. 版本特定注意事项nginx-1.3.15是一个较旧的版本使用时需注意不支持HTTP/2某些安全漏洞可能未修复与现代浏览器的兼容性问题如果可能建议考虑升级到稳定版。但如果必须使用这个版本建议在前端加WAF防护限制可访问IP定期检查日志8. 扩展配置建议8.1 虚拟主机配置示例配置server { listen 80; server_name example.com; root /var/www/example; index index.html; location / { try_files $uri $uri/ 404; } }8.2 日志分割使用logrotate进行日志管理创建/etc/logrotate.d/nginx/usr/local/nginx/logs/*.log { daily missingok rotate 30 compress delaycompress notifempty create 640 nginx nginx sharedscripts postrotate [ -f /usr/local/nginx/logs/nginx.pid ] kill -USR1 cat /usr/local/nginx/logs/nginx.pid endscript }8.3 性能监控建议安装nginx-status模块进行监控location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; }9. 安全加固措施禁用不需要的HTTP方法if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 405; }添加安全头部add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection 1; modeblock;限制访问敏感文件location ~* \.(ini|conf|sql|log)$ { deny all; }10. 维护与升级10.1 日常维护命令常用操作测试配置nginx -t重载配置nginx -s reload优雅停止nginx -s quit10.2 升级注意事项如果需要升级版本备份配置和网站文件停止旧版本编译安装新版本到不同目录测试无误后再切换我在实际运维中发现对于这种老版本nginx最稳妥的方式是在新服务器上部署测试环境确认所有功能正常后再进行迁移。曾经有一次直接升级导致多个站点的rewrite规则失效花了大量时间排查。