Node.js is a popular, easy-to-learn, and flexible platform for creating JavaScript applications. Node.js is a JavaScript runtime environment based on the V8 engine of the Chrome project. It has an efficient implementation and a progressive Node.js community, so it is one of the leading platforms for development.
Beget VPS with pre-installed Node.js is a ready-to-use build for developing and hosting your project. PM2 process manager and Nginx web server are already configured for easy administration and scalability with Node.js.
Installation package information
- Ubuntu 22.04.01
- Node.js 18.14
- nginx 1.18.0
- PM2 5.2.2
Node.js features
When creating a server, in addition to the standard parameters you will be asked to:
- Select a domain name under which your first Node.js application will be available. If you don't already have a domain name, you can register one or use a free technical domain in the .beget.app zone. An SSL certificate will be automatically issued and installed for the selected domain name.
Once the Node.js package is installed and deployed, you'll be able to get to work.
Working with Node.js and PM2
Immediately after installation, the first application has already been created to demonstrate functionality, which sends the text "Hello, World!" to the browser. The application is available under the domain name you specified during installation: https://your-domain
You can verify that the server works by using the browser or the console command:
$ curl https://mydomain.beget.app
Hello, World!To replace the test application with your application, you need to:
- Connect to your server via SSH or SFTP.
- Stop the test application with
sudo -u nodejs pm2 stop hello-worldcommand. - Go to the
/var/www/htmldirectory. - Load your application files.
- Install dependencies (see answers to the questions below for details on installing dependencies).
- Run your application with
sudo -u nodejs pm2 start /var/www/html/<name of your application input file>. - If your application uses a port other than 3000, change the port in the Nginx configuration file (see the answers to the questions below for details on changing the port).
- Delete the test application with
sudo -u nodejs pm2 delete hello-worldcommand. - Save PM2 services configuration for automatic application startup with
sudo -u nodejs pm2 savecommand.
FAQ
Node.js runs under the nodejs user.
To work with Node.js or npm, you must log in as the nodejs user with the command su - nodejs.
After switching to the nodejs user you can install packages via npm. Do not forget to go to the directory with the application.
- Connect to your server via SSH or SFTP.
- Go to the
/etc/nginx/sites-available directory. - Open
nodejs.confwith any text editor you like. - On the
proxy_pass http://localhost:3000;line replace port 3000 with the required port. - Save the changes and update the Nginx configuration with
nginx -s reloadcommand.
