一、前置说明
- 安装包:
nginx-1.28.0.tar.gz,已上传至/root目录 - 系统:CentOS 7.x(适配你当前的环境)
- 安装方式:源码编译安装,默认安装到
/usr/local/nginx - 前置依赖:gcc、pcre、zlib、openssl(脚本自动安装)
二、一键安装脚本(推荐直接使用)
vim/root/nginx_install.sh- 脚本如下:
#!/bin/bash# Nginx 1.28.0 源码编译安装脚本(CentOS 7.x,root执行)# ==================== 配置参数 ====================NGINX_VERSION="1.28.0"NGINX_PACKAGE="/root/nginx-${NGINX_VERSION}.tar.gz"INSTALL_PATH="/usr/local/nginx"# =================================================# 1. 检查root权限if[$UID-ne0];thenecho"请使用root用户执行此脚本!"exit1fi# 2. 检查安装包是否存在if[!-f"${NGINX_PACKAGE}"];thenecho"错误:/root目录下未找到nginx-${NGINX_VERSION}.tar.gz安装包!"exit1fi# 3. 安装编译依赖echo"===== 安装编译依赖 ====="yuminstall-ygcc gcc-c++makepcre pcre-devel zlib zlib-devel openssl openssl-devel# 4. 解压源码包echo"===== 解压Nginx源码包 ====="cd/roottar-zxfnginx-${NGINX_VERSION}.tar.gzcdnginx-${NGINX_VERSION}# 5. 配置编译参数(含SSL、gzip、rewrite等核心模块)echo"===== 配置编译参数 ====="./configure\--prefix=${INSTALL_PATH}\--with-http_ssl_module\--with-http_gzip_static_module\--with-http_stub_status_module\--with-http_realip_module\--with-pcre# 6. 编译并安装echo"===== 编译并安装Nginx ====="make&&makeinstall# 7. 配置环境变量(方便直接调用nginx命令)echo"===== 配置环境变量 ====="if!grep-q"nginx"/etc/profile;thenecho"export PATH=\$PATH:${INSTALL_PATH}/sbin">>/etc/profilefisource/etc/profile# 8. 创建系统服务(systemd管理,开机自启)echo"===== 创建Nginx系统服务 ====="cat>/usr/lib/systemd/system/nginx.service<<EOF [Unit] Description=Nginx Web Server After=network.target [Service] Type=forking ExecStart=${INSTALL_PATH}/sbin/nginx ExecReload=${INSTALL_PATH}/sbin/nginx -s reload ExecStop=${INSTALL_PATH}/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF# 重载systemd配置并设置开机自启systemctl daemon-reload systemctlenablenginx# 9. 开放防火墙端口(80/443)echo"===== 开放防火墙端口 ====="firewall-cmd --add-service=http--permanentfirewall-cmd --add-service=https--permanentfirewall-cmd--reload# 10. 启动Nginx并验证echo"===== 启动Nginx并验证 ====="systemctl start nginxsleep2ifpsaux|grepnginx|grep-vgrep>/dev/null;thenecho"====================================="echo"✅ Nginx${NGINX_VERSION}安装成功!"echo"安装路径:${INSTALL_PATH}"echo"配置文件:${INSTALL_PATH}/conf/nginx.conf"echo"网页目录:${INSTALL_PATH}/html"echo"启动命令:systemctl start nginx"echo"访问地址:http://你的服务器IP"echo"====================================="elseecho"❌ Nginx 启动失败,请检查安装日志!"exit1fi赋权执行脚本+重新加载环境变量
chmod+x nginx_install.shsh/root/nginx_install.shsource/etc/profile三、手动分步安装流程(可选)
步骤1:安装编译依赖
yuminstall-ygcc gcc-c++makepcre pcre-devel zlib zlib-devel openssl openssl-devel步骤2:解压源码包
cd/roottar-zxfnginx-1.28.0.tar.gzcdnginx-1.28.0步骤3:配置编译参数
./configure\--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_gzip_static_module\--with-http_stub_status_module\--with-http_realip_module\--with-pcre步骤4:编译并安装
make&&makeinstall步骤5:配置环境变量
echo"export PATH=\$PATH:/usr/local/nginx/sbin">>/etc/profilesource/etc/profile步骤6:创建systemd服务
cat>/usr/lib/systemd/system/nginx.service<<EOF [Unit] Description=Nginx Web Server After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF# 重载配置并设置开机自启systemctl daemon-reload systemctlenablenginx步骤7:开放防火墙端口
firewall-cmd --add-service=http--permanentfirewall-cmd --add-service=https--permanentfirewall-cmd--reload步骤8:启动并验证
# 启动Nginxsystemctl start nginx# 验证进程psaux|grepnginx# 验证端口(80端口)ss-tlnp|grep80# 本地访问测试curlhttp://127.0.0.1步骤9、自定义HTML文件部署步骤
替换默认首页(快速测试)
如果仅需替换Nginx默认的欢迎页,直接将自定义的index.html覆盖默认文件即可:
# 备份默认首页(可选)cp/usr/local/nginx/html/index.html /usr/local/nginx/html/index.html.bak# 将本地自定义的index.html上传到服务器(示例用scp,也可通过rz、FTP等方式)scp/本地路径/index.html root@你的服务器IP:/usr/local/nginx/html/# 刷新页面即可访问(无需重启Nginx)----#####我这里用的xftp¥¥¥¥¥----
四、安装后核心目录说明
| 目录/文件 | 作用说明 |
|---|---|
/usr/local/nginx/conf/nginx.conf | 主配置文件,所有反向代理、站点配置都在这里 |
/usr/local/nginx/conf/conf.d/ | 自定义站点配置目录(可手动创建,在nginx.conf中include) |
/usr/local/nginx/html/ | 默认网页根目录,部署前端项目时将文件放在这里 |
/usr/local/nginx/logs/ | 日志目录,包含access.log(访问日志)和error.log(错误日志) |
/usr/local/nginx/sbin/nginx | Nginx主程序,可直接执行命令(如nginx -s reload重载配置) |
五、常用管理命令
# 启动Nginxsystemctl start nginx# 停止Nginxsystemctl stop nginx# 重启Nginxsystemctl restart nginx# 重载配置(修改配置文件后用,不中断服务)systemctl reload nginx# 或直接用主程序nginx-sreload# 检查配置文件语法nginx-t# 查看Nginx状态systemctl status nginx# 查看Nginx进程psaux|grepnginx# 查看端口占用ss-tlnp|grepnginx六、部署前端项目示例
- 将前端打包文件上传至
/usr/local/nginx/html/你的项目目录(如/usr/local/nginx/html/frontend) - 编辑配置文件
/usr/local/nginx/conf/nginx.conf,添加站点配置:
server { listen 80; server_name 你的服务器IP; location / { root /usr/local/nginx/html/frontend; index index.html index.htm; try_files $uri $uri/ /index.html; # 解决SPA路由刷新404问题 } # 反向代理后端接口示例 location /api { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }- 检查配置并重载:
nginx-t&&nginx-sreload七、常见问题排查
- 端口被占用:执行
ss -tlnp | grep 80查看占用进程,停止占用进程或修改Nginx监听端口 - 配置文件语法错误:
nginx -t会明确提示错误位置,根据提示修改配置文件 - 访问403 Forbidden:检查网页目录权限,确保Nginx进程用户(默认nobody)有读取权限
- 无法访问:检查防火墙是否开放80/443端口,服务器安全组是否放行