This guide shows you how to host several WordPress installations in subdirectories (like example.com/site1, example.com/site2) on a single Nginx server. You’ll end up with a clean, maintainable setup where each site has its own files, database, and configuration, without interfering with the others. Before starting, you need root or sudo access to your server, a working Nginx installation, and PHP-FPM configured.
What You’ll Need
- A Linux server (Ubuntu/Debian recommended) with Nginx installed.
- PHP-FPM installed and running (version 7.4 or newer).
- MySQL or MariaDB database server.
- WordPress package downloaded (from wordpress.org).
- Domain name pointed to your server’s IP.
- SSH access with sudo privileges.
Step-by-Step
- Create directory structure
Create a parent directory for all subdirectory sites, e.g.,/var/www/html. Inside it, create a folder for each site:mkdir -p /var/www/html/site1andmkdir -p /var/www/html/site2. - Download and extract WordPress
For each site, download WordPress into its folder:cd /var/www/html/site1 && wget https://wordpress.org/latest.tar.gz && tar -xzf latest.tar.gz --strip-components=1 && rm latest.tar.gz
Repeat for site2. - Set file permissions
Ensure the web server user (usuallywww-data) owns the files:chown -R www-data:www-data /var/www/html/site1 /var/www/html/site2 - Create databases and users
Log into MySQL:mysql -u root -p. Then for each site:CREATE DATABASE site1_db; CREATE USER 'site1_user'@'localhost' IDENTIFIED BY 'strong_password'; GRANT ALL PRIVILEGES ON site1_db.* TO 'site1_user'@'localhost'; FLUSH PRIVILEGES;
Repeat for site2 with different names.
- Configure Nginx server block
Edit your main site’s Nginx config (e.g.,/etc/nginx/sites-available/example.com). You’ll add location blocks for each subdirectory. Start with the base server block: - Add location blocks for WordPress subdirectories
Inside theserver { }block, add:location ^~ /site1 { alias /var/www/html/site1; try_files $uri $uri/ /site1/index.php?$args; location ~ \.php$ { if (!-f $request_filename) { return 404; } include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; } }Repeat for
/site2with the appropriate paths. Note: usingaliaswith WordPress requires careful handling oftry_filesto avoid 404 errors. - Adjust WordPress configuration for subdirectory
In each site’swp-config.php, add the following lines before the “stop editing” comment:define('WP_HOME', 'http://example.com/site1'); define('WP_SITEURL', 'http://example.com/site1');This ensures WordPress generates correct URLs.
- Set up permalinks
After installation, go to Settings → Permalinks in each WordPress admin and choose a structure (e.g., “Post name”). This updates the.htaccessequivalent for Nginx—no file needed, but you must add a rewrite rule in the location block:try_files $uri $uri/ /site1/index.php?$args;(already added). - Test and reload Nginx
Runnginx -tto check config syntax. If OK, reload:systemctl reload nginx. - Install WordPress
Visithttp://example.com/site1/wp-admin/install.phpand follow the installer. Enter the database details you created. Repeat for site2.
Common Mistakes
- Using
rootinstead ofalias– Therootdirective appends the URI to the path, so/site1would look for files in/var/www/html/site1/site1. Always usealiasfor subdirectory WordPress. - Incorrect
try_filespath – Iftry_filespoints to/index.phpinstead of/site1/index.php, WordPress will load the wrong site or return 404. - Missing
WP_HOME/WP_SITEURL– Without these, WordPress will redirect to the root domain or break CSS/JS. - PHP location block inside alias – The regex location for PHP must be nested inside the alias location, and you must check that the file exists before passing to PHP-FPM.
- Forgetting to reload Nginx – Changes won’t take effect until you reload.
Tips
- Use a separate PHP-FPM pool for each site to isolate resources and improve security.
- Enable caching plugins (like W3 Total Cache) but configure them for Nginx, not Apache.
- Keep each site’s
wp-config.phpwith unique database credentials—don’t share a single database across sites. - For HTTPS, use Let’s Encrypt with a wildcard certificate or a multi-domain cert; Nginx will handle subdirectory paths fine.
- Back up each site’s directory and database separately.
Troubleshooting
- 404 errors on subdirectory – Check your
try_filesline. It must include the subdirectory prefix. Also ensure the alias path is correct and that the directory exists. - WordPress redirects to root domain – This usually means
WP_HOMEandWP_SITEURLare not set correctly. Double-check they are defined inwp-config.php. - CSS/JS broken – Often due to incorrect
WP_HOMEor because the site URL is still set to the root. Re-save permalinks after fixing the constants. - “No input file specified” – This indicates PHP-FPM can’t find the script. Ensure your
fastcgi_param SCRIPT_FILENAMEuses$request_filename(which works with alias) and that the file exists. - Admin panel loads but front page 404s – Go to Settings → Permalinks and click “Save Changes” to flush rewrite rules.
- Nginx won’t start – Run
nginx -tto see the exact error. Common issues: missing semicolons, duplicate location blocks, or invalid regex.
Frequently Asked Questions
Can I run WordPress in a subdirectory and also have a separate site at the root?
Yes, you can. Just add a separate location block for the root (e.g., location / { ... }) that points to another WordPress installation or a static site. Ensure the subdirectory locations are more specific and use ^~ to avoid conflicts.
Do I need to install WordPress separately for each subdirectory?
Yes, each subdirectory should have its own complete WordPress installation (files and database). This gives full independence—updates, plugins, and themes won’t affect other sites.
Will this work with a single SSL certificate?
Yes, if you have a wildcard certificate (*.example.com) or a certificate covering the domain, subdirectories will work fine because they share the same domain. If you use subdomains, you’d need separate certs.
How do I update WordPress in a subdirectory?
You can update each site from its own admin dashboard, just like a standalone site. For core updates, WordPress will ask for FTP credentials—make sure the web server user has write permissions or set FS_METHOD to direct in wp-config.php.
Can I use a plugin like WP Multisite instead?
Yes, WordPress Multisite is an alternative that uses a single installation for multiple sites. However, subdirectory installations are simpler to manage if you need separate databases or want to isolate failures.
What if I want to move a subdirectory site to its own domain later?
You can export the site using a plugin like Duplicator, or manually move files and database, then update the site URL in wp-config.php and the database. The subdirectory setup doesn’t prevent moving.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Use Mac More Efficiently: 21 Proven Tips
- Best CPUs for Gaming and Streaming: Encoder Guide
- How to Compare AI Features and Performance in Phones
- Nginx Blocks in CentOS: Config Guide
- Create Google Meet Account: Step-by-Step Guide
- AI Frameworks: How Developers Choose and Ship Them