Guides and tutorials

Hundreds of tutorials and step by step guides carefully written by our support team.

Cómo instalar NodeJS en un Cloud con SWPanel

In this manual we will see how to install NodeJS in a Cloud with SWPanel. In addition, you will learn how to create a startup service so that the NodeJS server is always active as a service of the server and starts automatically in case of restarting the Cloud.

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

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

All the actions we will perform will be through the server console using a user with administrator privileges, so you will need to connect through a SSH client or through the SWPanel console.

info Before installing any package, we recommend that you make a Snapshot of the server, so that if there is any incompatibility between NodeJS and any of the sites you already have on the server, you can undo the actions and everything will work again: How to create and manage SnapShots of your Cloud Server.

NodeJS Server Installation and Configuration

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

apt update -y && apt upgrade -y

Next, we will need to install the nodejs package:

apt install nodejs -y

For the next point, you will need to know the port on 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('¡Hola mundo desde SWPanel!');
});

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

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

Since the Hosting services automatically deployed by SWPanel are configured to work in conjunction with Apache2 and Nginx, we will need to make a change to 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/swhosting/vhosts/domain.com.conf and comment out the following block by adding # at the beginning of each line or directly 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 for both port 80 (top of the configuration file) and 443 (bottom of the configuration file), so you will need to do this step, and the next one, twice.

Then 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 will need to do this step for the configuration block for port 443 as well.

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

Save the changes with “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 have reached this point, we can start the NodeJS server manually to check that it is working correctly:

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

Access the domain through the browser and see how the content loads.

info To stop the server use the combination “Ctrl + C”.

Service creation

In the case that the NodeJS server must be started permanently, it is interesting to create a startup service, in this way, the NodeJS server will be running in the background as another Cloud service.

There are some points to check before creating the startup service:

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

2 - The main server file must have execution permission for the owner user, to do this, run the following command:

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

3 - The main file must have as owner user and owner group the CHROOT user assigned to the Hosting service. To know the user and group owner 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 change the user and group owner 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 to your domain name and SW002NO432 to the user who owns your directory.

Once we have checked the previous points, we can create the startup service. To do this, you must create and edit a file in the /etc/systemd/system/ directory, this directory contains the system services:

nano /etc/systemd/system/nodejs_swmanual.service

info Choose the name of your choice to replace nodejs_swmanuales

danger The file should 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, to suit your work environment.

Once this is done, we can start the service and enable it to start 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 will be able to 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 the Immediate Help.