在 Linux 上配置 Swagger,通常取决于你使用的后端语言/框架。下面我按最常见场景给你一个清晰、可操作的说明,并附上通用思路。
Swagger 现在通常指:
| 名称 | 说明 |
|---|---|
| Swagger UI | 用于可视化查看和调试 API |
| Swagger Editor | 编写 OpenAPI 文档 |
| Swagger Codegen | 生成客户端/服务端代码 |
| Springfox / springdoc-openapi | Java Spring 的 Swagger 集成 |
✅ 90% 的 Linux 使用场景是:在项目中集成 Swagger UI
springdoc-openapi(替代老 Springfox)<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
mvn spring-boot:run
http://服务器IP:8080/swagger-ui.html
或
http://服务器IP:8080/swagger-ui/index.html
✅ 无需额外配置 Linux 环境
npm install swagger-jsdoc swagger-ui-express
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const options = {
definition: {
openapi: '3.0.0',
info: { title: 'API', version: '1.0.0' }
},
apis: ['./index.js']
};
const spec = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(spec));
http://IP:3000/api-docs
pip install fastapi uvicorn
启动后直接访问:
http://IP:8000/docs
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.0.0.zip
unzip v5.0.0.zip
cp -r swagger-ui-5.0.0/dist /usr/share/nginx/html/swagger
http://服务器IP/swagger
✅ 检查:
netstat -tunlp | grep 8080
curl http://localhost:8080/swagger-ui.html
# 开放端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
你可以直接告诉我:
1️⃣ 使用什么语言 / 框架?
2️⃣ Linux 发行版(CentOS / Ubuntu / 麒麟)?
3️⃣ 是 本地部署 还是 生产服务器?
我可以给你一步一步的命令级配置方案 ✅