Deploy multiple WordPress instances using a single Apache virtual host while serving them under different subdirectories (e.g., site.com/site1 and site.com/site2), this approach will show you how to achieve that. Each WordPress instance will have its own database, but share the same web root and server settings.
How to Deploy Multiple WordPress Instances on the Same Apache Virtual Host
This guide explains how to deploy multiple WordPress instances under a single domain using subdirectories. Each WordPress instance will have its own MySQL database and run under a single Apache virtual host, simplifying the setup while keeping the sites separate.
Prerequisites
- Ubuntu server with sudo access.
- A domain (e.g.,
site.com) and desired subdirectory structure (e.g.,site.com/site1,site.com/site2). - Familiarity with Linux commands.
Step 1: Update Your System
First, ensure your system is up-to-date:
sudo apt update
sudo apt upgrade
Step 2: Install Apache2
If Apache2 is not installed, install it with:
sudo apt install apache2
Start and enable Apache2:
sudo systemctl start apache2
sudo systemctl enable apache2
Step 3: Install MySQL
Install MySQL to handle the databases for each WordPress instance:
sudo apt install mysql-server
Secure the MySQL installation:
sudo mysql_secure_installation
Then, log in to MySQL:
sudo mysql
Create separate databases and users for each WordPress instance:
CREATE DATABASE wp_site1;
CREATE USER 'wp_user1'@'localhost' IDENTIFIED BY 'password1';
GRANT ALL PRIVILEGES ON wp_site1.* TO 'wp_user1'@'localhost';
FLUSH PRIVILEGES;
CREATE DATABASE wp_site2;
CREATE USER 'wp_user2'@'localhost' IDENTIFIED BY 'password2';
GRANT ALL PRIVILEGES ON wp_site2.* TO 'wp_user2'@'localhost';
FLUSH PRIVILEGES;
EXIT;
This creates two separate databases (wp_site1 and wp_site2) and users for each WordPress instance.
Step 4: Install PHP 8.3
Next, install PHP 8.3 and the necessary extensions for WordPress:
sudo apt install php8.3 libapache2-mod-php8.3 php8.3-mysql php8.3-xml php8.3-gd php8.3-curl php8.3-mbstring php8.3-zip php8.3-intl
Confirm the PHP installation:
php -v
Step 5: Configure Apache for a Single Virtual Host
Create a Single Virtual Host for Your Domain
Edit or create an Apache virtual host file for the main domain (e.g., site.com):
sudo nano /etc/apache2/sites-available/site.com.conf
Add the following configuration (replace site.com with your actual domain):
<VirtualHost *:80>
ServerAdmin admin@site.com
ServerName site.com
DocumentRoot /var/www/site.com
<Directory /var/www/site.com>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/site.com_error.log
CustomLog ${APACHE_LOG_DIR}/site.com_access.log combined
</VirtualHost>
Enable the Virtual Host
Enable the new site configuration and disable the default site:
sudo a2ensite site.com.conf
sudo a2dissite 000-default.conf
Reload Apache to apply the changes:
sudo systemctl reload apache2
Step 6: Set Up Directory Structure for Multiple WordPress Sites
Now, create subdirectories for each WordPress instance under /var/www/site.com:
sudo mkdir -p /var/www/site.com/site1
sudo mkdir -p /var/www/site.com/site2
Step 7: Download and Install WordPress
Download WordPress
Download and extract WordPress in /tmp:
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
Copy WordPress to Each Subdirectory
For site1:
sudo cp -r wordpress/* /var/www/site.com/site1/
For site2:
sudo cp -r wordpress/* /var/www/site.com/site2/
Assign the correct permissions:
sudo chown -R www-data:www-data /var/www/site.com/site1
sudo chown -R www-data:www-data /var/www/site.com/site2
sudo chmod -R 755 /var/www/site.com/site1
sudo chmod -R 755 /var/www/site.com/site2
Step 8: Configure WordPress Instances
Each WordPress instance needs to be linked to its respective database.
Configure wp-config.php for Site 1
Navigate to the site1 directory:
cd /var/www/site.com/site1
sudo cp wp-config-sample.php wp-config.php
Edit the wp-config.php file to match the wp_site1 database settings:
sudo nano wp-config.php
Modify these lines:
define( 'DB_NAME', 'wp_site1' );
define( 'DB_USER', 'wp_user1' );
define( 'DB_PASSWORD', 'password1' );
define( 'DB_HOST', 'localhost' );
Configure wp-config.php for Site 2
Now, configure site2 in the same way:
cd /var/www/site.com/site2
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
Modify the database settings:
define( 'DB_NAME', 'wp_site2' );
define( 'DB_USER', 'wp_user2' );
define( 'DB_PASSWORD', 'password2' );
define( 'DB_HOST', 'localhost' );
Step 9: Enable Apache Rewrite Module
Enable the rewrite module to allow WordPress permalinks to function correctly:
sudo a2enmod rewrite
sudo systemctl restart apache2
Ensure that AllowOverride All is included in the virtual host configuration (already done in Step 5).
Step 10: Complete WordPress Setup in the Browser
Open your web browser and go to each of the WordPress setup pages:
- Site 1:
http://site.com/site1 - Site 2:
http://site.com/site2
Follow the WordPress setup wizard to complete the installation for each site by providing the appropriate database credentials and site information.
Step 11: Optional – Use HTTPS with Let’s Encrypt
It’s a good idea to secure your site with HTTPS. You can use Let’s Encrypt to generate SSL certificates.
First, install Certbot:
sudo apt install certbot python3-certbot-apache
Run Certbot for your domain:
sudo certbot --apache -d site.com
Certbot will automatically configure SSL for the domain. After running the commands, reload Apache:
sudo systemctl reload apache2
Conclusion
You’ve successfully deployed multiple WordPress sites on a single domain using subdirectories. Each site is fully isolated with its own database, but shares the same Apache virtual host. This setup is ideal when you want to manage multiple sites under the same domain without the complexity of multiple virtual hosts.
Commands
Here’s a summary of all the command-line steps to deploy multiple WordPress sites under a single Apache virtual host:
1. Update System
sudo apt update
sudo apt upgrade
2. Install Apache2
sudo apt install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
3. Install MySQL
sudo apt install mysql-server
sudo mysql_secure_installation
Create Databases and Users for WordPress Sites
sudo mysql
CREATE DATABASE wp_site1;
CREATE USER 'wp_user1'@'localhost' IDENTIFIED BY 'password1';
GRANT ALL PRIVILEGES ON wp_site1.* TO 'wp_user1'@'localhost';
FLUSH PRIVILEGES;
CREATE DATABASE wp_site2;
CREATE USER 'wp_user2'@'localhost' IDENTIFIED BY 'password2';
GRANT ALL PRIVILEGES ON wp_site2.* TO 'wp_user2'@'localhost';
FLUSH PRIVILEGES;
EXIT;
4. Install PHP 8.3 and Required Extensions
sudo apt install php8.3 libapache2-mod-php8.3 php8.3-mysql php8.3-xml php8.3-gd php8.3-curl php8.3-mbstring php8.3-zip php8.3-intl
php -v
5. Configure Apache Virtual Host
sudo nano /etc/apache2/sites-available/site.com.conf
Add the following content:
<VirtualHost *:80>
ServerAdmin admin@site.com
ServerName site.com
DocumentRoot /var/www/site.com
<Directory /var/www/site.com>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/site.com_error.log
CustomLog ${APACHE_LOG_DIR}/site.com_access.log combined
</VirtualHost>
Enable the Virtual Host
sudo a2ensite site.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
6. Create Directory Structure for WordPress Sites
sudo mkdir -p /var/www/site.com/site1
sudo mkdir -p /var/www/site.com/site2
7. Download and Install WordPress
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
Copy WordPress Files to Each Subdirectory
sudo cp -r wordpress/* /var/www/site.com/site1/
sudo cp -r wordpress/* /var/www/site.com/site2/
Set Correct Permissions
sudo chown -R www-data:www-data /var/www/site.com/site1
sudo chown -R www-data:www-data /var/www/site.com/site2
sudo chmod -R 755 /var/www/site.com/site1
sudo chmod -R 755 /var/www/site.com/site2
8. Configure WordPress Instances
Configure wp-config.php for Site 1
cd /var/www/site.com/site1
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
Change database settings:
define( 'DB_NAME', 'wp_site1' );
define( 'DB_USER', 'wp_user1' );
define( 'DB_PASSWORD', 'password1' );
define( 'DB_HOST', 'localhost' );
Configure wp-config.php for Site 2
cd /var/www/site.com/site2
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
Change database settings:
define( 'DB_NAME', 'wp_site2' );
define( 'DB_USER', 'wp_user2' );
define( 'DB_PASSWORD', 'password2' );
define( 'DB_HOST', 'localhost' );
9. Enable Apache Rewrite Module
sudo a2enmod rewrite
sudo systemctl restart apache2
10. Install Let’s Encrypt SSL (Optional)
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d site.com
sudo systemctl reload apache2
You can now open your browser and complete the WordPress installation for both sites:
http://site.com/site1http://site.com/site2

Leave a comment