【问题标题】:having trouble deploying express backend and react front-end on same server在同一台服务器上部署快速后端和响应前端时遇到问题
【发布时间】:2021-08-01 05:42:42
【问题描述】:

我一直在尝试将我的第一个完整 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.我将所有的快速代码复制到服务器的根目录中

```
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:无法先解析主机 sudo:ufd:找不到命令 ubuntu@first:/etc/nginx/sites-enabled$ sudo fwd sudo:无法先解析主机 sudo: fwd: 找不到命令 ubuntu@first:/etc/nginx/sites-enabled$ sudo ufw status 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)

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

【问题讨论】:

标签: ubuntu nginx deployment server devops


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    如果你不想使用 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 了解如何设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2020-05-29
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多