Home > Article > Operation and Maintenance > 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!