I cannot create a directory when installing a module I have a clean install of Drupal 8 on a new Centos 7 server. This is the first module I have attempted to install. I'm using the built in update manager. I get the following error.


  File Transfer failed, reason: Cannot create directory /var/www/html/modules/somemodule


This seems like a common enough permissions problem but no answers I've found on similar questions have worked. The webserver user is apache (not www-data) and it owns the modules directory. I have confirmed this with ps aux | grep -i 'www\|apache\|nginx'.

These are the current permissions.

drwxr-xr-x.  2 apache apache    24 Jan  3 13:27 modules


I have chowned and chmodded until I'm blue in face, but it hasn't changed anything since the apache user already owns the directory and should be able to write it. 

/admin/reports/status shows all green including FILE SYSTEM Writable (public download method).

What could be wrong? How can I further troubleshoot?

I am using PHP 7.1.14, Apache httpd-2.4.6-67.el7.centos.6.x86_64, and Drupal 8.4.4.

Update: 

I have tried chmod -R 777 as a test and receive the same error. I have also tried creating the directory for the module manually but the update manager is still unable to copy files to it. 

Update 2: 

I have downgraded to php 7.1 to be fully compliant with Drupal 8 requirements but this has not solved the issue or changed the behavior.
This turned out to be an SELinux issue. SELinux is enabled and running in enforcing (strictest) mode by default in Centos 7 and this will prevent the web user from being able to write to /modules or /files (or anywhere) regardless of the file permissions set. 

To see if you may be having the same problem:

more /etc/selinux/config


This will display the SELinux config file and let you know if it is enabled and what mode it is in. 

The issue can be corrected with the following commands:

sudo chcon -Rt httpd_sys_content_t /var/www/html
sudo chcon -Rt httpd_sys_rw_content_t /var/www/html/sites/default/files
sudo chcon -Rt httpd_sys_rw_content_t /var/www/html/modules
sudo chcon -Rt httpd_sys_rw_content_t /var/www/html/vendor


This will correct the context of the directories you want to grant write permissions on so that Drupal will work without disabling SELinux. For more information see the man pages for selinux and chcon.