Deploy your admin backend to Ubuntu server
The goal of this tutorial is to help people deploy their admin backend to Ubuntu server.

Connect to your Ubuntu server using SSH

Before starting anything, you have to make sure you're able to connect to your server using SSH.
Command line
Output
1
ssh -i ~/.ssh/aws.pem [email protected]
Copied!
1
Warning: Permanently added 'ec2-18-204-18-81.compute-1.amazonaws.com,18.204.18.81' (ECDSA) to the list of known hosts.
2
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-1021-aws x86_64)
3
...
Copied!

Copy the code of your admin backend to your remote server

There are many ways to copy the code of your admin backend to a remote server. For example, you can use rsync command, or use a versioning system like git.
We strongly advise to version the code of your admin backend using git and host it to a private repository on Github, Bitbucket, Gitlab or other providers.

rsync

rsync is a utility for efficiently transferring and synchronizing files across computer systems, by checking the timestamp and size of files. It is commonly found on Unix-like systems and functions as both a file synchronization and file transfer program.
Rsync is typically used for synchronizing files and directories between two different systems. (source: wikipedia)
The syntax used is rsync OPTIONS SOURCE TARGET.
1
rsync -avz -e "ssh -i ~/.ssh/aws.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --exclude=node_modules --exclude=.git --progress QuickStart [email protected]:~/
Copied!
In the example above, we use a SSH connection to transfer the file and we connect to the remote server using an identity_file (a private key).
Option
Description
-a
archive mode; same as -rlptgoD (no -H)
-v
increase verbosity
-z
compress file data during the transfer
-e
specify the remote shell to use
--exclude=PATTERN
exclude files matching PATTERN
--progress
show progress during transfer
Once done, you can find the code of your admin backend on the home directory of your remote server.
Command line
Output
1
ssh -i ~/.ssh/aws.pem [email protected]
2
[email protected]:~$ cd Quickstart/
3
[email protected]:~/QuickStart$ ls -l
Copied!
1
total 5116
2
-rw-r--r-- 1 ubuntu ubuntu 1386 Oct 22 08:11 app.js
3
-r-------- 1 ubuntu ubuntu 1692 Oct 23 12:51 aws.pem
4
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 12 10:19 bin
5
-rw-r--r-- 1 ubuntu ubuntu 5126311 Oct 12 11:20 database.dump
6
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 19 11:49 forest
7
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 19 12:22 models
8
-rw-r--r-- 1 ubuntu ubuntu 69568 Oct 22 07:30 package-lock.json
9
-rw-r--r-- 1 ubuntu ubuntu 717 Oct 22 07:30 package.json
10
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 12 10:19 public
11
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 22 07:48 routes
12
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 19 11:53 serializers
13
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 19 11:55 services
14
[email protected]:~/QuickStart$
Copied!

git

First, you need to initialize a git repository for the code of your admin backend. From the directory of your admin backend, simply run:
1
git init
Copied!
Then, you can add all the files and create your first commit.
1
git add .
2
git commit -am "First commit"
Copied!
Finally, you can add your git remote and push the code on your favorite platform. To do so, first create a new QuickStart repository on your github account. Then run the following command after changing YourAccount to your account name:
1
git remote add origin [email protected]:YourAccount/QuickStart.git
2
git push -u origin master
Copied!
Now, you can connect to your remote server using SSH and clone the repository using the HTTPS method.
Command line
Output
1
ssh -i ~/.ssh/aws.pem [email protected]
2
git clone https://github.com/YourAccount/QuickStart.git
Copied!
1
Cloning into 'QuickStart'...
2
remote: Enumerating objects: 34, done.
3
remote: Counting objects: 100% (34/34), done.
4
remote: Compressing objects: 100% (21/21), done.
5
remote: Total 34 (delta 7), reused 34 (delta 7), pack-reused 0
6
Unpacking objects: 100% (34/34), done.
Copied!
That's it. Your admin backend's code is available on your remote server.
Command line
Output
1
[email protected]:~$ cd QuickStart/
2
[email protected]:~/QuickStart$ ls -l
Copied!
1
total 5112
2
-rw-rw-r-- 1 ubuntu ubuntu 1386 Oct 23 13:42 app.js
3
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 23 13:42 bin
4
-rw-rw-r-- 1 ubuntu ubuntu 5126311 Oct 23 13:42 database.dump
5
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 23 13:42 forest
6
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 23 13:42 models
7
-rw-rw-r-- 1 ubuntu ubuntu 69568 Oct 23 13:42 package-lock.json
8
-rw-rw-r-- 1 ubuntu ubuntu 717 Oct 23 13:42 package.json
9
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 23 13:42 public
10
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 23 13:42 routes
11
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 23 13:42 serializers
12
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 23 13:42 services
13
[email protected]:~/QuickStart$
Copied!

Install dependencies

First, you have to make sure you have Node.js and NPM correctly installed on your server.
1
sudo apt update
2
sudo apt install nodejs npm
Copied!
Then, you will be able to install all the dependencies listed on the package.json file.
1
npm install
Copied!

Create the database

PostgreSQL

