By default HestiaCP does not include support to publish applications developed with NodeJS, but with a few simple steps your server will be ready. Below, we show you in detail the steps to publish with HestiaCP an application under app.sock or listening on port 3000.
Installing NodeJS
By default, the preinstalled image of HestiaCP does not have NodeJS installed, so you will need to install "npm" (which also installs NodeJS as a dependency).
To do this, run the following command:
# apt update && apt install npm -y
Next install "pm2" to manage the startup of your applications:
# npm install pm2 -g
Creating Nginx templates using port 3000
In this section we will explain how to publish NodeJS applications using Nginx as a proxy to redirect requests that arrive on port 80 to port 3000 of NodeJS.
First, create the following files and set the appropriate permissions:
# touch /usr/local/hestia/data/templates/web/nginx/nodejs3000.sh # touch /usr/local/hestia/data/templates/web/nginx/nodejs3000.tpl # touch /usr/local/hestia/data/templates/web/nginx/nodejs3000.stpl # chmod 755 /usr/local/hestia/data/templates/web/nginx/nodejs3000.sh
Add the following code in the file nodejs3000.sh:
#!/bin/bash
user=$1
domain=$2
ip=$3
home=$4
docroot=$5
mkdir "$home/$user/web/$domain/nodeapp"
chown -R $user:$user "$home/$user/web/$domain/nodeapp"
rm "$home/$user/web/$domain/nodeapp/app.sock"
runuser -l $user -c "pm2 start $home/$user/web/$domain/nodeapp/app.js"
sleep 5
chmod 777 "$home/$user/web/$domain/nodeapp/app.sock"
Add the following code in the file nodejs3000.tpl:
server {
listen %ip%:%proxy_port%;
server_name %domain_idn% %alias_idn%;
error_log /var/log/%web_system%/domains/%domain%.error.log error;
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_cache_bypass $http_upgrade;
}
location /error/ {
alias %home%/%user%/web/%domain%/document_errors/;
}
location @fallback {
proxy_pass http://127.0.0.1:3000:/$1;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
include %home%/%user%/conf/web/nginx.%domain%.conf*;
}Add the following code in the file nodejs3000.stpl:
server {
listen %ip%:%proxy_port%;
server_name %domain_idn% %alias_idn%;
return 301 https://%domain_idn%$request_uri;
}
server {
listen %ip%:%proxy_ssl_port% http2 ssl;
server_name %domain_idn% %alias_idn%;
ssl_certificate %ssl_pem%;
ssl_certificate_key %ssl_key%;
error_log /var/log/%web_system%/domains/%domain%.error.log error;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types image/svg+xml svg svgz text/plain application/x-javascript text/xml text/css;
gzip_vary on;
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_cache_bypass $http_upgrade;
}
location /error/ {
alias %home%/%user%/web/%domain%/document_errors/;
}
location @fallback {
proxy_pass https://127.0.0.1:3000:/$1;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}Creating Nginx templates using the app.sock socket
In this section we will explain how to configure a template in case your NodeJS application needs to listen on a unix socket. In this case you will redirect the requests arriving at port 80 to NodeJS using the socket created in app.sock.
To do this, create the following files and set the appropriate permissions:
# touch /usr/local/hestia/data/templates/web/nginx/nodejssock.sh # touch /usr/local/hestia/data/templates/web/nginx/nodejssock.tpl # touch /usr/local/hestia/data/templates/web/nginx/nodejssock.stpl # chmod 755 /usr/local/hestia/data/templates/web/nginx/nodejssock.sh
Add the following code in the file nodejssock.sh:
#!/bin/bash
user=$1
domain=$2
ip=$3
home=$4
docroot=$5
mkdir "$home/$user/web/$domain/nodeapp"
chown -R $user:$user "$home/$user/web/$domain/nodeapp"
rm "$home/$user/web/$domain/nodeapp/app.sock"
runuser -l $user -c "pm2 start $home/$user/web/$domain/nodeapp/app.js"
sleep 5
chmod 777 "$home/$user/web/$domain/nodeapp/app.sock"
Add the following code in the file nodejssock.tpl:
server {
listen %ip%:%proxy_port%;
server_name %domain_idn% %alias_idn%;
error_log /var/log/%web_system%/domains/%domain%.error.log error;
location / {
proxy_pass http://unix:%home%/%user%/web/%domain%/nodeapp/app.sock:$request_uri;
location ~* ^.+\.(%proxy_extensions%)$ {
root %docroot%;
access_log /var/log/%web_system%/domains/%domain%.log combined;
access_log /var/log/%web_system%/domains/%domain%.bytes bytes;
expires max;
try_files $uri @fallback;
}
location /error/ {
alias %home%/%user%/web/%domain%/document_errors/;
}
location @fallback {
proxy_pass http://unix:%home%/%user%/web/%domain%/nodeapp/app.sock:/$1;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
include %home%/%user%/conf/web/nginx.%domain%.conf*;
}
Add the following code in the file nodejssock.stpl:
server {
listen %ip%:%proxy_port%;
server_name %domain_idn% %alias_idn%;
return 301 https://%domain_idn%$request_uri;
}
server {
listen %ip%:%proxy_ssl_port% http2 ssl;
server_name %domain_idn% %alias_idn%;
ssl_certificate %ssl_pem%;
ssl_certificate_key %ssl_key%;
error_log /var/log/%web_system%/domains/%domain%.error.log error;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types image/svg+xml svg svgz text/plain application/x-javascript text/xml text/css;
gzip_vary on;
location / {proxy_pass http://unix:%home%/%user%/web/%domain%/nodeapp/app.sock:$request_uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
location ~* ^.+\.(%proxy_extensions%)$ {root %sdocroot%;
access_log /var/log/%web_system%/domains/%domain%.log combined;
access_log /var/log/%web_system%/domains/%domain%.bytes bytes;
expires max;
try_files $uri @fallback;
add_header Pragma public;
add_header Cache-Control "public";
}
location /error/ {
alias %home%/%user%/web/%domain%/document_errors/;
}
location @fallback {
proxy_pass http://unix:%home%/%user%/web/%domain%/nodeapp/app.sock:/$1;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}
Using templates created in the HestiaCP panel
Connect to the Hestia panel by accessing the url https://nombre_host:8083 with the user "admin" and the password you will see in the client panel.
Inside the Hestia panel go to "WEB" and click on the domain where you want to use the NodeJS template.
In the "NGINX Proxy Support" section, click on the dropdown which by default shows "default" and select "nodejs3000" or "nodejssock" depending on your project’s requirements.
Important
The example shown below is outdated and does not currently work properly. We are working on providing a more up-to-date example.
Example: start a NodeJS project in HestiaCP
For the example we will use a sample project from "contentful.com" which can be found on GitHub.
In the HestiaCP panel, edit your web domain and select the nodejs3000 template and save the configuration. It will automatically set the configuration for Nginx and create the directory "/home/admin/web/domain_name/nodeapp"
To start the sample project run the following commands:
git clone https://github.com/contentful/the-example-app.nodejs.git /tmp/the-example-app.nodejs
mv -f /tmp/the-example-app.nodejs/* /home/admin/web/domain_name/nodeapp
chown -R admin.admin /home/admin/web/domain_name/nodeapp
find /home/admin/web/domain_name/nodeapp -type f -exec chmod 644 {} ";"
find /home/admin/web/domain_name/nodeapp -type d -exec chmod 755 {} ";"
cd /home/admin/web/domain_name/nodeapp
npm install
npm run start:dev
Now we already have the application running on port 3000 and if we access the domain we have configured we will see the following result:
If you have any questions about the configuration of your cloud server or about any other matter, write to us at soporte@clouding.io. Our Technical Support team will be happy to help you with whatever you need.