How to Add User to Apache Group in CentOS to Fix WordPress Permission Issues


4 views

When managing WordPress on CentOS, you might encounter permission issues during updates or file modifications. The error message you're seeing typically looks like this:

Warning: copy(/wp-admin/includes/update-core.php) [function.copy]: 
failed to open stream: Permission denied in /wp-admin/includes/class-wp-filesystem-direct.php on line 200

This occurs because your user account doesn't have write permissions to WordPress files owned by the Apache user (usually apache or www-data).

First, verify your current group membership with:

groups
id

To check if the apache group exists:

getent group apache
# Or alternatively:
cat /etc/group | grep apache

Use this command to add your current user to the apache group (replace yourusername with your actual username):

sudo usermod -a -G apache yourusername

Key flags explanation:

  • -a: Append to existing groups (don't replace)
  • -G: Specify secondary groups

If you prefer not to modify groups, you can change WordPress file ownership:

sudo chown -R yourusername:apache /path/to/wordpress
sudo chmod -R g+w /path/to/wordpress

This grants write permissions to the group while keeping apache as the group owner.

After making changes, verify them with:

groups yourusername
ls -ld /path/to/wordpress
ls -l /path/to/wordpress/wp-admin/includes/update-core.php

Group membership changes require you to:

  1. Log out and log back in
  2. Or run: newgrp apache

For immediate effect without logout, you can use:

exec su -l $USER

When adding users to the apache group:

  • Only add trusted users as they gain significant system access
  • Consider using specific directory permissions instead of broad group access
  • For shared hosting, individual user accounts with proper umask settings might be better

Create a simple script to handle WordPress updates safely:

#!/bin/bash
WP_DIR="/var/www/html/wordpress"
sudo chown -R $USER:apache $WP_DIR
sudo chmod -R g+w $WP_DIR
# Perform WordPress update
sudo chown -R apache:apache $WP_DIR

Remember to make it executable with chmod +x scriptname.sh.


When managing WordPress installations on CentOS servers, many developers encounter permission-related errors during updates or file operations. The specific error message:

Warning: copy(/wp-admin/includes/update-core.php) [function.copy]: failed to open stream: Permission denied in /wp-admin/includes/class-wp-filesystem-direct.php on line 200

This occurs because Apache runs as the apache user (or sometimes www-data), and your files are owned by your personal user account. The web server needs proper permissions to modify WordPress files during updates.

First, verify your current groups and the Apache group name:

groups
cat /etc/group | grep apache

On CentOS/RHEL systems, the Apache group is typically named apache, while on Debian/Ubuntu it's usually www-data.

To add your user (let's assume it's developer) to the apache group:

sudo usermod -a -G apache developer

The -a flag appends the group rather than replacing existing groups, while -G specifies secondary groups.

Group membership changes require you to log out and back in, or use this alternative:

newgrp apache

Verify the change took effect:

id

For WordPress to function correctly while maintaining security:

sudo chown -R developer:apache /path/to/wordpress
find /path/to/wordpress -type d -exec chmod 775 {} \;
find /path/to/wordpress -type f -exec chmod 664 {} \;

This configuration gives:

  • Read/write to owner (developer)
  • Read/write to group (apache)
  • Read-only to others

For development environments, you might consider changing Apache's user instead:

sudo sed -i 's/User apache/User developer/' /etc/httpd/conf/httpd.conf
sudo systemctl restart httpd

However, this is less secure for production environments.

Create a maintenance script fixwp.sh:

#!/bin/bash
WP_PATH="/var/www/html"
sudo chown -R developer:apache $WP_PATH
sudo find $WP_PATH -type d -exec chmod 775 {} \;
sudo find $WP_PATH -type f -exec chmod 664 {} \;

Make it executable:

chmod +x fixwp.sh