keepcalculator

06 Numbers and Powers

Run Multiple WordPress Sites in Subdirectories with Nginx

Card No. 06·72261 · Updated · Standard formula, shown below

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

  1. 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/site1 and mkdir -p /var/www/html/site2.
  2. 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.
  3. Set file permissions
    Ensure the web server user (usually www-data) owns the files:
    chown -R www-data:www-data /var/www/html/site1 /var/www/html/site2
  4. 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.

  5. 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:
  6. Add location blocks for WordPress subdirectories
    Inside the server { } 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 /site2 with the appropriate paths. Note: using alias with WordPress requires careful handling of try_files to avoid 404 errors.

  7. Adjust WordPress configuration for subdirectory
    In each site’s wp-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.

  8. Set up permalinks
    After installation, go to Settings → Permalinks in each WordPress admin and choose a structure (e.g., “Post name”). This updates the .htaccess equivalent 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).
  9. Test and reload Nginx
    Run nginx -t to check config syntax. If OK, reload: systemctl reload nginx.
  10. Install WordPress
    Visit http://example.com/site1/wp-admin/install.php and follow the installer. Enter the database details you created. Repeat for site2.

Common Mistakes

  • Using root instead of alias – The root directive appends the URI to the path, so /site1 would look for files in /var/www/html/site1/site1. Always use alias for subdirectory WordPress.
  • Incorrect try_files path – If try_files points to /index.php instead 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.php with 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_files line. 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_HOME and WP_SITEURL are not set correctly. Double-check they are defined in wp-config.php.
  • CSS/JS broken – Often due to incorrect WP_HOME or 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_FILENAME uses $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 -t to 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

The whole Numbers and Powers drawer →