Sails.js is a Javascript framework for Node.js. It is used for developing real-time applications very quickly. It allows you to resemble the MVC architecture from frameworks like Ruby on Rails. It comes with blueprints that help jumpstart your app’s backend without writing any code. It is compatible with other front-end including, Angular, React, iOS, Android, Windows Phone, custom hardware, or something else entirely.

In this post, we will show you how to install Sails.js with Nginx on CentOS 8

Prerequisites

  • A server running CentOS 8.
  • A root password is configured on the server.

Install Node.js

First, install all the required dependencies using the following command:

dnf install curl gcc-c   make -y

Once all the dependencies are installed, add the Node source repository with the following command:

curl -sL https://rpm.nodesource.com/setup_16.x | bash -

After adding the Node source repository, install the Node.js with the following command:

dnf install nodejs -y

Once the installation is completed, verify the Node.js version with the following command:

node --version

You should get the following output:

v16.4.0

Install Sails.js

You can install the Sails.js using the NPM command as shown below:

npm -g install sails

Next, create your project using Sails.js with the following command:

sails new myproject

You will be asked to choose a template for your application:

 Choose a template for your new Sails app:
 1. Web App  ·  Extensible project with auth, login, & password recovery
 2. Empty    ·  An empty Sails app, yours to configure
 (type "?" for help, or  to cancel)
? 2

Type 2 and hit Enter to install the application. You should get the following output:

 info: Installing dependencies...
Press CTRL C to cancel.
(to skip this step in the future, use --fast)

 info: Created a new Sails app `myproject`!

Start the Sails.js Application

Next, change the directory to myproject and start your application with the following command:

cd myproject

sails lift

You should get the following output:

 info: Starting app...

 info: 
 info:                .-..-.
 info: 
 info:    Sails              <|    .-..-.
 info:    v1.4.3              |
 info:                       /|.
 info:                      / || 
 info:                    ,'  |'  
 info:                 .-'.-==|/_--'
 info:                 `--'-------' 
 info:    __---___--___---___--___---___--___
 info:  ____---___--___---___--___---___--___-__
 info: 
 info: Server lifted in `/root/myproject`
 info: To shut down Sails, press    C at any time.
 info: Read more at https://sailsjs.com/support.

debug: -------------------------------------------------------
debug: :: Thu Jun 24 2021 04:46:13 GMT-0400 (Eastern Daylight Time)

debug: Environment : development
debug: Port        : 1337
debug: -------------------------------------------------------

Press CTRL C to stop the application.

Create a Systemd Service File for Sails.js

Next, you will need to create a systemd service file to manage your application.

You can create it with the following command:

nano /lib/systemd/system/sails.service

Add the following lines:

[Unit]
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/myproject
ExecStart=/usr/bin/sails lift
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save and close the file then reload the systemd daemon with the following command:

systemctl daemon-reload

Next, start the Sails service and enable it to start at system reboot:

systemctl start sails

systemctl enable sails

You can check the status of the Sails with the following command:

systemctl status sails

You should get the following output:

? sails.service
   Loaded: loaded (/usr/lib/systemd/system/sails.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-06-24 04:47:07 EDT; 5s ago
 Main PID: 47388 (node)
    Tasks: 22 (limit: 25014)
   Memory: 148.1M
   CGroup: /system.slice/sails.service
           ??47388 node /usr/bin/sails lift
           ??47395 grunt

Jun 24 04:47:09 centos8 sails[47388]:  info:  ____---___--___---___--___---___--___-__
Jun 24 04:47:09 centos8 sails[47388]:  info:
Jun 24 04:47:09 centos8 sails[47388]:  info: Server lifted in `/root/myproject`
Jun 24 04:47:09 centos8 sails[47388]:  info: To shut down Sails, press    C at any time.
Jun 24 04:47:09 centos8 sails[47388]:  info: Read more at https://sailsjs.com/support.
Jun 24 04:47:09 centos8 sails[47388]: debug: -------------------------------------------------------
Jun 24 04:47:09 centos8 sails[47388]: debug: :: Thu Jun 24 2021 04:47:09 GMT-0400 (Eastern Daylight Time)
Jun 24 04:47:09 centos8 sails[47388]: debug: Environment : development
Jun 24 04:47:09 centos8 sails[47388]: debug: Port        : 1337
Jun 24 04:47:09 centos8 sails[47388]: debug: -------------------------------------------------------

At this point, Sails is started and listening on port 1337.

Configure Nginx as a Reverse Proxy for Sails Application

It is recommended to install and configure Nginx as a reverse proxy to your Sails application.

First, install the Nginx package with the following command:

dnf install nginx -y

After installing Nginx, create an Nginx virtual host configuration file for Sails:

nano /etc/nginx/conf.d/sails.conf

Add the following lines:

server {
 listen       80;
 server_name  sails.domain.com;
   location / {
     proxy_pass        http://localhost:1337/;
     proxy_set_header  Host $host;
     proxy_buffering   off;
   }
 }

Save and close the file when you are finished.

Next, verify the Nginx for any configuration error with the following command:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, start the Nginx service and enable it to start at system reboot:

systemctl start nginx

systemctl enable nginx

Now, check the status of the Nginx service with the following command:

systemctl status nginx

Configure Firewall

Next, you will need to allow port 80 through the firewall. You can allow it with the following command:

firewall-cmd --permanent --zone=public --add-port=80/tcp

Next, reload the firewall to apply the changes:

firewall-cmd --reload

Once you are finished, you can proceed to the next step.

Access Sails.js Web Interface

Now, open your web browser and access the Sails.js web interface using the URL http://salis.domain.com. You should see the Sails.js default page on the following screen:

<img alt="Sails.js" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/10/echo/p1.png6172c1ec911ac.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="403" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! you have successfully installed Sails.js with Nginx as a reverse proxy on CentOS 8. You can now start developing real-time applications with Sails.