Node.js Server-Side Environment#
CodeRed Cloud provides a minimal production-ready Node.js environment based on Debian, with corepack enabled.
Server Entrypoint: cr-run.sh
#
The entrypoint, or main process, of your Node.js app is always cr-run.sh
. This file is required for the server to start. If this file is missing, a default boilerplate app will be created instead.
Most likely, this will contain a single command as so:
#!/bin/sh
exec node --v8-pool-size=${WORKERS} app.js
Note
You must have a foreground process serving on port 3000
. CodeRed cloud specifically looks for this port, and proxies web traffic using a production-grade web server using the domain name, SSL certificates, etc.
Installed Software#
Managed Node.js on CodeRed Cloud includes the selected version of Node and its bundled version of npm
.
It is not recommended to build your software on the server or use the server as a development environment. For maximum security and uptime, always build/bundle your software first, ensure the build was successful, then deploy the bundle.
If you do need to install software on the server, you can install it by running npm install
on the server in your cr-run.sh
file, or manually via SSH. This will create a node_modules
folder. You cannot install software globally on the server.
Using PM2 on CodeRed Cloud#
For small or minimal sites, simply using node
to serve your app is sufficient.
For more robust capabilities, PM2 is the recommended solution, and is pre-installed.
To run PM2 on CodeRed Cloud, provide an ecosystem.config.js
file as so:
module.exports = {
apps: [{
name: "My Website",
// Use CodeRed Cloud recommended number of instances.
// See: https://www.codered.cloud/docs/nodejs/environment/
instances: process.env.WORKERS,
exec_mode: "cluster",
script: "app.js",
port: 3000,
autorestart: true,
watch: false,
source_map_support: false,
// Logging.
// See: https://www.codered.cloud/reference/logging/
error_file: "/log/error.log",
out_file: "/log/pm2.log",
}]
}
And invoke PM2 from your cr-run.sh
file:
#!/bin/sh
exec pm2-runtime start ecosystem.config.js
Note
Always use pm2-runtime
for your main process instead of pm2
, as CodeRed Cloud expects a foreground process.
Environment Variables#
The following environment variables are automatically configured:
CR_ENV
=prod
in the production environment andstaging
in the staging environment.
PM2_HOME
=/tmp/pm2
.
NODE_ENV
is always set toproduction
.
VIRTUAL_HOST
is the primary domain name your website is running under. This is preferable to hard-coding domains.
WORKERS
is the recommended number of worker processes based on the amount of resources allocated to your CodeRed Cloud plan.
Custom environment variables can be set via the CodeRed Cloud dashboard under Website > Deployment > Environment Variables. These are stored encrypted in a keyvault, and are automatically decrypted and loaded into the server at runtime.
Logging#
By default, no logging is enabled. However, you can safetly log to the /log/
directory (also symlinked in /var/log
).
For details see Logging on CodeRed Cloud.
Remote Access#
You can access your files remotely via SFTP, and paid plans have access to SSH. See How to Transfer Files with SFTP and SSH on CodeRed Cloud.