Deploying Node.js apps to production(ish)

I am on a roll this week! Three articles about Node.js, get me! Anyway, after playing with various Nodey bits I decided a good next step would be to get my code running somewhere other than my development machine. Thankfully I have an Ubuntu staging server that I use at work to demo prototypes and other bits and bobs, so I thought I’d chuck the uploader there and get it running over the network. As with the last few articles, here’s the steps I took, mainly for my own records but also for anyone else who’s interested:

Step 1: Install Node.js on your server

I mean… duh. Obviously. Chances are you’ll likely be running this on Linux in some form, so you can find instructions on the Node.js website here: https://nodejs.org/en/download/package-manager. If you’re on Ubuntu, like me, you can just head straight here: https://github.com/nodesource/distributions/blob/master/README.md#deb

Step 2: Get your app onto the server

Again, a bit of a given. Use some form of transfer, be it FTP, Git, direct network connection – whatever floats your boat. One thing to remember is that you don’t need to transfer your node_modules folder. This can get quite big and have a lot of files inside it – just make sure you’ve got the package.json file in your project and, when you have your files where you need them, cd into the project folder and run:

sudo npm install

That will go and grab all of the Node packages you’ve used in your project and install them for you. Simples!

Step 3: PERMISSIONS!!!

This one tripped me up late in the stages of getting this working, so I’m bringing it in now. YOU NEED TO MAKE SURE YOUR PROJECT FOLDER IS NOT OWNED BY ROOT. I hit a wall with various things and this turned out to be the culprit, so use your own user or, better yet, create a user for your project and switch over to that user. Then, in your project folder, do a:

sudo chown <your-user> * -R

Which will set all the project files to be owned by your user.

Step 4: Install PM2

Right then – PM2 is our secret sauce in this process. It’s a Node package that will effectively run your projects as background tasks pretty much ad-infinitum. Big thanks to this post for the next few steps: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04. So, let’s get PM2 installed:

sudo npm install -g pm2

The -g installs the package globally so that it can be used from anywhere. For now though, make sure you’re logged into the root folder of your app as the user you’re going to be running the app as.

Step 5: Get PM2 to run the app

Run the following:

pm2 start <your-app>

This will start the Node application running in the PM2 environment and… well, that’s it! If you’re running the app on port 3000, just point your browser at http://your.server:3000 and your app should appear.

There’s a lot you can do with PM2 and the article I linked above has some handy tips about the type of commands you can try out.

What about the “ish”?

Ah yes, that. Well, obviously this is great for getting your app running on a network production server or some kind of intranet environment, but what if you want to get it accessible online via a URL? That’s something I don’t have the answer to as I don’t yet have a use case. There’ll be plenty of handy guides out there and I’ll be looking into it in more detail in the future but, for now, this is where we are – use PM2 to run and manage your Node.js apps on production. Again, simples as I’m finding with most things in this framework.

As always, let me know if you’ve found this article useful or if you just want to chat about Node.js further by chucking a comment down below!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s