Host multiple websites on a VPS with Nginx (2026)
With Nginx, a single Linux VPS can host multiple websites, each on its own domain name. This guide gives every site its own vhost, PHP-FPM pool, MariaDB database and HTTPS certificate, then backs them all up. It starts from the stack in the guide Install Nginx and PHP-FPM on a Linux VPS. That stack must already run on Debian or Ubuntu, with root or sudo access.
Prepare the server
Hosting several sites takes a little planning. Each active site uses memory for PHP, the database and the cache. The sites must stay apart from each other, and their backups should be planned from day one. The server also needs to be secured, as explained in the guide Secure a Linux VPS.
Check your PHP version
The commands in this guide include the PHP version number, in paths and in the service name. Each system ships its own version.
| System | Bundled PHP | Service | Pool folder |
|---|---|---|---|
| Debian 12 | 8.2 | php8.2-fpm | /etc/php/8.2/fpm/pool.d/ |
| Debian 13 | 8.4 | php8.4-fpm | /etc/php/8.4/fpm/pool.d/ |
| Ubuntu 22.04 | 8.1 | php8.1-fpm | /etc/php/8.1/fpm/pool.d/ |
| Ubuntu 24.04 | 8.3 | php8.3-fpm | /etc/php/8.3/fpm/pool.d/ |
The examples use PHP 8.4, the version in Debian 13. On another system, replace 8.4 with your version. Check it on the server.
php -v
ls /etc/php/
The Supported Versions page on php.net shows how long each version is supported. PHP 8.1, the one in Ubuntu 22.04, has been off that list since December 31, 2025. The section on running two PHP versions, further down, shows how to install a newer one.
Install MariaDB
The guide Install Nginx and PHP-FPM does not install a database. Install MariaDB, then run its security script and accept the suggested answers.
sudo apt install mariadb-server -y
sudo mariadb-secure-installation
The php8.4-mysql extension, installed with PHP in that same guide, lets the sites connect to it.
Point the domains to the VPS
Each domain needs an A record pointing to the VPS's main IP, for the domain itself and for www. Do it early, because DNS changes take a while to spread.
site-a.com. IN A 203.0.113.10
www.site-a.com. IN A 203.0.113.10
If your domain is with OuiHeberg, open the domain's DNS zone tab and click Add. In the Add a record window, pick Type A. Enter @ in Name for the domain, or www for the subdomain, then the VPS IP in Value. The guide How to edit the DNS records of your domain explains each field.

