简体   繁体   English

我如何在我的树莓派上公开托管我的 React JS 网站

[英]How can i host my react js website publicly on my raspberry pi

i made a react js website and everything is working fine but i can't figure out how to host it on my rasp pi4 and make it publicly visitable by other people.我创建了一个 React JS 网站,一切正常,但我不知道如何将它托管在我的 rasp pi4 上,并让其他人可以公开访问它。 I also bought a domain.我也买了一个域名。 So my question is: How can i make my reactjs site public and running on my rasp.所以我的问题是:如何让我的 reactjs 网站公开并在我的锉刀上运行。 Thanks!谢谢!

You have to configure a web server like Apache and build the React app with npm or yarn.您必须配置一个 web 服务器,例如 Apache,并使用 npm 或 yarn 构建 React 应用程序。 Then copy the build files to the /var/www/html/ directory.然后将构建文件复制到 /var/www/html/ 目录。 You will also have to look into port forwarding your router to your local web server and open it to the public.您还必须查看将路由器转发到本地 web 服务器的端口,并将其向公众开放。 Just be careful with security.只是要小心安全。

You need to configure a web server to host the site.您需要配置一个 web 服务器来托管站点。 Either Apache2 or Nginx (I have a personal preference to Nginx, but either works fine for this). Apache2 或 Nginx(我个人更喜欢 Nginx,但两者都适用)。 Under Debian/Raspberry Pi OS, /var/www/html should be served on port 80 on all of the pi's IP addresses.在 Debian/Raspberry Pi OS 下, /var/www/html应该在 pi 的所有 IP 地址上的端口 80 上提供服务。 Place the site files there and make sure you can access the site from the pi's IP address.将站点文件放在那里并确保您可以从 pi 的 IP 地址访问该站点。

For making the site available outside of your.network, you will either need to look at port forwarding, hosting a VPS with a public IP, or using a tunnel.为了使该站点在您的网络之外可用,您需要查看端口转发,托管具有公共 IP 的 VPS,或使用隧道。 Port forwarding is likely going to be the hardest option and may not always work, but doesn't require any external services outside of your DNS provider.端口转发可能是最困难的选择,可能并不总是有效,但不需要 DNS 提供商之外的任何外部服务。 Tunneling is probably the second easiest, and using a VPS is almost certainly the easiest.隧道可能是第二简单的,而使用 VPS 几乎肯定是最简单的。

For port forwarding, yu will need to verify that your ISP does not use CGNAT, otherwise this will not work.对于端口转发,yu 需要验证您的 ISP 不使用 CGNAT,否则这将不起作用。 Assuming they don't, you will need to access your routers configuration and set up port 80 on TCP to forward to your pi's IP address.假设他们不这样做,您将需要访问您的路由器配置并在 TCP 上设置端口 80 以转发到您的 pi 的 IP 地址。 I would assign your pi a static IP address, either on the pi itself, or using DHCP reservations.我会给你的 pi 分配一个 static IP 地址,可以在 pi 本身上,也可以使用 DHCP 保留。 Next, you need to see if your DNS provider offers Dynamic DNS. If not, you will need to manually update your DNS settings in the case your.networks public IP changes (unless you purchased a static IP from your provider).接下来,您需要查看您的 DNS 提供商是否提供动态 DNS。如果没有,您将需要手动更新您的 DNS 设置,以防您的网络公共 IP 发生变化(除非您从您的提供商处购买了 static IP)。 In this setup, you point your domain at your.networks public IP. Traffic goes directly between the client's browser and your pi.在此设置中,您将域指向 your.networks public IP。流量直接在客户端浏览器和您的 pi 之间进行。

Tunneling is a fair bit easier.隧道要容易一些。 I personally use Cloudflare for my DNS (I set my domain with my registrar to point to Cloudflare, then used their tunneling tool (Cloudflared) to tunnel traffic from their servers to my pi. There are other tunneling services, but I think Cloudflare's is the best out of all of the ones I used. In this setup, you point your domain at Cloudflare, which forwards the traffic to you via the tunnel. Traffic goes from the browser to Cloudflare to your pi.我个人将 Cloudflare 用于我的 DNS(我通过注册商将我的域设置为指向 Cloudflare,然后使用他们的隧道工具 (Cloudflared) 将流量从他们的服务器传输到我的 pi。还有其他隧道服务,但我认为 Cloudflare 是我用过的最好的。在这个设置中,你将你的域指向 Cloudflare,它通过隧道将流量转发给你。流量从浏览器到 Cloudflare 再到你的 pi。

Using a VPS is probably the easiest, and your knowledge of working with the pi applies to working with a VPS, assuming you run Debian linux or similar on your VPS.使用 VPS 可能是最简单的,并且您使用 pi 的知识适用于使用 VPS,假设您在 VPS 上运行 Debian linux 或类似命令。 You would install the web server on your VPS, put the app on the VPS, and point your domain at your VPS's public IP. In this setup, traffic goes from the client browser to your VPS.您将在 VPS 上安装 web 服务器,将应用程序放在 VPS 上,并将您的域指向 VPS 的公共 IP。在此设置中,流量从客户端浏览器转到您的 VPS。 This is the only non-free option (excluding the price of the domain itself), and keeps your local private.network safer by not putting public services on it.这是唯一的非免费选项(不包括域本身的价格),并且通过不在其上放置公共服务来使您的本地 private.network 更安全。 You can also run a tunnel between your pi and your VPS if you want (see https://www.jeffgeerling.com/blog/2022/ssh-and-http-raspberry-pi-behind-cg-nat for an example), but I don't personally see the point unless you really want the app to be served from your pi.如果需要,您还可以在 pi 和 VPS 之间运行隧道(有关示例,请参见https://www.jeffgeerling.com/blog/2022/ssh-and-http-raspberry-pi-behind-cg-nat ) ,但我个人不明白这一点,除非您真的希望从您的 pi 提供该应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM