Why Medusa?
Medusa is an open-source headless commerce platform that provides a flexible and extensible foundation for building custom e-commerce experiences. Unlike traditional monolithic platforms, Medusa separates the backend commerce engine from the frontend presentation layer.
Prerequisites
VPS Requirements
- • RAM: 2 GB (4 GB recommended)
- • CPU: 1 vCPU (2 vCPUs recommended)
- • Storage: 20 GB SSD
- • OS: Ubuntu 22.04 or 24.04 LTS
Before You Begin
- • Root or sudo access to your VPS
- • Domain name pointed to your VPS IP
- • Basic Linux command line knowledge
- • SSH access to your server
Initial Server Setup
Update the System
Connect to your VPS via SSH and update all packages:
sudo apt update && sudo apt upgrade -yCreate a Dedicated User
Create a non-root user for running Medusa:
sudo adduser medusa
sudo usermod -aG sudo medusaConfigure Firewall
Enable UFW and allow necessary ports:
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enableNode.js Installation
Install Node.js 20
Medusa requires Node.js 20 or higher. Install it using NodeSource:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejsVerify Installation
Verify the installation:
node --version
npm --versionTip: You should see Node.js v20.x.x or higher.
PostgreSQL Setup
Install PostgreSQL
Install PostgreSQL:
sudo apt install -y postgresql postgresql-contribCreate Database and User
Switch to the postgres user and create the Medusa database:
sudo -u postgres psqlCREATE USER medusa WITH PASSWORD 'your_secure_password';
CREATE DATABASE medusa_db OWNER medusa;
GRANT ALL PRIVILEGES ON DATABASE medusa_db TO medusa;
\qWarning: Replace 'your_secure_password' with a strong, unique password. Store this securely.
Redis Installation
Install Redis
Redis is used for caching, session storage, and background job queues:
sudo apt install -y redis-serverConfigure Redis
Configure Redis to start on boot:
sudo systemctl enable redis-server
sudo systemctl start redis-serverVerify Redis
Verify Redis is running:
redis-cli pingYou should see PONG as the response.
Medusa Installation
Switch to Medusa User
su - medusaCreate Medusa Project
Use the Medusa CLI to create a new project:
npx create-medusa-app@latest my-storeDuring installation, you'll be prompted for:
- Project directory name
- PostgreSQL database URL (use the credentials from earlier)
- Whether to create a Next.js storefront
Manual Installation (Alternative)
If you prefer manual setup:
mkdir ~/my-store && cd ~/my-store
npm init -y
npm install @medusajs/medusa-cli -g
medusa new . --skip-dbConfiguration
Environment Configuration
Navigate to your project and edit the .env file:
cd ~/my-store
nano .envConfigure the following environment variables:
# Database
DATABASE_URL=postgres://medusa:your_secure_password@localhost:5432/medusa_db
# Redis
REDIS_URL=redis://localhost:6379
# JWT Secrets (generate unique values)
JWT_SECRET=your-super-secret-jwt-key-min-32-chars
COOKIE_SECRET=your-super-secret-cookie-key-min-32
# Server
MEDUSA_ADMIN_ONBOARDING_TYPE=default
STORE_CORS=https://your-storefront-domain.com
ADMIN_CORS=https://admin.your-domain.comGenerate Secure Secrets
Generate random secrets for JWT and cookies:
openssl rand -hex 32Run this command twice and use the outputs for JWT_SECRET and COOKIE_SECRET.
Run Database Migrations
npx medusa db:migrateCreate Admin User
npx medusa user -e admin@your-domain.com -p your-admin-passwordSystemd Service
Create Service File
Create a systemd service to manage Medusa as a background process:
sudo nano /etc/systemd/system/medusa.serviceAdd the following configuration:
[Unit]
Description=Medusa Commerce Server
After=network.target postgresql.service redis.service
[Service]
Type=simple
User=medusa
WorkingDirectory=/home/medusa/my-store
ExecStart=/usr/bin/npm run start
Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=medusa
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.targetEnable and Start Service
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable medusa
sudo systemctl start medusaCheck Service Status
sudo systemctl status medusaNginx Reverse Proxy
Install Nginx
sudo apt install -y nginxCreate Nginx Configuration
sudo nano /etc/nginx/sites-available/medusaAdd the following configuration:
server {
listen 80;
server_name api.your-domain.com;
location / {
proxy_pass http://localhost:9000;
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;
}
}Enable Site
Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/medusa /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxSSL Certificate
Install Certbot
Use Certbot to obtain a free SSL certificate from Let's Encrypt:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d api.your-domain.comCertbot will automatically configure SSL and set up auto-renewal.
Important: Ensure your domain's DNS A record points to your VPS IP address before running Certbot.
Admin Dashboard
After completing the setup, access the Medusa admin dashboard at:
https://api.your-domain.com/appLog in with the admin credentials you created earlier.
Admin Dashboard Features
Connect a Storefront (Optional)
Official Next.js Starter
Medusa supports various frontend frameworks:
npx create-medusa-app@latest --with-nextjs-starterGatsby Storefront
git clone https://github.com/medusajs/gatsby-starter-medusa
cd gatsby-starter-medusa
npm installConfigure Storefront
Configure the storefront's environment to point to your Medusa backend:
NEXT_PUBLIC_MEDUSA_BACKEND_URL=https://api.your-domain.comMaintenance and Operations
Viewing Logs
sudo journalctl -u medusa -fRestarting Medusa
sudo systemctl restart medusaUpdating Medusa
cd ~/my-store
npm update @medusajs/medusa
npx medusa db:migrate
sudo systemctl restart medusaDatabase Backup
Create regular backups of your PostgreSQL database:
pg_dump -U medusa medusa_db > backup_$(date +%Y%m%d).sqlTroubleshooting
| Issue | Solution |
|---|---|
| Database connection failed | Verify DATABASE_URL in .env and PostgreSQL is running |
| Redis connection error | Check Redis service: sudo systemctl status redis-server |
| Port 9000 already in use | Check for conflicting services: sudo lsof -i :9000 |
| SSL certificate issues | Ensure DNS is properly configured before running Certbot |
| CORS errors | Update STORE_CORS and ADMIN_CORS in .env file |
