Deploy on Ubuntu
This is the official documentation of the @forestadmin/agent
Node.js agent.
The goal of this tutorial is to help developers deploy their admin backend to an 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.
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 versioning the code of your admin backend using git and hosting 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
.
In the example above, we use an SSH connection to transfer the file and we connect to the remote server using an identity_file (a private key).
-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.
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:
Then, you can add all the files and create your first commit:
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:
Now, you can connect to your remote server using SSH and clone the repository using the HTTPS method.
That's it. Your admin backend's code is available on your remote server.
Install dependencies
First, you have to make sure you have Node.js and NPM correctly installed on your server.
Then, you will be able to install all the dependencies listed on the package.json file.
Create the database
PostgreSQL
This step is optional if you already have a running database.
First, you need to install PostgreSQL:
Then, you will be able to connect to the database server:
Now, we can export the database from your local environment (your computer) to import it to your Ubuntu server.
For security reasons, 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:
Then, we will create a new DB user and schema from the remote server:
And finally, import the dump:
That's it, your database is now fully imported.
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.
Then, you can restart your server to take these new variables into account or simply type:
Run your admin backend
From your admin backend's directory, simply type:
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
Run your admin backend using PM2
(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.
To do so, edit (with sudo
access) the file located /etc/nginx/sites-available/default
and replace the existing section location /
by this one:
Then, restart nginx:
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 updated