This step is optional if you already have a running database.
First, you need to install PostgreSQL:
1
sudo apt-get install postgresql postgresql-contrib
Copied!
Then, you will be able to connect to the database server:
Command line
Output
1
sudo -u postgres psql
Copied!
1
psql (10.5 (Ubuntu 10.5-0ubuntu0.18.04))
2
Type "help" for help.
3
4
postgres=
Copied!
Now, we can export the database from your local environment (your computer) to import it to your Ubuntu server.
For security reason, we will not allow remote connections to this database. This is why transfer the database dump to the remote server using rsync.
From your computer:
1
PGPASSWORD=secret pg_dump -h localhost -p 5416 -U forest forest_demo --no-owner --no-acl -f database.dump
2
rsync -avz -e "ssh -i ~/.ssh/aws.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress database.dump [email protected]:~/
Copied!
Then, we will create a new DB user and schema from the remote server:
1
sudo -u postgres psql
2
postgres=# CREATE USER forest WITH ENCRYPTED PASSWORD 'secret';
3
postgres=# CREATE DATABASE forest_demo;
4
postgres=# GRANT ALL PRIVILEGES ON DATABASE forest_demo TO forest;
5
postgres=# \q
Copied!
And finally import the dump:
1
PGPASSWORD=secret psql -U forest -h 127.0.0.1 forest_demo < database.dump
Copied!
That's it, your database is now fully imported.
Command line
Output
1
PGPASSWORD=secret psql -U forest -h 127.0.0.1 forest_demo
Copied!
1
psql (10.5 (Ubuntu 10.5-0ubuntu0.18.04))
2
Type "help" for help.
3
4
forest_demo=>
Copied!
Command line
Output
1
forest_demo=> \d
Copied!
1
List of relations
2
Schema | Name | Type | Owner
3
--------+---------------------+----------+----------
4
public | Companies_id_seq | sequence | postgres
5
public | addresses | table | postgres
6
public | addresses_id_seq | sequence | postgres
7
public | appointments | table | postgres
8
public | appointments_id_seq | sequence | postgres
9
public | companies | table | postgres
10
public | customers | table | postgres
11
public | customers_id_seq | sequence | postgres
12
public | deliveries | table | postgres
13
public | deliveries_id_seq | sequence | postgres
14
public | documents | table | postgres
15
public | documents_id_seq | sequence | postgres
16
public | orders | table | postgres
17
public | orders_id_seq | sequence | postgres
18
public | products | table | postgres
19
public | products_id_seq | sequence | postgres
20
public | transactions | table | postgres
21
public | transactions_id_seq | sequence | postgres
22
(18 rows)
Copied!

Export the environment variables

You must export the environment variables FOREST_ENV_SECRET FOREST_AUTH_SECRET and DATABASE_URL. To do so, open and edit the file /etc/environment:
The FOREST_ENV_SECRET and FOREST_AUTH_SECRET environment variables will be given by Forest after creating a production environment from the interface. See how to get them here.
1
sudo vim /etc/environment
Copied!
/etc/environment
1
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
2
FOREST_ENV_SECRET=2417520743be37a9c5af198c018e0ddee9b7c41de1ccb8e76c9d027faa74059e
3
FOREST_AUTH_SECRET=Piq7a9Kv5anLbK4gj81rirsLhfaJ0pdL
4
DATABASE_URL=postgres://forest:[email protected]/forest_demo
Copied!
Then, you can restart your server to take these new variables into account or simply type:
1
for env in $( cat /etc/environment ); do export $(echo $env | sed -e 's/"//g'); done
Copied!

Run your admin backend

From your admin backend's directory, simply type:
Command line
Output
1
npm start
Copied!
1
> [email protected] start /home/ubuntu/QuickStart
2
> node ./bin/www
3
4
🌳 Your back office API is listening on port 3000 🌳
5
🌳 Access the UI: http://app.forestadmin.com 🌳
Copied!
Congrats, your admin backend is now running on production. But we strongly advise you to continue following the next steps. If you chose not to do it, you can go back to your Forest interface to create a production environment. Check out here how to do it.
The admin backend is by default listening on port 3310. Be sure you authorized the inbound traffic on this port or set up a web server (like NGINX) as a Reverse Proxy Server to use the port 80.

Manage Application with PM2

PM2 is a Production Runtime and Process Manager for Node.js applications with a built-in Load Balancer. It allows you to keep applications alive forever, to reload them without downtime and facilitate common Devops tasks. source: npmjs/pm2

Install PM2

1
sudo npm install pm2 -g
Copied!

Run your admin backend using PM2

1
pm2 start bin/www
Copied!

(Optional) Set Up Nginx as a Reverse Proxy Server

Now that your admin backend is running and listening on localhost:3310, we will set up the Nginx web server as a reserve proxy to allow your admin panel's users access it.
1
sudo apt install nginx
Copied!
To do so, edit (with sudo access) the file located /etc/nginx/sites-available/default and replace the existing section location / by this one:
/etc/nginx/sites-available/default
1
location / {
2
proxy_pass http://localhost:3000;
3
proxy_http_version 1.1;
4
proxy_set_header Upgrade $http_upgrade;
5
proxy_set_header Connection 'upgrade';
6
proxy_set_header Host $host;
7
proxy_cache_bypass $http_upgrade;
8
}
Copied!
Then, restart nginx:
1
sudo systemctl restart nginx
Copied!
That's it, your admin backend is now listening on the port 80. Make sure your firewall allows inbound traffic from this port.
We now require that you configure HTTPS (port 443) on your admin backend service for security reasons. http://nginx.org/en/docs/http/configuring_https_servers.html
Once you've completed the above steps, it does not mean your project is deployed to production on Forest Admin. To deploy to production, check out this section.
Last modified 1yr ago