在同一台服务器上部署快速后端和反应前端时遇到问题

时间:2021-05-10 19:53:41

标签: ubuntu nginx deployment server devops

我一直在尝试将我的第一个完整的 MERN 堆栈部署到实际服务器,而不是 Heroku 和 Netlify,

我买了一台 ubuntu 操作系统的服务器,

  1. 我设置了服务器,

2.安装nodejs、pm2、Nginx

  1. 我使用 ssh 复制了 react build 文件夹中的所有文件:

    asset-manifest.json  favicon.ico  index.html  logo192.png  logo512.png  manifest.json  robots.txt  static```
    
    
  2. 我将 Nginx 设置为将该文件夹作为我在端口 80 中的 IP 的默认请求

    server {
            listen 80;
            listen [::]:80;
    
            root /var/www/qdx/html;
            index index.html index.htm index.nginx-debian.html;
    
            server_name your_domain www.your_domain;
    
            location / {
                    try_files $uri $uri/ =404;
            }
    }```
    
    

到目前为止一切顺利,我可以使用 URL 访问我的网站:http://185.97.117.14/

现在我想在同一台服务器上设置后端,以便它们可以相互连接:

5.我把我所有的express代码复制到服务器的根目录

```
ubuntu@first:~/server$ ls
config  node_modules  package.json  package-lock.json  README.md  src

```

然后我使用 pm2 start 设置 pm2

ubuntu@first:~/server$ pm2 show 0
 Describing process with id 0 - name qdx-server
???????????????????????????????????????????????????????????????????
? status            ? online                                      ?
? name              ? qdx-server                                  ?
? namespace         ? default                                     ?
? version           ? 1.0.0                                       ?
? restarts          ? 0                                           ?
? uptime            ? 7h                                          ?
? script path       ? /home/ubuntu/server/src/index.js            ?
? script args       ? N/A                                         ?
? error log path    ? /home/ubuntu/.pm2/logs/qdx-server-error.log ?
? out log path      ? /home/ubuntu/.pm2/logs/qdx-server-out.log   ?
? pid path          ? /home/ubuntu/.pm2/pids/qdx-server-0.pid     ?
? interpreter       ? node                                        ?
? interpreter args  ? N/A                                         ?
? script id         ? 0                                           ?
? exec cwd          ? /home/ubuntu/server/src                     ?
? exec mode         ? fork_mode                                   ?
? node.js version   ? 16.1.0                                      ?
? node env          ? N/A                                         ?
? watch & reload    ? ?                                           ?
? unstable restarts ? 0                                           ?
? created at        ? 2021-05-10T11:55:04.319Z                    ?
???????????????????????????????????????????????????????????????????
 Actions available
??????????????????????????
? km:heapdump            ?
? km:cpu:profiling:start ?
? km:cpu:profiling:stop  ?
? km:heap:sampling:start ?
? km:heap:sampling:stop  ?
??????????????????????????
 Trigger via: pm2 trigger qdx-server <action_name>

 Code metrics value
??????????????????????????????????????
? Heap Size              ? 29.31 MiB ?
? Heap Usage             ? 95.33 %   ?
? Used Heap Size         ? 27.94 MiB ?
? Active requests        ? 0         ?
? Active handles         ? 4         ?
? Event Loop Latency     ? 0.31 ms   ?
? Event Loop Latency p95 ? 1.07 ms   ?
??????????????????????????????????????
 Divergent env variables from local env
?????????????????????????????????????????
? XDG_SESSION_ID ? 18                   ?
? SSH_CLIENT     ? 5.123.134.1 44336 22 ?
? OLDPWD         ? /home/ubuntu/server  ?
? SSH_TTY        ? /dev/pts/0           ?
? PWD            ? /home/ubuntu/server/ ?
? SSH_CONNECTION ? 5.123.134.1 44336 18 ?
?????????????????????????????????????????

6.虽然 express 应用应该运行在 5000 端口 并且我使用了防火墙并允许访问 API,我似乎无法打开 Express 应用程序并连接它,

ubuntu@first:/etc/nginx/sites-enabled$ sudo ufd sudo:无法先解析主机 须藤:ufd:找不到命令 ubuntu@first:/etc/nginx/sites-enabled$ sudo fwd sudo:无法先解析主机 sudo: fwd: 命令未找到 ubuntu@first:/etc/nginx/sites-enabled$ sudo ufw 状态 sudo:无法先解析主机 状态:活跃

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
5000                       ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
5000 (v6)                  ALLOW       Anywhere (v6)

我一直在研究这个并尝试学习所有内容一周,只是为了学习部署, 任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

服务器应用程序是否正在运行?您看到端口打开了吗 (netstat -an |grep 5000)? 网络服务器和服务器应用程序在同一主机上,因此您应该能够通过 localhost:5000 访问

我确实喜欢他们链接到的 Taleodor 的回答。如果需要,让事情变得更加简单和便携。

答案 1 :(得分:0)

如果你不想使用 nginx,你可以这样做:

server {
    listen 80;
    listen [::]:80;

    root /var/www/qdx/html;
    index index.html index.htm;

    server_name your_domain www.your_domain;

    location / {
            proxy_pass http://localhost:5000;
    }
}

我推荐这个来源:webdock tutorial 了解如何设置。