How to Install Sails.js MVC Framework with Nginx on Debian 11

Sails.js is a full-stack Node.js framework built on top of Express socket.io. It's a powerful MVC framework inspired by Ruby on Rails, but with supports of data-driven APIs and scalable, also service-oriented architecture. Sails.js is a suitable framework for building modern and enterprise-grade applications, especially data-driven applications.

Sails.js provides auto-generate REST APIs and a powerful ORM called Waterline that allows developers to use any databases such as MySQL, PostgreSQL, MongoDB, Redis, etc.

Prerequisites

In this article, you will learn how to install the Sails.js framework on the Debian 11 Bullseye. Also, you will learn how to create a new project using Sails.js and setting up Nginx as a reverse proxy for the Sails.js application.

To get started, ensure you've got the following requirements.

  • Operating System: Debian 11 Bullseye
  • Root privileges

Now let's jump to the installation.

Installing Development Tools

First, you will be installing some development tools such as build-essentials and GCC to your Debian system.

1. Log in to your server and run the apt command below to update repositories.

sudo apt update

2. Next, execute the following command to install development tools.

sudo apt install curl build-essential gcc g++ make

Type 'y' to confirm the installation and press 'Enter' to continue.

Install Node.js LTS and Yarn Package Manager

Before installing Sails.js, you must install Node.js to your Debian system.

In this step, you will be installing Node.js using the nodesource.com repository. And you will be installing the latest LTS version Node.js 16.x.

1. Execute the following command to add the Node.js repository.

curl -fsSL https://deb.nodesource.com/setup_16.x | bash -

The command will automatically add the Node.js repository and refresh repositories on your system.

In the end, you will see the output messages that suggest you install the Yarn package manager.

Install Nodejs with Nodesource repository

2. Add yarn GPG key and repository using the following command.

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

3. Next, update your Debian repositories, then install the Node.js and Yarn package manager using the command below.

sudo apt update && sudo apt install nodejs yarn

Type 'y' to confirm the installation and press 'Enter' to continue.

4. If Node.js and yarn installation are complete, verify the Node.js version and yarn version using the following command.

node --version
yarn --version

You will see a similar output as below.

# nodejs version
v16.13.1

# yarn version
1.22.15

Install Sails.js with Yarn Package Manager

In this step, you will be installing Sails.js latest version using the yarn package manager.

1. Execute the following command to install Sails.js globally or system-wide.

sudo yarn global add sails

This command will add binary file 'sails' to your '$PATH' directory.

2. Run the 'sails' command below to verify the version.

sails --version

In this example, we've installed Sails.js version 1.5.

1.5.0

3. Now check the help page for the 'sails' command as below.

sails --help

Below are all available options for the 'sails' command.

  Usage: sails [command]


  Options:

    -v, --version  output the version number
    -h, --help     output usage information


  Commands:

    version
    lift|l [options]
    new [options] [path_to_new_app]
    generate
    upgrade
    migrate
    console|c [options]
    www
    debug                            (for Node v5 and below)
    inspect                          (for Node v6 and above)
    run
    test
    lint
    deploy
    debug-console|dc
    help [command]

Now move to the next step to create a new Sails.js project.

Create First Project with Sails.js

In this step, you will learn how to start a new project using Sails.js. For security reasons, it's recommended to start a new Sails.js project with a non-root user.

1. Log in to your user using the following command.

su - username

2. Execute the 'sails' command below to create a new application. In this example, we will create an application 'testapp'.

sails new testapp

Choose the template for your application.

Type number '1' to select a full web application template that includes some essential features like authentication, login page, etc.

Type number '2' to select empty and classic Sails.js app.

Create new Sails.js application

When your application is created, you will see the output message such as 'Created a new Sails app `testapp`!'. And you will get a new directory 'testapp' on your home directory.

3. Change your working directory to 'testapp' and run the Sails.js application using the command as below.

cd testapp/
sails lift

Now you will see a similar output message as below.

Running Sails.jd Application

Your Sails.js application is running on the 'development' with the default port '1337'.

