Setting up Multiple Domain WordPress Websites on a Single Apache Installation

I published an article back in May of 2020 on how to go about setting up a domain WordPress Website on a Raspberry Pi, model 3B+ and self-hosting that Website. While this is not as difficult a task as one might imagine, being able to self-host that WordPress Website on an SBC from your home can save you a ton of money if you know how to do it. I encourage you to go to that article and look at how easy it is to accomplish this and consider doing it yourself. You’ll have to do all the “heavy lifting”; that is to say, installing the operating system on the Raspberry Pi, purchasing a Domain from a Domain Registrar, configuring the DNS for your domain so that it knows your ISP-provided IP address, registering your Domain through ICANN, installing the Apache2 Web server, ensuring PHP is installed and available to Apache2, installing and configuring a MySQL database for WordPress, installing WordPress (latest version), linking the MySQL database to your instance of WordPress, and configuring Apache2 to access your Website. These are things that a Webhosting company will do for you if you pay them the big bucks on a monthly or yearly basis. But, doing it yourself can be both satisfying and cost-effective.

After completing the process outlined in my earlier article, you should have a working WordPress Website you can be proud of. The Website you’re accessing this article from is a self-hosted WordPress Website set up using those directions. But, did you know that you can host multiple WordPress Websites from a single installation of the Apache2 Web server so that you don’t have to install multiple versions of Apache2 running on more than one Raspberry Pi? In fact, all of your Domain Websites can be hosted from the same WAN IP and run locally on a single Raspberry Pi 3B+ (1GB of RAM) or 4 (1GB or 4GB of RAM) SBC very easily. Here’s how this can be accomplished.

I already have two domain Websites being hosted on Apache2 Web server running on my Raspberry Pi, model 3B+ (1GB) with 256GB of available storage via a MicroSD card. These domains are (1) datapioneer-network.org and (2) danlcalloway.com. Each of these Websites has its own MySQL database and these databases are connected to their own installation of WordPress so they can function as Content Management System (CMS) Websites. So, If I ping both of these Websites, you can see that they are valid sites that are reachable via the W3.

Pinging Two Domain Websites in Linux

This means that they are functioning normally and everything is configured properly. Also, as noted in the image above, both Websites resolve to the identical IP address of 71.81.208.130, an IP address provided to me by my ISP, Spectrum High-Speed Internet. So, rather than show you how I set up these two Websites as WordPress Websites using the same instance of Apache2, I’m going to demonstrate how to go about setting up a third Website on the same instance of Apache2. This Website’s Domain is dancalloway.com (not to be confused with danlcalloway.com), which I recently purchased through Google Domains, my Domain Registrar.

What follows is the sequence of steps that you will need to take to add a 2nd or, in the case of this demonstration, a 3rd domain Website to the same installation of Apache2 Web Server.

Purchase a Domain and Register DNS

To begin the process of adding a 3rd domain WordPress Website running in Apache2 on my SBC, I had to go out and purchase the Domain Name. This Domain Name was dancalloway.com. You don’t have to use Google Domains as your Domain Registrar, but I chose them since they are very easy to buy a domain and register it. The website can be found at googledomains.com. After purchasing the new domain, I registered the DNS for it by clicking on Manage to the right of the Domain Name under My Domains and then clicking DNS. This opens a new screen that is shown below:

Registering a new Domain in Google Domains

As shown above, you need to enter the Domain Name under the Hostname column, enter “A” under the Type column, and finally enter the IP address provided by your ISP under the Data column. This will point your new domain to that IP address. Be patient as this process might take a few minutes to an hour or more to occur.

SSH Into Your Raspberry Pi and cd to the /var/www Directory

After running the command below in my local Desktop PC’s Linux terminal:

$ ssh pi@192.168.1.90

Then, enter your Raspberry Pi user’s password and you should connect to your Raspberry Pi as that user. You’ll need to be root when you get into setting up the MySQL database, so let’s go ahead and switch to the root user on the Pi. If you’re like me, you may not have set up a root user yet on the Raspberry Pi. Let’s take care of that first. To accomplish this, run the following command in the Linux terminal on the Pi:

$ sudo passwd root

You’ll be prompted to enter a new password for root, then you’ll need to repeat that password and after hitting the Enter key, you’ll be notified that the password has been set and you’re good to go. Now, switch to the root user on the Pi by entering this command in the terminal:

$ su

You’ll be prompted to enter the root‘s password. After entering the root password, you’ll receive an elevated user prompt and will be logged in as root:

root@rpi3Bplus:#

