One server can comfortably host dozens of small websites. Nginx decides which site to serve from the domain name in each request, so adding a site is mostly a matter of creating a directory and a short configuration file. Doing it well — with each site isolated, logged separately and secured with its own certificate — takes a little more planning. This guide shows a layout that stays manageable as the number of sites grows.
Prerequisites
Nginx and PHP-FPM installed as in installing a LEMP stack, and DNS A records for each domain pointing to the server.
A consistent directory layout
/var/www/
├── site-a.com/
│ ├── public/ ← document root
│ ├── logs/ ← optional per-site logs
│ └── tmp/
├── site-b.com/
│ └── public/
└── _default/
└── public/ ← catch-all for unknown domains
Name directories after the domain, so you always know where a site lives.
Server blocks
Each site gets one file in /etc/nginx/sites-available/. A template for a PHP site:
server {
listen 80;
listen [::]:80;
server_name site-a.com www.site-a.com;
root /var/www/site-a.com/public;
index index.php index.html;
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?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/site-a.sock;
}
location ~ /\.(?!well-known) { deny all; }
}
Enable it:
sudo ln -s /etc/nginx/sites-available/site-a.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Reuse common settings with snippets
Put shared rules in /etc/nginx/snippets/ and include them, so a security fix applies to every site at once:
# /etc/nginx/snippets/site-common.conf
location ~ /\.(?!well-known) { deny all; }
location ~* \.(css|js|jpg|jpeg|png|gif|webp|svg|ico|woff2?)$ { expires 30d; access_log off; }
location = /favicon.ico { log_not_found off; access_log off; }
include snippets/site-common.conf;
A catch-all default server
When a request arrives with a domain you have not configured — or just the bare IP — Nginx serves the default server for that port. Without one, it picks the first site alphabetically, so a random domain pointed at your IP would show one of your customers’ sites. That is bad for SEO (duplicate content under foreign domains) and for privacy.
# /etc/nginx/sites-available/_default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444; # close the connection without a response
}
For HTTPS, add a matching default with a self-signed certificate, or on Nginx 1.19.4+ use ssl_reject_handshake on;:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_reject_handshake on;
}
Isolate sites from each other
By default every PHP site runs as www-data. If one site is hacked through an outdated plugin, the attacker can read every other site’s files, including database passwords in their config files. Give each site its own PHP-FPM pool and system user:
sudo adduser --system --group --home /var/www/site-a.com site-a
sudo chown -R site-a:site-a /var/www/site-a.com
sudo chmod 750 /var/www/site-a.com
sudo usermod -aG site-a www-data # Nginx can read static files
/etc/php/8.3/fpm/pool.d/site-a.conf:
[site-a]
user = site-a
group = site-a
listen = /run/php/site-a.sock
listen.owner = www-data
listen.group = www-data
pm = ondemand
pm.max_children = 6
pm.process_idle_timeout = 20s
pm.max_requests = 500
php_admin_value[open_basedir] = /var/www/site-a.com:/tmp
php_admin_value[upload_tmp_dir] = /var/www/site-a.com/tmp
php_admin_value[disable_functions] = exec,passthru,shell_exec,system,proc_open,popen
open_basedir stops PHP scripts from reading files outside the site’s directory. Remove functions from disable_functions if an application genuinely needs them.
Restart PHP-FPM: sudo systemctl restart php8.3-fpm. Each site now has its own socket, user and limits.
SSL for every domain
Request certificates per site:
sudo certbot --nginx -d site-a.com -d www.site-a.com --redirect
sudo certbot --nginx -d site-b.com -d www.site-b.com --redirect
All certificates renew automatically with one timer. List them with sudo certbot certificates. More in Nginx and Let’s Encrypt.
Different IP addresses per site
Many sites can share one IP. Separate IPs are useful when you want sites not to appear related, for SEO hosting, or for separate reputations (for example mail). After adding IPs to the server, bind each server block to its address:
server {
listen 203.0.113.11:80;
listen 203.0.113.11:443 ssl;
server_name site-b.com www.site-b.com;
...
}
If any block listens on a specific IP, give the default server blocks the same addresses too, or requests to that IP for unknown domains may be answered by the wrong site. Check what is listening where with sudo ss -tlnp | grep nginx.
Servers designed for many sites with many IPs are described in SEO hosting.
Static sites
Static sites (Hugo, Astro, plain HTML) need no PHP:
server {
listen 80;
server_name docs.site-a.com;
root /var/www/docs.site-a.com/public;
location / { try_files $uri $uri/ $uri.html =404; }
include snippets/site-common.conf;
}
Planning resources
| Resource | What uses it | Watch |
|---|---|---|
| RAM | PHP-FPM workers × sites, database buffer pool | Sum of all pools’ max_children × ~60 MB |
| CPU | PHP execution, TLS, compression | Load average vs cores |
| Disk | Sites, uploads, logs, backups | df -h; log rotation |
| Database connections | All sites share one MariaDB | max_connections |
pm = ondemand for low-traffic sites keeps memory usage low; use dynamic for busy ones. Add full-page caching so most requests do not need PHP at all.
Logs and rotation
Per-site logs make troubleshooting easy. Debian and Ubuntu’s logrotate configuration for Nginx (/etc/logrotate.d/nginx) covers /var/log/nginx/*.log automatically. If you write logs inside site directories, add a logrotate rule for them.
Adding a new site: checklist
- DNS A record → server IP.
- Directory, system user, PHP pool.
- Server block from the template;
nginx -t; reload. certbot --nginx -d domain -d www.domain.- Database and user, if needed — see MariaDB.
- Add to backups.