nginx-python is a Python package designed to automatically and easily configure and host your Python web applications (Django, Flask, FastAPI) with Nginx using simple Python code. This tool aims to streamline the deployment process, reducing the complexity and potential for errors associated with manual configuration.
- Automatic Nginx Configuration: Generate Nginx configuration files tailored to your application’s needs with minimal effort.
- Service Management: Start, stop, and reload Nginx services directly from your Python code.
- SSL Certificate Integration: Optionally integrate Let's Encrypt SSL certificates to secure your applications.
- Multi-Framework Support: Seamlessly supports Django, Flask, and FastAPI web frameworks.
- User-Friendly: Simplifies the deployment process, making it accessible even to developers who are not familiar with server configuration.
Install Python-nginx via pip:
pip install nginx-python
Here’s a quick example of how to set up Nginx for a FastAPI application:
from nginx_python.core import create_nginx_config, manage_service
# Define your server name and application port
server_name = 'example.com'
app_port = 8000
# Generate the Nginx configuration
create_nginx_config(server_name, app_port)
# Start the Nginx service
manage_service('start')
from nginx_python.core import create_nginx_config, manage_service
# Define your Nginx conf setup
server_name = 'example.com'
app_name = 'fastapi'
app_port = 8000
use_ssl = True
ssl_certificate_path='your_certificate_path'
ssl_certificate_key_path = 'your_certificate_key_path'
# Generate the Nginx configuration
create_nginx_config(server_name=server_name, app_port=app_port,
app_name=app_name, use_ssl=use_ssl,
ssl_certificate_path=ssl_certificate_path,
ssl_certificate_key_path=ssl_certificate_key_path)
# Start the Nginx service
manage_service('start')
Python-nginx also provides a CLI for easy management:
nginx_python setup --server-name example.com --app-port 8000
nginx_python start
nginx_python stop
nginx_python reload
The default Nginx configuration template used by Python-nginx:
server {
listen 80;
server_name {{ server_name }};
location / {
proxy_pass http://{{ app_name }}:{{ app_port }};
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
Contributions are welcome! Please read our contributing guide to get started.
This project is licensed under the MIT License. See the LICENSE file for more details.
This repository includes a GitHub Actions workflow to automatically test and publish the package to PyPI. For more information, see the workflow file.
For any questions or feedback, please contact [email protected].
With nginx-python, deploying your Python web applications has never been easier. Simplify your server configuration and focus on building great applications!