Private files in a DDEV Drupal environment

1.1k views Asked by At

Here is something mysterious: I am running a Drupal 9 installation on Windows WSL2 with Docker and DDEV, which works great. Now I have decided to set up a private files directory for images. I have done so, following all the instructions (e.g. private directory at the same level as the web directory). I did the same for a live server on cPanel. Then I created a simple test entity with only an image field, specifying that uploaded images were to go into the private directory.

The cPanel server version works exactly as expected -- when I created a new image entity, the image went right into the private directory. So I'm pretty sure I have the procedure down correctly.

But the DDEV version does mysterious things. When I create a new image entity, everything goes just fine, the entity is created, I can see the image, etc. But the private directory is empty. Furthermore, if I search the entire WSL2 file system, the image file is nowhere to be found. And yet it is there, somewhere. I have created several such entities, and now have a nice list of about ten image files, all of which display nicely. And all of which are nowhere to be found in the WSL2 file system (at least through a filename search), and certainly not in the specified private directory.

So where are they?? Any ideas about this mystery? :-)

Thanks!

P.S. There is only one clue I have noticed. The first time, I accidentally specified my private files directory to be below the web directory. Then it worked correctly, and I saw the uploaded image file. But as you know, the instructions are that it should NOT be inside the web directory, but rather outside of it, not HTTPS accessible. So maybe DDEV has some kind of unusual characteristic that makes it a problem to have the private directory outside of the web directory.

3

There are 3 answers

0
atomiclupine On BEST ANSWER

The question was answered by the comments of rfay to the original post.

0
Kari Kääriäinen On

To configure the private file system with Drupal 8.9 in ddev do this:

  1. Create the private folder in /var/www/html, for example /var/www/html/privatefiles (not above html, it will be removed at ddev restart)

  2. Make sure the folder is writable by the web server, like the web folder

     drwxr-xr-x  2 myuser   myuser  4096 january  15 16:36 privatefiles/
     drwxr-xr-x  8 myuser   myuser  4096 january  15 18:59 web/
    
  3. Set the $settings['file_private_path'] value in your settings.php

     $settings['file_private_path'] = '/var/www/html/privatefiles';
    
  4. Clear caches and Drupal will generate the appropriate .htaccess file in the folder you specified

  5. Confirm no File system or Private files directory errors found at admin/report/status. Settings can be viewed at admin/config/media/file-system.

0
leymannx On

You can simply add the following lines to your distributed settings.php to only take effect inside DDEV to create and set the private files dir:

if (isset($_ENV['DDEV_PROJECT'])) {

  $private_files_path = $app_root . '/' . $site_path . '/files/private';

  if (!is_dir($private_files_path)) {
    mkdir($private_files_path, 0755);
  }

  $settings['file_private_path'] = $private_files_path;
}