Install Sails.js framework using Nginx on CentOS 8

Install Sails.js framework using Nginx on CentOS 8

Sails.js is a Javascript framework for Node.js. It is used to develop real-time applications very quickly. It allows you to resemble the MVC architecture in frameworks such as Ruby on Rails. It comes with a blueprint to help you quickly launch the back end of your application without writing any code. It is compatible with other front ends, including Angular, React, iOS, Android, Windows Phone, custom hardware or anything else.

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

prerequisites

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

Install Node.js

First, install all required dependencies using the following command:

dnf install curl gcc-c++ make -y

After installing all the dependencies, use the following command to add the Node source repository:

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

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

dnf install nodejs -y

After the installation is complete, use the following command to verify the Node.js version:

node --version

You should get the following output:

v16.4.0

Install Sails.js

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

npm -g install sails

Next, create your project using Sails.js and 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 <CTRL+C> to cancel)
? 2

Type 2 and click Enter 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 myprojectsails 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 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 reloads the systemd daemon using the following command:

systemctl daemon-reload

Next, start the Sails service and enable it to start when the system restarts:

systemctl start sailssystemctl enable sails

You can check the status of 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 starts and listens on port 1337.

Configure Nginx as a reverse proxy for Sails applications

It is recommended to install and configure Nginx as a reverse proxy for Sails applications.

First, install the Nginx package using the following command:

dnf install nginx -y

After installing Nginx, create a 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        https://localhost:1337/;
     proxy_set_header  Host $host;
     proxy_buffering   off;
   }
 }

Save and close The completed file.

Next, use the following command to verify if there are any configuration errors in Nginx:

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 when the system restarts:

systemctl start nginxsystemctl enable nginx

Now, use the following command to check the status of the Nginx service:

systemctl status nginx

Configure firewall

Next, you 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 completed, you can proceed to the next step.

Access Sails.js web interface

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

in conclusion

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