disqus4u

How to host Multiple WordPress Websites using Apache

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

To configure Apache server to host multiple WordPress websites, follow these steps:

Before we proceed, let’s first update our system and it’s always recommended to do it:

 apt-get update && apt-get upgrade

In this article, we’re creating a setup for two WordPress sites we’re calling example1 and example2

Install  LAMP Stack

LAMP stands for Linux, Apache, MySQL, PHP and can be installed once as a stack using the following:

sudo tasksel install lamp-server

Create Your Site Databases and Users

WordPress is a PHP application that takes in and stores dynamic data. This is why we need the Database for things like data storage and handling:

Since we’re setting up two WordPress sites, we will have a database and a database user for each. See the table below:

WordPress sitesDatabaseUsernamePassword
example1.comexample1_wordpressexample1_wpuserpassword1
example2.comexample2_wordpressexample2_wpuserpassword2

From the above table, you will realize each WordPress has a database, a username, and a password.

Log into the database

To create the databases, let’s first log in using the following command:

sudo mysql -u root

Create the Database for example1. site

Run the following command to create the database for the first site:

CREATE DATABASE example1_wordpress;

In this example, the name of the database is example1_wordpress but you can replace it with your own name you prefer.

Now, create the user that can be assigned to this database. Run the following command:

CREATE USER 'example1_wpuser' IDENTIFIED BY 'password1';

Make sure you replace example1_wpuser with your preferred database username and passwod1 with the most secure password.

Create the Database for example2 site

Let’s create the database for the second WordPress site using this command:

CREATE DATABASE example1_wordpress;

Change example1_wordpress with your own preferred name of the database you want to create.

Just like in the first database, we need to create a username and password. Let’s do it by running the following command:

CREATE USER 'example2_wpuser' IDENTIFIED BY 'password2';

Again, replace example2_wpuser and password2 with preferences.

Grant the users privileges for their respective databases

Run the following commands each at a time, replacing the credentials in them with your own you’ve configured earlier.

GRANT ALL PRIVILEGES ON example1_wordpress.* TO 'example1_wpuser';
GRANT ALL PRIVILEGES ON example1_wordpress.* TO 'example2_wpuser';

The database creation process is done and you need to exit. Run the following command:

quit

Create directories

You need to create directories where files for each WordPress site can be placed. These directories have to be created in the following directory path:

/var/www/html

In this article, we’re creating directories named example1.com and example2.com using the following command:

sudo mkdir -p /var/www/html/{example1.com,example2.com}/public_html

Replace example1.com and example2.com with your preferred names.

Let’s also create a src directory to hold the WordPress tarball and files:

sudo mkdir /var/www/html/src/

Download WordPress files

It’s now time to download WordPress tarball into the src folder

cd /var/www/html/src/
sudo wget http://wordpress.org/latest.tar.gz

let’s extract the tarball

sudo tar -zxvf latest.tar.gz

Let’s also make a backup copy of the original WordPress source files, renaming the folder latest.tar.gz to wordpress followed by the date. This could be useful if you install new versions in the future and probably want to revert to a previous release.

sudo mv latest.tar.gz wordpress-`date "+%Y-%m-%d"`.tar.gz

Copy the WordPress files to the directory of each website

Run the following commands

