首先,通过命令行在Debian系统上安装Nginx:
c
sudo apt update
sudo apt install nginx -y
安装完成后,启动Nginx并设置为开机自动启动:
cpp
sudo systemctl start nginx
sudo systemctl enable nginx
你需要将HTML、CSS、JS等静态文件放到Nginx可以访问的目录。
在服务器上创建项目目录,例如:
cpp
sudo mkdir -p /var/www/bom-tool/html
设置正确的目录权限:
cpp
sudo chown -R www-data:www-data /var/www/bom-tool/html
创建站点配置文件
在 /etc/nginx/sites-available/ 目录下创建一个新的配置文件,例如 bom-tool
cpp
sudo nano /etc/nginx/sites-available/bom-tool
将以下配置内容粘贴到文件中。这个配置确保了当有人访问服务器时,Nginx会从你指定的目录提供文件,并且对于单页应用(如果你的工具使用了前端路由),未找到的路径会回退到 index.html:
cpp
server {
listen 80;
# 将 your_server_ip 替换为你的服务器IP,或者使用 _ 来匹配所有访问
server_name your_server_ip;
root /var/www/bom-tool/html;
index index.html;
location / {
# 此配置对单页应用很重要,确保路由能正确指向index.html
try_files $uri $uri/ /index.html;
}
}
启用站点并重载配置
创建一个符号链接,将配置文件链接到 sites-enabled 目录:
cpp
sudo ln -s /etc/nginx/sites-available/bom-tool /etc/nginx/sites-enabled/
测试Nginx配置是否正确,确保没有语法错误:
cpp
sudo nginx -t
如果测试成功,重新加载Nginx使新配置生效
cpp
sudo systemctl reload nginx
完成配置后的步骤
写入配置文件后,请继续执行以下命令:
创建符号链接启用站点:
cpp
bash
sudo ln -sf /etc/nginx/sites-available/bom-tool /etc/nginx/sites-enabled/
删除默认配置(如果存在):
cpp
bash
sudo rm -f /etc/nginx/sites-enabled/default
测试Nginx配置:
cpp
bash
sudo nginx -t
重新加载Nginx:
cpp
bash
sudo systemctl reload nginx
检查Nginx状态:
cpp
bash
sudo systemctl status nginx
debian10算已经不更新的版本了,换源更新建议换归档源
cpp
# 腾讯云 Debian 归档源 for buster
deb http://mirrors.cloud.tencent.com/debian-archive/debian/ buster main contrib non-free
deb-src http://mirrors.cloud.tencent.com/debian-archive/debian/ buster main contrib non-free这个给我个命令直接写入
报错解决:
oot@linaro-alip:/home/zzw# sudo nginx -t
nginx: [warn] conflicting server name "_" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
解决配置冲突
- 检查当前启用的站点
cpp
bash
sudo ls -la /etc/nginx/sites-enabled/
- 删除或禁用冲突的配置
bash
删除默认配置(如果存在)
cpp
sudo rm -f /etc/nginx/sites-enabled/default
如果有其他自定义配置冲突,也一并删除或注释掉
- 重新测试配置
cpp
bash
sudo nginx -t
如果还有警告,我们可以修改配置为更具体的设置:
- 更新你的bom-tool配置(如果需要)
bash
cpp
sudo sh -c 'cat > /etc/nginx/sites-available/bom-tool << "EOF"
server {
listen 80 default_server;
server_name _;
root /var/www/bom-tool/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
EOF'
注意我添加了 default_server 来明确指定这是默认服务器。
继续部署步骤
无论警告是否解决,只要语法检查通过,我们就可以继续:
重新加载Nginx配置:
cpp
bash
sudo systemctl reload nginx
检查Nginx服务状态:
bash
sudo systemctl status nginx
检查Nginx是否在监听80端口:
bash
sudo netstat -tulpn | grep :80
现在需要将你的HTML文件上传到Web目录:
bash
确保目录存在
cpp
```cpp
sudo mkdir -p /var/www/bom-tool/html
# 上传你的文件(假设你的文件在当前目录)
```cpp
sudo cp -r ./* /var/www/bom-tool/html/
设置正确的权限
cpp
sudo chown -R www-data:www-data /var/www/bom-tool/html
sudo chmod -R 755 /var/www/bom-tool/html
测试访问
现在你可以通过浏览器访问你的服务器IP地址来测试BOM工具是否正常工作:
bash
查看服务器IP
cpp
ip addr show
或者
cpp
hostname -I
然后在浏览器中输入:http://你的服务器IP
如果仍有问题
如果访问时遇到问题,可以检查Nginx错误日志:
bash
cpp
sudo tail -f /var/log/nginx/error.log
示例:我有两个工具与一个主页。
步骤1:创建一个简单的 index.html 作为导航页面,链接到你的两个工具:
cpp
# 创建导航页面
sudo sh -c 'cat > /var/www/bom-tool/html/index.html << "EOF"
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BOM工具集</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; }
.container { max-width: 800px; margin: 0 auto; }
h1 { color: #333; text-align: center; }
.tool-list { list-style: none; padding: 0; }
.tool-item { margin: 20px 0; padding: 20px; border: 1px solid #ddd; border-radius: 5px; }
.tool-link { display: block; padding: 10px 15px; background: #007cba; color: white;
text-decoration: none; border-radius: 4px; text-align: center; }
.tool-link:hover { background: #005a87; }
.description { margin-bottom: 15px; color: #666; }
</style>
</head>
<body>
<div class="container">
<h1>BOM工具集</h1>
<ul class="tool-list">
<li class="tool-item">
<div class="description">
<h3>BOM对比工具</h3>
<p>上传两个Excel格式的BOM文件,对比并识别位号重复、物料不同等差异。</p>
</div>
<a href="bomcompare.HTML" class="tool-link">打开BOM对比工具</a>
</li>
<li class="tool-item">
<div class="description">
<h3>BOM匹配工具</h3>
<p>BOM物料匹配工具,用于其他BOM相关操作。</p>
</div>
<a href="bommatch.HTML" class="tool-link">打开BOM匹配工具</a>
</li>
</ul>
</div>
</body>
</html>
EOF'
部署步骤
无论选择哪种方案,部署步骤都是一样的:
cpp
# 1. 创建目录
sudo mkdir -p /var/www/bom-tool/html
# 2. 上传你的HTML文件和其他资源(CSS, JS, 图片等)
# 假设你的文件在当前目录
sudo cp -r ./* /var/www/bom-tool/html/
# 3. 如果选择方案1,创建导航页面(上面的命令)
# 如果选择方案2,更新Nginx配置(上面的命令)
# 4. 设置权限
sudo chown -R www-data:www-data /var/www/bom-tool/html
sudo chmod -R 755 /var/www/bom-tool/html
# 5. 重新加载Nginx
sudo systemctl reload nginx
后续使用建议
- 访问方式
导航页面: http://你的服务器IP/ (如果你创建了index.html)
直接访问:
cpp
http://你的服务器IP/bomcompare.HTML
http://你的服务器IP/bommatch.HTML
- 维护和管理
cpp
查看Nginx状态:
bash
sudo systemctl status nginx
重启Nginx服务 (如果需要):
bash
sudo systemctl restart nginx
查看访问日志:
bash
sudo tail -f /var/log/nginx/access.log
查看错误日志 (如果遇到问题):
bash
sudo tail -f /var/log/nginx/error.log
3. 更新工具版本
当你有新版本的HTML文件时,只需重新上传:
bash
上传新版本文件
cpp
sudo cp bomcompare.HTML /var/www/bom-tool/html/
sudo cp bommatch.HTML /var/www/bom-tool/html/
设置权限
sudo chown -R www-data:www-data /var/www/bom-tool/html
- 备份配置
建议备份你的Nginx配置:
cpp
bash
sudo cp /etc/nginx/sites-available/bom-tool /etc/nginx/sites-available/bom-tool.backup