Folder writeable by "others" in a linux server - php

I need to have a folder where php can write. As far as I know php is "others" in a unix system and so I had to do:
chmod o=rwx /path/to/writeable/dir
My question is: can a folder like that represent a security problem for the system?

Depending of the user of apache (usually www-data or apache2) I would give the folder to apache :
chown apache2 myFolder -Rf

Related

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

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.

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.

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.

Lamp Server 403 Forbidden

Hi I just installed ubuntu alongside my Win 7 and I have been using xampp and am very familiar with it, but I just installed lamp and am using the apache2, php, and mysql from terminal and I copied a web folder over from my xampp side and it is saying that I do not have permission to access that file.
I know that on my pc I had some htaccess files but on Ubuntu I am yet to figure out how to view those. Is this a product of those .htaccess files or something else?
That happens if you install WAMP/XAMMP on system partition.
so you copied the files form windows to linux?
sounds like a classic file permissions problem.
per default the www folder is in /var/www, so you can simply set the owner of this folder to the apache user which is called www-data, run this in terminal:
sudo chown -R www-data:www-data /var/www
but now you will no longer be able to write to those files yourself, because they are owned by www-data. checkout this answer for more details and how to get write access: https://askubuntu.com/a/51337
or you make it writeable for everyone (which is a bad idea): sudo chmod -R 777 /var/www
about editing .htaccess files:
I don't use linux with a graphical user interface, but you can edit them with the terminal editor of your choice, on ubuntu you have nano installed by default:
nano /var/www/.htaccess
or if you want a more advanced editor, I suggest vim https://help.ubuntu.com/community/VimHowto
vim /var/www/.htaccess

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).

Categories