How to Install Sails.js Framework with Nginx on AlmaLinux 9

Sails.js is a Javascript framework that you can 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 on AlmaLinux 9 and how to access the web-based interface by installing and configuring an Nginx reverse proxy setup.

Install Required Packages

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

sudo dnf install curl gcc-c++ make -y

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

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

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

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

More information on the current version can be found by visiting the NodeSource.

Next, install Node.js.

sudo dnf install nodejs -y

Lastly, ensure your NPM is up-to-date using the following command, this is critical do not skip.

sudo npm install npm@latest -g

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 the systemd service. The tutorial will use the www directory.

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

Create the “testapp” using 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 AlmaLinux 9

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

Example:

How to Install Sails.js Framework with Nginx on AlmaLinux 9

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

cd testapp && sudo sails lift

Example output:

How to Install Sails.js Framework with Nginx on AlmaLinux 9

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

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, change the “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

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 AlmaLinux 9

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 available on AlmaLinux 9.

sudo dnf install nginx -y

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

sudo nginx -v

Before you continue, you must start the Nginx service.

sudo systemctl enable nginx --now

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 AlmaLinux 9

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 that 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

Restart the service once Nginx returns an ok status during the dry test.

sudo systemctl restart nginx

Configure FirewallD Rules

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 that you can set up HTTPS later in the tutorial if you are 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 AlmaLinux 9

Congratulations, you have successfully installed Sails.js.

Secure Nginx with Let’s Encrypt SSL Free Certificate

Ideally, you would want to run your Apache web server 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).

AlmaLinux 9 is one of few distributions without Certbot available with DNF. However, you can install snap to manage this until the EPEL/EPEL Next 9 catches up.

First, install snapd.

sudo dnf install snapd -y

Once installed, enable it immediately and on system startup to monitor for updates.

sudo systemctl enable snapd --now

Next, install the snap core to cover all dependencies required for snap packages.

sudo snap install core

Create a symlink for the snapd folder.

sudo ln -s /var/lib/snapd/snap /snap

Install Certbot snap package.

sudo snap install --classic certbot

Lastly, create another symlink for certbot.

sudo ln -s /snap/bin/certbot /usr/bin/certbot

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

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

Alternatively, use the following command and follow the prompts if more accessible.

sudo certbot certonly --nginx

This ideal setup includes force HTTPS 301 redirects, a 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.

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

Next, use the systemctl-timers to confirm that there is a cron job currently working to check and renew your certificate.

systemctl list-timers

Example output:

How to Install Sails.js Framework with Nginx on AlmaLinux 9

There is a timer going to check and renew the certificate before it expires, so you do not need to worry again.

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.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!