Now, cd to the /var/www directory on the Pi and you’ll see that I have two WordPress accounts already set up: WordPress setup in the root of this directory and another site I named site2. Within this site2 directory is the WordPress sub-directory and all the files that WordPress uses are contained within that sub-directory. We want to add a third WordPress Website, so we need to create another sub-directory we’ll call site3 under /var/www. To do this, cd to /var/www and then let’s run the following command:

root@rpi3Bplus:/var/www# mkdir site3

If we cd back into the site2 directory we’ll see a wordpress directory and if we cd into that directory and list its contents, what we will see is the following:

Contents of /var/www/site2/wordpress

What we see are the files that WordPress uses to create and run our site2 WordPress Website with the assistance of the Apache2 Web Server, PHP, and the MySQL database associated with it. So, how do we populate our site3 directory with similar content for our 3rd WordPress Website? Let’s do that next.

Installing WordPress Into /var/www/site3

We want to get the latest version of WordPress into our /var/www/site3 directory since it is currently empty. To accomplish this, we need to run the following command:

root@rpi3Bplus:/var/www/site3# wget https://wordpress.org/latest.tar.gz

After hitting the Enter key, the latest version of WordPress will be downloaded into our /var/www/site3 directory as a tar.gz file. Next, we will want to unarchive that file so the contents are unpacked under the site3 directory. To accomplish this, we run the following command:

root@rpi3Bplus://var/www/site3# tar xvzf latest.tar.gz

Once the latest.tar.gz file has been unpacked, we will have the following structure when we list out the contents of our site3/wordpress directory:

Contents of the /var/www/site3/wordpress Directory

Now that we have installed our WordPress files into the 3rd site directory located at /var/www/site3, let’s redirect our attention to setting up the MySQL database for our WordPress Website. First, since we are logged in as root on our Pi, we will not have to worry about a username or password for our MySQL database. So, as root, let’s fire up a MySQL database by running the command:

root@rpi3Bplus:/var/www/site3/wordpress# mysql

The MariaDB server prompt will appear and to start the database process, we will need to run the following command:

MariaDB [(none)]> create database wp_site3 default character set utf8 collate utf8_unicode_ci;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on wp_site3.* to 'wp_site3_user'@'localhost' identified by 'unsecurepw';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
bye

As shown above, we accessed the MariaDB server and ran a command which created the database for wp_site3 with the default character set of utf8, then we granted all privileges to wp_site3 and all tables within wp_site3 (.*) to the wp_site3_user at localhost (not from a remote location), and identified the unsecured password for this database, which is unsecurepw. Finally, we flushed the privileges for the database and exited it. This is all that we need to do in creating and configuring the MySQL database for this 3rd site, wp_site3. So, we now have an empty database for our WordPress 3rd site ready to go and ready to hook up to Apache2 so that it can be accessed and function as a CMS application. Now, we need to hook up this database to WordPress in the next step.

Connecting the Empty MySQL Database to WordPress

To accomplish this, let’s first cd into /var/www/site3/wordpress and locate the file: wp-config-sample.php. After locating it, let’s copy that file to a new file called wp-config.php to make our wp-config file for WordPress. Next, we need to open that file and make some changes. To open the file for editing, I’m going to use the nano editor. You can use vim, nano, or any file editor you wish, but nano is available on most systems so that is what I’m going to use. The command to run is shown below:

root@rpi3Bplus:/var/www/site3/wordpress# nano wp-config.php

When we open the file, we see the contents of this file which is identical to the contents of the wp-config-sample.php since we copied it to the file we created. Once open, we will need to change the values on a few sections. We need to scroll down to the ‘DB_Name’ section and enter ‘wp_site3’ in place of what you see there, then scroll down to ‘DB_User’ and enter ‘wp_site3_user’ in place of what you see there, and finally, scroll down to ‘DB_PASSWORD’ and change this entry to ‘unsecurepw’ for the contents of that field.

Contents of /var/www/site3/wordpress/wp-config.php

Now, scroll down to the SALT section of this file which contains 8 lines that need to be reconfigured. You need to copy the link address of https://api.wordpress.org/secret-key/1.1/salt/ a few lines above the SALT section and enter that into your Web browser. Once you go to the link you placed in the Address Bar, you’ll be taken out to this site containing the replacement for these 8 lines in our wp-config.php file:

Replacement SALT Section for wp-config.php

Return to the wp-config.php file after copying the 8 lines from the website, remove the 8 lines in the SALT section and paste them with the 8 lines that were copied from the website we visited from the site link above. Save this file. We have now effectively linked the MySQL database we created earlier to the WordPress install. Next, we need to turn our attention to the Apache2 Web Server installed on our Raspberry Pi so we can complete setting up our 3rd WordPress Website.

