Setup Apache2 VirtualHost for Multiple WordPress Blogs

So, you want to host multiple WordPress blogs on a single server? With Apache2 VirtualHost feature, one can run multiple websites on a single server easily, and this brief tutorial is going to show you how to accomplish that.

Each virtualhost runs independently of the others. So if you’re going to be running a WordPress website on each VirtualHost, it will run as if it’s the only website on the server. This allows you to save cost on additional servers and resources.

This tutorial is going to be short.. and won’t waste your time with other unnecessary stuff.

Step 1: Setup Ubuntu Server

I find Ubuntu easy to manage and maintain so this tutorial is going to be based on Ubuntu Linux. You can get Apache2 VirtualHost to work on other Linux distributions, but Ubuntu is a great distribution for new users and beginners.

So, install Ubuntu with root access and run the commands below to update it.

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

Step 2: Install Apache2 Web Server

After updating Ubuntu, run the commands below to install Apache2 web server.

sudo apt-get install apache2

Step 3: Install MySQL Database Server

After installing Apache2, run the commands below to install MySQL database server.

sudo apt-get install mysql-server mysql-client

During the installation, you’ll be prompted to create a new password for MySQL root user. Do it! This password will be used to logon to MySQL server.

Step 4: Install PHP and other Modules

After installing MySQL server, run the commands below to install PHP and other PHP modules.
sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-pear php-imagick php-imap php-mcrypt php-recode php-tidy php-xmlrpc

At this point, all the servers and packages WordPress requires to function are installed. You next steps will be to create WordPress databases, configure Apache2 VirtualHosts and download WordPress content from online.

Step 5: Create WordPress Databases and Users

Run the commands below to logon to MySQL server

sudo mysql -u root -p

WordPress requires a database. Run the commands below to create new databases as well as a database users. Just duplicate the commands for additional databases and users for as many as you want. Make sure they all have unique names.

The commands below create a new database called wpdatabase

CREATE DATABASE wpdatabase;

The commands below create a new database user called wpuser and grant the user access to the wpdatabase.

GRANT ALL ON wpdatabase.* TO 'wpuser'@'localhost' IDENTIFIED BY 'type_new_password_here';

[alert-note]Repeat the steps above to create additional databases for additional WordPress sites. For example, create database wpdatabase1, wpdatabase2 as well as the users for the additional WordPress websites.[/alert-note]

When you’re done, run the commands below to save your changes and exit.

FLUSH PRIVILEGES;
exit

Step 6: Create Multiple VirtualHost for Multiple Sites

After creating multiple databases for the multiple WordPress sites you want to run, follow the guide below to create multiple VirtualHosts for the sites. Copy the default Apache2 site configuration file to create additional VirtualHosts (sites).

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.net.conf

You see we create two virtualhosts for two different websites.. (example.com.conf and example.net.conf)

Each of the above site config file will host a unique domain.

Now open each file by running the commands below for each..

sudo nano /etc/apache2/sites-available/example.com.conf

Then for example.com.conf file, configure the server name, server alias, DocumentRoot and others unique to example.com domain. Repeat the same for other virtualhosts.

<VirtualHost *:80>
        # However, you must set it for any further virtual host explicitly.
        ServerName example.com
        ServerAlias www.example.com
        ServerAdmin webmaster@example.com
        DocumentRoot /var/www/html/example.com
        # 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>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Do the above for all the sites that you want to host.. each with unique identity, including the DocumentRoot, ServerName, ServerAlias and Domain

When you’re done. save your changes and close out.

Next, enable the sites you’ve just created by running the commands below for each of the virtualhosts.

sudo a2ensite example.com.conf
sudo a2ensite example.net.conf

Enable other modules as well

sudo a2enmod headers
sudo a2enmod rewrite
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime

Now move the the next step.

Step 7: Create Each Directory for the sites

Now that you’ve configured the multiple sites in Apache2, go and create the DocumentRoot folders for each of the sites you defined in the configuration above.. The line the reads DocumentRoot should match each location.

sudo mkdir -p /var/www/html/example.com
sudo mkdir -p /var/www/html/example.net

Configure appropriate folder permissions for all the sites

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Restart Apache2 web server by running the commands below

sudo systemctl restart apache2

At this point, all your sites should be already for WordPress content. Next, go and download WordPress content and extract into the DocumentRoot folder for each site.

Step 8: Download WordPress content

Now download WordPress content and extract into the root directory for each site.

cd /tmp/ && wget http://wordpress.org/latest.tar.gz

Then extract the downloaded file.

tar -xzvf latest.tar.gz

And copy to each root folder for each site.

sudo cp -R wordpress/* /var/www/html/example.com
sudo cp -R wordpress/* /var/www/html/example.net

Step 9: Configure WordPress database settings

Run the commands below to create WordPress wp-config.php settings file from its sample.

sudo cp /var/www/html/example.com/wp-config-sample.php /var/www/html/example.com/wp-config.php
sudo cp /var/www/html/example.net/wp-config-sample.php /var/www/html/example.net/wp-config.php

Then open wp-config.php file and make the following highlighted changes to reference the database and user you created above.

sudo nano /var/www/html/example.com/wp-config.php

When the file opens, make the changes and save.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wpdatabase');

/** MySQL database username */
define('DB_USER', 'wpuser');

/** MySQL database password */
define('DB_PASSWORD', 'type_password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

Do the above for each of the site you create, making sure the database connection info is correct for each site.

sudo systemctl reload apache2

Now open your web browser and browse to the server domains and you should see WordPress default setup page for each site.

WordPress default setup page

Enjoy!

You may also like the post below:

https://websiteforstudents.com/new-wordpress-setup-beginners/