在 Linux 下部署 Swagger(通常指 Swagger UI / Swagger Editor / Swagger 相关服务),常见有三种场景。下面按从简单到完整给你说明,你可以根据实际需求选择。
适合:
✅ 已有 OpenAPI 接口文档(JSON / YAML)
✅ 只想让别人通过浏览器查看接口文档
cd /usr/local
sudo git clone https://github.com/swagger-api/swagger-ui.git
cp -r swagger-ui/dist /var/www/swagger
编辑:
vim /var/www/swagger/swagger-initializer.js
修改:
url: "https://petstore.swagger.io/v2/swagger.json"
改成你自己的接口文档地址,例如:
url: "http://your-domain.com/api/openapi.json"
sudo yum install nginx -y # CentOS
sudo apt install nginx -y # Ubuntu
Nginx 配置:
server {
listen 80;
server_name swagger.xxx.com;
root /var/www/swagger;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
重启:
nginx -t
systemctl restart nginx
✅ 浏览器访问:
http://swagger.xxx.com
适合:
✅ 编写 / 调试 OpenAPI 文档
docker run -d -p 8080:8080 swaggerapi/swagger-editor
访问:
http://服务器IP:8080
git clone https://github.com/swagger-api/swagger-editor.git
cd swagger-editor
http-server -p 8080
适合:
✅ Java 后端
✅ 自动生成接口文档
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
@SpringBootApplication
@EnableOpenApi
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
http://IP:端口/swagger-ui/index.html
架构:
Nginx
├── /api → 后端服务
└── /swagger → Swagger UI
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
location /swagger/ {
root /var/www;
index index.html;
}
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
| 方式 | 适合场景 |
|---|---|
| 静态 Swagger UI | 已有接口文档 |
| Swagger Editor | 编写 API |
| Springfox / springdoc | Java 项目 |
| Docker | 快速部署 |
如果你愿意,可以告诉我:
我可以给你一份完全贴合你项目的部署方案。