Prerequisites
Requirements
| Requirement | Details |
|---|---|
| RamNode VPS | Minimum 1GB RAM, 1 vCPU recommended |
| Operating System | Ubuntu 22.04 LTS or Debian 12 |
| Domain Name | Pointed to your VPS IP address |
| SSH Access | Root or sudo-enabled user |
| Local Development | AdonisJS project ready for deployment |
Initial Server Setup
Connect and Update System
ssh root@your_server_ipapt update && apt upgrade -yCreate Deploy User
Running applications as root is a security risk. Create a dedicated deployment user:
# Create user
adduser deploy
# Add to sudo group
usermod -aG sudo deploy
# Switch to new user
su - deployInstalling Node.js
Using NodeSource Repository
AdonisJS requires Node.js 20.x or later. Install using the NodeSource repository:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejsVerify Installation
node --version
npm --versionYou should see Node.js v20.x.x and npm v10.x.x or later.
Database Configuration
Installing PostgreSQL
sudo apt install -y postgresql postgresql-contribCreate Database and User
sudo -u postgres psqlCREATE USER adonisapp WITH PASSWORD 'your_secure_password';
CREATE DATABASE adonisdb OWNER adonisapp;
GRANT ALL PRIVILEGES ON DATABASE adonisdb TO adonisapp;
\qReplace 'your_secure_password' with a strong, unique password and store it securely.
Deploy Your Application
Create Application Directory
sudo mkdir -p /var/www/adonisapp
sudo chown -R deploy:deploy /var/www/adonisappTransfer Application
rsync -avz --exclude 'node_modules' --exclude '.env' ./ deploy@your_server_ip:/var/www/adonisapp/Install Dependencies
cd /var/www/adonisapp
npm ci --productionEnvironment Configuration
NODE_ENV=production
PORT=3333
HOST=127.0.0.1
APP_KEY=your_generated_app_key
DB_CONNECTION=pg
PG_HOST=127.0.0.1
PG_PORT=5432
PG_USER=adonisapp
PG_PASSWORD=your_secure_password
PG_DB_NAME=adonisdbnode ace generate:keyRun Migrations and Build
# Run database migrations
node ace migration:run --force
# Build TypeScript for production
node ace build --productionProcess Management with PM2
Install PM2
PM2 ensures your application stays running and automatically restarts on crashes:
sudo npm install -g pm2Create Ecosystem File
module.exports = {
apps: [{
name: 'adonisapp',
script: './build/server.js',
instances: 'max',
exec_mode: 'cluster',
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production'
}
}]
};Start and Configure PM2
# Start application
pm2 start ecosystem.config.js
# Enable startup on boot
pm2 startup
pm2 savePM2 will display a command to run with sudo - execute it to enable automatic startup.
Nginx Reverse Proxy
Install Nginx
sudo apt install -y nginxCreate Server Block
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3333;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_read_timeout 86400;
}
location /public {
alias /var/www/adonisapp/build/public;
expires 30d;
add_header Cache-Control "public, immutable";
}
}Enable Site
sudo ln -s /etc/nginx/sites-available/adonisapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxSSL Certificate with Let's Encrypt
Install Certbot
sudo apt install -y certbot python3-certbot-nginxObtain SSL Certificate
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comVerify Auto-Renewal
sudo systemctl status certbot.timer
sudo certbot renew --dry-runFirewall Configuration
Configure UFW
# Set default policies
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow necessary services
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'
# Enable firewall
sudo ufw enableVerify Rules
sudo ufw status verboseDeployment Automation
Create Deployment Script
#!/bin/bash
set -e
APP_DIR="/var/www/adonisapp"
cd $APP_DIR
echo "Pulling latest changes..."
git pull origin main
echo "Installing dependencies..."
npm ci --production
echo "Running migrations..."
node ace migration:run --force
echo "Building application..."
node ace build --production
echo "Restarting PM2..."
pm2 reload adonisapp
echo "Deployment complete!"chmod +x ~/deploy.shPerformance Optimization
Troubleshooting
| Issue | Solution |
|---|---|
| 502 Bad Gateway | Check if PM2 is running: pm2 status. Verify the application port matches Nginx proxy_pass. |
| Database Connection Failed | Verify PostgreSQL is running: sudo systemctl status postgresql. Check .env credentials. |
| Permission Denied | Ensure deploy user owns the directory: sudo chown -R deploy:deploy /var/www/adonisapp |
| SSL Certificate Error | Run: sudo certbot renew. Check domain DNS points to server IP. |
| High Memory Usage | Adjust PM2 max_memory_restart in ecosystem.config.js. Consider upgrading VPS. |
AdonisJS Deployed Successfully!
Your AdonisJS application is now running in production with PostgreSQL, PM2 process management, Nginx reverse proxy, and SSL encryption. For ongoing maintenance, regularly update system packages, monitor application logs, and keep your Node.js dependencies current.
