在 Debian 上部署 Swagger(通常指 Swagger UI 或 Swagger Editor),常见做法有几种,取决于你是只想查看/测试 API,还是集成到项目中。下面按最常用场景给你一个清晰、可操作的说明。
适合:
sudo apt update
sudo apt install -y nginx
cd /var/www/html
sudo git clone https://github.com/swagger-api/swagger-ui.git
或者只取 dist:
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.14.zip
unzip v5.17.14.zip
mv swagger-ui-5.17.14/dist swagger-ui
编辑配置:
sudo nano /etc/nginx/sites-available/swagger
内容示例:
server {
listen 80;
server_name your-domain-or-ip;
location / {
root /var/www/html/swagger-ui;
index index.html;
}
}
启用配置:
sudo ln -s /etc/nginx/sites-available/swagger /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
浏览器打开:
http://服务器IP
swagger-initializer.jsnano /var/www/html/swagger-ui/swagger-initializer.js
修改:
url: "http://your-api-domain/api/openapi.yaml",
cp your-openapi.yaml /var/www/html/swagger-ui/
然后访问:
http://IP/?url=/your-openapi.yaml
cd /var/www/html
git clone https://github.com/swagger-api/swagger-editor.git
location /editor {
root /var/www/html/swagger-editor;
index index.html;
}
访问:
http://IP/editor
如果你已安装 Docker:
docker run -p 8080:8080 \
-e SWAGGER_JSON=/api/openapi.yaml \
-v $(pwd):/usr/share/nginx/html/api \
swaggerapi/swagger-ui
docker run -p 8081:8080 swaggerapi/swagger-editor
如果你用的是 Spring Boot:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
访问:
http://localhost:8080/swagger-ui.html
sudo ufw allow 80
| 场景 | 推荐方式 |
|---|---|
| 仅查看 API | Nginx + Swagger UI |
| 编辑 API | Swagger Editor |
| 快速测试 | Docker |
| Spring Boot | springdoc-openapi |
如果你愿意,可以告诉我:
我可以给你完全定制的部署方案。