How to install NodeJS on a Cloud with SWPanel

Aktualisiert am 2026-10-23 05:01:19 21 Besuche

In this manual we will see how to install NodeJS on a Cloud with SWPanel. Additionally, you will learn how to create a boot service so that the NodeJS server remains always active as one more service of the server and starts automatically in case of restarting the Cloud.

First, you will need a server with SWPanel as a management panel; for this, you can follow the manual: Handbuch: How to create a Cloud with SWPanel as a management panel.

Also, you will need to create a Hosting service within this server; in this way, your NodeJS server will respond to requests through the domain. You can follow the manual Handbuch: Create a Web Hosting in my Cloud.

All the actions we will carry out will be through the server console using a user with administrator privileges; therefore, you will need to connect via an SSH client Handbuch: Connect to your Cloud via console using PuTTY or through the SWPanel console Handbuch: How do I access my Cloud using the console via SWPanel.

info Before installing any package, we recommend that you take a Snapshot of the server; in this way, if there were any incompatibility between NodeJS and any of the websites you already have on the server, you could undo the actions and everything would work again: Handbuch: How to create and manage SnapShots of your Cloud Server.

Installation and configuration of the NodeJS server

First of all, we will update the server through the following command:

apt update -y && apt upgrade -y

Next, we must install the nodejs package:

apt install nodejs -y

For the following point, you will need to know the port through which your NodeJS server will listen; we will use the following code as an example. In our case, the file is located in the directory /var/www/nodejs.swmanuales.com/datos/web/server.js.

As you can see, the server listens on port 3000:

#!/usr/bin/env node

const { createServer } = require('node:http');

const hostname = '127.0.0.1';
const port = 3000;

const server = createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello world from SWPanel!');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

danger If you have multiple NodeJS servers on the same cloud, the port must be different for each of them.

Since the Hosting services automatically deployed by SWPanel are configured to work together with Apache2 and Nginx, we must make a change in the Nginx configuration file so that it interacts with port 3000 (in this case) of the NodeJS server.

We will edit the Nginx vhost file using nano /etc/nginx/swpanel/vhosts/dominio.com.conf and we will comment out the following block by adding # at the beginning of each line or directly by deleting this block:

#        location / {
#                proxy_pass http://127.0.0.1:8080;
#                proxy_set_header Host             $host;
#                proxy_set_header X-Real-IP        $remote_addr;
#                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
#                proxy_set_header X-Forwarded-Proto $scheme;
#                access_log off;
#                proxy_cache_key "$scheme$request_method$host$request_uri";
#                proxy_no_cache $no_cache $http_pragma $http_authorization $arg_nocache;
#                proxy_cache_bypass $no_cache $http_pragma $http_authorization $arg_nocache;
#                proxy_cache nodejs.swmanuales.com_proxy;
#                proxy_cache_valid "5";
#                proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 updating;
#        }

info If your service has an SSL certificate installed, this block will appear both for port 80 (top part of the configuration file) and for 443 (bottom part of the configuration file), so you must perform this step, and the next one, twice.

Next, add the following configuration just below the commented block:

location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Fowarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Fowarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;

       try_files $uri $uri/ =404;
}

info Again, if your Hosting service has an SSL certificate, you must also do this step for the configuration block for port 443.

info Remember to change the port to the same one that is configured in the NodeJS server file

Save the changes using "Ctrl + O" and exit the editor with "Ctrl + X".

Once the Nginx VirtualHost configuration has been modified, we will check that it is correct:

nginx -t

If it is correct, we will restart the Nginx service:

systemctl restart nginx

Once we reach this point, we can start the NodeJS server manually to check that it works correctly:

node /var/www/nodejs.swmanuales.com/datos/web/server.js

We access the domain through the browser and see how the content loads.

info To stop the server use the combination "Ctrl + C"

Creation of the service

In the event that the NodeJS server must be permanently started, it is interesting to create a boot service; in this way, the NodeJS server will be running in the background as one more service of the Cloud.

Some points must be reviewed before creating the boot service:

1 - The main server file must contain #!/usr/bin/env node on the first line; otherwise, the service will not start.

2 - The main server file must have execution permission for the owner user; for this, execute the following command:

chmod u+x /var/www/nodejs.swmanuales.com/datos/web/server.js

3 - The main file must have the CHROOT user assigned to the Hosting service as the owner user and owner group. To know the owner user and group of the service you can execute the following command:

stat -c '%U' /var/www/nodejs.swmanuales.com/datos/web

The response will be a user similar to the following:

SW002NO432

To modify the owner user and group of the file, use this command:

chown SW002NO432:SW002NO432 /var/www/nodejs.swmanuales.com/datos/web/server.js

info Remember to change nodejs.swmanuales.com for the name of your domain and SW002NO432 for the owner user of your directory.

Once the previous points have been reviewed, we can create the boot service. For this, you must create and edit a file in the /etc/systemd/system/ directory; in this directory the system services are located:

nano /etc/systemd/system/nodejs_swmanuales.service

info Choose the name of your preference to replace nodejs_swmanuales

danger The file must end in ".service"

In this new file, you must specify the following configuration:

[Unit]
Description=NodeJS SWManuales

[Service]
ExecStart=/var/www/nodejs.swmanuales.com/datos/web/server.js
Restart=always
User=SW002NO432
Group=SW002NO432
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/nodejs.swmanuales.com/datos/web/
    
[Install]
WantedBy=multi-user.target

info Make changes in the configuration: Description, ExecStart, User, Group and WorkingDirectory, so that it adapts to your work environment.

Once this is done, we can start the service and enable it so that it starts automatically:

systemctl daemon-reload
systemctl enable nodejs_swmanuales.service
systemctl start nodejs_swmanuales.service
systemctl status nodejs_swmanuales.service

If the command systemctl status nodejs_swmanuales.service indicates that the server is started, you can now access it from the browser and view your website.

Otherwise, you can use the command journalctl -u nodejs_swmanuales.service to check for errors.

If you have any questions, do not hesitate to contact us through Immediate Help Handbuch: Talk to support with Quick Help.