1. 项目概述在Linux服务器环境中Nginx作为高性能的Web服务器和反向代理服务器被广泛使用。虽然现在大多数Linux发行版都提供预编译的Nginx软件包但从源码编译安装仍然是许多专业运维人员的首选方案。这种方式不仅能获得最新的功能特性还能根据实际需求进行定制化编译优化性能参数。本文将详细记录在CentOS 7系统上从源码编译安装Nginx 1.3.15版本的全过程。这个特定版本虽然较旧但在某些特定业务场景下仍有使用需求。我们将从环境准备开始逐步完成依赖安装、源码编译、配置优化到服务管理的完整流程。2. 环境准备与依赖安装2.1 系统基础环境检查在开始安装前我们需要确保系统环境符合编译要求。首先登录CentOS 7服务器执行以下命令检查系统版本cat /etc/redhat-release uname -r确认输出显示为CentOS 7.x和3.10.x内核版本。接下来更新系统基础软件包yum update -y注意生产环境中建议先在测试环境验证更新避免直接更新线上服务器导致兼容性问题。2.2 安装编译工具链Nginx是使用C语言编写的因此需要安装GCC等编译工具yum groupinstall Development Tools -y yum install gcc make automake pcre-devel zlib-devel openssl-devel -y这里安装的依赖包包括pcre-develPerl兼容正则表达式库Nginx的rewrite模块需要zlib-devel压缩库用于gzip压缩功能openssl-develSSL/TLS支持启用HTTPS必需2.3 创建专用用户为安全考虑Nginx不应以root身份运行。我们创建一个专用系统用户useradd -r -s /sbin/nologin -M www-data参数说明-r创建系统用户-s /sbin/nologin禁止登录shell-M不创建家目录3. 源码编译安装Nginx3.1 下载并解压源码包首先下载nginx-1.3.15.tar.gz源码包wget http://nginx.org/download/nginx-1.3.15.tar.gz验证文件完整性md5sum nginx-1.3.15.tar.gz解压源码包tar zxvf nginx-1.3.15.tar.gz cd nginx-1.3.153.2 配置编译选项执行configure脚本进行编译前配置./configure \ --prefix/usr/local/nginx \ --userwww-data \ --groupwww-data \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-debug关键参数说明--prefix指定安装目录--user/--group运行身份--with-http_ssl_module启用SSL支持--with-http_stub_status_module启用状态监控--with-debug启用调试日志生产环境可去掉3.3 编译与安装配置完成后开始编译make编译成功后进行安装make install安装完成后验证版本/usr/local/nginx/sbin/nginx -v4. 系统集成与服务管理4.1 配置环境变量为方便使用将Nginx可执行文件路径加入系统PATHecho export PATH$PATH:/usr/local/nginx/sbin /etc/profile source /etc/profile现在可以直接使用nginx命令nginx -t # 测试配置文件4.2 创建systemd服务单元创建/etc/systemd/system/nginx.service文件[Unit] DescriptionThe NGINX HTTP and reverse proxy server Aftersyslog.target network.target remote-fs.target nss-lookup.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 Userwww-data Groupwww-data [Install] WantedBymulti-user.target启用并启动服务systemctl daemon-reload systemctl enable nginx systemctl start nginx4.3 防火墙配置允许HTTP和HTTPS流量firewall-cmd --permanent --add-servicehttp firewall-cmd --permanent --add-servicehttps firewall-cmd --reload5. 配置优化与安全加固5.1 基础配置文件调整编辑/usr/local/nginx/conf/nginx.confworker_processes auto; # 自动匹配CPU核心数 worker_rlimit_nofile 65535; # 文件描述符限制 events { worker_connections 10240; # 每个worker的连接数 use epoll; # 使用epoll事件模型 multi_accept on; # 同时接受多个连接 } http { server_tokens off; # 隐藏Nginx版本号 ... }5.2 虚拟主机配置示例创建/usr/local/nginx/conf/conf.d/example.com.confserver { listen 80; server_name example.com; access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; location / { root /var/www/example.com; index index.html; } location ~ /\.ht { deny all; # 禁止访问.htaccess文件 } }5.3 日志轮转配置创建/etc/logrotate.d/nginx/var/log/nginx/*.log { daily missingok rotate 30 compress delaycompress notifempty create 640 www-data www-data sharedscripts postrotate [ -f /usr/local/nginx/logs/nginx.pid ] kill -USR1 cat /usr/local/nginx/logs/nginx.pid endscript }6. 常见问题排查6.1 启动失败排查步骤检查错误日志tail -n 50 /usr/local/nginx/logs/error.log测试配置文件nginx -t检查端口冲突netstat -tulnp | grep :806.2 性能优化检查点检查当前worker进程数是否合理ps -ef | grep nginx | grep -v grep监控连接数netstat -ant | awk {print $6} | sort | uniq -c | sort -n检查打开文件数限制ulimit -n6.3 安全加固建议禁用不需要的HTTP方法if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 405; }添加基础安全头add_header X-Frame-Options SAMEORIGIN; add_header X-XSS-Protection 1; modeblock; add_header X-Content-Type-Options nosniff;限制敏感文件访问location /wp-config.php { deny all; } location /\.git { deny all; }7. 版本特性与升级建议虽然Nginx 1.3.15是一个较旧的稳定版本但它仍然具备以下核心功能基础HTTP服务器功能反向代理和负载均衡基本的SSL/TLS支持Gzip压缩访问日志和错误日志如果需要升级到更新版本建议操作步骤备份当前配置和网站数据下载新版源码包使用相同的configure参数重新编译执行make upgrade平滑升级在实际生产环境中建议至少升级到Nginx 1.18.x或更高版本以获得更好的性能和安全更新。