PHP Write Permission - FC13 - php

I have recently installed FC13 and am attempting to write a mechanism in my PHP code that caches gathered data into a specific directory (for our purposes here, let's call it /var/www/html/_php_resources/cache).
I copy my files over to the /var/www/html directory and then run chown -R apache:apache /var/www/html/* and chmod a+w /var/www/html/_php_resources/cache on the new data. For right now I am just using the global write permission for convenience. I will tweak the permissions later.
When I attempt to use the chmod or mkdir PHP functions I wind up with:
Warning: chmod(): Permission denied in /var/www/html/_include/php/CacheInit.php
or
Warning: mkdir(): Permission denied in /var/www/html/_include/php/CacheInit.php
Now, when I disable SELinux everything works just fine. The problem is that I would prefer not to disable SELinux and actually get the permissions set up correctly so that I can port it over to servers where someone does not have such explicit control.
As an example: my personal site host allows me to set read/write permissions on directories but will not allow for SELinux policy changes.
FYI:
uname -r = 2.6.34.7-56.fc13
*php -version * = PHP 5.3.3
rpm -qa | grep httpd = httpd-2.2.16-1.fc13
Does anyone have any suggestions?

I had the same problem, trying to mkdir from php. Not so much information on google but this is what I found and I guess this is the correct solution. One have to label the dir in which apache should create directories.
Label should be "httpd_sys_script_rw_t" and I found that info here: http://docs.fedoraproject.org/en-US/Fedora_Core/5/html/SELinux_FAQ/index.html#id672528
Here's how to label the dir: chcon -R -t httpd_sys_script_rw_t <dir>
Reference somewhere here: http://www.centos.org/docs/5/html/Deployment_Guide-en-US/rhlcommon-chapter-0017.html
Hope this help someone out there.

Related

Getting error "CodeIgniter\Cache\Exceptions\CacheException Cache unable to write to ... " for a new Codeigniter 4 project

Almost always, when I am starting a Codeigniter 4 project, I am getting the following error:
"CodeIgniter\Cache\Exceptions\CacheException Cache unable to write to "/path/to/codeigniter-project/writable/cache/."
SYSTEMPATH/Cache/Handlers/FileHandler.php at line 61
which points at the below line:
throw CacheException::forUnableToWrite($this->path);
What is the best way to solve this?
After doing a research, I've concluded into two options
Option 1:
The very quick way to just solve the above issue is to go to the command tool (e.g. terminal) and change the permissions of the folder to be writable and also to change the owner of the folder to be the one that is used from your server application. For example, I am using Apache and the user for apache is www-data:
chmod -R 755 writable/
chown -R www-data:www-data writable/
or for macOS:
chmod -R 755 writable/
chown -R _www:_www writable/
Pros:
A quick copy-paste solution
Those commands - most of the times - works out of the box
Even if you don't have access to command tool, you can change the permissions and ownership of the folder through FTP (e.g. through FileZilla UI interface)
Cons:
You always need to add those commands into a new project
You may experience issues on development when you are trying to access the folder with your username
Option 2 (suggested for local development):
Add the www-data or _www (for macOS) to be at the same group role as your user.
for Linux and Apache:
sudo usermod -aG www-data your_username
or for macOS and Apache:
sudo dseditgroup -o edit -a your_username -t user _www
If you are still getting the error also make sure that you have the folder as writable with the following command:
chmod -R 755 writable/
I don't know why but it may also need to do a restart of the Apache.
Pros:
It works for new projects with no extra effort
You don't need to change the owner of the folder so you can have access to the folder for development
Once this change is available to the server or locally, you don't need to use sudo anymore if the chmod command is required
Cons:
Not that safe for production environments (especially if you don't know what you are doing) since you would prefer to have specific users with specific roles rather than have users to have access everywhere
It requires more effort and it is not that straight forward for beginners
It needs more permissions to the server (e.g. sudo access)
The OP has a good answer. Though sometimes you don't have access to the terminal or a secure shell (ssh), especially in a shared hosting environment to be able to run those commands.
I got a similar issue while working on a new project. I noticed that this normally happened because my application failed to write session files in the writable/session folder of my root project directory because of permission restrictions. I was using the CodeIgniter\Session\Handlers\FileHandler as my configured "session storage driver".
I managed to solve it by instructing the application to write these temporary files in the server's tmp directory.
This is by default in the /tmp directory.
Excerpt from php-src/php.ini-production
; Directory where the temporary files should be placed.
; Defaults to the system default (see sys_get_temp_dir)
;sys_temp_dir = "/tmp"
Solution
I opened my .env file residing at the root of my project directory and added the line below.
app.sessionSavePath = '/tmp/my_project_name/ci4_session'
If you don't have this .env file, create one starting with this default file content:
Notice the dot (.) in front of the filename.
CodeIgniter4 env file
Addendum
Similarly, in your case, the application fails to write cache files in the writable/cache folder of your root project directory because of permission restrictions. You most certainly are using file as your cache handler.
Solution
Open your .env file residing at the root of your project directory and add the line below.
cache.storePath = '/tmp/my_project_name/ci4_cache'
TIP(S)
These and more configurations can be found/modified in your .env file using the rules below:
The configuration class file name, in lower case. I.e cache.
Followed by a dot(.).
Then the public configuration class's property name. I.e storePath.
Steps 1,2,3 combined makes, cache.storePath, which becomes your .env file's key declaration. Then an equals sign =. And lastly, the .env file's value ('/tmp/my_project_name/ci4_cache') declaration. Finally coming up with, cache.storePath = '/tmp/my_project_name/ci4_cache'.
The configuration classes can be found in the app/Config folder of your root project directory. I.e:
app/Config/Cache.php
app/Config/App.php
This worked for me, U should try this
Firstly cd into where your file is Example
cd /opt/lampp/htdocs/
This is where my file is
Secondly Write this command below in your command prompt
sudo chmod -R 777 file-name
I just create a folder inside "writable" folder with name "cache" and my project properly running.

Ubuntu server, PHP - Apache: Permission Denied

I've done a server, but I'm receiving a bad message when I try to create a php file in "/var/www/html". It says: permission denied.
I've looked for a solution, but I don't want make "myuser" permission like "root" amd I have also read that it is a bad practice.
I've tried, with PHPStorm, to access to the server with SFTP, but in the end I however can't upload a file or create it.
Thank you before!
By default the owner of the files within /var/www/html is the www:data group. All you have to do is add your self to the www-data group.
sudo adduser <username> www-data
sudo chown -R www-data:www-data /var/www
The first line adds you to the www-data group, the second line clears up any files with messed up ownership.
See more about best practices here https://askubuntu.com/questions/46331/how-to-avoid-using-sudo-when-working-in-var-www
Hope this helps!

move_uploaded file:failed to open stream: Permission denied in /srv/http/upload.php

I am trying to run a server on ArchLinux, I have apache and PHP running normally, but i can't upload a file with PHP. I have seen many questions of this sort on Stackoverflow and I seem to have used all the suggestions I found, but I still get a
failed to open stream: Permission denied in /srv/http/upload.php
error.
To be precise
Warning: move_uploaded_file(): Unable to move '/tmp/phpZvJK1l' to '/home/administrator/SCRIPTS/tr/solution.cpp' in /srv/http/upload.php on line 20
I set the permissions for /tmp/, /home/administrator/SCRIPTS/tr and /srv/http to 777 (I know thats not right, but I am working locally right now and I want to get it working somehow).
Also I set all the owner of these directories to http (that is the user running PHP), but the thing still doesnt work.
Some PHP configurations do not allow file access outside the users docroot directory, so you may not have access to /tmp from within PHP. Try uploading your file(s) to a temp directory within your /home/administrator directory - preferably to a directory that isn't accessible to web browsers (a sibling directory to your docroot).
give permission to the /tmp dir :
sudo chmod 777 /tmp
As an Arch user I have the same issue when I work on web projects.
I'll recommend you to see this part of the Arch Wiki
User directories are available by default through http://localhost/~yourusername/ and show the contents of ~/public_html (this can be changed in /etc/httpd/conf/extra/httpd-userdir.conf).
So do to so you have to create the ~/public_html directory then
You must make sure that your home directory permissions are set properly so that Apache can get there. Your home directory and ~/public_html must be executable for others ("rest of the world"):
$ chmod o+x ~
$ chmod o+x ~/public_html
$ chmod -R o+r ~/public_html
After that you don't need to put your file under /srv/http/ you can now use ~/public_html as development directory.

Php fails to create directory while all permissions should be set:

I've read all the relevant topics and went trough the anwers,
but my program is still not workint properly.
I get the following error message:
Warning: mkdir(): Permission denied in /var/www/printing/uploader_temp.php on line 28
Where line 28 looks like this:
mkdir("upload", 0777, true);
The current user who is logged in is called "server".
The owner of both var/www/ and var/www/printing is "server". (without " of course)
The attribute of var/www/ptinting is -777.
I modified the following rows in /etc/apache2/envvars to look like this:
export APACHE_RUN_USER=server
export APACHE_RUN_GROUP=server
and restarted apache server.
I can create folders manually on server or using FTP.
Does anyone have an idea what I did wrong?
Where else should I set permissions?
The OS is Ubuntu.
Thank you for your answers.
Did you tried to give permissions to apache user? I had this problem on my web application and I solved it in two commands:
chown -R www-data:www-data /path/to/webserver/www
chmod -R g+rw /path/to/webserver/www
Hope this helps for you too.
Try to have a look to AppArmor:
https://wiki.ubuntu.com/AppArmor
Try to create a file or a directory in the /tmp/, can you?

Changing permissions via chmod at runtime errors with "Operation not permitted"

When I use chmod() to change permissions at run time, it gives me the below message:
Warning: chmod() [function.chmod]: Operation not permitted in /home/loud/public_html/readalbum.php
How can I remove this error and make the chmod function work?
$ sudo chmod ...
You need to either be the owner of the file or be the superuser, i.e., user root. If you own the directory but not the file, you can copy the file, rm the original, then mv it back, and then you will be able to chown it.
The easy way to temporarily be root is to run the command via sudo. ($ man 8 sudo)
In order to perform chmod, you need to be owner of the file you are trying to modify, or the root user.
This is a tricky question.
There a set of problems about file permissions. If you can do this at the command line
$ sudo chown myaccount /path/to/file
then you have a standard permissions problem. Make sure you own the file and have permission to modify the directory.
If you cannnot get permissions, then you have probably mounted a FAT-32 filesystem. If you ls -l the file, and you find it is owned by root and a member of the "plugdev" group, then you are certain its the issue. FAT-32 permissions are set at the time of mounting, using the line of /etc/fstab file. You can set the uid/gid of all the files like this:
UUID=C14C-CE25 /big vfat utf8,umask=007,uid=1000,gid=1000 0 1
Also, note that the FAT-32 won't take symbolic links.
Wrote the whole thing up at http://www.charlesmerriam.com/blog/2009/12/operation-not-permitted-and-the-fat-32-system/
You, or most likely your sysadmin, will need to login as root and run the chown command:
http://www.computerhope.com/unix/uchown.htm
Through this command you will become the owner of the file.
Or, you can be a member of a group that owns this file and then you can use chmod.
But, talk with your sysadmin.

Categories