askrobots / dbbasic-deploy
Public
Deploy dbbasic-web apps to Ubuntu servers over SSH: nginx, Let's Encrypt and systemd, no Docker or Ansible.
setup.py · 3.8 KB · 121 lines
| 1 | """Server setup commands for Ubuntu servers.""" |
| 2 | |
| 3 | import paramiko |
| 4 | |
| 5 | |
| 6 | def setup_server(host, domain, app_name="app", port=3000, user="root", key_path=None): |
| 7 | """ |
| 8 | Set up an Ubuntu server for hosting dbbasic-web apps. |
| 9 | |
| 10 | This installs: |
| 11 | - Python 3.12 |
| 12 | - nginx |
| 13 | - certbot for SSL |
| 14 | - Creates app user and directories |
| 15 | - Configures systemd service |
| 16 | - Sets up nginx reverse proxy |
| 17 | """ |
| 18 | print(f"Setting up server {host} for {domain}...") |
| 19 | |
| 20 | ssh = paramiko.SSHClient() |
| 21 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 22 | |
| 23 | # Connect |
| 24 | connect_kwargs = {"hostname": host, "username": user} |
| 25 | if key_path: |
| 26 | connect_kwargs["key_filename"] = key_path |
| 27 | |
| 28 | ssh.connect(**connect_kwargs) |
| 29 | |
| 30 | def run(command, description=None): |
| 31 | """Run command and print output.""" |
| 32 | if description: |
| 33 | print(f" {description}...") |
| 34 | stdin, stdout, stderr = ssh.exec_command(command) |
| 35 | exit_status = stdout.channel.recv_exit_status() |
| 36 | output = stdout.read().decode() |
| 37 | error = stderr.read().decode() |
| 38 | |
| 39 | if exit_status != 0: |
| 40 | print(f" Error: {error}") |
| 41 | raise Exception(f"Command failed: {command}") |
| 42 | |
| 43 | if output: |
| 44 | print(f" {output.strip()}") |
| 45 | return output |
| 46 | |
| 47 | # Update system |
| 48 | run("apt-get update", "Updating package lists") |
| 49 | |
| 50 | # Install Python 3.12 |
| 51 | run("apt-get install -y software-properties-common", "Installing prerequisites") |
| 52 | run("add-apt-repository -y ppa:deadsnakes/ppa", "Adding Python PPA") |
| 53 | run("apt-get update", "Updating package lists") |
| 54 | run("apt-get install -y python3.12 python3.12-venv python3-pip", "Installing Python 3.12") |
| 55 | |
| 56 | # Install nginx and certbot |
| 57 | run("apt-get install -y nginx certbot python3-certbot-nginx", "Installing nginx and certbot") |
| 58 | |
| 59 | # Create app user |
| 60 | run(f"useradd -m -s /bin/bash {app_name} || true", f"Creating {app_name} user") |
| 61 | |
| 62 | # Create app directories |
| 63 | run(f"mkdir -p /var/www/{app_name}", f"Creating /var/www/{app_name}") |
| 64 | run(f"chown -R {app_name}:{app_name} /var/www/{app_name}", "Setting permissions") |
| 65 | |
| 66 | # Configure firewall |
| 67 | run("ufw allow 'Nginx Full' || true", "Configuring firewall") |
| 68 | run("ufw allow OpenSSH || true", "Allowing SSH") |
| 69 | run("ufw --force enable || true", "Enabling firewall") |
| 70 | |
| 71 | # Create systemd service |
| 72 | service_content = f"""[Unit] |
| 73 | Description={app_name} dbbasic-web application |
| 74 | After=network.target |
| 75 | |
| 76 | [Service] |
| 77 | Type=simple |
| 78 | User={app_name} |
| 79 | WorkingDirectory=/var/www/{app_name} |
| 80 | Environment="PATH=/var/www/{app_name}/venv/bin" |
| 81 | ExecStart=/var/www/{app_name}/venv/bin/uvicorn dbbasic_web.asgi:app --host 127.0.0.1 --port {port} --workers 2 |
| 82 | Restart=always |
| 83 | RestartSec=10 |
| 84 | |
| 85 | [Install] |
| 86 | WantedBy=multi-user.target |
| 87 | """ |
| 88 | |
| 89 | run(f"cat > /etc/systemd/system/{app_name}.service << 'EOF'\n{service_content}\nEOF", |
| 90 | f"Creating systemd service") |
| 91 | |
| 92 | # Create nginx config |
| 93 | nginx_content = f"""server {{ |
| 94 | listen 80; |
| 95 | server_name {domain}; |
| 96 | |
| 97 | location / {{ |
| 98 | proxy_pass http://127.0.0.1:{port}; |
| 99 | proxy_set_header Host $host; |
| 100 | proxy_set_header X-Real-IP $remote_addr; |
| 101 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 102 | proxy_set_header X-Forwarded-Proto $scheme; |
| 103 | }} |
| 104 | }} |
| 105 | """ |
| 106 | |
| 107 | run(f"cat > /etc/nginx/sites-available/{app_name} << 'EOF'\n{nginx_content}\nEOF", |
| 108 | "Creating nginx config") |
| 109 | |
| 110 | run(f"ln -sf /etc/nginx/sites-available/{app_name} /etc/nginx/sites-enabled/{app_name}", |
| 111 | "Enabling nginx site") |
| 112 | |
| 113 | run("nginx -t", "Testing nginx config") |
| 114 | run("systemctl reload nginx", "Reloading nginx") |
| 115 | |
| 116 | print(f"\n✓ Server setup complete!") |
| 117 | print(f"\nNext steps:") |
| 118 | print(f"1. Deploy your app: dbbasic-deploy push --host {host} --domain {domain}") |
| 119 | print(f"2. Set up SSL: ssh {user}@{host} 'certbot --nginx -d {domain}'") |
| 120 | |
| 121 | ssh.close() |