php 网站的独立文件服务器
我有两个网站服务器。一台服务器将有 php 代码和数据库,另一台速度较低的服务器仅用于存储文件。我需要以这样的方式实现这些:通过网站上传的文件必须存储在另一台服务器上,然后也可以从那里下载。
谁能建议我实现这一目标的最佳方法。我知道文件可以在通过网站上传后通过 php 的 FTP 功能传输到另一台服务器,但这似乎不是正确的方法。
或者两个服务器可仅用于静态媒体内容(例如图像)。
谢谢
I have two servers for a website. One server will have php code and database and another less speed server to store files only. I need to implement these in a way that file uploaded through the website must store at another server and also then can be downloaded from there.
Can anyone suggest me best way to achieve that. I know the files can be transferred to another server by FTP functions of php just after uploading through website but doesn't seems a correct way.
Or two server can be used for static media content like images only.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最好的想法是将所有文件,包括“存储服务器”上的网站文件。基本上,您要做的就是安装“共享文件夹”,这意味着您需要的网站文件和其他文件。 (大多数时候,存储服务器上只有一个 /var/www-local/ 文件夹,您可以将其挂载到网络服务器上的 /var/www/ 中)。
确保使用 NFS 挂载它,方法是将其添加到 Web 服务器上的 /etc/fstab 文件中。
(有关 NFS 的更多信息)
这种想法的优点是,当您想要要进行扩展,可以通过放置软件负载平衡器(例如 HAProxy)、添加任意数量的网络服务器来轻松实现这一点,并且您将同步数据。
The best idea is to just have ALL the files, including the websites files on the "storage server". Basically what you do is mount the "shared folder", this means the websites files and other files you will be needing. (Most of the times you just have a /var/www-local/ folder on the storage server, which you mount in /var/www/ on the webserver).
Make sure you mount it using NFS by adding it in the /etc/fstab file on the webserver.
(More info on NFS)
The advantage of this idea is that when you want to expand, this is easily possible by putting a software loadbalancer (like HAProxy), adding as much webservers as you like and you will have your data in sync.
我使用了名为 Gluster 的东西,它允许这样的事情。我使用它的情况更多的是用于负载平衡而不是替代内容分发。
此外,像 Stack Overflow 这样的网站还使用内容分发网络服务来获取其网站上的某些信息。像这样的解决方案实际上可能比购买/设置/维护全新服务器更具成本效益。
I've used something called Gluster which allows for things like this. The situation I use it for is more for load balancing than for alternate content distribution.
Also, sites like Stack Overflow use Content Distribution Network services for certain pieces of information on their site. A solution like this might actually be more cost effective than buying/setting up/maintaining a whole new server.
也许您可以将上传目录挂载到您的网络服务器。
检查 Linux NFS
May be you can mount the upload directory to your web server.
Check Linux NFS
不知道这是否是最好的方法,但为什么不简单地让文件服务器有一个单独的子域呢?这样它就可以处理所有文件下载细节,并且您可以从主服务器通过 FTP 或 SFTP 连接到它。
基本上,您可以使用以下过程:
Don't know if this is the best way, but why not simply have the file server a separate subdomain? That way it can handle all of the file download minutia and you can connect to it via FTP or SFTP from the main server.
Basically, here is a process you could use:
通过使用 url 包装,您可以使用默认的 move_uploaded_file(),如果您的 ftp 服务器接受此连接类型。或者,您可以使用 PHP 的 ftp 函数,尤其是 ftp_put(),将文件上传到服务器。
对于内容交付,您需要有一个数据库或其他方式来获取内容分发服务器上的原始 url,并将该 url 放入 html 参数中:
处理上传文件的示例代码将是 或
ftp_put():
By using an url wrapper, you can use your default move_uploaded_file(), if your ftp server accepts this connection type. Alternatively, you can use PHP's ftp functions, especially ftp_put(), to upload the file to the server.
For content delivery, you need to have a database or other means to get the original url on the content distribution server and put the url in the html arguments:
An example code to handle an uploaded files would be
or with ftp_put():
如果您的应用程序需要对文件服务器进行密集读/写,那么我认为将其分开是一个坏主意。也许您可以将 NFS 镜像到您的主 Web 服务器以减少延迟!
使用 nginx 处理所有对静态文件的请求(例如:css、图像、javascript)
if your application requires intensive read/write to the file server, then I would think it's a bad idea to separate it. maybe you can mirror the NFS to your main web server to reduce latency!
Use nginx to handle all request to static files (eg: css, images, javascript)
在文件服务器上创建一个共享到您要下载和上传文件的目录。如果您打算将文件保存在单独的文件夹中,则需要两个共享。
使用 PHP 代码库映射 Web 服务器上的文件夹。确保您使用登录名连接到具有读写访问权限的共享。
映射共享后,在网站上创建一个到共享文件夹的虚拟目录(这一切都取决于您的网络服务器)。
检查 PHP 如何处理访问网站中共享驱动器上的文件,根据您的网络服务器,它可能与您通常处理文件的方式不同。
我喜欢这种方法,因为服务器操作系统负责将文件传输到文件服务器或从文件服务器传输文件。 PHP 只是告诉它它需要哪些文件。
Create a share on the file server to tbe directory where you want to download and upload files. Two shares if you plan to keep tyem in separate folders.
Map the folders on the web server with the PHP code Base. Make sure you use a login to conect to the share(s) that will have read and write access.
Once you have mapped the share(s), create a virtual directory on the website to the shared folder (this all depends on your web server).
Check how PHP handles accessing files on a shared drive in a website, depending on your webserver it may be different than how you normally handle files.
I like this approach because the server OS handles transfering the files to and from the file server. PHP just handles telling it which files it needs.