Permission denied: httpd: could not open error log file /etc/httpd/logs/error_log
When restarting httpd on CentOS 6.7, you might encounter a failure where the Apache server refuses to start despite the service appearing to be configured correctly. The standard output often reads:
Stopping httpd: [FAILED]
Starting httpd: (13)Permission denied: httpd: could not open error log file /etc/httpd/logs/error_log.
Unable to open logs [FAILED]
This specific error is frequently misdiagnosed as a simple SELinux issue because the httpd process runs with restricted privileges by default. However, the root cause here lies in how CentOS 6 handles filesystem attributes beyond standard Unix permissions. The ls -Z output reveals the critical detail:
-rw-r--r--. root root unconfined_u:object_r:var_t:s0 /etc/httpd/logs/error_log
The file is owned by root, which matches the expectation for an error log, but the context label is set to unconfined_u:object_r:var_t:s0. This specific security context indicates that the file was likely created or moved using cp or a similar command without preserving the original SELinux type.
In CentOS 6, the default behavior for the Apache error log directory (/etc/httpd/logs/) is to be labeled with the httpd_log_t type. When httpd attempts to write to a file labeled var_t, even if the file permissions are rw-r--r--, the SELinux policy denies the write operation. This results in the "Permission denied" error, which is misleadingly named because standard Linux permissions (the first three sets of letters) actually allow access; the denial occurs at the MAC (Mandatory Access Control) layer.
Disabling SELinux entirely, as mentioned in your scenario, resolves the immediate issue but removes a crucial security layer. Instead, the robust solution involves correcting the file's context so that Apache can write to it without compromising system integrity. You should use chcon or, preferably, restorecon to realign the file with its expected policy type.
To fix this without disabling SELinux, run the following command to restore the default context for the entire logs directory:
sudo restorecon -Rv /etc/httpd/logs/
The -Rv flags ensure recursive restoration and verbose output so you can confirm the change. If restorecon fails due to missing rules or specific policy constraints, you can manually set the context using chcon:
sudo chcon --type=httpd_log_t /etc/httpd/logs/error_log
After applying these changes, retry the service restart:
sudo /etc/init.d/httpd restart
It is also worth verifying that the directory itself has the correct label. Sometimes the file is corrected, but the parent directory retains a restrictive context that prevents writing. Ensure the /etc/httpd/logs directory is labeled as httpd_log_t as well.
A common pitfall in this scenario arises during system upgrades or when migrating configurations from other distributions where SELinux policies differ. CentOS 6 uses a specific policy module that strictly enforces httpd_log_t for logs generated by the web server. If you manually created the file using standard touch commands after an upgrade, the context might default to var_t unless explicitly overridden. Always prefer restorecon over manual chcon calls in production environments to ensure compliance with the expected policy rules.
If you are managing multiple servers and need a consistent approach, documenting this specific SELinux context requirement is essential. Ignoring it leads to intermittent startup failures that are difficult to debug without checking the security context directly. For modern applications running on Apache, such as those built with Laravel, ensuring that logs are writable by the apache user (or httpd in older versions) while maintaining strict SELinux contexts is a best practice for both security and stability.
For further guidance on securing your web server stack and managing permissions correctly in production environments, you might find additional resources relevant to enterprise deployments at https://laravelcompany.com.