How to Use Node.js in HestiaCP
By default, HestiaCP does not include support for publishing applications developed under Node.js, but you can easily set up your server to work with Node.js applications. Follow these steps to publish an application using HestiaCP either under app.sock or by listening on port 3000.
Installing Node.js:
HestiaCP’s pre-installed image already comes with Node.js, but you’ll need to install npm to manage your Node.js applications. Execute the following command:
# apt install npm
Creating templates for Nginx using port 3000:
To publish Node.js applications, we’ll use Nginx as a proxy to redirect requests arriving at port 80 to port 3000 where Node.js is running. Perform the following steps to create the necessary files and set the correct 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
# chmod 755 /usr/local/hestia/data/templates/web/nginx/nodejs3000.tpl
# chmod 755 /usr/local/hestia/data/templates/web/nginx/nodejs3000.stpl
Add the following code to the nodejs3000.sh file:
#!/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 to the nodejs3000.tpl file:
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 to the nodejs3000.stpl file:
server {
listen %ip%:%proxy_port%;
server_name %domain_idn%;
return 301 https://%domain_idn%$request_uri;
}
server {
listen %ip%:%proxy_ssl_port% http2 ssl;
server_name %domain_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/s%proxy_system%.%domain%.conf*;
}
Creating templates for Nginx using the app.sock socket:
Alternatively, you can redirect requests arriving at port 80 to Node.js using a socket named app.sock. Follow these steps to create the necessary files and set the correct 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
# chmod 755 /usr/local/hestia/data/templates/web/nginx/nodejssock.tpl
# chmod 755 /usr/local/hestia/data/templates/web/nginx/nodejssock.stpl
Add the following code to the nodejssock.sh file:
#!/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 to the nodejssock.tpl file:
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_extentions%)$ {
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 to the nodejssock.stpl file:
server {
listen %ip%:%proxy_port%;
server_name %domain_idn%;
return 301 https://%domain_idn%$request_uri;
}
server {
listen %ip%:%proxy_ssl_port% http2 ssl;
server_name %domain_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_extentions%)$ {
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/s%proxy_system%.%domain%.conf*;
}
Using templates in the HestiaCP panel:
To connect to your HestiaCP panel, access https://host_name:8083 with the username “admin” and the password provided in the client panel.
Once inside the Hestia panel, go to “WEB” and click on the domain where you want to use the Node.js template.
- In the “NGINX Proxy Support” section, click on the dropdown that shows “default” (the default option) and select either “nodejs3000” or “nodejssock” depending on your project’s requirements.
Example: Starting a Node.js project in HestiaCP:
For this 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, then save the configuration. This will automatically set up the configuration for Nginx and create the directory “/home/admin/web/domain_name/nodeapp.”
-
To start the example project, execute the following commands:
# cd /tmp
# git clone https://github.com/contentful/the-example-app.nodejs.git
# cd the-example-app.nodejs/
# mv .* /home/admin/web/domain_name/nodeapp
# mv * /home/admin/web/domain_name/nodeapp
# cd /home/admin/web/domain_name/nodeapp
# cd ..
# chown -R admin.admin nodeapp/
# find nodeapp/ -type f -exec chmod 644 {} ";"
# find nodeapp/ -type d -exec chmod 755 {} ";"
# cd nodeapp
# npm install
# npm run start:dev
Now, the application is running on port 3000, and if you access it, you should see the application’s output.
Please note that the links provided in the example are placeholders and may not be functional. Be sure to replace them with your actual domain and paths.