AdonisJs is a Node.js web framework with a breath of fresh air and drizzle of elegant syntax on top of it. They prefer developer joy and stability over anything else.
At the time of writing this article, Adonisjs v5 preview was released. Deploying adonisjs v5 is a bit different from deploying version 4. I will write about that soon.
Create a GCP instance
If you don’t have a Google Cloud account, you can get one by clicking the link below.
GET FREE 12 MONTHS $300 GCP CREDIT
Now that you have a functional GCP account, let’s create our first instance.
If everything goes as plan, you should see something like the image below:
Now, click on the menu Icon and look for Compute Engine as shown below:
Okay. Cool. We are now on our Google Compute Engine Virtual Machine instance dashboard. Let’s create a new instance for our adonisjs project. Take a look at the image below:
After clicking that, a dialogue box will come up where you can setup your instance. The image(s) below shows you how.
Since we want to use Ubuntu Operating System, let’s pick ubuntu.
Choose, the version you want. I will recommended v18.04 plus.
Set network tags. Just tick the two protocols as shown below to allow traffic from all of them since it’s a web app.
Now that we are set, click on the create button and you should see your instance created as shown on the image below.
Install Apache Server
We will be using apache to proxy our adonis app. You can use Nginx to do the same.
So, let’s SSH into our instance and install apache2. But before then, let’s update all outdated ubuntu packages with this command.
sudo apt-get update
Okay. Cool. Let’s install our apache server by executing the command below:
sudo apt-get install -y apache2
That’s it! When you visit your external IP, you should see the screen below:
We will install node v14.*. I wrote an article on how to upgrade or install Node here.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Now, install Node with apt-get as below:
sudo apt-get install -y nodejs
Cool. Now you can confirm the NPM and Node versions by executing the commands below respectively.
npm -v //Result will be 6.14.4
node -v //Result will be v14.2.0
Deploy our app
Let’s create our project folder inside /var/www directory as below:
cd /var/www && sudo mkdir adonisjs && cd adonisjs
Now that we have our folder created and changed our working directory to this newly created directory, let’s pull our code from any version control service hosting our source codes e.g Github.
Let’s SSH into our instance if not already loggedin and clone the repo with the command below.
sudo git clone your_repository_link .
Note, we have a leading (.) which means we want to clone the repository into the directory we are.
Install NPM dependencies
sudo npm install
Install adonis cli
sudo npm i -g @adonisjs/cli
Let’s confirm that our adonis cli was installed correctly by executing the command below.
You should see the image below if everything works fine.
Now we can create our .env file.
sudo cp .env.example .env
After that is done, we need to generate our app key.
sudo adonis key:generate
Now we are set to serve our adonis project. However, adonis will be served in our localhost environment with the IP 127.0.0.1 on port 3333.
When this is done, we still won’t be able to access our app through our domain or external IP address. So, we need to proxy all request from our external ip on port 80 to 127.0.0.1:3333 where our adonis app is served.
To setup our proxy, we need to edit our configuration. Here is the code to update our VirtualHost for port 80.
First, let’s open our configuration file like this:
cd /etc/apache2/sites-available && sudo nano 000-default.conf
Then, change the DocumentRoot /var/www/html to the code below:
#DocumentRoot /var/www/html ProxyRequests On ProxyPass / http://127.0.0.1:3333/ ProxyPassReverse / http://127.0.0.1:3333/
Press Control X then Y then Enter to save and exit the editor.
Apache has many modules bundled with it that are available but not enabled in a fresh installation. Let’s enable the ones we’ll use in this tutorial.
The modules we need are
mod_proxy itself and several of its add-on modules, which extend its functionality to support different network protocols. Specifically, we will use:
mod_proxy, the main proxy module Apache module for redirecting connections; it allows Apache to act as a gateway to the underlying application servers.
mod_proxy_http, which adds support for proxying HTTP connections.
mod_lbmethod_byrequests, which add load balancing features for multiple backend servers.
Here are the commands to run:
sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_balancer sudo a2enmod lbmethod_byrequests
After that, we need to restart our apache server.
sudo service apache2 restart
Now open a new tab and enter your external IP, you should see the screen below:
Boom. We have proxied our adonis app server address properly. It’s time to start our adonis app and open our external ip again.
cd /var/www/adonisjs && sudo adonis serve --dev
We should see this image below:
Yeepy! we’ve successfully deployed our adonis app on GCP.
In my next article, I will show you how to serve your adonis app properly with a process so we keep the app alive 24/7.
If you have any questions or anything regarding this tutorial, do drop your comments on the comment section below.