How to Host Multiple PHP Websites on One VPS with Separate Users
When you host multiple PHP websites on one VPS, the easy setup is also the risky one: every site runs as the same system user, reads the same files, and shares the same PHP pool. One hacked plugin or one bad upload script can then reach every other site on the server. Giving each site its own Linux user and its own PHP-FPM pool fixes that at the source. This guide walks through the full setup on Ubuntu with Nginx: separate users, separate pools, separate server blocks, and permissions that keep sites apart. The first site takes about an hour; each one after that takes around ten minutes, since the pattern is the same every time.
Why Separate Users Matter When You Host Multiple PHP Websites
On a default LEMP stack, Nginx and PHP-FPM run as www-data, and every site lives under /var/www owned by that same account. That is fine for one site. With three or five sites, a file upload flaw in site A gives an attacker read access to the config files of sites B and C, including database passwords.
Separate users change the math. Each site gets its own user, its own home directory, and its own PHP-FPM pool running as that user. PHP code for site A runs as sitea, not as a shared account, so it cannot read files owned by siteb. The isolation is enforced by the operating system, not by a plugin.
There is a second benefit: troubleshooting gets easier. When a site eats all the RAM or fills the disk with logs, ps and du show you which user is responsible. You can restart one pool without touching the others, and you can hand a developer SFTP access to one site without handing them the whole server.
Plan Your Layout Before You Create Anything
Pick a naming scheme and stick to it. Use short usernames that match the site, such as sitea for sitea.com. Keep each site in its own directory under /var/www/, with the public files in a public folder inside it, plus logs and tmp folders alongside.
If PHP is not installed yet, follow the guide to installing PHP 8.4 on Ubuntu with Nginx and PHP-FPM first, then come back here.
For each site, note the domain name, the username, and the PHP-FPM socket name before you start. For site A, that means user sitea, root /var/www/sitea.com/public, and socket /run/php/php8.4-fpm-sitea.sock.
Create a Separate Linux User for Each Site
Create the user with no login shell and no password. The user exists only to own files and run PHP; nobody logs in as it.
sudo adduser --disabled-password --disabled-login --gecos "" sitea
sudo mkdir -p /var/www/sitea.com/public /var/www/sitea.com/logs /var/www/sitea.com/tmp
sudo chown -R sitea:sitea /var/www/sitea.com
sudo chmod 750 /var/www/sitea.com
The 750 on the site directory is the part that does the isolating. The owner can read, write, and enter the directory, the group can read and enter it, and everyone else gets nothing, so user siteb cannot even list the contents of site A’s folder.
Repeat the same four commands for each site, swapping the names. If you need to give a real person access to one site later, add their SSH key to that site’s user or set up SFTP chrooted to that site’s directory. Do not add them to the www-data group as a shortcut; that undoes the separation you just built. For a deeper look at how these permission bits work, see the guide to Linux file permissions and how to secure /var/www the right way.
Give Each Site Its Own PHP-FPM Pool
PHP-FPM pools are the core of this setup. A pool is a group of PHP worker processes with its own user, group, socket, and settings. Copy the default pool file for the new site:
sudo cp /etc/php/8.4/fpm/pool.d/www.conf /etc/php/8.4/fpm/pool.d/sitea.conf
sudo nano /etc/php/8.4/fpm/pool.d/sitea.conf
Change these lines in the new file:
[sitea]
user = sitea
group = sitea
listen = /run/php/php8.4-fpm-sitea.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
Three details matter here. First, user and group are the site user, so PHP code runs as sitea. Second, listen.owner stays www-data because Nginx runs as www-data and needs permission to talk to the socket. Third, pm.max_children = 5 caps how many PHP workers one site can spawn, so a traffic spike on site A cannot starve site B of memory. Tune the numbers per site once you know its traffic.
Validate the config and restart PHP-FPM, then confirm the socket exists:
sudo php-fpm8.4 -t
sudo systemctl restart php8.4-fpm
ls -l /run/php/php8.4-fpm-sitea.sock
Wire Nginx Server Blocks to the Right Pool
Each site gets its own Nginx server block that points fastcgi_pass at that site’s socket. Create /etc/nginx/sites-available/sitea.com:
server {
listen 80;
server_name sitea.com www.sitea.com;
root /var/www/sitea.com/public;
index index.php index.html;
access_log /var/www/sitea.com/logs/access.log;
error_log /var/www/sitea.com/logs/error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm-sitea.sock;
}
}
Enable the site and test the config before reloading Nginx:
sudo ln -s /etc/nginx/sites-available/sitea.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
The one line that must match the pool is fastcgi_pass. If it points at the wrong socket, site A runs PHP as site B’s user, and the isolation silently disappears. When you copy a server block for the next site, change the server name, root, logs, and socket together, then run nginx -t again. If you are new to server blocks, the step-by-step guide to setting up Nginx server blocks on Ubuntu 24.04 covers the directory layout and the enable/disable flow in more detail.
Lock Down File Permissions and Test the Isolation
Set permissions inside the site directory: files 640, directories 750, all owned by the site user:
sudo chown -R sitea:sitea /var/www/sitea.com
sudo find /var/www/sitea.com -type d -exec chmod 750 {} ;
sudo find /var/www/sitea.com -type f -exec chmod 640 {} ;
WordPress and similar apps need one writable folder for uploads. Give that folder write permission for the site user only:
sudo chmod 770 /var/www/sitea.com/public/wp-content/uploads
sudo chown sitea:sitea /var/www/sitea.com/public/wp-content/uploads
Now test that the separation actually works. Drop a PHP file into site A’s public folder that prints the current user and tries to read site B’s config:
<?php
echo get_current_user() . "n";
echo @file_get_contents("/var/www/siteb.com/public/wp-config.php") ?: "Blocked: cannot read site Bn";
Load that page in a browser. You should see sitea and the “Blocked” message. If it prints site B’s config instead, check the pool’s user line and the 750 permission on /var/www/siteb.com. Delete the test file as soon as the check passes; a script that reads other sites’ files should never stay on a live server.
Common breakage at this stage: a socket path typo gives a 502 Bad Gateway, a missing listen.owner gives a permission denied error in the Nginx error log, and files uploaded over SFTP as root end up owned by root, so re-run the chown above after every upload done as root.
Repeat the whole flow for site B: new user, new pool, new server block, same permission pass. Each site takes about ten minutes once the first one works.
FAQ
Can I host multiple PHP websites on one VPS without separate users?
Yes, and most tutorials do exactly that, but every site then shares one account and one PHP pool. A flaw in one site can read the database passwords of the others. Separate users cost a few extra commands per site and remove that risk.
How many PHP websites can one VPS hold this way?
It depends on RAM, not on the number of users. Each pool holds a few worker processes, and each worker uses memory. On a 2 GB VPS, five small sites with pm.max_children = 5 each is a sane starting point. Lower the worker counts if the server starts swapping.
Does each site need its own database user too?
Yes. Create a separate MySQL or MariaDB user per site with access to only that site’s database. Separate Linux users stop file access between sites; separate database users stop one site’s code from reading another site’s tables.
What if two sites need different PHP versions?
Install both PHP versions, then create each site’s pool under its own version’s pool directory and point its Nginx server block at that version’s socket. The pool and socket names keep the versions apart, so the sites never interfere with each other.