manno's picture
I cannot create files in PHP using my LAMP VM. When I upload the site to my web host however everything works fine. I know that it's possible to enable this functionality in my Turnkey LAMP appliance, but I clearly don't know what terms to search for, because I haven't found anything. I know this is a frustrating newbie question, but any help would be greatly appreciated.
Jeremy Davis's picture

You need to make sure that your files are in /var/www and that the webserver account (www-data) can read them (Apache should have read access by default).

So upload your file (let's call it `test.php`) to `/var/www` (so the full file path is `/var/www/test.php`) on your LAMP appliance. Then in your web browser browse to `http://lamp_ip/test.php` and it should be there working!

If that is still not working for you, please tell me what you see (instead of your test.php, e.g. an error message, etc). Plus please post the output of the following commands:

# show the turnkey version

# show the status of the Apache server i.e. running or not?
service apache2 status

# show the last few entries from the Apache error log
tail /var/log/apache2/error.log

# show the directory listing (inc ownership and permissions) of the webroot
ls -l /var/www

# show which Apache sites are enabled
ls -l /etc/apache2/sites-enabled
manno's picture

Hi Jeremy, thank you for responding! PHP Scripts themselves seem to work fine, the issue is when I try to write a file nothing happens, no errors are output to the browser, it just doesn't create the file.

The information regading those commands are as follows:



service apache2 status:

* apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2)
   Active: active (running) since Thu 2016-05-19 18:46:59 UTC; 20min ago
  Process: 497 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)


ls -l /var/www:

test.php and test2.php


ls -l /etc/apache2/sites-enabled

lrwxrwxrwx 1 root root 35 Mar  3 05:17 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 31 Mar  3 05:17 adminer.conf -> ../sites-available/adminer.conf


Jeremy Davis's picture

I have tweaked the settings of your user account so hopefully your posts won't trigger the spam filter so easily next time. I also condensed your posts into a single one to make it easier to read... FWIW we have tons of issues with spammers. It's so hard to balance allowing legitimate users to post freely and stopping spammers. Despite the measures that made it so hard for you to post, you'd still be surprised at the amount of spam I need to delete from the forums!

Anyway, back to the issue at hand...

Thanks for the additional info. From that my guess is that it's a permissions issue. Although I can't be sure as you didn't post the output of the 2 commands that are probably most likely to show that up. So please try again to post these 2:

# so I can see ownership of the files and the directory:
ls -la /var/www 

# see if Apache has been recording any errors:
tail /var/log/apache2/error.log

So to clarify that I understand you correctly, your test.php files are meant to write new files within your doc root (i.e. /var/www)? If so please try the following:

chown -R www-data:www-data /var/www

That will give the webserver (Apache) and anything running under the webserver (e.g. PHP) full ownership to your document root. My suspicion is that it is currently owned by root so only read access is given to the webserver and any code it is running (PHP).

Note that giving write access to the webserver is a potential security concern. If there are bugs in your code (or in Apache or PHP) then an attacker could rewrite important parts of the code. Obviously in some cases that is a necessary evil. However to mitigate the potential damage one way is to make a sub directory (e.g. /var/www/uploads) that is writeable by the webserver and leave your main PHP code read only.

manno's picture

Awesome, thank you so much for the reply, and sorry for all the trouble. I'm turning in right now but I will give it a shot tomorrow, after setting up a sub directory as you suggested. Again thanks for all the help!
manno's picture

Your solution worked, you were correct it was a permissions issue. Keep in mind I have no clue what I'm doing in Linux, but I'll list the steps and my understanding of what each step does for other users:

  1. I created a sub directory in "/var/www/" lets call it "NewFolder" so its path is "/var/www/NewFolder"
  2. I changed the ownership of "NewFolder" to the www-data user in the www-data group (I think...) using the following command:
chown -R www-data:www-data /var/www/NewFolder

From what I understand this command is read:

  • chown = Change Onwer
  • -R = Recursively, or to all files and folders within the directory 
  • www-data = for the user "www-data"
  • :www-data = in the group "www-data"
  • /var/www/NewFolder = for the directory "/var/www/NewFolder"

I think that's how you read the command, regardless, once I did this I was creating files left and right all over my server. Thanks again Jeremy!

Jeremy Davis's picture

Awesome. Glad to hear it's all working for you now. And yes you are right on the money with your understanding of the chown command.

Add new comment