linux安装node.js、nginx并部署前端react,vue项目

226 阅读1分钟

1、安装node.js

cd /usr/local/src
// 安装node
wget https://nodejs.org/dist/v14.17.1/node-v14.17.1-linux-x64.tar.gz   
// 解压文件
tar xzf node-v14.17.1-linux-x64.tar.gz
// 重命名
mv node-v14.17.1-linux-x64 nodejs
// 全局配置npm和node
ln -sf /usr/local/src/nodejs/bin/node /usr/local/bin/node
ln -sf /usr/local/src/nodejs/bin/npm /usr/local/bin/npm

2、安装Nginx

// 安装C语言环境
yum install gcc-c++
// 安装Nginx相关依赖
yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
// 创建nginx目录并进入
cd /usr/local
mkdir nginx
cd nginx
// 安装Nginx并解压
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
// 进入目录
cd /usr/local/nginx/nginx-1.13.7
// 安装证书
./configure --with-http_stub_status_module --with-http_ssl_module
make
make install
// 到此安装完成,启动/停止 Nginx
进入nginx的sbin目录: 
cd /usr/local/nginx/sbin
./nginx  启动
./nginx -s stop 停止
./nginx -s reload 重启

3、配置Nginx的反向代理

修改nginx的配置文件
进入nginx配置文件的命令:cd /usr/local/nginx/conf
查看文件命令:vim nginx.conf
编辑文件命令:i
修改为如下配置:
```
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8083;  #  配置的前端服务端口
        server_name  localhost;  # 配置的前端页面访问地址
        location / {
            root /home/front/dist;  # 项目在服务器存放路径
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;   # 解决刷新页面变成404问题的代码
        }
        location  ^~/api/ {	  # 配置nginx 的反向代理
            rewrite  ^/api/(.*)$  /$1 break;  # 重写地址
            proxy_pass   http://106.13.xx.xxx:8888;  # 配置的后端端口
        }       
        location = /50x.html {
            root   html;
        }
    }
}
```

参考:blog.csdn.net/weixin_4646…