Manually Deploying Node.js (CentOS 7.2)
Overview
The best practices for Huawei Cloud ECS guide you through the manual deployment of Node.js on a Linux ECS.
Node.js is a JavaScript runtime environment based on the Google Chrome V8 engine for building fast and scalable network applications. Based on the event-driven and non-blocking I/O model, Node.js is lightweight and efficient. It is ideal for running data-intensive real-time applications on distributed devices.
For more information about Node.js, see https://nodejs.org.
This section uses CentOS 7.2 64bit (40 GB) and Node.js installation packages node-v10.14.1-linux-x64.tar and node-v10.14.2-linux-x64.tar as an example to describe how to deploy Node.js.
Prerequisites
- An ECS has been created. For details, see Purchasing a Custom ECS.
- The target ECS has an EIP bound. For instructions about how to bind an EIP to an ECS, see Assigning an EIP.
- The image source has been updated to a Huawei Cloud image source. For details, see How Can I Use an Automated Tool to Configure a Huawei Cloud Image Source (x86_64 and Arm)? This facilitates obtaining and updating the system and software.
- A tool (for example, PuTTY) for accessing the Linux ECS has been installed on the local computer.
Procedure
- Install the Node.js software packages.
- Using the binary file
- Log in to the ECS.
- Run the following command to download the Node.js installation package:
wget https://nodejs.org/dist/v10.14.1/node-v10.14.1-linux-x64.tar.xz
- Run the following command to decompress the file:
- Run the following commands in any directory to set up a soft connection for node and NPM, respectively:
ln -s /root/node-v10.14.1-linux-x64/bin/node /usr/local/bin/node
ln -s /root/node-v10.14.1-linux-x64/bin/npm /usr/local/bin/npm
- Run the following commands to check the node and NPM versions:
npm -v
- Using the NVM version manager
- Log in to the ECS.
- Run the following command to install git:
- Run the following command to copy the source code to the local ~/.nvm directory using git and check the version:
git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
- Run the following command to activate NVM and add it to the profile file:
echo ". ~/.nvm/nvm.sh" >> /etc/profile
- Run the following command for the environment variables to take effect:
- Run the following command to list available Node.js versions:
- Run the following command to install multiple Node.js versions:
nvm install v10.14.2
- Run the following command to check the installed versions:
- Run the following command to switch the Node.js version to V10.14.2:
- Run the nvm alias default v10.14.2 command to set the default version to 10.14.2.
- Run the nvm help command to obtain more information about NVM.
- Using the binary file
- Verify the deployment.
- Run the following command to go to the home directory:
- Run the following command to create a test.js project file:
touch test.js
- Use VIM to edit the test.js file.
- Run the following command to open the test.js file:
vim test.js
- Press i to enter insert mode.
Modify the file as follows:
const http = require('http'); const hostname = '0.0.0.0'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
The port number can be customized. - Press Esc to exit insert mode. Then, enter :wq to save the settings and exit.
- Run the following command to open the test.js file:
- Run the following command to view enabled port:
If the port is unavailable, log in to the ECS console and change the security group rule. For details, see Adding a Security Group Rule.
- Add exception ports in the firewall configuration.
- For example, to add port 3000, run the following command:
firewall-cmd --zone=public --add-port=3000/tcp --permanent
If the following information is displayed, the firewall is disabled. Then, go to step 2.f.[root@ecs-centos7 ~]# firewall-cmd --zone=public --add-port=3000/tcp --permanent FirewallD is not running
If the following information is displayed, the firewall is enabled, and the exception port has been added:[root@ecs-centos7 ~]# firewall-cmd --zone=public --add-port=3000/tcp --permanent success
- Reload the policy configuration for the new configuration to take effect.
- Run the following command to view all enabled ports:
[root@ecs-centos7 ~]# firewall-cmd --list-ports 3000/tcp
- For example, to add port 3000, run the following command:
- Run the following command to run the project:
- Enter http://EIP:3000 in the address bar to access Node.js. If the following page is displayed, Node.js has been deployed.
Figure 1 Deployment and testing
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot