ThisCalculator

Everyday

Run Two WordPress Sites Under One Domain with Nginx

Updated · Standard formula, shown below

Running two WordPress sites under one domain with Nginx lets you host a primary site and a secondary site (like a blog, shop, or client portal) without buying a second domain or server. You’ll use subdirectories (e.g., example.com/site2) or subdomains (e.g., blog.example.com) with separate WordPress installations. This guide covers both approaches, focusing on the subdirectory method since it’s the most common. You’ll need root access to your server, Nginx installed, and two WordPress installations ready (either in separate directories or databases).

What You’ll Need

  • A server running Nginx (Ubuntu/Debian commands shown, adjust for your distro)
  • Root or sudo SSH access
  • Two WordPress installations (files and databases) – you can create them fresh or use existing ones
  • Optional: a domain name pointed to your server’s IP

Step-by-Step

  1. Set up your WordPress files
    Place your primary site in /var/www/html (or your web root) and the second site in a subdirectory like /var/www/html/site2. Ensure both have correct permissions: sudo chown -R www-data:www-data /var/www/html.
  2. Create separate databases
    Each WordPress needs its own database. Use MySQL or MariaDB: CREATE DATABASE site1_db; CREATE DATABASE site2_db; and create users with privileges for each.
  3. Configure WordPress for the subdirectory
    For the second site, edit /var/www/html/site2/wp-config.php and set:
    define('WP_HOME','http://example.com/site2');
    define('WP_SITEURL','http://example.com/site2');
    If the site was previously at a different URL, you may need to update the siteurl and home options in the database via phpMyAdmin or WP-CLI.
  4. Create an Nginx server block
    Edit your site’s config (e.g., /etc/nginx/sites-available/example.com). Start with a basic server block for the primary site.
  5. Add a location block for the second site
    Inside the server block, add:
    location ^~ /site2/ {
        alias /var/www/html/site2/;
        try_files $uri $uri/ /site2/index.php?$args;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # adjust PHP version
        }
    }

    The ^~ ensures this location takes precedence over regex locations.

  6. Handle static assets correctly
    For CSS/JS/images, WordPress generates URLs like /site2/wp-content/.... Your alias block should serve them correctly. If you see 404s, check that the alias path matches exactly and that try_files includes the @rewrite for pretty permalinks.
  7. Enable pretty permalinks
    In WordPress Settings → Permalinks, choose a non-plain structure. Then add this to your Nginx server block (outside the location block):
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    For the subdirectory, the try_files inside the location block already handles it.

  8. Test and reload Nginx
    Run sudo nginx -t to check syntax, then sudo systemctl reload nginx.
  9. Update WordPress URLs in the database (if needed)
    If the second site was previously on a different domain, run WP-CLI: wp search-replace 'http://old-domain.com' 'http://example.com/site2' --path=/var/www/html/site2 --all-tables or use a database tool.
  10. Test both sites
    Visit example.com and example.com/site2. Check that admin pages load and assets work.

Common Mistakes

  • Using root instead of alias in the subdirectory location – this causes file paths to be wrong.
  • Forgetting to set WP_HOME and WP_SITEURL – the site loads but links break.
  • Not adding the PHP location block inside the subdirectory location – PHP files download or show source.
  • Using the same database for both sites – tables collide and content mixes.
  • Not updating the database URL after moving a site – you get redirect loops or missing assets.

Tips

  • Use a subdomain (e.g., blog.example.com) if you want cleaner separation – it’s easier to manage and avoids path conflicts.
  • Keep each WordPress installation in its own directory with its own database and user – this simplifies backups and security.
  • If you use a security plugin that modifies .htaccess, note that Nginx ignores it – you must replicate rules in Nginx config.
  • For SSL, use Let’s Encrypt with certbot – it can handle multiple domains and subdirectories, but ensure your server block listens on 443 and redirects HTTP.
  • Test with a staging copy before applying to production.

Troubleshooting

Site2 returns 404 on all pages except home
Your try_files directive is likely missing the /site2/index.php fallback. Ensure it matches the example above.

CSS/JS not loading
Check the browser console for 404s. The alias path must exactly match the directory. Also verify that WP_HOME and WP_SITEURL are set correctly.

PHP files download instead of executing
Your location block for PHP is missing or not inside the subdirectory location. Add the location ~ \.php$ block as shown.

Redirect loop when visiting site2
This usually means the database still has the old URL. Run the search-replace command or update the siteurl and home options directly.

Admin panel of site2 shows “connection refused” or 500
Check PHP-FPM socket path and that the user has permission to read the files. Also verify the database credentials in wp-config.php.

Primary site breaks after adding subdirectory
Ensure your primary site’s location / block does not interfere. The ^~ /site2/ location should take precedence, but if you have regex locations, they may override – use ^~ to prevent that.

Frequently Asked Questions

Can I use the same database for both sites?

Technically yes, but you’d need to change the table prefix for one site (e.g., wp2_). It’s not recommended because updates and backups become messy, and a security issue on one site could expose the other.

Will this work with WordPress Multisite?

Multisite is a different setup that allows multiple sites from one installation. This guide is for separate installations. If you want subdirectory-based sites, Multisite might be easier, but it has its own configuration.

How do I set up a subdomain instead?

Create a separate server block for blog.example.com with its own root pointing to the second WordPress directory. You’ll also need to set WP_HOME and WP_SITEURL to http://blog.example.com.

Do I need to change anything in WordPress for the subdirectory to work?

Yes, you must set WP_HOME and WP_SITEURL in wp-config.php to include the subdirectory path. Also, if you change the URL after installation, update the database.

Why does my subdirectory site show a 404 for pretty permalinks?

Your try_files directive in the subdirectory location is probably missing the fallback to /site2/index.php. Add it as shown in step 5.

Can I run one site at the root and another in a subdirectory with different PHP versions?

Yes, you can specify different fastcgi_pass sockets in each location block, as long as you have multiple PHP-FPM versions installed and configured.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.