How to use a different user and group for just one Virtual Host in Apache

apacheWith mpm-itk, a multi-processing module (MPM), you can run each VirtualHost with its own user ID and group ID. This is commonly used to secure shared hosting, since you don’t have to make every VirtualHost folder readable by the Apache user.

mpm-itk is based on the traditional prefork MPM, which means it’s non-threaded; in short, this means you can run non-thread-aware code (like many PHP extensions) without problems.

sudo apt-get install libapache2-mpm-itk
How the VirtualHost should be configured:
<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /path/to/web/root

  <IfModule mpm_itk_module>
    AssignUserId vhost-user vhost-group
  </IfModule>
</VirtualHost>
It is good practice to change the website root ownership to the user and group set for the vhost:
sudo chown -R vhost-user:vhost-group /path/to/webroot
Be careful with this command, as it will change the owner and group of all files under the specified path. And finally, restart Apache:
sudo apache2ctl configtest && sudo systemctl restart apache2

Modern Performance Note: Because mpm-itk is tied to the prefork process model, modern deployments usually achieve stronger isolation and HTTP/2 concurrency by combining Apache MPM Event with per-user PHP-FPM pools (user / group per site).

Reference: Apache MPM Documentation

Comments are Disabled