PHP cannot create directories in htdocs folder

Keywords: LAMP/MAMP/WAMP - AWS - Technical issue - Services (Apache, MariaDB, MySQL…)

bndiagnostic ID: a12a4d3c-fd98-6aac-0a78-abda86c330e4

bndiagnostic output:

? Apache: Found possible issues
? Resources: Found possible issues
https://docs.bitnami.com/general/apps/wordpress/troubleshooting/debug-errors-apache/

bndiagnostic failure reason: The suggested guides are not related with my issue

Description:
I’ve got a bitnami lamp stack on a lightsail instance and I need to be able to execute makedir from PHP but i can’t seem to get the permissions working on htdocs without doing 777. I’ve got the system user (bitnami) and the php user (daemon) in a group that owns htdocs, but i still get this error when i try to execute:

Got error 'PHP message: PHP Warning: mkdir(): Permission denied in /opt/bitnami/apache/htdocs/DP_PPEngine/dp_first_pp.php on line 19

Any help much appreciated!

Hi @noah1,

Please check the following guide that explains how to reset permissions:

https://docs.bitnami.com/aws/apps/wordpress/administration/understand-file-permissions/

Regards,
Michiel

Thanks michael, I appreciate your help.

I’m afraid the basic bitnami permission set doesn’t let php make directories within htdocs in the bitnami lightsail lamp stack. I went ahead and set the permissions using the steps in that guide you sent, and i still get “Got error 'PHP message: PHP Warning: mkdir(): Permission denied” when php tries to execute makedir() in htdocs.
Any thoughts or help would be much appreciated!

Hi @noah1,

Can you launch a fresh LAMP instance and run a PHP script that uses mkdir? Does that show the same error?

Regards,
Michiel

Hi @michiel ,

Yes, I’m afraid so: Got error 'PHP message: PHP Warning: mkdir(): Permission denied.

Hi @noah1,

Could you send a snippet example? I will try to reproduce it.

Regards,
Michiel

Sure thing, thank you! Here’s a simplified version for reproducing the issue:

<?php $directory = '/opt/bitnami/apache2/htdocs/testdirectory'; //create directory mkdir($directory); ?>

And here’s the whole block from our code:

<?php $directory = '/opt/bitnami/apache2/htdocs/'.$_POST['slug']; //check if directory exists, and if so return error if ( is_dir( $directory ) ) { echo "exists"; die(); } mkdir($directory); //place files in directory //index $source = '/opt/bitnami/apache/htdocs/defaults/index.php'; $destination = '/opt/bitnami/apache/htdocs/'.$_POST['slug'].'/index.php'; copy( $source, $destination ); //template $source = '/opt/bitnami/apache/htdocs/defaults/template.html'; $destination = '/opt/bitnami/apache/htdocs/'.$_POST['slug'].'/template.html'; copy( $source, $destination ); ?>

Sorry, here it is better formatted:

Simplified:

<?php
$directory = '/opt/bitnami/apache2/htdocs/testDirectory';
mkdir($directory);
?>

Full code:

<?php
    $directory = '/opt/bitnami/apache2/htdocs/'.$_POST['slug'];
    //check if directory exists, and if so return error
    if ( is_dir( $directory ) ) {
      echo "exists";
      die();
    }
    mkdir($directory);

    //place files in directory
    //index
    $source = '/opt/bitnami/apache/htdocs/defaults/index.php';
    $destination = '/opt/bitnami/apache/htdocs/'.$_POST['slug'].'/index.php';
    copy( $source, $destination );
    //template
    $source = '/opt/bitnami/apache/htdocs/defaults/template.html';
    $destination = '/opt/bitnami/apache/htdocs/'.$_POST['slug'].'/template.html';
    copy( $source, $destination );
?>

Hi @noah1,

Try this:

cd /opt/bitnami/apache/htdocs/
sudo mkdir test
sudo chmod 755 test
sudo chown daemon:daemon test

And then:

<?php
$directory = '/opt/bitnami/apache2/htdocs/test/testDirectory';
mkdir($directory);
?>

Best regards,
Michiel

@michiel Thank you! That worked fine, and so I did the same thing for htdocs:

sudo chmod 755 htdocs
sudo chown daemon:daemon htdocs

The only problem is that now I can’t delete files in htdocs via FTP using the bitnami user. Would you recommend a solution that would allow php to make directories, but allow me to edit them via FTP? Thank you!

  • Noah

Hi @noah1,

Could you try changing the owner to bitnami but the group to daemon?

sudo chown bitnami:daemon htdocs

Regards,
Michiel

@michiel Thanks! I’m afraid mkdir() gets “permission denied” again after switching ownership to bitnami:daemon. I switched it back to daemon:daemon and it’s working fine again, but no ftp access.

Any other thoughts? Thanks so much for your help!

Hi @noah1,

I suggest changing the permissions when you need to use SFTP. I also recommend asking in an SFTP forum to check if there is another way to do that.

Regards,
Michiel

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.