In this part, you will know how to set up your local WordPress multisite project in subdomains. As a requirement, you should have a fully working WordPress multisite platform setup in Apache localhost. The details are discussed in the first part of the tutorial.

Step1 – Define the subdomain name in hosts file

Remember in the first part of this tutorial where you have created a local server name “codex.tld”. This is the local domain name of your main/primary WordPress site.

To add sites in a multisite setup, it will be added as subdomains (since WordPress is configured to use this in the first part of the tutorial) of your primary WordPress site.

In a local development server, this should be defined first in the hosts file. This is a very important requirement before actually adding the subdomain in WordPress admin panel.

Supposing you will be naming your first site to be added as:

project1.codex.tld

Below are the steps on how to define it in the hosts file:

1.) Launch the terminal and login as root:

sudo -s -H

2.) Open the hosts file in Vim editor:

vim /etc/hosts

3.) Add this line to the hosts file to define your first subdomain:

127.0.0.1 project1.codex.tld

This is how your hosts file will look like:

3.) Save the changes and exit the Vim editor.

Step2 – Configure Apache to recognize the subdomain project

The next step is to edit the existing Apache configuration to recognize the subdomain. Follow the steps below:

1.) Make sure you are still logged-in as root.
2.) Enter this command in the terminal to acces inside the Apache available sites:

cd /etc/apache2/sites-available

3.) Open your main WordPress site Apache configuration in Vim. In part 1, this is named as “codex.tld”:

vim codex.tld

4.) Change the ServerName from:

ServerName codex.tld

TO:

ServerName project1.codex.tld

This is the only line to be edited on this file. The directory path and other directives stays the same.

5.) Now it is important to save this file as:

project1

IMPORTANT: When saving the file, do not overwrite the codex.tld configuration in the /etc/apache2/sites-available instead save the edited file as:

project1

This is a requirement. If you overwrite codex.tld configuration, the main WordPress website will not work anymore.

This is the screenshot of the project1 configuration:

6.) Exit the Vim editor and view the /etc/apache2/sites-available directory by issuing this command:

ls -l -a

You should see the two configuration file namely codex.tld (for your main WordPress site) and project1 (for your first site in your multisite network).

7.) The next step is to enable the project1 site. Enter this command in the terminal (make sure you still log-in as root):

a2ensite project1

8.) It also requires that you reload the Apache configuration:

service apache2 reload

9.) Now confirm if the site has been enabled by going to the sites enabled directory, enter this command:

cd /etc/apache2/sites-enabled

And view the files:

ls -l -a

This is how it looks like. Take note that project1 is now added, so it is enabled:

The subdomain is now ready to be used by WordPress.

Step3 – Add the subdomain to your WordPress multisite project

Finally you can now add the subdomain since the localhost server is now configured to use it. Follow the steps:

1.) Login as WordPress admin to your main site for example:

http://codex.tld/wp-admin

2.) Go to My Sites – Network Admin – Sites.

3.) This page will give you the complete list of sites in your network. At first, there is only one, e.g. codex.tld since this is the main site and you still have no other sites added.

This time, you will be adding your first subdomain project. Click “Add New”.

4.) In the “Site Address” field, type exactly the name of the subdomain defined in your Apache localhost, for example:

project1

Also fill-up the “Site Title” as well as the administrator email address. This is how it looks like:

5.) Finally click “Add Site”. If you are able to see the message:

Site added. Visit Dashboard or Edit Site

The configuration is successful.

6.) Now click “Visit Dashboard” link. There should be no errors. This is the admin dashboard of project1.codex.tld. As you can see the URL of the admin panel is now change to:

http://project1.codex.tld/wp-admin/

7.) You can now add themes like any other WordPress site.

Step4 – Enable a theme for your first WordPress multisite subdomain

1.) Copy the theme folder to /wp-content/themes. This is the theme that you want to be used for your first project. If the theme already exist, rename the theme to something unique that you can still be able to recognize.

Since a multisite WordPress project only has one core installation, there should only be one /wp-content/themes folder that will be used by all of your sites.

All subdomains are virtual except for your main site. They only exist in WordPress database (after adding them in the WordPress admin panel). They came to life after some Apache and hosts file configuration. Therefore what you actually see in your WordPress server is only one installaton of WordPress and one wp-content.

2.) Now login back to your WordPress admin. Go to “My Sites” – “Network Admin” – “Dashboard”.

3.) Then go to “Themes” — “Installed Themes”. You should be able to see your theme that you have just copied to themes directory. Click “Network Enable” so that you can use that theme to your first WordPress multisite subdomain.

4.) Now go back to the admin dashboard of your WordPress local subdomain e.g. http://project.codex.tld/wp-admin

5.) Go to “Appearance” — “Themes”.
6.) Under “Available themes”, you should be able to see the selected theme for your project. Click “Activate” to use it.

7.) View the site in the browser by entering the URL, e.g.

http://project1.codex.tld/

The site should be loading normally such as shown below:

You have just completed adding your first subdomain in WordPress multisite project. If you like to add more subdomains, repeat the entire steps mentioned in this tutorial.

Author:

Codex loves PHP, Wordpress, Linux and everything about open source. He like coding PHP projects, writing coding tutorials and developing websites. He also a plugin contributor to Wordpress such as Sort SearchResult By Title. He also contributed and published a lot of open source PHP source codes in his PHP developer blog as well as an active open source .NET developer using Mono. You can follow him on Twitter to get the latest updates.

5 Comments

  1. Thank you very much for this tutorial. It helped me work out a local installation on ‘lubuntu 12.04 lts’. Everything worked to perfection. I enjoyed all three posts: from installing the dev environment, to enabling the first subdomain. Keep on ;-)

Leave a Comment

Feedback Form
Customer Feedback