Setup Apache Virtual Host for WordPress

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


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';

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.

When you’re done, run the commands below to save your changes and 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/
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

You see we create two virtualhosts for two different websites.. ( and

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/

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

<VirtualHost *:80>
        # However, you must set it for any further virtual host explicitly.
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/
        # 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
# 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
sudo a2ensite

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/
sudo mkdir -p /var/www/html/

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

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/
sudo cp -R wordpress/* /var/www/html/

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/ /var/www/html/
sudo cp /var/www/html/ /var/www/html/

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/

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


You may also like the post below:

5 Replies to “Setup Apache Virtual Host for WordPress”

  1. doesnt work The config of apache needs much clearer explanation. all i get is a blank page and a non working server.

  2. This is the best explanation I’ve seen of how to set up multiple WordPress blogs in Apache2 virtual hosts. It’s a simpler method than what Debian recommends, and keeps the different WordPress blogs completely independent of each other. It works perfectly.

    There is a small downside compared with the usual Debian recommendation: you can’t update from the repository. But the gain in simplicity outweighs that.

  3. the above explanation of setting up wordpress blogs in apache is resourceful. I have came across many relevant service provider like India Access and hostasp where one can find fine services at reasonable price.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.