Configuring Apache2 For Our New WordPress Website

In order to configure Apache2 to work with our site3 WordPress Website, we need to change directories into Apache2 as follows:

root@rpi3Bplus:/var/www/site3/wordpress# cd /etc/apache2

root@rpi3Bplus:/etc/apache2# ls
apache2.conf  conf-available  conf-enabled  envvars  magic  mods-available  mods-enabled  ports.conf  ports.conf.backup  sites-available  sites-enabled

Here, you will see two directories: sites-available, and sites-enabled. If we cd into the sites-available directory and list out its contents, we see the following:

root@rpi3Bplus:/etc/apache2/sites-available# ls
000-default.conf  000-default-le-ssl.conf  default-ssl.conf  site2.conf

The 000-default.conf file is the configuration file for a default site, which in my case belongs to datapioneer-network.org. The site2.conf file is the configuration file for my 2nd WordPress domain Website of danlcalloway.com. What we desire to do is create a 3rd configuration file we will call site3.conf which will be the configuration file for our 3rd WordPress Website. To accomplish this we can open the site2.conf file first to look at the contents. This is its contents:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerName danlcalloway.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/site2/wordpress/

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Note that the ServerName is the Domain Website itself and the DocumentRoot (where Apache2 looks for this Website) is /var/www/site2/wordpress/. So, when requests for danlcalloway.com come into my LAN to the Raspberry Pi’s IP address to serve up the Website to a site visitor, Apache2 will go to the DocumentRoot location for that Website and serve up the Webpage(s) requested. The idea here is that we need to copy site2.conf to a file we call site3.conf, then reconfigure it changing the Domain URL and DocumentRoot location which corresponds to dancalloway.com (our 3rd Website, site3). Therefore, use the cp command to copy site2.conf to site3.conf and then open site3.conf for editing. This looks like the following:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerName dancalloway.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/site3/wordpress/

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

We then modify this file changing the ServerName to dancalloway.com and the DocumentRoot to /var/www/site3/wordpress/. Now, Apache2 will listen on port 80 all interfaces (*:80) for requests for our new Domain Website at the DocumentRoot location we identified in the file. Save the file and exit the file. So, now we have an additional site listed under the Sites-Available directory:

root@rpi3Bplus:/etc/apache2/sites-available# ls
000-default.conf  000-default-le-ssl.conf  default-ssl.conf  site2.conf  site3.conf

This new site3.conf file corresponds to our new WordPress domain of dancalloway.com. So, next, let’s go into our sites-enabled directory. Change into that directory to read its contents:

root@rpi3Bplus:/etc/apache2/sites-enabled# ls
000-default.conf  000-default-le-ssl.conf  site2.conf

Note here that we have only 000-default.conf and site2.conf enabled. We need to enable site3.conf as well. To accomplish this, we need to run a command as follows:

root@rpi3Bplus:/etc/apache2# a2ensite site3.conf
Enabling site site3.
To activate the new configuration, you need to run:
   systemctl reload apache2

Before we run the command to activate the new configuration, we can list out the contents of sites-enabled once again and see that it now includes site3.conf. The a2ensite stands for Apache2 enable site and is necessary to enable our 3rd Website.

root@rpi3Bplus:/etc/apache2/sites-enabled# ls
000-default.conf  000-default-le-ssl.conf  site2.conf  site3.conf

Run the systemctl reload apache2 command to reload the Apache2 Web Server since we made configuration changes. Now, our WordPress Website is configured, connected to its MySQL database and Apache2 is ready to receive requests to run it.

Finalizing The WordPress Setup

If we now go out to a Web browser on the local machine and go to dancalloway.com, it should take us out to the following Webpage:

WordPress Setup Screen

By now, the new Domain should be pointing to the IP address of our Raspberry Pi as we configured earlier in the Google Domains DNS. From the WordPress Setup page, we can fill out the Site Title (I chose dancalloway.com), the Username (I chose admin), copy the Password provided to you in the Password field, enter your email address in the Your Email field, and click the Install WordPress button. The last thing that we need to test out on our new domain Website is to log in. After configuring the WordPress Setup and clicking the Install Button, we will be taken out to that WordPress Admin Login page.

WordPress Admin Login Page

Now, all that’s left is to enter the Username or Email Address, which is admin for my purposes, paste in the password you copied earlier in the Password field, and click the Log In button. This will take you out to your WordPress Dashboard where you can start building your Website.

WordPress Dashboard

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.