发布于2026-08-21 阅读(0)
扫一扫,手机访问
在Ubuntu上部署静态网站可以通过多种方式实现,以下是一些常见的方法:

安装Apache:
sudo apt update
sudo apt install apache2启用必要的模块:
sudo a2enmod rewrite
sudo systemctl restart apache2将静态网站文件复制到Apache的默认文档根目录:
sudo cp -r /path/to/your/static/site /var/www/html/Apache配置(可选):若您有自定义配置的需求,可编辑/etc/apache2/sites-a vailable/000-default.conf文件,并添加或修改以下内容:
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
重启Apache:
sudo systemctl restart apache2安装Nginx:
sudo apt update
sudo apt install nginx将静态网站文件复制到Nginx的默认文档根目录:
sudo cp -r /path/to/your/static/site /var/www/html/Nginx配置要点:对/etc/nginx/sites-a vailable/default文件进行编辑,加入或调整如下内容:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}重启Nginx:
sudo systemctl restart nginx如果你更喜欢使用Node.js来部署静态网站,可以使用Express框架。
安装Node.js和npm:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs创建一个新的Node.js项目:
mkdir my-static-site
cd my-static-site
npm init -y安装Express:
npm install express创建一个简单的Express服务器:创建一个server.js文件,添加以下内容:
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});将静态网站文件移动到public目录:
mkdir public
cp -r /path/to/your/static/site/* public/启动服务器:
node server.js现在,你可以通过访问http://localhost:3000来查看你的静态网站。
如果你熟悉Docker,可以使用Docker来部署静态网站。
安装Docker:
sudo apt update
sudo apt install docker.io创建一个Dockerfile:在你的静态网站目录中创建一个Dockerfile,添加以下内容:
FROM nginx:latest
COPY . /usr/share/nginx/html构建Docker镜像:
docker build -t my-static-site .运行Docker容器:
docker run -d -p 80:80 --name my-static-site-container my-static-site现在,你可以通过访问http://localhost来查看你的静态网站。
选择适合你的方法进行部署即可。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
4
5
6
7
8
9