User

Environment Variables

How to configure environment variables for Node.js apps managed by PM2 in Opterius Panel.

Last updated 1775606400

Environment variables keep sensitive data (database passwords, API keys, secrets) out of your source code. The panel automatically passes PORT to your app — everything else you configure yourself.

The PORT Variable

When you deploy an app, the panel passes PORT as an environment variable matching the port you specified. Your app should always use it:

const PORT = process.env.PORT || 3000;
app.listen(PORT);

Using a .env File

The simplest approach for Node.js apps is a .env file in your project directory, loaded with the dotenv package:

Install dotenv:

npm install dotenv

Create .env in your project root (via File Manager or SSH):

DATABASE_URL=postgresql://myuser_mydb_u:password@127.0.0.1:5432/myuser_mydb
API_SECRET=your-secret-key
NODE_ENV=production

Load it at the top of your entry file:

require('dotenv').config();

// Now use process.env.DATABASE_URL etc.
const dbUrl = process.env.DATABASE_URL;

[!WARNING] Never commit .env files to version control. Add .env to your .gitignore.

PM2 Ecosystem File

For more control, use a pm2.config.js (ecosystem file) in your project root. This lets you set environment variables that PM2 injects directly, without relying on a .env file:

// pm2.config.js
module.exports = {
  apps: [{
    name: 'myapp',
    script: 'server.js',
    env_production: {
      NODE_ENV: 'production',
      PORT: 3000,
      DATABASE_URL: 'postgresql://...',
    }
  }]
};

Start with the ecosystem file via SSH:

pm2 start pm2.config.js --env production
pm2 save

[!NOTE] When using an ecosystem file, start the app manually via SSH. The panel's Deploy App form uses a simpler pm2 start command that doesn't read ecosystem files.

Checking Current Environment

Via SSH:

pm2 show myuser_myapp
# Look for the "Exec mode" and "Env" sections

Or in your app:

console.log(process.env); // logs all environment variables

Common Variables

Variable Example value
NODE_ENV production
PORT 3000 (set by panel)
DATABASE_URL PostgreSQL connection string
SESSION_SECRET Random 32+ char string
API_KEY Your third-party API key

Updating Variables

After changing your .env file, restart the app:

  1. Go to Node.js → your app → Restart
  2. Or via SSH: pm2 restart myuser_myapp

Next Steps