Contents
After spending 2-3 hours pulling my hair trying to set up a supposed to be simple PHP/MySQL web application on an Amazon EC2 instance running on CentOS 7. Apache logs keep saying that it can’t write to file due to permission where file permissions are properly set up, only to realize it was SELinux in action.
Problem 1: Can’t serve files on a custom directory
The first problem I have encountered is that I tried to set up the application inside /data/www/html/sites/mysite
When viewed on the browser, it says 403 Forbidden and error logs says:
1 |
|
The directory structure has proper ownership and permissions, ex: directory is owned by apache:apache
file permission is 0644
and directory permission is 0755
. It doesn’t make sense at all. I noticed though that the default document root has no problem serving the PHP file so I decided to serve it off the directory /var/www/html/mysite
, which is the default document root.
Problem 2: Can’t write to file
Moving to the default document root directory did the trick and I was able to run the application but with errors. The error says it can’t write to file although again, proper permissions are already set to the directory. Below is the error (it is a custom error log, but if writing to log file doesn’t work, imagine how your upload functionality would work):
1 |
|
Surprise! SELinux is here!
You guys choose CentOS, so you got SELinux as well.
After realizing that it was SELinux whose messing with me for the past 2 hours, I was thinking of ditching CentOS and go with the recommended Ubuntu instead. But then my instinct tells me that if SELinux is blocking the read/write operations, it just did it for a good reason, and that was for security. I realize that you need to specify which files/directories Apache can serve files and which files/directories it can write into.
SELinux seems to have some rules/policies that apply to files/directories on top of the Unix file permissions structure. When I run the command below on the default document root, I saw more information on the file/directory permissions.
1 |
|
Below is the output (some information removed):
1 2 |
|
And below is what I got for other normal directories:
1 |
|
Therefore, we can conclude that we need to specify the proper SELinux permissions on directories in order to serve files on a custom directory and set another SELinux permissions to allow writing to file. Therefore, we can solve the original problem then.
Fixing the original problem
So we want to serve our files at /data/www/html/sites/mysite
and enable writing to log files and file uploads as well? Let’s play nice with SELinux.
First, copy the files as usual to /data/www/html/sites/mysite
, then set the proper ownership and permissions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
httpd_sys_content_t
– for allowing Apache to serve these contents and httpd_sys_rw_content_t
– for allowing Apache to write to that path.
That’s it!
Revisions
- November 6, 2017 @ 17:55:06 [Current Revision] by Sharing Solution
- November 6, 2017 @ 17:55:06 by Sharing Solution
No comments yet.