sudo cp -R /var/www/html/src/wordpress/* /var/www/html/example1.com/public_html/
sudo cp -R /var/www/html/src/wordpress/* /var/www/html/example2.com/public_html/

Make sure to replace the names of the website directories you created

Grant Apaache ownerships

You need to let Apache have ownership over WordPress sites’ home directories

sudo chown -R www-data:www-data /var/www/html/{example1.com,example2.com}/

Configure Apache Virtual Hosts

In Apache, each website needs a virtual host with configurations, defining how it behaves

It’s important to note that the virtual host configuration files are placed in the following directory path of the Apache server:

/etc/apache2/sites-available/

Create Virtual Hosts file for example1.com

We’re going to name the virtual Host for the first site as example1.conf. Let’s use the nano text editor to create it:

nano /etc/apache2/sites-available/example1.conf

Inside this file, copy and paste the following code:

<VirtualHost *:80>
    # The primary domain for this host
    ServerName example1.com
    # Optionally have other subdomains also managed by this Virtual Host
    ServerAlias example1.com *.example1.com
    DocumentRoot /var/www/html/example1.com/public_html
    <Directory /var/www/html/example1.com/public_html>
        Require all granted
        # Allow local .htaccess to override Apache configuration settings
        AllowOverride all
    </Directory>
    # Enable RewriteEngine
    RewriteEngine on
    RewriteOptions inherit

    # Block .svn, .git
    RewriteRule \.(svn|git)(/)?$ - [F]

    # Catchall redirect to www.example1.com
    RewriteCond %{HTTP_HOST}   !^www.example1\.com [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         https://www.example1.com/$1 [L,R]

    # Recommended: XSS protection
    <IfModule mod_headers.c>
        Header set X-XSS-Protection "1; mode=block"
        Header always append X-Frame-Options SAMEORIGIN
    </IfModule>
</VirtualHost>

    

Create Virtual Hosts file for example2.com

Similarly, we need to create the virtual host configuration file for the second site, example2.com. We’re calling this file, example2.conf

nano /etc/apache2/sites-available/example2.conf

Replace the example2.conf with your preferred file name for this Virtual Host

Just like we did in the first file, lets copy and paste the following code into our file:

<VirtualHost *:80>
  # The primary domain for this host
  ServerName example2.com
  # Optionally have other subdomains also managed by this Virtual Host
  ServerAlias example2.com *.example2.com
  DocumentRoot /var/www/html/example2.com/public_html
  <Directory /var/www/html/example2.com/public_html>
      Require all granted
      # Allow local .htaccess to override Apache configuration settings
      AllowOverride all
  </Directory>
  # Enable RewriteEngine
  RewriteEngine on
  RewriteOptions inherit

  # Block .svn, .git
  RewriteRule \.(svn|git)(/)?$ - [F]

  # Catchall redirect to www.example2.com
  RewriteCond %{HTTP_HOST}   !^www.example2\.com [NC]
  RewriteCond %{HTTP_HOST}   !^$
  RewriteRule ^/(.*)         https://www.example2.com/$1 [L,R]

  # Recommended: XSS protection
  <IfModule mod_headers.c>
      Header set X-XSS-Protection "1; mode=block"
      Header always append X-Frame-Options SAMEORIGIN
  </IfModule>
</VirtualHost>
    

Customize the directives

Now that we created the virtual hosts, let’s customize certain directives as it’s crucial while accessing our sites;

ServerName

In each Virtual host file you’ve created, look for the ServName directive and change the assigned flag

For example, for the first site, you need to change example1.com with the actual domain you want to assign your WordPress site.

   ServerName example1.com

For the second site, go to its Virtual Host file and find the ServerName directive. Replace example2.com with the actual domain name you want to use

   ServerName example2.com

DocumentRoot & Directory Paths

Inside the Virtual Host files of all your sites, you need to find both the DocumentRoot and Directory directives so you can replace the paths to website public_html directories. This is the path to where your WordPress files are located on the server

DocumentRoot /var/www/html/example1.com/public_html
<Directory /var/www/html/example1.com/public_html>

Rewrite Directives

These are used for redirection URL rewrites. Open the Virtual Host file for each website and find the following:

 # Catchall redirect to www.example.com
 
 # Replace with www.example\.com  your with your own domain
  RewriteCond %{HTTP_HOST}   !^www.example\.com [NC]
  RewriteCond %{HTTP_HOST}   !^$
  
  # This rule will redirect the www.example.com domain version to the https://example.com
  RewriteRule ^/(.*)         https://example.com/$1 [L,R] #

Make sure you replace the domain name in this directive with your domains per virtual host.

Enable the Virtual Host files

We need to enable the Virtual host files so Apache can recognize them using the following commands:

 sudo a2ensite example1.conf
 sudo a2ensite example2.conf

Since in this example we have two websites, we only have two virtual host files. But for your case if you have more websites, then you have to make sure you enable all of them.

Enable the Rewrite Module

This module helps us to redirect our domains from let’s www.example.com to https:example.com. This is actually what we’ve implemented in these directives inside the Virtual Host files.

 # Catchall redirect to www.example.com
 
 # Replace with www.example\.com  your with your own domain
  RewriteCond %{HTTP_HOST}   !^www.example\.com [NC]
  RewriteCond %{HTTP_HOST}   !^$
  
  # This rule will redirect the www.example.com domain version to the https://example.com
  RewriteRule ^/(.*)         https://example.com/$1 [L,R] #

This rewrite redirection is needed especially if you’ve installed SSL and you want to make sure your visitors only access the https version of the website domains.

So to enable the rewrite module, run the following command:

 sudo a2enmod rewrite

Reload apache for the configurations you set up to reflect. Here is the command to use:

 sudo systemctl reload apache2

Configure WordPress installation

In this step, you need to get the IP address or the domain names you allocated to each of your sites and open them inside the browser to start the process of installing WordPress.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed. 

Further Reading