Deploy Node.js app into Passenger host

Nikolai Minkevich
1 min readJan 14, 2021

Passenger is web-server with support for Node.js, Python, etc. It runs over Apache server and allows to launch Node.js scripts without starting node.

First if all prepare your code for deploy to production
a) Change dotenv.config() to relative path like
dotenv.config({path:__dirname + `/../../.env`})
b) Keep in mind database initialization params in a shared host differs from the localhost’s.
For localhost use string
host: process.env.DB_HOST,
For shared host place
socketPath: `/tmp/mysql.sock`,
instead.
You should think how you will switch it (example: use process.env.NODE_ENV variable).

Then copy your node project into root directory of the site using FTP (Path for example: ~/domains/example.com/). Don’t place the sources into public_html!

After that connect via SSH
ssh username@203.0.113.0
and create .htaccess with following content
SetEnv GHOST_NODE_VERSION_CHECK false
PassengerStartupFile
server.js
PassengerResolveSymlinksInDocumentRoot on
Require all granted
PassengerAppType node
PassengerAppRoot
/home/username/domains/example.com/src
Options -MultiViews

See on line 2 and 6. You should replace the bold text to your own data —the name of main script of project (If you’re not sure about it check “main” field in package.json) and the path to main script directory respectively.

For automatically redirection HTTPS add the following code in top of .htaccess
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

Finally go to root directory of the site (where placed package.json) and type
node i

Node.js project will automatically start after installing.

For restart server just create file named tmp/restart.txt in PassengerAppRoot directory. Also you can create tmp/always_restart.txt for development (node is restarting if server recieve incoming any HTTP request).

--

--