This guide provides step-by-step instructions on how to set up Swagger UI on your own web server to host your API documentation.
openapi.yaml.swagger-ui-5.x.x.zip (or similar) file under the "Assets" section.
dist folder. This folder contains all the necessary HTML, CSS, and JavaScript files to run Swagger UI.
openapi.yaml file: Decide where you want to store your openapi.yaml file on your server. A common practice is to place it directly in the root of your Swagger UI deployment (e.g., your-docs-folder/openapi.yaml) or in a subfolder like specs/.
Example Placement: If your web server serves files from /var/www/html/api-docs/, then you might place your openapi.yaml directly into /var/www/html/api-docs/openapi.yaml or in /var/www/html/api-docs/specs/openapi.yaml.
/var/www/html/api-docs/ or /var/www/your-domain/public_html/api-docs/./usr/share/nginx/html/api-docs/.dist contents: Copy all the contents of the dist folder (from your Swagger UI download) into the directory you created on your web server.
So, if your downloaded dist folder contained index.html, swagger-ui.css, swagger-ui-bundle.js, etc., these files should now be directly inside your web server's api-docs/ directory.
In modern Swagger UI versions (5.x.x+), the configuration is in swagger-initializer.js.
swagger-initializer.js: Navigate to your Swagger UI deployment directory (e.g., api-docs/) on your server and open the swagger-initializer.js file in a text editor.
SwaggerUIBundle configuration: Find the window.onload function containing the SwaggerUIBundle initialization. It will look similar to this:
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json", // This is the line to change!
dom_id: '#swagger-ui',
// ... other configuration options
});
// End Swagger UI call region
};
url parameter:
openapi.yaml is in the same directory:
Change url: "https://petstore.swagger.io/v2/swagger.json" to:
url: "./openapi.yaml",
openapi.yaml is in a subfolder (e.g., specs/):
Change url: "https://petstore.swagger.io/v2/swagger.json" to:
url: "./specs/openapi.yaml",
url: "https://your-api.com/v1/openapi.json", // Example absolute URL
swagger-initializer.js: Save the changes to the file.
This step makes your documentation accessible and secure. You'll need root/administrator access to your server.
/etc/apache2/sites-available/your-docs.conf).
<VirtualHost *:80>
ServerName docs.your-domain.com # Change to your domain/subdomain
DocumentRoot /var/www/html/api-docs # Path where you copied Swagger UI files
<Directory /var/www/html/api-docs>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/api-docs_error.log
CustomLog ${APACHE_LOG_DIR}/api-docs_access.log combined
</VirtualHost>
sudo a2ensite your-docs.conf
sudo systemctl restart apache2
(or sudo service apache2 restart)
/etc/nginx/sites-available/your-docs.conf).
server {
listen 80;
server_name docs.your-domain.com; # Change to your domain/subdomain
root /usr/share/nginx/html/api-docs; # Path where you copied Swagger UI files
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
sudo ln -s /etc/nginx/sites-available/your-docs.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
(or sudo service nginx restart)
NEVER expose public-facing documentation without HTTPS.
certbot.
certbot for your web server and OS (e.g., sudo apt install certbot python3-certbot-apache or sudo apt install certbot python3-certbot-nginx).sudo certbot --apache or sudo certbot --nginx and follow the prompts. It will handle certificate generation and web server configuration automatically, including redirects from HTTP to HTTPS.docs.your-domain.com) points to the public IP address of your server where Swagger UI is hosted.
https://docs.your-domain.com/).
You should now see your interactive API documentation rendered by Swagger UI, loading your openapi.yaml file!
openapi.yaml (and potentially customized Swagger UI files) in a Git repository.