


How to install nodejs under ubuntu and implement Nginx reverse proxy server
1. New version of nodejs installation
It is strongly recommended to use nvm (node version manager), other installation methods have more or less problems.
The specific steps are as follows:
1. Download nvm through git command
The execution command is as follows, we download nvm to /root/git/ Go (remember to install git first):
[root@vm-22-180-ubuntu~]#pwd /root [root@vm-22-180-ubuntu~]#mkdir git [root@vm-22-180-ubuntu~]#cd git [root@vm-22-180-ubuntu~]#git clone https://github.com/creationix/nvm.git
2. Configure environment variables
Here you need to modify the .bashrc file. If you don’t know its location, You can return to the root directory and execute
#find . -name "*.bashrc" -print
to search and obtain the results:
Then modify the ./etc/skel/.bashrc file through vim (but I Personally, I have made the same modifications to the three files above), add the following two lines at the beginning of the file:
export nvm_nodejs_org_mirror=https://npm.taobao.org/mirrors/node source ~/git/nvm/nvm.sh
The first line is to modify the nvm mirror path to Alibaba, and the second line is to add nvm to Go to the system environment.
After saving, execute the command to make the configuration take effect:
#source .bashrc
3. Directly use nvm to install nodejs
Execute the command
#nvm install node
You can install a new version of nodejs. After the installation is successful, the latest version of node will be installed on the server:
2. Install nginx
This is not recommended as is node. Use apt-get/aptitude to install. It is recommended to compile and install from source code.
1. Dependency installation and download
Make sure gcc-c and libpcre3-dev are installed:
#aptitude install gcc-c++ libpcre3-dev
Then we locate /home/ubuntu Download some dependency packages and unzip them to this folder:
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz #wget http://prdownloads.sourceforge.net/libpng/zlib-1.2.8.tar.gz #wget https://www.openssl.org/source/openssl-1.1.0c.tar.gz #tar -xzvf pcre-8.36.tar.gz #tar -xzvf zlib-1.2.8.tar.gz #tar -xzvf openssl-1.1.0c.tar.gz
Here we are downloading pcre/zlib/openssl that the nginx module depends on. The download addresses of the compressed packages are all in them Corresponding to what is found on the official website.
One thing to note is that pcre should not be downloaded using pcre2, otherwise it will cause an error in the nginx compilation process:
It is recommended to use the pcre-8.36 version provided in the code snippet above.
Note that if the download speed on the terminal is too slow, it is recommended to use Thunder to download locally, and then synchronize the file to the cloud host through filezilla. Try it every time~
2. Download and decompress nginx
First go to the nginx official website to find the latest source code package download path (as of the time of this article, the stable version is http://nginx.org/download/nginx-1.10.2 .tar.gz), and then download it (I personally download it to the /root path):
[root@vm-22-180-ubuntu~]#cd [root@vm-22-180-ubuntu~]#pwd /root [root@vm-22-180-ubuntu~]#wget http://nginx.org/download/nginx-1.10.2.tar.gz
Then unzip this compressed package and go to the folder /nginx-1.10.2:
[root@vm-22-180-ubuntu~]#tar -xzvf nginx-1.10.2.tar.gz [root@vm-22-180-ubuntu~]#cd nginx-1.10.2
3. Compile and install
Execute the following three instructions in order:
#./configure --prefix=/home/ubuntu/nginx --with-pcre=/home/ubuntu/pcre-8.36 --with-zlib=/home/ubuntu/zlib-1.2.8 --with-openssl=/home/ubuntu/openssl-1.1.0c #make #make install
Their functions are: generate c source code files and makefiles, generate binary files, and install nginx to the specified directory (/home/ubuntu/nginx).
Note that the parameters after the ./configure directive specify the installation directory of nginx and the addresses of related dependent modules.
3. Start nginx
Execute
#/home/ubuntu/nginx/sbin/nginx
You can directly start the nginx service. At this time, execute it anywhere:
wget http://127.0.0.1
You can download a copy of index.html:
At this time, when we directly access the cloud host’s public IP address, we can directly see the default page:
Attention! If the page cannot be accessed through the public IP at this time, please ensure that the security group where the cloud host is located allows access to all ports.
4. Execute a node service and implement reverse proxy through nginx configuration
Here we simply implement a node page and listen on port 3000.
Let’s write an index.js file locally for fun:
const http = require('http'); const server = http.createserver((req, res) => { res.statuscode = 200; res.setheader('content-type', 'text/plain'); res.end('hello world\n'); }); server.listen(3000, () => { console.log(`node server is now running/`); });
If you execute node index, access http://localhost:3000/ and there will be "hello world" "Output:
Then we put this file on the cloud host.
Since I have linked it to github, readers can download the file directly through the following instructions:
#git clone https://github.com/vajoy/node-test.git
p.s. I personally downloaded it to the "/root/node-project/" folder , the command to execute node is:
#node /root/node-project/node-test/cp1/index
ok, let’s modify the nginx configuration first. If you forget where the nginx configuration is, you can execute this command to confirm:
#/home/ubuntu/nginx/sbin/nginx -t
本文的nginx是安装在 /home/ubuntu 下的,所以其配置文件路径是“/home/ubuntu/nginx/conf/nginx.conf”,我们这样编辑它(加上红框部分的代码):
这意味着当有请求路径为“/hello”时,nginx 会将请求代理到服务器的3000端口去(即node监听的端口)。
五. 重启 nginx 和 node
到这一步的时候别忘了两点 —— 1. 咱们修改 nginx 配置后还没重启nginx服务;2. 咱们还没有在云主机上跑node服务(上文仅仅是在本地电脑上跑了一次)。
所以我们分别执行如下指令(重启nginx+跑node):
#/home/ubuntu/nginx/sbin/nginx -s reload #node /root/node-project/node-test/cp1/index
这时候直接访问 http://公网ip/hello,就能直接看到node跑起来的页面内容了:
The above is the detailed content of How to install nodejs under ubuntu and implement Nginx reverse proxy server. For more information, please follow other related articles on the PHP Chinese website!

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINXUnit supports multiple programming languages and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software