AWS环境下WordPress多站点服务器横向扩展方案咨询
Hey there! Let's walk through exactly how to scale your WordPress Multisite setup horizontally by adding a second identical server. I've handled this scenario a few times, so here's a step-by-step breakdown that covers all critical pieces:
First, make sure you've got the basics covered to avoid mismatches down the line:
- Ensure both servers are identical: same OS version, PHP version, web server (Apache/Nginx), WordPress core version, and all installed plugins/themes.
- Full backup of your existing server: export the entire WordPress Multisite database, copy the
wp-contentdirectory (uploads, themes, plugins), and save config files likewp-config.phpand.htaccess.
Since Multisite relies on shared media, themes, and plugins, you can't have separate wp-content folders on each server:
- Deploy a shared storage solution (NFS or SMB work well) and migrate your existing
wp-contentdirectory to this shared location. - Mount the shared storage to
/var/www/html/wp-content(or your WordPress root'swp-contentpath) on both servers. - Verify web server permissions: run
sudo chown -R www-data:www-data /var/www/html/wp-contentto ensure the server user can read/write to the shared folder.
WordPress Multisite needs a single shared database to sync sites, users, and settings across servers:
- Export your existing Multisite database:
mysqldump -u [db-username] -p [multisite-db-name] > multisite-backup.sql - Set up a database server (can be a standalone instance or one of your web servers, but standalone is better for scalability) and create a database user that's authorized to access from both web server IPs.
- Import the backup to the shared database on your second server:
mysql -u [db-username] -p [multisite-db-name] < multisite-backup.sql - Update
wp-config.phpon both servers to point to the shared database: ensureDB_HOST,DB_NAME,DB_USER, andDB_PASSWORDare identical, along with all Multisite-specific defines (likeWP_ALLOW_MULTISITE,DOMAIN_CURRENT_SITE, andPATH_CURRENT_SITE).
You need a way to distribute traffic across both servers—here's how to handle it:
- Choose a load balancer: Nginx (as a reverse proxy), HAProxy, or a cloud-native LB (like AWS ALB) all work.
- Configure the LB to route traffic to both web server IPs, and set up health checks (e.g., pinging a static file or
/wp-admin/install.phpto verify server availability). - Fix session consistency: WordPress uses PHP sessions, so you have two options:
- Preferred: Use shared session storage (Redis or Memcached). Install the Redis PHP extension, then add these lines to
wp-config.phpon both servers:define('WP_REDIS_HOST', 'your-redis-server-ip'); define('WP_REDIS_PORT', 6379); define('SESSION_SAVE_HANDLER', 'redis'); define('SESSION_SAVE_PATH', 'tcp://your-redis-server-ip:6379?database=0'); - Sticky sessions (fallback): Configure your LB to route a user's traffic to the same server for their session, but note this reduces load-balancing efficiency.
- Preferred: Use shared session storage (Redis or Memcached). Install the Redis PHP extension, then add these lines to
Make sure both servers handle traffic the same way:
- Copy your existing web server config (e.g., Apache's
/etc/apache2/sites-available/wordpress.confor Nginx's/etc/nginx/sites-available/wordpress.conf) to the second server. - Double-check paths and domain names in the config (since
wp-contentis shared, paths should match exactly). - Restart the web server on both instances:
sudo systemctl restart apache2orsudo systemctl restart nginx.
Don't skip this step—verify everything works as expected:
- Visit your site via the load balancer's domain to confirm it loads correctly.
- Upload a test media file, then check the shared storage to ensure it's accessible from both servers.
- Log into wp-admin, create a test subsite, and verify it appears when accessing the site via either server directly.
- Test failover: Stop one web server and confirm traffic automatically routes to the remaining server without downtime.
To keep both servers aligned long-term:
- Use Git to track and sync config files like
wp-config.phpand web server settings. - Set up an automation tool like Ansible to push configuration changes to both servers at once, eliminating manual updates.
内容的提问来源于stack exchange,提问作者VIJAY THAKUR

