Apache Unable to Write to File - php

I am using Fedora 24 on VirtualBox to host a small PHP web app. I am trying to append text into a file that is in the same local directory as the PHP file (/var/www/html/). No matter what kind of permissions or ownerships I try to set onto the directory (html/) or the file I constantly get "Permission denied in /var/www/html/pdf.php on line 21" errors.
Is there any configuration settings in my php.ini file that I need to enable to allow editing of files? I've even tried setting the directory and file to chmod 777 just to see if it would give me access but even that is being rejected.
EDIT: I have also tried creating directories and files in other locations with the same results. I tried to have Apache run the mkdir and touch commands with the same results.
EDIT 2: At the request of the comment left to my initial question. Here is the ownership information for the target directory:
ls -l /var/www/html/
drwxrwxr-x. 2 apache apache 4096 Nov 23 21:28 docs
The ownership information for the file:
-rwxrwxr-x. 1 apache apache 1381 Nov 28 17:47 pdf.php

Try assigning to www-data:nobody
chown -R www-data:nobody *, check apache group on your httpd.conf.
Verify /var/html perms to 775 at least.

Spent a lot of time looking for this answer but it's all in bits and pieces and no one every posts a solution (well most of the time) so here is my solution and it's used on various web control panels as well.
install and use MOD_RUID2
Install PHP with CLI (this is standard on newer versions)
In your HTTPD.CONF file in the virtual hosts, you'll add the following, replacing username with the user's login name, and usergroup with the user's group (These are usually the same)
<IfModule !mod_ruid2.c>
SuexecUserGroup username usergroup
</IfModule>
<IfModule mod_ruid2.c>
RMode config
RUidGid username usergroup
RGroups #none
</IfModule>
An example of a Virtual host conf is:
<VirtualHost *:443>
DocumentRoot "/home/imtheuser/public_html"
ServerName imtheuser.com
<IfModule !mod_ruid2.c>
SuexecUserGroup imtheuser imtheuser
</IfModule>
<IfModule mod_ruid2.c>
RMode config
RUidGid imtheuser imtheuser
RGroups #none
</IfModule>
<Directory "/home/imtheuser/public_html">
allow from all
Options None
Require all granted
</Directory>
</VirtualHost>
This will allow apache/php to write to a directory owned by the user. It's much safer then setting your chmod to 0777.

Related

localhost 403 Forbidden PHP

Read almost all questions here, but no solution found. This drives me crazy already to do such simple stuff to run PHP on my localhost...
You don't have permission to access this resource.Server unable to
read htaccess file, denying access to be safe
ls -l
-rwxrwxrwx 1 user user 51 sep 27 14:42 .htaccess
ls -l
drwxrwxr-x 2 user user 4096 sep 27 15:17 www
Any help appreciated!
Check if there is an .htaccess file inside the www folder (and its permissions), since Apache looks for one at every directory up to the one where the requested file exists.
I'm coming from an Apache on Windows server background, but in regards to the .htaccess file, you can also put a "AllowOverride None" in your section of your httpd.conf file. At least in Windows, this means nothing can be overridden, therefore no need to look for .htaccess files.
You can also add a line to tell Apache that the "AccessFileName" is blank. Here are the entried I'm speaking of from my config file. I'm running a XAMPP server on Windows.
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Require all granted
</Directory>
AccessFileName ""

AWS Apache search permissions are missing on a component of the path

I have a very strange permission problem with my Apache. Let's me explain what happened. We bought AWS EC2 instance and installed there AMI Centos 7 with Webmin image. Our Root path is /home/centos/public_html but unfortunately, we got information on the web 'You don't have permission to access / on this server'. Of course, I change access to the folder and all files, even on 777 and owner 'centos' user. Apache user is part of centos group. When I checked log file I got
[core:error] [pid 3872] (13)Permission denied: [client X.X.X.X:60088] AH00035:
access to / denied (filesystem path '/home/centos/public_html')
because search permissions are missing on a component of the path
On the public_html folder, we have default index.php to show us configuration of PHP and is not working.
<?php
phpinfo();
?>
Our http.conf looks like that:
User apache
Group apache
<VirtualHost *:80>
DocumentRoot /home/centos/public_html
ServerName our_domain
ServerAlias *.our_domain
<Directory /home/centos/public_html>
Options Indexes FollowSymLinks
DirectoryIndex index.php index.html welcome.php
allow from all
AllowOverride All
Require all granted
AddType application/x-httpd-php .php
</Directory>
</VirtualHost>
Following this suggestion Apache - Permissions are missing on a component of the path
I had run command:
sudo chcon -R --type=httpd_sys_rw_content_t /home/centos/public_html/
and nothing, any ideas, thank you in advance for any suggestions.

How to allow Apache to display content from other directories than document root?

I have installed Apache version 2.4.29-1 in Manjaro Linux. The document root is /srv/http where apache properly displays files. But I don't like the idea of coding into the root user's directory of my system, so I want to keep the code in /home/aditya/Prog/PHP
I have set apache to access the directory by adding following lines into /etc/httpd/conf/httpd.conf
<Directory /home/aditya/Prog/PHP>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
Allow from all
</Directory>
Also, I have added a link to this directory in /srv/http
sudo ln -s /home/aditya/Prog/PHP/ PHP #in /srv/http
But when I try to access localhost/PHP, I get
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
localhost
Apache/2.4.29 (Unix) PHP/7.2.3
The permissions of /home/aditya/Prog/PHP and the test file inside are
drwxrwxrwx PHP
-rwxrwxrwx test.php
What else do I need to do?
You also need to change the DocumentRoot setting in your httpd.conf file.
Look for DocumentRoot and set the path to /your/path/to/project.
Apache2 site specific config files are kept in /etc/apache2/sites-available/. If you only want to apply config settings for a specific site.

