Intro

Hello and welcome back to our tutorial on building and deploying a Node.js API to Digital Ocean. Yesterday we quickly wrote our simple API and pushed it to Github. Now, we are ready to start creating our FreeBSD droplets then configuring them to run Nginx and Node.js.

Getting Started

First, let's open digitalocean.com and start by creating 2 droplets. One for Nginx and one for our Node API.

(video showing the droplet creation process)

Good, we have named one of them Nginx and the other Node1. Now let's open our terminal and ssh into our Nginx droplet. Make sure to copy the IP address from the Digital Ocean dashboard.

ssh freebsd@your_nginx_ip_address
ls
ls /

Good, we are in our FreeBSD shell and you can see what our home and root directories look like. It's time to begin the configuring so we will start by changing our root password. Let's get started.

sudo passwd

That was easy. Now that we have our own root password, let's learn how to use FreeBSD's package manager. We'll do this by installing the world's simplest command line text editor, Nano.

sudo pkg search nano
sudo pkg install nano

Again, this was easy. We started by using pkg to search for the Nano options that were out there. We didn't have to do this, but it's good to know the option is there in the event you need it. Lastly we just ran the install nano command and that was it.

It's also worth mentioning that vi is the default text editor on FreeBSD so if you do not wish to use Nano, you don't have to. I personally prefer vi but for the sake of making this as accessible as possible for most folks, we'll use Nano.

Now our next configuration we need to take care of is our firewall. FreeBSD has numerous options by default but we will go with IPFW. To do this, we first need to make some system file changes so follow along.

sudo nano /etc/rc.conf
# Firewall Configuration
firewall_enable="YES"
firewall_quiet="YES"
firewall_type="workstation"
firewall_myservices="22 80"
firewall_allowservices="any"
firewall_logdeny="YES"

Excellent, now let's save and exit from Nano and start the firewall

sudo service ipfw start

Good, now that our firewall is up and running, we need to configure ntpd and our time zone setup. This will be key to making sure all of our services run in sync with each other. Plus it will be good if we have to interact with any other 3rd party services.

sudo nano /etc/sysctl.conf
net.inet.ip.fw.verbose_limit=5
sudo nano /etc/rc.conf
# ntpd config
ntpd_enable="YES"
ntpd_start_on_sync="YES"
sudo service ntpd start
sudo tzsetup
clear

Great, now we need install Nginx.

sudo pkg install Nginx

That was hard wasn't it? Yeah not really. That said, let's handle one last configuration for Nginx to be able to truly do its job.

sudo nano /etc/rc.conf
# nginx
nginx_enable="YES"
sudo service nginx start

There we go. Now we have nginx up and running. We've done all the configuration we need to do for now on our Nginx droplet. Now we will focus on our Node server. We'll power through certain section a little faster because much of it will be the same. However, we will make sure to point out any important details along the way. Let's open a new terminal window and get started.

ssh freebsd@your-node-droplet-ip
sudo passwd
sudo pkg install nano
sudo nano /etc/rc.conf
# IPFW Firewall Configuration
firewall_enable="YES"
firewall_quiet="YES"
firewall_type="workstation"
firewall_myservices="22 80 3000"
firewall_allowservices="any"
firewall_logdeny="YES"

# NTPD Configuration
ntpd_enable="YES"
ntpd_sync_on_start="YES"
sudo nano /etc/sysctl.conf
net.inet.ip.fw.verbose_limit=5
sudo services ntpd start
sudo services ipfw start
sudo tzsetup
clear
sudo pkg install node
sudo pkg install npm
sudo pkg install git
sudo npm install -g pm2 nodemon yarn
git clone http://your/app/url

Great, now if you remember that Mongo url from before that we had on MLab, it's time to set it as an environment variable in our system so follow along.

sudo nano /etc/profile
export MONGO_URL=mongodb://your/mongo/url

Great, now let's exit the shell session.

exit

Fantastic, now, the next time we log in, the environment variable should be available for you to work with. To check simply type the printenv command and look for the variable in the lineup.

Conclusion

Now that we have configure our FreeBSD servers, we're going to want to learn how to actually launch our apps as a daemon along with how to monitor their performance. Tomorrow, we will learn how to do this using PM2. Thank you for your time and we'll see you soon.

Helpful Links

  1. www.freebsd.org
  2. The FreeBSD Handbook - https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/index.html

Alex Allen

Alex is an independent developer who is obsessed with both performance and information security. When not writing code, Alex is either playing guitar or working in his garden.

  1. Comments for Create FreeBSD droplets and configure them to run Nginx and Node.js

You must login to comment

You May Also Like