The Hidden Permission Trap Behind "Could Not Create Directory"
The error message Could not create directory during a WordPress plugin installation is notoriously misleading. It suggests that WordPress lacks the write permissions to create folders, but in environments like CentOS 6, the root cause is almost invariably a discrepancy between the user running the web server and the user attempting the installation.
When you successfully run sudo -u root touch /var/www/html/wordpress/wp-content/plugins/test.txt, you are effectively bypassing the problem by forcing an action as the superuser. However, this does not resolve the underlying logic error that WordPress throws. The core issue is that the web server processâtypically running as www-data on Debian/Ubuntu derivatives or apache (or nobody) on CentOS/RHEL systemsâdoes not own the target directory structure.
WordPress attempts to unpack the plugin zip file into a directory it does not control. Even if you have system-level permissions set correctly via sudo, the PHP process executing the installation script runs under a specific user context that lacks ownership of the /var/www/html/wordpress/wp-content/plugins path.
To fix this without relying on elevated privileges for every operation, you must align directory ownership with the web server's runtime user. On CentOS 6, identify your primary web server user using:
ps aux | grep -E 'httpd|apache'
You will likely see a username like apache or nobody. Once identified, change the ownership of the entire WordPress installation and its contents to match that user. This ensures that when WordPress needs to create a directory for a plugin, it can do so without needing sudo:
# Replace 'apache' with your actual web server user if different
chown -R apache:apache /var/www/html/wordpress
chmod -R 755 /var/www/html/wordpress
Note the distinction between chown and chmod. Changing permissions to 777 might make the directory writable for everyone, but it introduces a significant security vulnerability where any system user can execute scripts within your web root. Setting ownership correctly is the secure approach.
There is a specific nuance with the "Could not create directory" error that often trips up developers: WordPress may fail to create the directory even if you have set permissions on the parent folder, because the wp-content/plugins folder itself might be owned by a different user (e.g., root) while the web server tries to write to it. The command above addresses this by recursively updating ownership.
If you encounter issues where the installation still fails despite correct ownership, check if SELinux is blocking the access. CentOS 6 defaults to enforcing mode, which can restrict the web server from writing to certain paths even if permissions are correct. You can temporarily test this by putting SELinux into permissive mode:
setenforce 0
If the installation succeeds after running setenforce 0, you know SELinux is the culprit. The proper long-term fix involves creating a custom SELinux policy or adjusting the context of the WordPress directory:
chcon -R -t httpd_sys_rw_content_t /var/www/html/wordpress/wp-content
However, changing ownership remains the primary and most robust solution. By ensuring the web server user owns the target directories, you eliminate the need for sudo during routine plugin management, which aligns with best practices for production environments. This approach prevents future permission errors and maintains a secure boundary between the application logic and system-level privileges.