PHP errors written to wrong log

My Linux server is running PHP 7 as a CGI on Apache2. In php.ini I have
error_log = /var/log/apache2/php.log
Now, I have two problems:
1) All PHP errors go to /var/log/apache2/error.log instead of php.log. I have edited the right php.ini because ini_get('error_log') returns the full path of php.log in PHP.
2) When I try to open either of those files in PHP, I get permission denied. I have chmod'd both files to 777, but PHP is still unable to access them.
The server has been restarted multiple times so the configuration changes apply. My Apache configuration is this:
ErrorLog ${APACHE_LOG_DIR}/error.log
ScriptAlias /local-bin /usr/bin
AddHandler application/x-httpd-php7 php
Action application/x-httpd-php7 /local-bin/php-cgi7.0
<Directory "/usr/bin">
Require all granted
AllowOverride All
</Directory>
How can I redirect PHP errors to the right file, and also make it readable for PHP? Thanks in advance!
You need to change the owner and the group of the log directory and the log file/s (if already exist/s) to www-data:
sudo chown -R www-data:www-data /path/to/log/directory
Dont change the directory permissions to 777 as it might cause security issues, use 775 instead. For the log files use 664.
You might also want to add your system user to the group www-data if not already a member (use groups command to check for your existing groups), a system reboot is required after that for the changes to take effect:
sudo adduser user www-data #change 'user' to your

Can't view index.php of Zend Framework 1.0 after setting up Apache/Host requirements?

UPDATE: the following error is coming up in the Apache error logs...
[Sat Dec 08 16:41:39 2012] [error] [client 127.0.0.1]
PHP Fatal error: require_once():
Failed opening required 'Zend/Application.php'
(include_path='/Library/WebServer/Documents/quickstart/library:.:/php/includes:/Users/markmcdonnell/Dropbox/Library/PHP/ZendFramework-1.12.0/library')
in /Library/WebServer/Documents/quickstart/public/index.php on line 18
I'm using Mac OSX 10.8.2 (Mountain Lion) and I'm having problems getting the Zend framework set-up and running (just the basic QuickStart).
I'm using Apache 2.2.22
My localhost files are accessible from /Library/WebServer/Documents
I have PHP 5.3.15 running as well.
I've created a new project via the command line (using the zf.sh file) called 'quickstart' (as per the section on the Zend website recommends) and that is stored here: /Library/WebServer/Documents/quickstart
I've followed both the official Zend website instructions and also an online book about installing Zend: http://www.survivethedeepend.com/zendframeworkbook/en/1.0/creating.a.local.domain.using.apache.virtual.hosts
My php.ini (/private/etc/php.ini) file has been updated to include the path to the library folder inside the ZendFramework directory...
include_path = ".:/php/includes:/Users/<home>/Dropbox/Library/PHP/ZendFramework-1.12.0/library"
I've updated /etc/apache2/httpd.conf so it includes...
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
# Ensure "localhost" is preserved unchanged pointed
# to the default document root for our system.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerName quickstart.local
DocumentRoot /Library/WebServer/Documents/quickstart/public
SetEnv APPLICATION_ENV "development"
<Directory /Library/WebServer/Documents/quickstart/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
...I've also updated /etc/hosts so it includes...
127.0.0.1 localhost
127.0.0.1 quickstart.local
I'm restarting Apache using sudo apachectl graceful (I've also used sudo apachectl restart)
So if I try and now access http://localhost/ I see my /Library/WebServer/Documents directory and I can see PHP is running still with no problems. But if I try and access http://quickstart.local/ I just get an empty page?
Can any one advise as to what the problem might be, what I'm missing and what I can do to get the initial 'welcome' page to display.
I'm sure I'm just missing someone small, but it's obviously causing problem enough for the basic page not to show.
Thanks.
Mark
Just for others who may have followed this. The answer by Kamil, above, will work, but opens a security hole outside Apache's security configuration. That's fine as long as you know it and have another layer of security above Apache.
Another way to resolve the problem is to put the ZendFramework somewhere inside the root directory's hierarchy. Like:
DocumentRoot/Library/WebServer/Documents/ZendFramework-1.12.0/library
or
DocumentRoot/Library/WebServer/Documents/quickstart/public/ZendFramework-1.12.0/library
Any path that's inside the root directory of your Apache server.
Maybe a user permissions issue between the Zend files and the Apache process.
Since you can create the project using zf.sh, the Zend files are obviously there. But the fail you get via Apache suggests that the Zend files might not be accessible via the Apache process (that is, the user under which the Apache process runs).
$ chmod 0777 /Users/markmcdonnell/Dropbox/Library/PHP/ZendFramework-1.12.0/library
to test.
You could probably get away with much less expansive permissions - 0644, for example - but I confess I'm terrible at that aspect of it. ;-)

Categories