Guides 11792 Published by

The guide walks you through getting a fresh Sails.js app up and running on AlmaLinux 9, complete with Nginx as a reverse proxy and a systemd unit so the process stays alive in the background. It begins by installing Node 18 from Nodesource, then uses npx sails new to scaffold an application, runs npm install to pull in optional dependencies early, and explains why that pre‑step saves headaches later. After that, it shows how to configure Nginx with proxy_set_header directives for WebSocket support, restart the service, and cautions that missing headers can cause socket connections to drop. Finally, it teaches you how to create a systemd service that runs under your own user, enable it at boot, and offers quick troubleshooting tips such as checking journal logs or diagnosing 502 Bad Gateway errors when the Sails process isn’t listening on port 1337.



Install Sails.js Framework with Nginx on AlmaLinux 9: A No‑Nonsense Guide

You’ll learn how to get a fresh Sails.js app humming behind Nginx on AlmaLinux 9, complete with a systemd service so you don’t have to keep `npm start` in the foreground. Skip the vague “install Node” tutorial and jump straight into the steps that actually matter.

Prerequisites – What You Need Before Starting
  • AlmaLinux 9, fresh install, SSH access
  • Root or sudo privileges
  • A domain name pointing at your server’s IP (or just use 127.0.0.1 for testing)

If you’re missing any of those, your head will probably hurt before the first line of code.

Install Node.js 18.x on AlmaLinux 9

AlmaLinux ships an old Node in its repos; we need a recent LTS. The Nodesource setup script does the heavy lifting:

curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo dnf install -y nodejs

Why this matters: The script configures the right repo and installs Node 18, which Sails requires for ES‑module syntax. I once tried to run a Sails project with Node 14 on AlmaLinux 9 and got an “Unexpected token” error—just a reminder that version mismatches bite.

Check you’re good:

node -v   # should be v18.x
npm -v    # npm comes bundled
Set Up a Sails.js Project

Create the project in your home directory (or wherever you keep apps). The `sails new` command pulls from GitHub, so no worries about missing binaries.

npx sails new myapp
cd myapp
npm install

Why run `npm install`? Sails ships with a lot of optional dependencies that are pulled lazily. Installing them up front saves you later headaches when the server first boots.

Configure Nginx as a Reverse Proxy

Install Nginx if it’s not already on your box:

sudo dnf install -y nginx

Create a site config in `/etc/nginx/conf.d/myapp.conf`:

server {
    listen 80;
    server_name example.com;  # change to your domain

    location / {
        proxy_pass http://127.0.0.1:1337;   # Sails default port
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Restart Nginx:

sudo systemctl restart nginx

If you forget the `proxy_set_header` lines, websockets (e.g., Socket.io) will die on reconnects. I’ve seen this happen after a quick copy‑paste that omitted those headers.

Create a Systemd Service for Your App

Systemd keeps your app alive and restarts it if something crashes. Drop this file at `/etc/systemd/system/sails-myapp.service`:

[Unit]
Description=Sails.js Application – myapp
After=network.target

[Service]
Type=simple
User=youruser          # replace with the account that owns /home/youruser/myapp
WorkingDirectory=/home/youruser/myapp
ExecStart=npm start
Restart=on-failure
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Enable and start:

sudo systemctl daemon-reload
sudo systemctl enable --now sails-myapp.service

Why the `User=` line? Running a Node process as root is a bad idea. When I accidentally left it on root, my app had read/write permissions to the entire filesystem—a nightmare to debug.

Test the Deployment

Open your browser and navigate to your domain (or 127.0.0.1). You should see Sails’ default welcome page or whatever route you defined.

Check logs:

journalctl -u sails-myapp.service -f   # live tail

If you hit a “502 Bad Gateway” from Nginx, the service is probably not running or listening on 1337. Look for `node: command not found` errors if you see a crash—maybe your PATH in systemd is wrong.

You’ve got it! Your Sails app runs behind a hardened AlmaLinux 9 server with Nginx as a reverse proxy, and the systemd unit will keep it alive after reboots or crashes.