change apache user

How to Change Apache User

By default, Apache uses www-data user in Ubuntu/Debian and apache in CentOS/Redhat/Fedora to access your website files and run system commands. However, sometimes you may need to use a different user & group for your website files. Here are the steps to change Apache user.

How to Change Apache User

Here are the steps to change Apache user and group.

1. Open Apache Configuration File

Open terminal and run the following command to open Apache configuration file located at any of the following locations depending on your system & type of installation.

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf
$ sudo vi /etc/httpd/httpd.conf

Also read : How to Change Default Page in .htaccess

2. Update User & Group

Look for User and Group directives in Apache configuration file. They will be as follows.

User www-data
Group www-data

User apache
Group apache

Change it to a user of your choice (e.g. root)

User root
Group root

Also read : How to Make URL Case Insensitive in .htaccess

3. Change file ownership

Update your website files’ owner and group to the ones you specified above. Here is the syntax for it.

$ sudo chown --recursive username:groupname /your/website/root/

For example, if your website’s root is located at /var/www/html then run the following command to change owner & group.

$ sudo chown --recursive root:root /var/www/html/

Also read : How to Make Subfolder Root in .htaccess

4. Restart Apache Server

Restart Apache server to apply changes

$ sudo systemctl restart apache2 # Ubuntu, Debian, openSUSE and SLES 
$ sudo systemctl restart httpd # CentOS, Fedora and Red Hat

Also read : How to Make Directory Inaccessible using .htaccess

5. Test the changes

Run the ps aux command to list the owner and group of Apache processes.

$ ps aux | grep apache2
root  1288  0.0  0.1 162184  6634 ?        Ss   Apr29   0:02 /usr/sbin/apache2 -k start
root  2197  0.0  0.1 162184  6268 ?        S    Apr29   0:00 /usr/sbin/apache2 -k start
root  1298  0.0  0.1 162184  6926 ?        S    Apr29   0:00 /usr/sbin/apache2 -k start
root  1302  0.0  0.1 162184  7684 ?        S    Apr29   0:00 /usr/sbin/apache2 -k start

As you can see, your new user (root) is the owner of Apache processes. In this article, we have learnt how to change Apache user & group for your website.

Also read : How to Remove index.php using .htaccess

Leave a Reply

Your email address will not be published. Required fields are marked *