bottleneckcrm/configure_nginx_proxy.py

38 lines
1.4 KiB
Python

import os
import sys
import subprocess
def main():
source_conf = '/tmp/crmbackend.nginx.conf'
target_available = '/etc/nginx/sites-available/crmbackend'
target_enabled = '/etc/nginx/sites-enabled/crmbackend'
if not os.path.exists(source_conf):
source_conf = '/home/ubuntu/crmbackend/crmbackend.nginx.conf'
if not os.path.exists(source_conf):
print(f"Error: Nginx configuration source file not found at {source_conf}")
return
try:
# Copy to sites-available
subprocess.run(['sudo', 'cp', source_conf, target_available], check=True)
print(f"Copied {source_conf} to {target_available}")
# Enable site symlink
if not os.path.exists(target_enabled):
subprocess.run(['sudo', 'ln', '-sf', target_available, target_enabled], check=True)
print(f"Symlinked {target_available} to {target_enabled}")
# Test Nginx configuration
test_res = subprocess.run(['sudo', 'nginx', '-t'], capture_output=True, text=True)
if test_res.returncode == 0:
subprocess.run(['sudo', 'systemctl', 'reload', 'nginx'], check=True)
print("Nginx configuration verified and reloaded successfully.")
else:
print(f"Nginx config test failed: {test_res.stderr}")
except Exception as e:
print(f"Error configuring Nginx: {e}")
if __name__ == '__main__':
main()