Let’s pick right up where we left off. If you’re not currently logged into your DigitalOcean server as your superuser account, do that first. Now we’re going to install Node, but we’re going to skip using NVM on our server because some modules are a little sketchy with it, and we want a live server to be as stable as possible. To that end, we’re going to install the latest 8.x version, which should work fine with our code and not introduce any breaking changes, even if it's higher than 8.11.1.
Doing this is pretty straightforward, and frankly I could just link you to DigitalOcean’s own excellent tutorial on the subject, but it has you installing an older version of Node than our app wants, so I’m going to reproduce some of the steps here. It also installs Nginx as a proxy server, which is useful but outside the scope of this tutorial series, so we’re going to skip that step.
First, let’s get Node installed. Type the following:
cd ~ curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
Then run the script you just downloaded with this command:
sudo bash nodesource_setup.sh
You’ll need your sudo password for this, obviously. Once you’ve run the script, Ubuntu’s built-in
apt-get package manager will be prepared to download the proper version of Node, so type the following:
sudo apt-get install nodejs
Answer Yes when it asks you if it’s OK to use the disk space. We’ll also need a related package that will allow certain Node modules to build properly, so run this command:
sudo apt-get install build-essential
Answer yes, again, when it asks about disk space. Mildly important note: technically the binary installed by this process is called “nodejs” and not simply “node.” In practice, both commands work, so it doesn’t really matter.
We’re set with Node. Now we need some other stuff.
Install Helper Modules
We’re going to install some global Node modules that we’ll need to make our lives easier. The first is Yarn, which we’ll then use to get all the modules our actual application needs. The second is a module called PM2 which allows you to run a Node server as a background instance (if you were to just run your app from your terminal, it would shut down as soon as you quit the SSH session).
You could also set up your Node app as a service, but I like this method a bit better because PM2 provides a couple of nice features.
We can install them both with the following line:
sudo npm install -g pm2 yarn
Once that’s done its thing, head for your musiclist directory with:
And then type:
to install your modules. This will also auto-run
snyk-protect to apply security patches. It takes a couple of minutes, so don’t worry that it’s hanging.
We’re almost done here! We need to manually create our
config.json file because it’s not stored in github. You can use the text editor of your choice here … vi, emacs, pico, nano … we could either fight all day about which one’s the best, or we could not give a crap. I’m opting for the latter approach! If you have no preference, I suggest nano, because it’s easy. Type the following:
sudo nano config.json
You’ll be taken to a rudimentary-looking text editor. From here, you can just paste in the content from the
config.json file on your local machine. If you’re using PuTTY, right-clicking pastes. If you’re using the OSX SSH client, then it’s just
cmd-v as normal.
To save the file hit
cmd-x) to exit the program. It’ll ask if you want to save before exiting, so just hit y and move on.
Seems like we’re all set, but we have two rather important steps to go. The first is: we need to set up PM2 as a service so that it’ll restart itself (and our app) should our server reboot. The second is even more essential: we need to install and configure MongoDB.
PM2’s easy so let’s take care of that first. Just type the following:
pm2 startup systemd
This will generate a bunch of lines in your terminal. You need to copy and paste the final line in order to actually establish the service. It’ll look something like this:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u [yourusername] --hp /home/[yourusername]
Except with, of course, your user name in the appropriate places. Do not cut and paste the command above. Cut and paste the command you see in your SSH session. You’ll see a bunch of information, including a few “errors” about a missing dump file. You can feel free to not worry about those. PM2 should be established as a service. We just don’t have it doing anything yet.
Right, we’re all set here. We’ll fire up PM2 once we have MongoDB installed. Speaking of … that … I think we can get it done in this tutorial. Let’s rock and roll.
We begin with a few bits and bobs that need to be done before installation. Start with this command:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
This should give you some output that ends with the following two lines:
gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
Now create a list file with the following:
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
And finally, run this command:
sudo apt-get update
This will make sure
apt-get is prepared to install MongoDB, which is what we’re going to do next. So type in this command:
sudo apt-get install -y mongodb-org
-y skips the “do you want to use the disk space?” question. Once that’s done installing, let’s start it with
systemctl, which will run it as a background service. Here’s how:
sudo systemctl start mongod
That will appear to do nothing, but never fear! There’s nothing Unix commands love more than appearing to do nothing, sometimes while actually doing many, many things. In this case, we can check and make sure something was actually done by using:
sudo systemctl status mongod
Which will output a whole bunch of information, the most important of which is the green “active (running)” part. Our final step is making sure
systemctl starts Mongo back up when the server restarts. Do that with this line:
sudo systemctl enable mongod
That’s it for now. In the next tutorial, we’re going to make a few final security tweaks and get our app running. If all is working well, that will be the final tutorial in the Five Minute React series! I hope I’ll see you there!