Keystone js deployment on Ubuntu 16.04/18.04

Date : 07-Jan-2020

Let us see how to deploy Keystone js on Ubuntu 16.04/18.04

Step 1: Install Nginx webserver

Install nginx webserver in your Ubuntu machine using the following command.

apt-get install nginx

Post installation, you can check the status of the webserver using the command,

systemctl status nginx

To confirm that the software is running properly through your IP address, give the following url.

http://server_ip_address

It will access the default nginx landing page.

Setup your domain

  • Create a directory for your domain name in /var/www/
    Ex. /var/www/domainname.com/html
  • Assign the ownership of the directory.
    chown -R $USER:$USER /var/www/domainname.com/html
  • Make sure the permission of the directory created is 755.
    chmod -R 755 /var/www/domainname.com
  • Create a sample html page(index.html) in /var/www/domainname.com/html

Configure your domain

  • Create a domainname.conf file in /etc/nginx/sites-available
  • In /etc/nginx/sites-available/domainname.conf, paste the following contents.
    server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;
    server_name example.com www.example.com;
    location / {
    try_files $uri $uri/ =404;
    }
    }
  • Enable the file by creating a link from it to the sites-enabled directory.
    ln -s /etc/nginx/sites-available/domainname.com /etc/nginx/sites-enabled/
  • Test the syntax by giving the command, nginx -t
  • Restart nginx to enable the changes.
    systemctl restart nginx
  • Now nginx should serve your domain name. Test it out by giving the url as http://domainname.
  • The site should load with the sample html page created.

Step 2: Install Ruby

Install curl and git packages.

apt update
apt install curl git

Then run the commands below to add Node.js and Yarn repositories and keys to your system. Then install some core packages to get your environment going.

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

apt-get update
apt-get install nodejs yarn zlib1g-dev build-essential libpq-dev libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

After adding the repositories and installing necessary packages above, install Ruby with your local profile settings using rbenv… you’ll then use rbenv to install ruby-build…

cd ~/
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

After setting up your local profile… run the commands below to install Ruby version 2.5.3… If a newer version is available, replace the version number to that.

Note: You can check the latest version of Ruby in the following link.
https://www.ruby-lang.org/en/downloads/

rbenv install 2.5.3
rbenv global 2.5.3

To verify that Ruby is installed, run the commands below:

 ruby -v 

It will display the ruby version installed.

Step 3: Install Nodejs-8

Install Node.js using the following command.

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install -y nodejs

Step 4: Install MongoDB

Install MongoDB using the following command.

apt install mongodb
npm install mongodb --save

Once MongoDB server is installed, open a new terminal and start an interactive mongo prompt by typing the following command.

mongo

Press ctrl+c to exit.

Step 5: Install dotenv, pm2

npm install dotenv
npm install pm2@latest -g

Step 6: Install KeystoneJS (along with Yoeman)

npm install imagemagick
npm install -g generator-keystone
npm install -g yo

To start the server, run the below command

 node keystone 

Step 7: Run the service using pm2

You can run the service using pm2 command.

Go to the directory where you have installed the app. Then run the following command.

 pm2 start keystone.js 
You can watch logs using the command,  “pm2 logs” 
You can see the app status using the command, “pm2 status”

Thats it!! Your project is successfully running as you can see the logs response without any errors. The app port is 3000.

Now open your browser and browse to the hostname follow by port # 3000

http://localhost:3000

Thank you !!!

Leave a Reply