Aplikasi udah jadi, saatnya go live! Di artikel pamungkas series ASP.NET Core ini, kita akan membahas 3 cara deploy aplikasi ke production — dari yang paling managed (Azure App Service) sampai yang paling fleksibel (Docker container).
Persiapan Sebelum Deploy
Checklist Production Readiness
Sebelum deploy, pastikan:
- Environment-based config:
appsettings.Production.jsonterpisah - HTTPS enabled:
app.UseHttpsRedirection()dan HSTS - Connection string di environment variable atau Azure Key Vault (bukan di config file)
- Logging terkonfigurasi (Application Insights, Serilog, atau file log)
- Error handling: custom error page, jangan tampilkan stack trace
- Database migration strategy: auto-migrate atau manual?
- CORS terbatas ke domain production saja
- Health check endpoint untuk monitoring
- Build di Release mode
Build Production
# Build di Release modedotnet publish -c Release -o ./publish
# Output: folder ./publish berisi semua file yang siap deployFolder publish/ berisi:
MyApp.dll— compiled applicationMyApp.exe— executable untuk Windowsweb.config— konfigurasi IIS (jika deploy ke Windows)wwwroot/— static files (CSS, JS, images)- Semua dependency NuGet
Opsi 1: Deploy ke Azure App Service
Azure App Service adalah PaaS (Platform as a Service) — paling simpel, managed sepenuhnya oleh Microsoft.
Deploy via Visual Studio Code
- Install Azure App Service extension di VS Code
- Klik kanan folder project → Deploy to Web App
- Pilih subscription, buat atau pilih App Service
- Tunggu deploy selesai — aplikasi langsung live
Deploy via Azure CLI
# Login ke Azureaz login
# Buat resource groupaz group create --name MyAppRG --location southeastasia
# Buat App Service Plan (Linux)az appservice plan create \ --name MyAppPlan \ --resource-group MyAppRG \ --sku F1 \ # Free tier untuk testing --is-linux
# Buat Web Appaz webapp create \ --name my-ngopidulur-app \ --resource-group MyAppRG \ --plan MyAppPlan \ --runtime "DOTNET|8.0"
# Deploy aplikasiaz webapp deployment source config-zip \ --resource-group MyAppRG \ --name my-ngopidulur-app \ --src ./publish.zipConnection String di Azure
# Set connection string sebagai app settingaz webapp config appsettings set \ --resource-group MyAppRG \ --name my-ngopidulur-app \ --settings \ "ConnectionStrings__DefaultConnection=Server=tcp:myserver.database.windows.net;..." \ "ASPNETCORE_ENVIRONMENT=Production"Gunakan format
ConnectionStrings__DefaultConnection(double underscore) untuk nested JSON config.
Opsi 2: Deploy ke VPS Linux (Ubuntu + Nginx)
Untuk kontrol penuh dan biaya lebih rendah, deploy ke VPS sendiri.
Step 1: Install .NET Runtime di Server
# SSH ke serverssh user@your-vps-ip
# Install .NET 8 Runtimewget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.debsudo dpkg -i packages-microsoft-prod.debsudo apt updatesudo apt install -y aspnetcore-runtime-8.0
# Verifikasidotnet --list-runtimesStep 2: Upload & Jalankan Aplikasi
# Dari laptop lokal — upload publish folder ke serverscp -r ./publish user@your-vps-ip:/var/www/myapp/
# Di server — jalankan aplikasicd /var/www/myappdotnet MyApp.dll --urls "http://localhost:5000"Step 3: Setup Nginx sebagai Reverse Proxy
# Install Nginxsudo apt install -y nginx
# Buat konfigurasi sitesudo nano /etc/nginx/sites-available/myappKonfigurasi Nginx:
server { listen 80; server_name ngopidulur.my.id;
location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; 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; proxy_cache_bypass $http_upgrade; }}Aktifkan site:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/sudo nginx -t # test konfigurasisudo systemctl reload nginxStep 4: Setup Systemd agar Auto-Start
sudo nano /etc/systemd/system/myapp.service[Unit]Description=Ngopidulur ASP.NET Core AppAfter=network.target
[Service]WorkingDirectory=/var/www/myappExecStart=/usr/bin/dotnet /var/www/myapp/MyApp.dllRestart=alwaysRestartSec=10KillSignal=SIGINTSyslogIdentifier=myappUser=www-dataEnvironment=ASPNETCORE_ENVIRONMENT=ProductionEnvironment=ASPNETCORE_URLS=http://localhost:5000
[Install]WantedBy=multi-user.targetsudo systemctl enable myappsudo systemctl start myappsudo systemctl status myappStep 5: SSL dengan Let’s Encrypt
sudo apt install -y certbot python3-certbot-pluginsudo certbot --nginx -d ngopidulur.my.idOpsi 3: Deploy dengan Docker
Docker memberikan portability maksimal — jalan sama persis di development, staging, dan production.
Dockerfile
# Stage 1: BuildFROM mcr.microsoft.com/dotnet/sdk:8.0 AS buildWORKDIR /src
# Copy csproj dan restoreCOPY *.csproj .RUN dotnet restore
# Copy semua file dan buildCOPY . .RUN dotnet publish -c Release -o /app
# Stage 2: RuntimeFROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtimeWORKDIR /appCOPY --from=build /app .
# Set environmentENV ASPNETCORE_ENVIRONMENT=ProductionENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApp.dll"]Build & Run
# Build imagedocker build -t myapp:latest .
# Run containerdocker run -d \ --name myapp \ -p 8080:8080 \ -e ConnectionStrings__DefaultConnection="..." \ myapp:latest
# Check logsdocker logs myappDocker Compose (App + Database + Nginx)
version: '3.8'
services: app: build: . container_name: myapp environment: - ASPNETCORE_ENVIRONMENT=Production - ConnectionStrings__DefaultConnection=Server=db;Database=MyApp;User=sa;Password=YourPassword123! depends_on: - db networks: - app-network
db: image: mcr.microsoft.com/mssql/server:2022-latest container_name: myapp-db environment: - ACCEPT_EULA=Y - SA_PASSWORD=YourPassword123! volumes: - db-data:/var/opt/mssql networks: - app-network
nginx: image: nginx:alpine container_name: myapp-proxy volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf ports: - "80:80" - "443:443" depends_on: - app networks: - app-network
volumes: db-data:
networks: app-network: driver: bridge# Deploy satu perintah!docker-compose up -dTroubleshooting Deploy
Aplikasi Error 500
# Aktifkan detailed error (hanya untuk debugging!)ASPNETCORE_ENVIRONMENT=Development
# Cek logsudo journalctl -u myapp -f # Systemddocker logs myapp # DockerNginx 502 Bad Gateway
- Pastikan aplikasi .NET running di port yang benar
- Cek
proxy_passURL di konfigurasi Nginx sudo systemctl status myapp— pastikan service running
Database Connection Failed
- Cek connection string — formatnya benar?
- Firewall: port database terbuka?
- Untuk Docker: service name sebagai hostname (bukan localhost)
Rekomendasi Production
| Skala | Rekomendasi |
|---|---|
| Hobby / Personal | VPS $5/bulan (Hetzner, DigitalOcean) + Nginx |
| Startup / UKM | Azure App Service (Basic/Standard) — minim ops |
| Enterprise | Azure + CI/CD + Kubernetes + auto-scaling |
Kesimpulan
Deploy ASP.NET Core ke production sekarang jauh lebih mudah dibanding era .NET Framework. Dengan dukungan cross-platform, kamu bisa deploy ke Windows, Linux, container, atau managed cloud — pilih yang paling sesuai dengan kebutuhan dan budget.
Ingat: production readiness dimulai dari development. Terapkan best practices sejak awal, dan deploy kamu akan lancar tanpa drama.
Referensi: The Little ASP.NET Core Book — Nate Barbettini, Microsoft .NET Deployment Documentation
Deploy ASP.NET Core ke Production: Panduan Azure App Service, VPS Linux, dan Docker
© Rifky Awalul Huda | CC BY-NC-SA 4.0