python 静态资源托管
Under the topic of deployment with machine-learning, there are a lot of things to consider and a lot of different options that will provide you with a different result. Firstly, there are a lot of standard VPS and semi-shared hosts that you could go with for deploying your models. These are usually not great options for a beginner but allow for more freedom in what applications are used and how the file-system is arranged. Another option is, of course, a deployment solution like AWS or Google Cloud. While these are both great options, neither of them will give you the price-to-performance ratio and convenience of hosting a server yourself. Although there are of course drawbacks to hosting a server yourself, it is certainly a great option if you happen to have good and reliable internet and an extra computer lying around.
在使用机器学习进行部署的主题下,有很多事情需要考虑,许多不同的选项将为您提供不同的结果。 首先,可以使用许多标准VPS和半共享主机来部署模型。 对于初学者来说,这些通常不是很好的选择,但是可以为使用什么应用程序以及如何布置文件系统提供更大的自由度。 当然,另一个选择是部署解决方案,例如AWS或Google Cloud。 尽管这两个都是不错的选择,但它们都不会让您获得性价比,也无法自己托管服务器。 尽管自己托管服务器当然有弊端,但是如果您碰巧拥有良好且可靠的Internet和随处可见的额外计算机,那么这当然是一个不错的选择。
建立 (Setup)
When you’re running your own server, something very important to consider is what it is actually going to run on. You might not have a full-blown server sitting in your closet like many computing maniacs, but that is fine. For most applications, it’s unlikely that you’re going to have a need for 128GB of memory and a ridiculously high core count processor. For example, if you wish to only deploy a few endpoints, maybe serve some static files, you could go with essentially any computer with more than 1GB of memory.
当您运行自己的服务器时,需要考虑的非常重要的一点是它实际将要运行的内容。 您可能没有像许多计算狂一样坐在衣柜里的功能完善的服务器,但这很好。 对于大多数应用程序来说,您不太可能需要128GB的内存和可笑的高核数处理器。 例如,如果您只希望部署几个端点,或者提供一些静态文件,则基本上可以使用内存超过1GB的任何计算机。
That being said, another thing you’ll want to consider is heat. How hot does it get where you live, and do you have centralized air-conditioning? It’s important to keep your server components cool, and server processors generate a lot of heat and will require a lot more environmental cooling than your average computer. As a result, you could even have a server but not want to use it because of the heat in your environment.
话虽如此,您还要考虑的另一件事是热量。 您住的地方有多热,您是否装有中央空调? 保持服务器组件冷却很重要,并且服务器处理器会产生大量热量,并且比普通计算机需要更多的环境冷却。 结果,您甚至可能拥有一台服务器,但由于环境的热量而不想使用它。
Another thing that you might want to consider is power consumption. After all, if you’re not using all of your server’s power, but running a high TDP processor, it might not even be cost-effective to host it yourself — you might end up paying more for electricity than you bargained for.
您可能要考虑的另一件事是功耗。 毕竟,如果您没有使用服务器的全部功率,而是运行一个高TDP处理器,则自己托管它可能甚至不具有成本效益-您可能最终要付出比讨价还价更高的电费。
A great suggestion that I have when you don’t need all that much power is to use a laptop. Many people have an old I-7 or I-5 laptop with about 4GB of memory sitting around somewhere, and these are a great option for a simple server that can perform a lot of operations for incredibly cheap. Laptop processors have dramatically lower TDP compared to desktop processors, and as a result, they don’t generate nearly as much heat or use very much power.
当您不需要那么多电源时,我有一个很好的建议就是使用笔记本电脑。 许多人有一台旧的I-7或I-5笔记本电脑,周围大约有4GB的内存,这对于简单的服务器来说是一个不错的选择,该服务器可以执行很多操作,而价格却非常便宜。 与台式机处理器相比,笔记本电脑处理器的TDP大大降低,因此,它们不会产生那么多的热量或消耗太多的电能。
域,名称服务器和端口转发 (Domain, Nameserver, and Port Forwarding)
The first thing you’re going to need to do in order to serve any sort of files to the world wide web is set up port forwarding. Port forwarding allows your local connection to your internet service provider and network to be not only incoming from networks, but also outgoing into networks. Web-pages and websites are always configured to run on port 80. Sometimes port 80 acts as a socket port for a port like 8000, for example, and this is precisely how your model is likely going to use your ports.
为了将任何类型的文件提供给万维网,您要做的第一件事就是设置端口转发。 端口转发使您到Internet服务提供商和网络的本地连接不仅可以从网络传入,也可以传到网络。 网页和网站始终配置为在端口80上运行。例如,有时端口80充当8000等端口的套接字端口,这正是您的模型很可能会使用端口的方式。
A default gateway is the default device IP that all of the other devices are going to use in order to pass through to your ISP. In other words, the default gateway of your network is your router’s local IP address. On Linux, you can obtain this information using
默认网关是所有其他设备将使用的默认设备IP,以便传递给您的ISP。 换句话说,网络的默认网关是路由器的本地IP地址。 在Linux上,您可以使用以下方法获取此信息
route -n
On Windows, you’re going to first need to run command prompt as administrator. Next, you’ll simply type
在Windows上,您首先需要以管理员身份运行命令提示符。 接下来,您只需输入
ipconfig
into the command prompt, return, and then search for default gateway in the list of values provided. Grab the default gateway for your network and paste it into your web-browser. This should bring you to the login page for your router, which you will of course now need to login to.
进入命令提示符,返回,然后在提供的值列表中搜索默认网关。 获取网络的默认网关并将其粘贴到Web浏览器中。 这将带您进入路由器的登录页面,您现在当然需要登录该页面。
After logging in, your router’s setup will almost certainly vary, so it will be difficult to explain where port forwarding is going to be on every single router. Usually, it is either in its own tab or under the “ advanced” tab.
登录后,路由器的设置几乎肯定会有所不同,因此很难解释每个路由器上端口转发的位置。 通常,它在自己的标签中或“高级”标签下。
I’m not really sure why, but for some reason, it is likely your network is going to request an application name for forwarding your ports. This can be anything, it doesn’t really matter at all what the application is called. The only purpose this serves is for organization is my guess. You will want to forward ports 60–82 and ports 8000–8030. As to the question of TCP versus UDP, TCP is slower but will eat a lot less bandwidth, whereas UDP is fast but can cause unstable connections when used sparingly. To be clear, UDP is usually used for things that need a high-pinging connection like video-games and things of that nature. So for hosting a website, it’s probably a good idea to just use TCP. I usually just forward for both TCP and UDP, though, because the protocol doesn’t really take effect anywhere in this regard. The protocol is determined by the application, not the router, so all you would be doing by selecting TCP or UDP here is limiting your ability to use one or the other.
我不太确定为什么,但是由于某些原因,您的网络可能会请求用于转发端口的应用程序名称。 这可以是任何东西,应用程序的调用根本不重要。 我认为,这对组织服务的唯一目的是。 您将要转发端口60–82和端口8000–8030。 关于TCP与UDP的问题,TCP速度较慢,但占用的带宽要少得多,而UDP速度较快,但如果谨慎使用会导致不稳定的连接。 需要明确的是,UDP通常用于需要快速连接的事物,例如视频游戏和类似性质的事物。 因此,对于托管网站,仅使用TCP可能是一个好主意。 不过,我通常只转发TCP和UDP,因为该协议在这方面实际上并没有生效。 协议是由应用程序而不是路由器确定的,因此您在此处选择TCP或UDP所要做的所有事情就是限制您使用一种或另一种的能力。
Next, you will, of course, need a domain. This is a self-explanatory process —
接下来,您当然需要一个域。 这是一个不言自明的过程-
buy a domain.
购买域名。
Alternatively, you could always just use your server’s public IP address as your domain, as well. The next thing you will need to do is get your domain name server off of your router. This will let the domain registry know where to send incoming connections. You will need to then put this into the name-server on your chosen domain registration site.
另外,您也可以始终仅使用服务器的公共IP地址作为域。 接下来您需要做的是将域名服务器从路由器上移开。 这将使域注册表知道将传入连接发送到哪里。 然后,您需要将其放入所选域注册站点上的名称服务器中。
设置端点 (Setting up an endpoint)
Before getting into actually creating and deploying an endpoint, it’s important to test your server to see if it actually works. For this article, I’m going to be using Ubuntu as it is absolutely dominating the server market and beginner-friendly compared to RHEL or Arch servers. After connecting our new server to wifi, we’re going to want to either use dig or curl to get our public address. The problem with dig is that it often relies on name-servers that might identify you by your hardware address, not your IP address, so I usually do:
在开始实际创建和部署端点之前,重要的是测试您的服务器以查看其是否真正起作用。 对于本文,我将使用Ubuntu,因为与RHEL或Arch服务器相比,Ubuntu绝对统治了服务器市场,并且对初学者友好。 在将新服务器连接到wifi之后,我们将想要使用dig或curl来获取我们的公共地址。 dig的问题在于,它通常依赖于可能通过您的硬件地址而不是IP地址来识别您的名称服务器,因此我通常这样做:
curl ifconfig.co
Now that we have the IP of our server, copy it into another computer. This is a public IP address, so it won’t work through a local area connection. In other words, we are not making a request to a local server, so we wouldn’t get a local server — so it doesn’t matter if the computer is on the same network or not, this will not work if the ports are not forwarded properly. Now back on the server run
现在我们有了服务器的IP,将其复制到另一台计算机上。 这是一个公共IP地址,因此无法通过本地连接使用。 换句话说,我们不是向本地服务器发出请求,因此我们不会获得本地服务器,因此计算机是否在同一网络上都没有关系,如果端口位于转发不正确。 现在回到服务器上运行
python -im http.server
This will start up a development server that will serve the files stored on your machine statically. Now go ahead and navigate to that public IP address on your other computer, and if you end up in ~/ on your server, congratulations!
这将启动开发服务器,该服务器将静态处理存储在计算机上的文件。 现在,继续并导航至另一台计算机上的该公共IP地址,如果您最终在服务器上的〜/位置,恭喜!
In order to set up the virtual part of your server, your first going to need to have an idea of what options you have to do so. The two biggest and most used options are NGINX and Apache. I published an article a while ago which compared the two if you’d like to learn more about them individually to make your decision:
为了设置服务器的虚拟部分,首先需要了解必须使用哪些选项。 最大和最常用的两个选项是NGINX和Apache。 不久前,我发表了一篇文章,比较了两者(如果您想单独了解它们,以便做出决定):
You’re also going to need to use Supervisor and set one up to run your server. I’m not going to be including configurations here, as this article is more networking-based, but here are articles that will walk you through how to deploy both Gunicorn3 (Python production servers) and Genie (Julia production servers) with NGINX and Supervisor:
您还需要使用Supervisor并设置一个以运行服务器。 我不会在此处包括配置,因为本文是基于网络的,但是本文将带您逐步了解如何使用NGINX和Supervisor部署Gunicorn3(Python生产服务器)和Genie(Julia生产服务器) :
And if you wanted to serve static files, I have got you covered for that, as well:
如果您想提供静态文件,那么我也为您提供服务:
结论 (Conclusion)
The last step I would take in this project is installing Openssh on both machines with
我要在该项目中执行的最后一步是在两台计算机上同时安装Openssh
(my daily machine runs Fedora)
sudo dnf install openssh-client(The server runs Ubuntu)
sudo apt-get install openssh-server
This will allow you to connect to the server while it is headless and not connected to any peripherals. This means that you will be able to log into it without ever needing to actually use the computer itself, which is pretty handy! Another cool thing is that you can also login to your public IPV4 address from anywhere in the world using SSH, as well, as long as the SSH port, port 22, is forwarded as well.
这将允许您在无头且未连接任何外围设备的情况下连接到服务器。 这意味着您无需真正使用计算机本身就可以登录,这非常方便! 另一个很酷的事情是,只要还转发了SSH端口22端口,您还可以使用SSH从世界任何地方登录到公用IPV4地址。
I’d say that depending on your situation, and what exactly it is that you want to do with your server, running your own server off your router can be a fantastic and incredibly cost-effective approach. Running your own server allows you to manage your own hardware, rather than rent it. Using a low TDP processor can really save you a few bucks as well, as you could end up spending pennies in power each month to keep your websites and endpoints up. Need more storage? Buy a bigger hard-drive. Need more memory? Buy more memory. It is simple and cuts out an often unwanted middleman who is running your website from behind the curtain, giving you the freedom and the flexibility to take your website anywhere!
我想说的是,根据您的情况以及您要对服务器执行的确切操作,在路由器上运行自己的服务器可能是一种奇妙且极具成本效益的方法。 运行自己的服务器使您可以管理自己的硬件,而不必租用它。 使用低TDP处理器确实可以为您节省几美元,因为您可能每个月要花费几分钱来维持网站和终端的正常运行。 需要更多存储空间吗? 购买更大的硬盘。 需要更多内存吗? 购买更多内存。 它很简单,并且可以从幕后切出经常要运行您的网站的中间人,从而为您提供自由灵活的灵活性,可以将您的网站带到任何地方!
翻译自: https://towardsdatascience.com/how-to-host-your-own-python-models-dc820081c320
python 静态资源托管