3

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.

  • The private files directory should be specified with a path like /var/www/html/privatefiles in your case, right? So `$settings['file_private_path'] = '/var/www/html/privatefiles';` and of course create the directory. – rfay Oct 15 '20 at 13:45
  • 1
    Thanks - I tried this on both the remote cPanel site and on the local DDEV site. It worked fine on the remote cPanel site, exactly as expected: that is, the private files went into exactly that private directory I specified. On the local DDEV site, that didn't happen. The private files went _somewhere_, because they are displayed and clearly conserved. But I can't figure out where they are. Even a search on the local file system doesn't show them, even though they clearly exist somewhere in my computer. Well, I'll keep experimenting and see what I discover. Thanks again for the interest. – atomiclupine Oct 19 '20 at 09:25
  • You can visit admin/config/media/file-system to see what Drupal thinks the private files directory is, see screenshot: https://www.evernote.com/l/AA9LDMWE6D5NJrPrDTrHYJAdKlXbDW0w2Pc – rfay Oct 19 '20 at 22:01
  • 1
    In my post, I said " ... maybe DDEV has some kind of unusual characteristic ...". Well, you have described that characteristic to me now, and completely solved the mystery. I had NOT understood the key characteristic of DDEV that I had to deal with the path **inside** the web container, and that the path had the structure you illustrated (/var/www/html/ ...); nor that I had to look at it with ssh. I re-specified my private directory in that way, and the images went straight into that directory, just as they were supposed to. Thanks again for the help! – atomiclupine Oct 21 '20 at 17:46

3 Answers3

1

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

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

0

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;
}
leymannx
  • 5,138
  • 5
  • 45
  • 48