Internet.am logo
  • Դոմեն
  • Կոնստրուկտոր
  • Հոսթինգ
  • VPS
  • SSL
  • Օգնություն
  • Բլոգ
Open menu image
Internet.am logo

30.05.2025

Complete Guide: Node.js Setup with Auto- Start and .htaccess

Complete Guide: Node.js Setup with Auto- Start and .htaccess

1. Install Node.js

cd /home/USER_DIR/bin export
PATH=/home/USER_DIR/bin/node-v22.11.0-linux-x64/bin:$PATH
node
-v npm -v

2. Install and Configure PM2

npm install -g pm2
pm2 start /home/USER_DIR/server/server.js --name mynodeapp
pm2 save

3. Set Up Auto-Start via Crontab

crontab -e

Add the following line:

@reboot /bin/bash -c 'source /home/USER_DIR/.bashrc &&
/home/USER_DIR/bin/node-v22.11.0-linux-x64/lib/node_modules/pm2/bin/pm2
resurrect'

4. Example .bashrc

if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export NODE_HOME=/home/USER_DIR/bin/node-v22.11.0-linux-x64
export PATH=$NODE_HOME/bin:$HOME/.local/bin:$PATH
if command -v pm2 > /dev/null 2>&1; then
nohup pm2 resurrect > $HOME/pm2.log 2>&1 &
fi

5. Configure .htaccess

RewriteEngine On
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

6. Apache Logs

tail -f /etc/apache2/logs/domlogs/USER_DIR/error_log
tail -f /etc/apache2/logs/domlogs/USER_DIR/access_log

7. Verify Setup

Check PM2 process list:

pm2 list

Then visit:

http://yourdomain.com/

⚠️ Important:

Before going live, always ask your hosting administrator which port you're allowed to use. Never assume it's port 3000!

© 2023 Internet.am. All rights reserved.