Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Friday, June 1, 2012

Apache Unable to Read Files Unless SELinux Context is Set Correctly

If you're getting a "permission denied" error when Apache tries to read and serve out a file, even though the file-system permissions are correct, then it's most likely due to the SELinux context issue.

For Apache to be able to read the file, the file needs to have SELinux context something like "system_u:object_r:httpd_sys_content_t:s0"

To find out the current SELinux context of files, "ls -alZ"

To change the SELinux context of a file:
chcon <Context> <File>
e.g. chcon system_u:object_r:httpd_sys_content_t:s0 index.html

PHP Unable to Connect to MySQL

When you get an error something like "SQLSTATE[HY000] [2003] Can't connect to MySQL server" with a subsequent error code "13" later on in the error message....

This is due to selinux preventing the web server from making external network connections.

To fix this, issue the following command as root:
setsebool -P httpd_can_network_connect=1

Find Out Whether Apache Using PreFork or Worker

Use the command:
/usr/sbin/apache2 -l

Note: adjust accordingly for distros that use "httpd" instead e.g. Redhat based ones

Running Apache and PHP on Mac OS

Apache comes installed on Mac OS 10.5.

The config files are in:
/etc/apache2/

The log files are in:
/private/var/log/apache2/

To start the server:
sudo /usr/sbin/httpd -k start

To enable PHP, uncomment this line in /etc/apache2/httpd,conf:
LoadModule php5_module libexec/apache2/libphp5.so

DocumentRoot is:
/Library/WebServer/Documents

Server is accessible by:
http://localhost/

It is also pre-configured such that ~/Sites can be accessed by:
http://localhost/~username/

So with your regular user account you can just put your files in the Sites folder.