4. Open your web browser and type your IP address with port '1337' on the address bar.

http://192.168.1.50:1337/

You will see the default index page of the Sails.js web application.

Sails.jd index page

Now back to your terminal and press 'Ctrl+C' to stop the Sails.js application.

Move to the next step to set up your Sails.js application as a systemd service.

Setup Sails.app Application as a Systemd Service

In his step, you will be creating a new systemd service file for the Sails.js application.

1. Create a new service file 'testapp.service' using the nano editor.

sudo nano /etc/systemd/system/testapp.service

Copy and paste the following configuration. And make sure to change the 'User=johndoe' with your user and 'WorkingDirectory=/home/johndoe/testapp' with your project directory.

[Unit]
After=network.target

[Service]
Type=simple
User=johndoe
WorkingDirectory=/home/johndoe/testapp
ExecStart=/usr/local/bin/sails lift
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save the configuration and exit.

2. Next, reload the systemd manager to apply a new configuration using the following command.

sudo systemctl daemon-reload

3. Start your Sails.js application using the following command.

sudo systemctl start testapp

Verify your Sails.js application service using the command below.

sudo systemctl status testapp

And you will see a similar output message as below.

Setup Sails.js as systemd service

The Sails.js application service is active and running. it's running on the default 'development' environment with default port '1337'.

Move to the next step to set up Nginx as a reverse proxy for your Sails.js application.

Setup Nginx as a Reverse Proxy for Sails.js

In this step, you will be installing and configuring the Nginx web server as a reverse proxy for the Sails.js application.

1. First, install Nginx packages using the apt command below.

sudo apt install nginx -y

2. After Nginx installation completes, create a new server blocks configuration '/etc/nginx/sites-available/testapp' using nano editor.

sudo nano /etc/nginx/sites-available/testapp

Change the domain 'dev.example.io' with your IP address or local domain.

server {
 listen       80;
 server_name  dev.example.io;
   location / {
     proxy_pass        http://localhost:1337/;
     proxy_set_header  Host $host;
     proxy_buffering   off;
   }
 }

save the configuration and exit.

3. Next, activate the 'testapp' server blocks and verify the Nginx configuration using the following command.

sudo ln -s /etc/nginx/sites-available/testapp /etc/nginx/sites-enabled/
sudo nginx -t

If you don't have any errors on your Nginx configuration, you will see the output message as 'syntax is ok'.

4. Now restart the Nginx service to apply a new server block configuration.

sudo systemctl restart nginx

Setup Nginx as a reverse proxy for Sails.jd

5. Lastly, you need to allow the 'www-data' user to access your Sails.js application.

Change your working directory to the '/home/johndoe' using the following command. In this example, we're using the user 'johndoe'.

cd /home/johndoe/

Change the ownership and permission of the Sails.js application directory using the command below.

sudo chown -R johndoe:www-data testapp
sudo chmod -R g+rw testapp

Now you've completes the configuration of Nginx as a reverse proxy for the Sails.js application.

Verify Your Sails.js Application

1. On your machine, edit the 'hosts' configuration using the following command.

sudo nano /etc/hosts

Copy and paste the following configuration. Make sure to change the IP address and domain name with your own.

192.168.1.50  dev.example.io

Save the configuration and exit.

2. Next, open your web browser and type the local domain name of your Sails.js application as below.

http://dev.example.io/

You will see the default index of the Sails.js web application.

Sails.js application

3. Now click on the 'Sign Up' button and you will get the page for creating an account.

Example Sails.js authentication

Type details new user and password, then click the button 'Create account'.

4. After you logged in, you will see the welcome message from the Sails.js application as below.

Example Sails.js user dashboard

Conclusion

Congratulation! You've successfully learned how to install the Sails.js framework on the Debian 11 Bullseye. Also, you've learned how to create the Sails.js application and set up the Sails.js application as a systemd service.

Lastly, you've learned how to set up Nginx as a reverse proxy for your Sails.js application.

Share this page:

0 Comment(s)