Separate file server for php website

I have two servers for the website. One server will have php code and a database, and the other server at a lower speed for storing files. I need to implement them in such a way that a file downloaded through a website needs to be stored on another server, and then it can be downloaded from there.

Can anyone suggest me a better way to achieve this. I know that files can be transferred to another server using php ftp functions right after uploading via website, but it doesn't seem to be correct.

Or two servers can be used for static multimedia content such as images.

thanks

+6
source share
7 answers

The best idea is to simply have ALL files, including website files on the "storage server". Basically, what you do is mount the "shared folder", which means that you need website files and other files. (In most cases, you simply have a / var / www-local / folder on the storage server that you mount in / var / www / on the web server).

Make sure you mount it using NFS by adding it to the / etc / fstab file on the web server. ( More on NFS )

The advantage of this idea is that when you want to expand it, it can be easily done by installing a software loadbalancer (for example, HAProxy), adding as many web servers as you want, and you synchronize your data.

+13
source

I used something called Gluster that allows such things. The situation in which I use it is more for load balancing than for alternative distribution of content.

In addition, sites such as Stack Overflow use the Content Distribution Network services for certain pieces of information on their site. Such a solution may be more economical than buying / configuring / maintaining an entire new server.

+5
source

Perhaps you can connect the download directory to your web server. Check Linux NFS

+4
source

I don’t know if this is the best way, but why not just have a separate subdomain file server? Thus, it can handle all file upload thumbnails, and you can connect to it via FTP or SFTP from the main server.

Basically, here is a process you can use:

  • Configure the subdomain on the secondary server. I found information about this here .
  • Download the form on your main server, which processes and validates the file.
  • When the file is considered acceptable, send it to another server via FTP or SFTP. If you cannot use PHP tools for this, phpseclib can help. You can make this step multithreaded.
+3
source

Using url wrapper , you can use your default move_uploaded_file <) if your ftp server accepts this type of connection. Alternatively, you can use PHP ftp functions , especially ftp_put () , to upload the file to the server.

To deliver content, you need to have a database or other means to get the source URL on the content distribution server and put the URL in the html arguments:

<img src="http://cdn1.example.com/images/68263483.png" /> <a href="http://cdn2.example.com/files/9872345.pdf">Download PDF</a> 

Sample code for processing uploaded files will be

 <?php // ... $uploads_dir = 'images'; foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "ftp://user: pass@cdn1.example.com /$uploads_dir/$name"); // save url in your database for later retrieval ... } } 

or ftp_put () :

 <?php // ... $ftpCon = ftp_connect('cdn1.example.com') or die('Could not connect to ftp server'); $uploads_dir = 'images'; foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; ftp_put($ftpCon, "$uploads_dir/$name", $tmp_name) or die("could not upload $name to ftp server"); // save url in your database for later retrieval ... } } 
+1
source

if your application requires intensive read / write to a file server, then I think it is a bad idea to separate it. perhaps you can mirror NFS on your main web server to reduce latency!

Use nginx to handle the entire request for static files (for example: css, images, javascript)

0
source
  • Create a share on the file server in the directory into which you want to upload and download files. Two stocks if you plan to keep tyem in separate folders.

  • Map the folders on the web server to the PHP code base. Make sure that you use the login to connect to shared resources that will have read and write access.

  • After you have mapped the share, create a virtual directory on the website in the shared folder (it all depends on your web server).

  • Check how PHP handles access to files on a shared drive on a website, depending on your web server, it may differ from how you normally handle files.

I like this approach because the server OS handles file transfers to and from the file server. PHP just says which files it needs.

0
source

Source: https://habr.com/ru/post/895416/


All Articles