How Do I Give My Apache Server Access to Write to Files? - php

I am running an apache webserver on a virtual machine on my pc. The vm is running zorin linux. I have written a php script that writes some data to another text file in the same directory. Both files are located in /var/www/html/. I have already verified that the webserver user is www-data, and I have attempted to make that user owner of the /var/www/html directory, as well as everything within that directory. Here is the command that I have used to do this:
sudoo chown -R www-data /var/www/html/*
I have also verified that the www-data user is the owner of this directory and it's contents using
ls -dl /var/www/html/
ls -dl /var/www/html/*
both return that the owner is www-data.
Even after doing all of this, the php file is still unable to write to the file correctly. At this point I am stumped. Any help would be greatly appreciated.

Related

give access to file outside www/html folder

I have LAMP server
Server version: Apache/2.4.29 (Ubuntu)
PHP: PHP 7.2.5-0ubuntu0.18.04.1
I need to give an php SQLITE3 access to db outside www/html folder.
Right now my filesystem looks this way.
/root
./database
user.db
/var
./www
./html
index.html
reg.php
user.db must be located in /root/database, so just putting it inside var/www/html isn't solution for me.
So I need to give access to this folder for Apache or php.
I found some information here https://httpd.apache.org/docs/2.4/urlmapping.html, but didn't get how this works and where I need to put this?
The problem here is Linux permissions, not URL mapping, as PHP is running in the server, in the backend.
If you run PHP as an Apache module, (mod_php or something like that), it will run with the Apache user and group (usually www-data:www-data or nobody:nogroup, it depends of the LAMP configuration).
So, you should give permissions and change ownership to the user.db file and its tree, something like:
chmod o+x /root
chmod o+x /root/database
chown www-data:www-data /root/database/user.db
You can read more about permissions here.

Permission with the linux server using apache and php

I have setup an linux CentOS 7 server to run php and i using apache, but when all done php can't write file even i have set chmod 777 and i have chown apache at the owner of the directory.
If the file exist, i can write the data on it, but if file don't exist
i can create it, it notice me don't have permission (folder is chmod
777).
Thanks for your help.

linux (ubuntu) apache www folder access issue

I'm a newbie in Linux, just installed ubuntu 14.04 and wanted to install WAMP (MAMP), I followed a youtube tutorial to install php5, apache2, mysql and phpmyadmin. Installed successfully both http://localhost and http://localhost/phpmyadmin works fine logged in successfully.
now I wanted to add some files and folders to apache www folder which is located at /var/www but cant create files or folders. I'm a Windows user where I usually keep all my PHP work in www folder.
I would like to know if there is a way to use any-other folder as www folder or how to create folders and files in that www folder. I reckon its permission issues, since being a newbie don't know how to fix that.. please help
Regards
Ubuntu 14.04 runs apache as user www-data, you can change to this user using the command in terminal (shell)
sudo su
This will make you root and have access to /var/www directory, and you will be able to create files and directories. Say you had a directory of images containing img1.jpg, img2.jpg, etc. in say /home/user1/images
as root you could
sudo su <- change to the document folder
cd /var/www <- change to the document folder
cp /home/user1/images/ . <- copy what you need to copy
chown -r www-data images/ <- give the web server read/write permission to the folder / files
which will let you do access the images via a browser via the url
http://localhost/images/img1.jpg
The reason is that apache runs under different user (given your distro most likely www-data), while /var/www belongs either to the user or root.
So if you want to allow apache to write somewhere you have to give it permissions.
Since it is not good practice to give apache full permission to your disk, usually you would have a special directory where it can upload data.
For example creating directory
/var/www/myProject/uploaded
and then giving permission to all to write there
chmod 777 /var/www/myProject/uploaded
Alternatively you can change the owner/group with chown.

Allow creation of folder on apache2 in Ubuntu Server

I'm creating a site which I'm going to host on an Ubuntu Server. I have a user registration form on my site, which creates a user and a folder for that user (if filled out correctly). The form in handled by PHP and I'm running Apache 2 on the server. When I run the code on my laptop the folder is created, but when I run the same code on the server the folder is not there. I guess that it has something to do with permisssion. How can I tell apache that the code should have write permission in a specific folder called "users" located in /var/www/mysite/public/users/ ???
Try using chmod command that provides folder permission. Something like
chmod 777 -R foldername
If security is not an issue I would suggest:
chmod 775 -R /var/www/
chown -R [your_user]:www-data /var/www/
So both apache2 (www-data) and you can edit folders and files inside /var/www (or /var/www/html if this is your folder).
Notice that this may have issues with git and other tools that capture folder permissions (eg with git you may commit files with 775).

Ubuntu CHOWN, uploads as FTP user then www-data can't move file?

I'm having issues with a site I have set up on an Ubuntu VPS server running PHP 5.3.
I have 2 directories that I call in PHP:
/var/www/v-hosts/..../main/images/listing
/var/www/v-hosts/..../main/xml_sync/files
The second folder is one where a program using FTP uploads images. A script moves these files into the top directory using the www-data user, this script is just ran through a browser via cronjob.
However, the problem I face is that if I set the second folders owner to the FTP username, the www-data user can't move these folders in PHP when the script is run... So i have to do another chown for it...
Any way I can set it so BOTH www-data and my ftp user can upload files & my www-data php script can move the files without error?
UBUNTU CMD - when wanting FTP to allow uploads to files folder....
sudo chown -R myusername:www-data files
UBUNTU CMD - when wanting www-data to have access to new uploaded files...
sudo chown -R myusername:www-data files
PHP RENAME CODE:
rename($_SERVER['DOCUMENT_ROOT'].$file1,$_SERVER['DOCUMENT_ROOT'].$file2);
Any ideas / fixes are greatly welcome.
You can simply add ftp user to www-data group:
sudo usermod -a -G ftp www-data

Categories