How to Install Sails.js Framework with Nginx on Rocky Linux 8

Sails.js is a Javascript framework that you can use to easily and quickly build customized enterprise-grade for Node.js. It resembles the MVC architecture from such frameworks as Ruby on Rails, but with improved support for the more data-oriented modern style of developing web applications and is compatible with other front-end including Angular, React, iOS, Android, Windows Phone, and much more.

In the following tutorial, you will learn how to install Sails.js and access the web-based interface by installing and configuring an Nginx reverse proxy setup on Rocky Linux 8.

Prerequisites

  • Recommended OS: Rocky Linux 8.+.
  • User account: A user account with sudo or root access.
  • Required Packages: curl, gcc-c++, make

Update Operating System

Update your Rocky Linux operating system to make sure all existing packages are up to date:

sudo dnf upgrade --refresh -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@rockylinux ~]$ sudo whoami
root

To set up an existing or new sudo account, visit our tutorial on How to Add a User to Sudoers on Rocky Linux.

To use the root account, use the following command with the root password to log in.

su
Advertisement

Install Dependecies for Sails.js

The first task is to check or install the following packages by running the following command.

sudo dnf install curl gcc-c++ make

Example output:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Type Y, then press the ENTER KEY to proceed with installing the dependencies.

Once the dependencies are installed, you will also need to install Node.js.

The tutorial will install NPM 16, but you can change this to any alternative version that is still supported.

First, import the Node.js repository using the following terminal command.

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

Next, install Node.js.

sudo dnf install nodejs

Example output:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Type Y, then press the ENTER KEY to proceed.

During the installation, you will be prompted to import the GPG key.

Example:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Type Y, then press the ENTER KEY to proceed and complete the installation.

Once the installation has finished, verify the installation by checking the version build number.

node --version

Example output:

v16.12.0

Alternatively, if you are looking for a specific version of Node.js, visit our tutorial How to Install Node.JS 14 / 16 & NPM on Rocky Linux 8.

Install Sails.js

The next part of the installation will use NPM to install Sails.js. To do this, use the following command.

sudo npm -g install sails

Now that Sails.js is installed, the next step is to create a new project that you can name anything you like, but it will be called “testapp” for the tutorial.

First, create the directory, which can be located anywhere. Just note the complete location for later on for systemd service. The tutorial will use the www directory.

sudo mkdir -p /var/www/
cd /var/www/

To create the “testapp,” use the following command.

sudo sails new testapp

A prompt will be asked to choose a template for your Sails application.

Example:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Type 2 and press the ENTER KEY to proceed and complete the “testapp” creation.

Example:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Next, navigate and launch the “testapp” to test and verify.

cd mytestapp
sudo sails lift

Example output:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

To exit, use the (CTRL+C) command.

Advertisement

Create a Systemd Service File for Sails.js

To have and manage a systemd service for Sails.js, you must create a systemd service file.

Create the service file using the following command.

sudo nano /lib/systemd/system/sails.service

Next, copy and paste the following.

[Unit]
After=network.target

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

[Install]
WantedBy=multi-user.target

Note, if you place the Sail.js directory location in a different location, make sure to change “WorkingDirectory=/var/www/testapp” path in the systemd service.

Save the file (CTRL+O), then exit (CTLR+X).

Next, reload the systemd daemon.

sudo systemctl daemon-reload

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

sudo systemctl enable sails --now

Example output if successful:

Created symlink /etc/systemd/system/multi-user.target.wants/sails.service → /usr/lib/systemd/system/sails.service.

Now verify the status of the Sails.js service with the following command.

systemctl status sails

Example output:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Install & Configure Reverse Proxy

Install Nginx

The tutorial will use Nginx as the reverse proxy for using Sails. Other reverse proxies can be used. However, Nginx is more straightforward and reliable in security and performance than most other options.

First, install the default Nginx version that is available on Rocky Linux 8 App stream.

sudo dnf install nginx

Example output:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Type “Y,” then press the “ENTER KEY” to proceed with the installation.

Next, verify the version build and if the installation was successful.

sudo nginx -v

Example output:

nginx version: nginx/1.14.1

Before you continue, you must start the Nginx service.

sudo systemctl enable nginx --now

Example output if successful:

Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

Next, verify the status to make sure there are no errors.

systemctl status nginx

Example output if all is working correctly:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Configure Nginx as Reverse Proxy

Next, create a server block (virtual host) for the Sails application. This can be done with your favorite text editor.

Example:

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

Now copy and paste the following with modifying the “example.com” domain to your own.

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

Save the file (CTRL+O), then exit (CTRL+X).

Next, test the configuration file works, and there are no errors.

sudo nginx -t

Example output if no errors:

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

Once Nginx returns an ok status during the dry test, restart the service.

sudo systemctl restart nginx
Advertisement

Configure Firewall Rules

By default, no firewall rules are set to the standard port 80 or 443 ports when installing Nginx. Before you continue, you should set the following rules, this will depend on what ports you will use, but all options are listed.

Open port 80 or HTTP:

sudo firewall-cmd --permanent --zone=public --add-service=http

Open port 443 or HTTPS:

sudo firewall-cmd --permanent --zone=public --add-service=https

Reload firewall to make changes into effect

sudo firewall-cmd --reload

Note, you can set up HTTPS later on in the tutorial if unsure.

Access Sails.js Web Interface

Now that you have successfully installed and configured sails.js and Nginx as the reverse proxy, you can open your Sails.js applications by visiting the domain you specified by opening your favorite Internet Browser and typing the following.

http://salis.example.com

If successful, you should see the Sails.js default landing page.

Example:

How to Install Sails.js Framework with Nginx on Rocky Linux 8

Congratulations, you have successfully installed Sails.js on Rocky Linux 8.

Advertisement

Secure Nginx with Let’s Encrypt SSL Free Certificate

Ideally, you would want to run your Nginx on HTTPS using an SSL certificate. The best way to do this is to use Let’s Encrypt, a free, automated, and open certificate authority run by the nonprofit Internet Security Research Group (ISRG).

First, install the EPEL repository and the mod_ssl package for better-updated packages and security.

sudo dnf install epel-release mod_ssl -y

Next, install the certbot package as follows:

sudo dnf install python3-certbot-nginx -y

Once installed, run the following command to start the creation of your certificate:

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email you@example.com -d www.example.com

This is the ideal setup that includes force HTTPS 301 redirects, Strict-Transport-Security header, and OCSP Stapling. Just make sure to adjust the e-mail and domain name to your requirements.

Now your URL will be HTTPS://sails.example.com instead of HTTP://sails.example.com.

Note, if you use the old HTTP URL, it will automatically redirect to HTTPS.

Optionally, you can set a cron job to renew the certificates automatically. Certbot offers a script that does this automatically, and you can first test to make sure everything is working by performing a dry run.

sudo certbot renew --dry-run

If everything is working, open your crontab window by using the following terminal command.

sudo crontab -e

Next, specify the time when it should auto-renew. This should be checked daily at a minimum, and if the certificate needs to be renewed, the script will not update the certificate. If you need help with finding a good time to set, use the crontab.guru free tool.

00 00 */1 * * /usr/sbin/certbot-auto renew

Save (CTRL+O) then exit (CTRL+X), and the cronjob will be automatically enabled.

Comments and Conclusion

In the tutorial, you have learned how to install Sails.js Framework and create an Nginx reverse proxy to access the application. Overall, there are a lot of frameworks out there, but Sails.js is one that many enjoy using and is worth the look.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x