A Python automation tool to quickly create and configure Nginx virtual hosts on Linux systems.
This script automates the entire process of setting up a new Nginx virtual host, eliminating repetitive manual configuration steps. It handles vhost file creation, system directory setup, permission management, and Nginx configuration validation.
- Automated vhost creation - Generates Nginx configuration files from a template
- Automatic placeholder replacement - Replaces domain placeholder with your actual domain name
- Sites-available/sites-enabled management - Creates proper symlinks for Nginx vhost activation
- Directory and permission setup - Creates
/var/www/{domain}directory with correctnginx:nginxownership - Test page generation - Creates a basic
index.htmlfile for testing - Configuration validation - Runs
nginx -tto verify configuration before reloading - Service reload - Automatically reloads Nginx to apply changes
- Colored status output - Clear visual feedback for each step
The included Nginx configuration template (template) provides:
- HTTPS redirect - Automatically redirects HTTP traffic to HTTPS
- SSL/TLS support - Ready for SSL certificate configuration
- Proxy headers - Proper X-Forwarded headers for reverse proxying applications
- Static file caching - Optimized caching for images, CSS, JavaScript, etc.
- Security - Denies access to
.htfiles (Apache compatibility) - Logging - Separate access and error logs for each domain
python createVhost.py domain.comReplace domain.com with your actual domain name. The script will:
- Copy and rename the template to
/etc/nginx/sites-available/domain.com.conf - Replace all
domain.complaceholders with your domain - Create symlink in
/etc/nginx/sites-enabled/ - Create
/var/www/domain.comdirectory - Set ownership to
nginx:nginx - Create test
index.htmlfile - Validate and reload Nginx
- Linux system with Nginx installed
- Python 3.x
coloredlibrary (install viapip install colored)- Root or sudo privileges (required for system directories and Nginx operations)
- The script must be run with sufficient privileges to access
/etc/nginx/and/var/www/ - The template supports both HTTP and HTTPS configurations
- All status messages are color-coded for easy visual feedback