Runcloud How To Set Up Runcloud Node.JS

July 14, 2021 . 3 MIN READ

https://ourcodeworld.com/articles/read/977/how-to-deploy-a-node-js-application-on-aws-ec2-server

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-next-js-app-to-app-platform

https://marcocaggiano.medium.com/how-to-set-up-runcloud-node-js-85ef3d3a4dc8

https://serverpilot.io/docs/how-to-run-apps-in-any-language/

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04

 

 

 

 

 

How To Set Up Runcloud Node.JS

Mark Caggiano

 

Jul 26, 2020·4 min read

 

Setting up nodejs with runcloud.io can be confusing, but in reality it is very easy.

First, connect to your server with ssh:

ssh runcloud@yourserver.runcloud

Then cd to your home directorybecome root and install nodejs + npm:

yourserver.runcloud $ cd ~
yourserver.runcloud $ su
# Insert root password

Go to: https://github.com/nodesource/distributions/blob/master/README.md

Choose your distribution and follow the instruction to install nodejs, you need to. I will put the instructions for my specific case:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash –
sudo apt-get install -y nodejs

Then type node and npm to see if they are installed.

Now exit from super user and change npm configuration to use your $HOME directory:

# exit # Exit from super user
yourserver.runcloud $ NPM_PACKAGES=”$HOME/.npm-packages”
yourserver.runcloud $ mkdir -p “$NPM_PACKAGES”

Set npm to use this directory for its global package installs:

yourserver.runcloud $ echo “prefix = $NPM_PACKAGES” >> ~/.npmrc

Configure your PATH to see commands in your $NPM_PACKAGES prefix by adding the following to your .bashrc

# NPM packages in homedir
export NPM_PACKAGES=”$HOME/.npm-packages”

# Tell our environment about user-installed node tools
export PATH=”$NPM_PACKAGES/bin:$PATH”

# Tell Node about these packages
export NODE_PATH=”$NPM_PACKAGES/lib/node_modules:$NODE_PATH”

Now source the new bashrc, to make the modifications to work:

yourserver.runcloud $ source ~/.bashrc

Now your are half done. You setted up nodejs and npm correctly, now you need to create a web app and modify NGINX configuration easily.

Login to runcloud, go to webapps and create a new “Custom Webapp”:

Create A New Custom Web App

For this article we will use a test domain, but you can use your own custom domain. Copy the test domain, you will use to access your app.

Set Up Your Domain

Now it is really important that you choose “NGNIX + Custom Config” or you will unable to connect it to NODE JS

And click to add to webapp

Go to your WebApp Summary:

And click on NGINX Config

Click On Create Config and choose location.root:

And insert a filename, you can insert whatever you want:

Insert the following in Config Content, and hit the “Update config” Button:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8888; # Change 8888 to you app port

Your app should be configured correctly, now let’s test if it works.

Go back to your server ssh shell, enter you app directory, and create a simple nodejs express app, like this:

$ ssh runcloud@yourserver.runcloudyourserver.runcloud $ cd webapps/app-yourapp
yourserver.runcloud $ npm init
entry point: server.js #name it as you wishyourserver.runcloud $ npm install express –save

Now open server.js and paste the following:

const express = require(‘express’)const app = express()const port = 8888
app.get(‘/’, (req, res) => res.send(‘Hello World From NodeJS!’))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

Now run the app and open the test domain, you copied before…

yourserver.runcloud $ node server.js
Example app listening at http://localhost:8888

Open the runcloud link in the browser, and BOOM ! It works 😀

Output From Example App

Now if you want to daemonize and run your node app without running it from shell, you should install pm2 (on the server):

yourserver.runcloud $ npm install pm2@latest -g
# Now run pm2 startup and follow instructions, if you are not in sudoers, just run the sudo commands as su -c “command” or as root
yourserver.runcloud $ pm2 startup

pm2 startup will make possible to daemonize and run at startup all of your nodejs apps.

Now run your nodejs app with pm2 and you are done!

yourserver.runcloud $ pm2 start server.js –watch

Thanks for reading, hope it helped and please clap !

 

 

More command

 

npm install forever -g     under root directory

[sudo] npm install forever-monitor

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *