|
| 1 | +# Azure Deployment Guide |
| 2 | + |
| 3 | +This guide shows how to deploy the ByteCode Compiler on Azure VM with a REST API backend. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +``` |
| 8 | +┌──────────────────┐ ┌──────────────────────┐ |
| 9 | +│ Frontend │ │ Azure VM │ |
| 10 | +│ (Vercel) │ ─HTTP──>│ ┌────────────┐ │ |
| 11 | +│ - Code Editor │ │ │ Node.js │ │ |
| 12 | +│ - Submit Code │ │ │ Express │ │ |
| 13 | +│ - Show Output │ │ │ API │ │ |
| 14 | +└──────────────────┘ │ └─────┬──────┘ │ |
| 15 | + │ │ │ |
| 16 | + │ ┌─────▼──────┐ │ |
| 17 | + │ │ Compiler │ │ |
| 18 | + │ │ Binary │ │ |
| 19 | + │ └────────────┘ │ |
| 20 | + └──────────────────────┘ |
| 21 | +``` |
| 22 | + |
| 23 | +## Step 1: Set Up Azure VM |
| 24 | + |
| 25 | +### Create VM |
| 26 | +1. Go to Azure Portal |
| 27 | +2. Create a new Virtual Machine: |
| 28 | + - **OS**: Ubuntu 22.04 LTS |
| 29 | + - **Size**: Standard B2s (2 vCPUs, 4 GB RAM) or higher |
| 30 | + - **Authentication**: SSH public key |
| 31 | +3. Add inbound port rules: |
| 32 | + - Port 22 (SSH) |
| 33 | + - Port 80 (HTTP) |
| 34 | + - Port 443 (HTTPS) |
| 35 | + - Port 3000 (API - temporary, will use NGINX) |
| 36 | + |
| 37 | +### Connect to VM |
| 38 | +```bash |
| 39 | +ssh azureuser@<your-vm-ip> |
| 40 | +``` |
| 41 | + |
| 42 | +## Step 2: Install Dependencies on Azure VM |
| 43 | + |
| 44 | +```bash |
| 45 | +# Update system |
| 46 | +sudo apt update && sudo apt upgrade -y |
| 47 | + |
| 48 | +# Install build tools |
| 49 | +sudo apt install -y build-essential cmake git |
| 50 | + |
| 51 | +# Install Node.js |
| 52 | +curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - |
| 53 | +sudo apt install -y nodejs |
| 54 | + |
| 55 | +# Install NGINX |
| 56 | +sudo apt install -y nginx |
| 57 | + |
| 58 | +# Install certbot for SSL |
| 59 | +sudo apt install -y certbot python3-certbot-nginx |
| 60 | +``` |
| 61 | + |
| 62 | +## Step 3: Deploy Compiler on Azure VM |
| 63 | + |
| 64 | +### Upload and build compiler |
| 65 | +```bash |
| 66 | +# Clone your repository |
| 67 | +cd ~ |
| 68 | +git clone https://github.com/IsVohi/ByteCode-Compiler.git |
| 69 | +cd ByteCode-Compiler |
| 70 | + |
| 71 | +# Build the compiler |
| 72 | +mkdir build && cd build |
| 73 | +cmake .. |
| 74 | +make |
| 75 | + |
| 76 | +# Verify it works |
| 77 | +./compiler |
| 78 | +``` |
| 79 | + |
| 80 | +## Step 4: Set Up Backend API |
| 81 | + |
| 82 | +### Install dependencies |
| 83 | +```bash |
| 84 | +cd ~/ByteCode-Compiler |
| 85 | +npm init -y |
| 86 | +npm install express cors body-parser |
| 87 | +``` |
| 88 | + |
| 89 | +### Create API server (see `server/api.js`) |
| 90 | + |
| 91 | +The backend API is in the `server/` folder. It provides: |
| 92 | +- `POST /api/compile` - Compile and run code |
| 93 | +- `POST /api/repl` - Interactive REPL session |
| 94 | +- `GET /api/health` - Health check |
| 95 | + |
| 96 | +## Step 5: Run Backend as a Service |
| 97 | + |
| 98 | +### Create systemd service |
| 99 | +```bash |
| 100 | +sudo nano /etc/systemd/system/compiler-api.service |
| 101 | +``` |
| 102 | + |
| 103 | +Add: |
| 104 | +```ini |
| 105 | +[Unit] |
| 106 | +Description=ByteCode Compiler API |
| 107 | +After=network.target |
| 108 | + |
| 109 | +[Service] |
| 110 | +Type=simple |
| 111 | +User=azureuser |
| 112 | +WorkingDirectory=/home/azureuser/ByteCode-Compiler/server |
| 113 | +ExecStart=/usr/bin/node api.js |
| 114 | +Restart=on-failure |
| 115 | +RestartSec=10 |
| 116 | + |
| 117 | +[Install] |
| 118 | +WantedBy=multi-user.target |
| 119 | +``` |
| 120 | + |
| 121 | +Enable and start: |
| 122 | +```bash |
| 123 | +sudo systemctl daemon-reload |
| 124 | +sudo systemctl enable compiler-api |
| 125 | +sudo systemctl start compiler-api |
| 126 | +sudo systemctl status compiler-api |
| 127 | +``` |
| 128 | + |
| 129 | +## Step 6: Configure NGINX |
| 130 | + |
| 131 | +```bash |
| 132 | +sudo nano /etc/nginx/sites-available/compiler |
| 133 | +``` |
| 134 | + |
| 135 | +Add: |
| 136 | +```nginx |
| 137 | +server { |
| 138 | + listen 80; |
| 139 | + server_name your-domain.com; # Replace with your domain or IP |
| 140 | +
|
| 141 | + location /api { |
| 142 | + proxy_pass http://localhost:3000; |
| 143 | + proxy_http_version 1.1; |
| 144 | + proxy_set_header Upgrade $http_upgrade; |
| 145 | + proxy_set_header Connection 'upgrade'; |
| 146 | + proxy_set_header Host $host; |
| 147 | + proxy_cache_bypass $http_upgrade; |
| 148 | + proxy_set_header X-Real-IP $remote_addr; |
| 149 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +Enable site: |
| 155 | +```bash |
| 156 | +sudo ln -s /etc/nginx/sites-available/compiler /etc/nginx/sites-enabled/ |
| 157 | +sudo nginx -t |
| 158 | +sudo systemctl restart nginx |
| 159 | +``` |
| 160 | + |
| 161 | +## Step 7: Add SSL (Optional but Recommended) |
| 162 | + |
| 163 | +```bash |
| 164 | +sudo certbot --nginx -d your-domain.com |
| 165 | +``` |
| 166 | + |
| 167 | +## Step 8: Test the API |
| 168 | + |
| 169 | +```bash |
| 170 | +# Test health endpoint |
| 171 | +curl http://your-vm-ip/api/health |
| 172 | + |
| 173 | +# Test compilation |
| 174 | +curl -X POST http://your-vm-ip/api/compile \ |
| 175 | + -H "Content-Type: application/json" \ |
| 176 | + -d '{"code": "let x = 10; print(x * 2);"}' |
| 177 | +``` |
| 178 | + |
| 179 | +## Frontend Integration |
| 180 | + |
| 181 | +From your Vercel frontend, call the API: |
| 182 | + |
| 183 | +```javascript |
| 184 | +const API_URL = 'https://your-domain.com/api'; |
| 185 | + |
| 186 | +async function compileCode(code) { |
| 187 | + const response = await fetch(`${API_URL}/compile`, { |
| 188 | + method: 'POST', |
| 189 | + headers: { 'Content-Type': 'application/json' }, |
| 190 | + body: JSON.stringify({ code }) |
| 191 | + }); |
| 192 | + return await response.json(); |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +## Security Considerations |
| 197 | + |
| 198 | +1. **Rate Limiting**: Add rate limiting to prevent abuse |
| 199 | +2. **Timeout**: Set execution timeouts (default: 5 seconds) |
| 200 | +3. **Resource Limits**: Limit memory and CPU usage |
| 201 | +4. **Input Validation**: Sanitize input code |
| 202 | +5. **Firewall**: Configure Azure NSG properly |
| 203 | +6. **SSL**: Always use HTTPS in production |
| 204 | + |
| 205 | +## Monitoring |
| 206 | + |
| 207 | +```bash |
| 208 | +# View API logs |
| 209 | +sudo journalctl -u compiler-api -f |
| 210 | + |
| 211 | +# View NGINX logs |
| 212 | +sudo tail -f /var/log/nginx/access.log |
| 213 | +sudo tail -f /var/log/nginx/error.log |
| 214 | + |
| 215 | +# Monitor system resources |
| 216 | +htop |
| 217 | +``` |
| 218 | + |
| 219 | +## Updating the Compiler |
| 220 | + |
| 221 | +```bash |
| 222 | +cd ~/ByteCode-Compiler |
| 223 | +git pull origin main |
| 224 | +cd build |
| 225 | +cmake .. |
| 226 | +make |
| 227 | +sudo systemctl restart compiler-api |
| 228 | +``` |
| 229 | + |
| 230 | +## Troubleshooting |
| 231 | + |
| 232 | +### API not responding |
| 233 | +```bash |
| 234 | +sudo systemctl status compiler-api |
| 235 | +sudo journalctl -u compiler-api -n 50 |
| 236 | +``` |
| 237 | + |
| 238 | +### Build errors |
| 239 | +```bash |
| 240 | +cd ~/ByteCode-Compiler/build |
| 241 | +cmake .. |
| 242 | +make clean && make |
| 243 | +``` |
| 244 | + |
| 245 | +### Port already in use |
| 246 | +```bash |
| 247 | +sudo lsof -i :3000 |
| 248 | +sudo kill -9 <PID> |
| 249 | +``` |
| 250 | + |
| 251 | +## Cost Estimation |
| 252 | + |
| 253 | +- **Azure VM B2s**: ~$30-40/month |
| 254 | +- **Domain**: ~$10-15/year |
| 255 | +- **SSL**: Free (Let's Encrypt) |
| 256 | + |
| 257 | +**Total**: ~$30-40/month |
| 258 | + |
| 259 | +--- |
| 260 | + |
| 261 | +**Next Steps:** |
| 262 | +1. Follow this guide to deploy on Azure |
| 263 | +2. Create frontend (see `frontend/` folder) |
| 264 | +3. Deploy frontend on Vercel |
| 265 | +4. Connect frontend to Azure backend |
0 commit comments