If the VPS has an IPv6 address, add an AAAA record too. The vhosts in this guide listen on both IPv4 and IPv6.
Create a user and a folder for each site
Each site gets a system user, with no shell and no password. PHP will run the site's code under this account.
sudo useradd -r -s /usr/sbin/nologin site-a
sudo useradd -r -s /usr/sbin/nologin site-b
sudo useradd -r -s /usr/sbin/nologin site-c
Then create one folder per site under /var/www/, named after the domain. With this rule, the configuration files and the backup script stay easy to read.
/var/www/
site-a.com/
site-b.com/
site-c.com/
sudo mkdir -p /var/www/site-a.com
sudo mkdir -p /var/www/site-b.com
sudo mkdir -p /var/www/site-c.com
Give each folder to its user and close it to other accounts. Nginx runs as www-data and must read the files. So add www-data to each site's group.
# Each folder belongs to its site's user
sudo chown -R site-a:site-a /var/www/site-a.com
sudo chown -R site-b:site-b /var/www/site-b.com
sudo chown -R site-c:site-c /var/www/site-c.com
# Closed to other accounts
sudo chmod 750 /var/www/site-a.com
sudo chmod 750 /var/www/site-b.com
sudo chmod 750 /var/www/site-c.com
# Nginx (www-data) can read each site's files
sudo usermod -aG site-a www-data
sudo usermod -aG site-b www-data
sudo usermod -aG site-c www-data
Nginx picks up these groups at its next reload. If you upload files as root, hand them back to the site's user with the same chown command.
This way,
www-datacan read the files of every site. The separation applies to PHP, since one site's code cannot read the other sites' folders. To go further, you need containers or separate VPS.
Create an Nginx vhost for each site
Each site has its own file in /etc/nginx/sites-available/. One file per site is easier to read, debug and disable than a single shared file.
Write the site file
sudo nano /etc/nginx/sites-available/site-a.com
Paste this configuration for the first site.
server {
listen 80;
listen [::]:80;
server_name site-a.com www.site-a.com;
root /var/www/site-a.com;
index index.php index.html;
# Logs for this site only
access_log /var/log/nginx/site-a.com.access.log;
error_log /var/log/nginx/site-a.com.error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP goes through the site's pool
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/site-a.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Sensitive files
location ~ /\.(ht|git|env) {
deny all;
}
}
Repeat for each site, changing the domain, the folder, the logs and the socket (site-b.sock, site-c.sock). PHP errors from a site land in its own log, here /var/log/nginx/site-a.com.error.log.
The site-a.sock socket does not exist yet. It comes with the PHP-FPM pool, in the next section. nginx -t does not check that it exists, but the site returns a 502 error until its pool is in place.
Enable the sites
sudo ln -s /etc/nginx/sites-available/site-a.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site-b.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site-c.com /etc/nginx/sites-enabled/
Remove the default site if it is still enabled. The default vhost from the section "Reject requests for unknown sites" will replace it. Always test the configuration before reloading Nginx.
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
The Nginx documentation describes the server_name directive, which ties a domain to its vhost.
Create a PHP-FPM pool for each site
Without a dedicated pool, all sites run as the same user, often www-data. A hacked site can then read the other sites' files, such as a .env, a wp-config.php or API keys. One pool per site changes three things.
Each site runs as its own user.
Each site has its own process limit. A traffic spike on one site does not starve the others of PHP.
Processes carry the site's name, which helps you spot the one causing trouble.
For your own projects, a single pool can be enough. For client or third-party sites, plan one pool per site. The PHP-FPM documentation describes each directive.
Write the pool file
Start from the default pool, copied under the site's name.
sudo cp /etc/php/8.4/fpm/pool.d/www.conf /etc/php/8.4/fpm/pool.d/site-a.conf
sudo nano /etc/php/8.4/fpm/pool.d/site-a.conf
Replace [www] with [site-a], then set these directives. The socket is named after the site, not after the PHP version.
[site-a]
user = site-a
group = site-a
listen = /run/php/site-a.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
Do the same for site-b.conf and site-c.conf, with their own pool name, user and socket. The pm.* values are set site by site, as the memory section explains.
Remove the default pool
Once every site has its pool, the www pool is no longer needed. First check that no enabled site still uses its socket. The first command must print nothing.
grep -R "php8.4-fpm.sock" /etc/nginx/sites-enabled/
sudo mv /etc/php/8.4/fpm/pool.d/www.conf /etc/php/8.4/fpm/pool.d/www.conf.disabled
A site still pointing to
php8.4-fpm.sockwould return a 502 error after this change.
Test the PHP-FPM configuration, restart the service, then check the sockets. The /run/php/ folder must contain site-a.sock, site-b.sock and site-c.sock.
sudo php-fpm8.4 -t
sudo systemctl restart php8.4-fpm
ls -l /run/php/
Test a site
This test does not need the domain to point to the VPS yet. Create a page that prints the user and the PHP version, call it from the server, then delete it.
echo '<?php echo posix_getpwuid(posix_geteuid())["name"], " ", PHP_VERSION, "\n";' | sudo -u site-a tee /var/www/site-a.com/index.php
curl -H "Host: site-a.com" http://127.0.0.1/
sudo rm /var/www/site-a.com/index.php
The response starts with site-a, followed by the PHP version. Any other user name means a vhost points to the wrong socket.
Reject requests for unknown sites
A request whose domain matches no server_name is not rejected. Nginx hands it to the default server for that port. Unless you set one, that is the first vhost loaded, and the files in sites-enabled load in alphabetical order. A visit to the VPS IP, or to a domain pointing there by mistake, therefore shows one of your sites. The page How nginx processes a request describes this behavior.
A default vhost solves the problem. It answers with code 444, specific to Nginx, which closes the connection without sending anything.
Write the default vhost
sudo nano /etc/nginx/sites-available/default-catchall
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
# Rejects HTTPS connections without presenting a certificate
ssl_reject_handshake on;
return 444;
}
The ssl_reject_handshake directive also rejects HTTPS connections, with no certificate needed. It exists since Nginx 1.19.4, so on Debian 12 and 13 and on Ubuntu 24.04.
Ubuntu 22.04 ships Nginx 1.18, which does not know it. Create a self-signed certificate instead, since Certbot cannot issue one for _.
sudo mkdir -p /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
-keyout /etc/nginx/ssl/self-signed.key \
-out /etc/nginx/ssl/self-signed.crt \
-subj "/CN=localhost"
In the default vhost, then replace the ssl_reject_handshake on; line with these two lines.
# Self-signed certificate to absorb unrecognized HTTPS requests
ssl_certificate /etc/nginx/ssl/self-signed.crt;
ssl_certificate_key /etc/nginx/ssl/self-signed.key;
Enable the default vhost
sudo ln -s /etc/nginx/sites-available/default-catchall /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
A visit to the VPS IP now gets no answer. Bots that scan IP addresses learn nothing about your sites.
Create a MariaDB database for each site
Give each site its own MariaDB database and user. A user shared by several sites would expose every database as soon as one site is hacked.
sudo mariadb -u root
-- Site A
CREATE DATABASE site_a_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'site_a_user'@'localhost' IDENTIFIED BY 'strong_password_a';
GRANT ALL PRIVILEGES ON site_a_db.* TO 'site_a_user'@'localhost';
-- Site B
CREATE DATABASE site_b_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'site_b_user'@'localhost' IDENTIFIED BY 'strong_password_b';
GRANT ALL PRIVILEGES ON site_b_db.* TO 'site_b_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Repeat the block for each site. Each site then connects to localhost with its own credentials.
Replace each sample password with a strong value. The command
openssl rand -base64 24generates one.
Enable HTTPS on each domain
Certbot handles each domain's certificate separately. Install it as in the guide Install Nginx and PHP-FPM. The domains must already point to the VPS, and ports 80 and 443 must be open. The guide How to set up the UFW firewall on Ubuntu or Debian explains these rules.
sudo certbot --nginx -d site-a.com -d www.site-a.com
sudo certbot --nginx -d site-b.com -d www.site-b.com
sudo certbot --nginx -d site-c.com -d www.site-c.com
Certbot adds the HTTPS block and the redirect from HTTP to each vhost. It asks for an email address the first time.
Renewal runs through a systemd timer installed with Certbot. List the certificates, check the timer, then run a renewal dry run.
sudo certbot certificates
sudo systemctl status certbot.timer
sudo certbot renew --dry-run
The guide Certbot: install a Let's Encrypt SSL certificate on a VPS covers renewal, wildcard certificates and common errors.
Run two PHP versions side by side
Each pool belongs to one PHP version. An older site can therefore stay on PHP 8.3 while the others move to 8.4. The Debian and Ubuntu repositories ship only one version. To add another, use the repository of Ondřej Surý, who publishes PHP packages for Debian and Ubuntu.
Add the repository on Debian 12 or 13
These commands follow the repository's instructions.
sudo apt update
sudo apt install -y lsb-release ca-certificates curl
sudo curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update
Add the repository on Ubuntu 22.04 or 24.04
On Ubuntu, the same repository comes as a PPA.
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Once the repository is added, updates also replace the system's PHP packages with the ones from the repository.
Install another version and move a site to it
Install both versions. For each one, add the extensions your sites need, changing the number (php8.3-mysql, php8.3-mbstring). The guide Install Nginx and PHP-FPM lists them.
sudo apt install php8.3-fpm php8.4-fpm -y
Each version has its own pool folder and its own service. To move site-b to PHP 8.3, move its pool and disable the PHP 8.3 default pool. Then restart both services, one after the other.
# Move site-b's pool to PHP 8.3
sudo mv /etc/php/8.4/fpm/pool.d/site-b.conf /etc/php/8.3/fpm/pool.d/
# PHP 8.3 default pool, not needed here
sudo mv /etc/php/8.3/fpm/pool.d/www.conf /etc/php/8.3/fpm/pool.d/www.conf.disabled
# One service after the other
sudo systemctl restart php8.4-fpm
sudo systemctl restart php8.3-fpm
The socket keeps its name, site-b.sock. The site's vhost therefore does not change. In the terminal, the php command stays tied to a single version. For a site's cron jobs or Composer, call php8.3 or php8.4 directly.
Plan memory usage
How many sites a VPS can handle depends on their traffic, their CMS and their plugins. Here are a few rough figures.
A WordPress site with OPcache and few simultaneous visitors uses 128 to 256 MB of RAM.
A static site or a light application uses much less.
A busy WooCommerce store, or a site loaded with heavy plugins, can go over 512 MB.
With 4 GB of RAM, hosting several low-traffic sites is reasonable. Always leave headroom for the system, MariaDB and unexpected peaks. Once the sites are live, measure the real usage.
free -h
ps -C php-fpm8.4 -o user,rss,cmd --sort=user
The RSS column gives the memory of each PHP process in kilobytes, and the USER column tells you which site it belongs to. Multiply a site's average by its pm.max_children to know its ceiling. Then adjust that value in its pool.
A pool caps its site. A traffic spike or a PHP error on one site therefore does not use up the others' processes. MariaDB, on the other hand, serves every site. If it saturates, all sites slow down. Keep an eye on its connections.
sudo mariadb -e "SHOW PROCESSLIST;"
Add or disable a site
Add a site
A new site does not affect the existing ones. Go through the sections of this guide in order.
Point the domain to the VPS.
Create the site's user and folder.
Write its vhost and enable it.
Create its PHP-FPM pool, then reload PHP-FPM with
sudo systemctl reload php8.4-fpm.Create its MariaDB database.
Request its certificate with Certbot.
The backup script in the next section picks up the new site on its own. For a WordPress site, also follow the guide How to install WordPress on a Linux VPS. It adds OPcache and upload size settings, without changing the pools. For a site leaving shared hosting, follow the guide How to migrate from shared hosting to a VPS. It covers copying the files, exporting the database and testing before the DNS switch.
Disable a site
Remove its link in sites-enabled, then reload Nginx.
sudo rm /etc/nginx/sites-enabled/site-a.com
sudo nginx -t && sudo systemctl reload nginx
The file stays in sites-available, and the default vhost now answers in place of the site. To enable it again, recreate the link with ln -s, as you did at first.
Back up all sites
With several sites, automate backups from the start. A single script goes through the folders in /var/www/ and the MariaDB databases.
Write the backup script
sudo nano /usr/local/bin/backup-sites.sh
#!/bin/bash
BACKUP_DIR="/root/backups"
DATE=$(date +%F)
mkdir -p "$BACKUP_DIR"
chmod 700 "$BACKUP_DIR"
# Files of each site
for SITE in /var/www/*/; do
SITE_NAME=$(basename "$SITE")
tar -czf "$BACKUP_DIR/${SITE_NAME}-files-${DATE}.tar.gz" -C /var/www "$SITE_NAME"
done
# All databases except MariaDB's internal ones
for DB in $(mariadb -N -e "SHOW DATABASES" | grep -Ev '^(information_schema|performance_schema|mysql|sys)$'); do
mariadb-dump --single-transaction "$DB" | gzip > "$BACKUP_DIR/${DB}-${DATE}.sql.gz"
done
# Delete backups older than 7 days
find "$BACKUP_DIR" -type f -mtime +7 -delete
The script archives each site folder and exports each database, except MariaDB's internal ones. It deletes backups older than 7 days. A new site is therefore backed up without touching the script.
Make it executable and run it once to test it. Then open root's crontab.
sudo chmod +x /usr/local/bin/backup-sites.sh
sudo /usr/local/bin/backup-sites.sh
sudo ls -l /root/backups/
sudo crontab -e
Add this line at the end of the file.
# Daily backup at 3 a.m.
0 3 * * * /usr/local/bin/backup-sites.sh
Restore a site
Replace the date with the one of the backup you want. The first command puts the site's files back, the other one reloads its database.
sudo tar -xzf /root/backups/site-a.com-files-2026-11-04.tar.gz -C /var/www
sudo gunzip -c /root/backups/site_a_db-2026-11-04.sql.gz | sudo mariadb site_a_db
Keep a copy off the VPS
These backups stay on the VPS. They protect you neither from a server failure nor from a hack. Send a copy elsewhere, for example with the guide Rclone: cloud synchronization on a Linux VPS. Automatic backup of the whole VPS is a paid option, described in the guide How to set up automatic VPS backups.
Need help?
Open a ticket from Tickets › Open a Ticket, or from the Contact support tile at the bottom of the VPS page. You can also ask your question on the OuiHeberg Discord.
Root access, your choice of distribution and management from the OuiHeberg client area.
View plans →