WordPress MultiSite Development in Ubuntu Apache Localhost Part 1

WordPress MultiSite Development in Ubuntu Apache Localhost Part 1

WPWebHost WordPress Hosting

 

If you are an aspiring WordPress web developer or simply a webmaster that manages a lot of WordPress websites; multisite development is a good option as you only need one WordPress core installation for all of your sites.

This two-part tutorial will illustrate how you are going to prepare your Apache localhost for WordPress multisite development. It is assumed that you already attain the following requisites:

a.) A fully working LAMPP localhost installation in Ubuntu.
b.) One fully working installation of WordPress core (using latest version 3.4.1 as of July 2012) in the LAMPP document root folder. For example,

http://localhost/wordpress

This is where the WordPress core files are installed as a folder in your document root.

c.) To ensure compatibility of commands and features discussed in this tutorial, it is recommended that you will be using Ubuntu 12.04.

The concepts of this tutorial can also be applied for developers using XAMPP, MAMP or WAMPP although the exact details and commands is beyond the scope of this tutorial. The implementation should be fairly similar since an Apache web server will have the same configuration files/syntax regardless of development platform being used.

The strategy in multisite installation is simple. Create a server name for your main WordPress installation. Enable multisite in that WordPress installation. You can then mapped several WordPress projects as a subdomain of your main installation.

Subdomain structure is recommended as it treats your development site like a unique domain instead of a subdirectory. This is exactly the way it will be implemented in a remote server (in most cases). This makes development work easy.

This first part will talk about assigning a server name (or local domain name) to WordPress that makes it convenient to set up multisites using subdomains which will be discussed in part 2.

Mapping Server Name to your Main WordPress Installation in Apache

In Apache, a server name is mapped to a specific document root directory, for example (when you set document root to a www folder inside your home directory):

http://localhost ----- > /home/username/www

Most WordPress are installed as a folder inside a localhost document root accessible as:

http://localhost/wordpress/

The document root path for this would be (example only):

/home/username/www/wordpress

To create a new local server name for your main WordPress installation, you need to edit the hosts file in Ubuntu, follow the detailed steps below:

1.) Launch terminal (press Control – ALT – T).
2.) Enter the command:

sudo -s -H

Enter your Ubuntu password. This is a requirement, you cannot edit hosts file unless login as root.

3.) It’s time to edit your hosts file using Vim editor:

vim /etc/hosts

4.) Since you already installed LAMPP and fully working, you should have this line:

127.0.0.1 localhost

This means that localhost will resolve into local IP address 127.0.0.1 which is the default. You will then add a new line for your main WordPress site server name. Supposing you would like to name it as:

codex.tld

Then this line will be added to your hosts file:

127.0.0.1 codex.tld

To enter a text string in Vim editor, press “i” button in your keyboard then type the text. After entering the above code, press Esc button in your keyboard to disable text editing in Vim. This is how it looks like:

5.) Save the changes by pressing colon “:” key followed by “w” then press enter key. You can then exit the VIM editor by pressing “:” followed by “q” then press enter. Read this guide for more help in VIM.

Configure Apache to use the WordPress local server name

Now you have configured the main WordPress project site to resolve to 127.0.0.1; the next thing is to tell Apache about the changes.

1.) Make sure you are still logged-in as root.
2.) Navigate to the Apache configuration folder. Enter this command:

cd /etc/apache2/sites-available/

3.) Then open the default available website (you should have this file):

vim default

4.) Press “i” in your keyboard to enable text editing mode in VIM. You need to do four things here:

a.) Add the “ServerName” defined in hosts file next to “ServerAdmin”:

ServerName codex.tld

b.) Edit the “DocumentRoot” to point to WordPress directory, e.g.

DocumentRoot /home/codex/www/wordpress

c.) Edit also the “Directory” path to point WordPress directory:

<Directory /home/codex/www/wordpress/>

d.) Change “AllowOverride None” under the define WordPress path in “Directory” to:

AllowOverride All

This command will let you use .htaccess in your website. Leave all other things unchanged. This is the screenshot of the four changes being made (indicated inside the dotted yellow box).

Finally press ESC key then press “:” then type

w codex.tld

The above command will save it as codex.tld in VIM.

b.) Press “:” again then type q to exit. This will go back to the terminal command mode.

Enter this command to view the files:

ls -l -a

You should see a new file codex.tld created. See below:

Finally if you see the file created, you should enable the site by typing this command:

a2ensite codex.tld

After that, reload Apache:

service apache2 reload

This output is normal:

*Reloading web server config
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

Enable Mod_Rewrite module in Apache localhost

WordPress multisite requires that mod_rewrite module should be enabled. Follow the steps:

1.) Enable rewrite module with symlink to mods-enabled folder, type this command:

ln -s /etc/apache2/mods-available/rewrite.load /etc/apache/mods-enabled/rewrite.load

2.) Enter this command to go inside mods enabled directory:

cd /etc/apache2/mods-enabled

3.) Display all the files and directories:

ls -l -a

You should be able to see rewrite.load enabled by symlink, see screenshot below:

4.) Restart Apache by entering this command:

/etc/init.d/apache2 restart

The output should be similar to reloading Apache as shown previously.

Use the Newly Assigned ServerName in WordPress

1.) Launch a web browser. Clear all browser cache.
2.) Go to Wodpress admin panel: http://localhost/wordpress/wp-admin/
3.) Login as administrator then go to “Settings” – “General”.

Change the “Wordpress adress” and “Site Address” to use the new servername, see screenshot:

4.) Click “Save Changes”.
5.) Clear again all browser cache, and access the new WordPress server name in the browser:

http://codex.tld/

You should see the homepage loading fine using the new local domain, but you cannot access its pages and post (returns internal server error) since you still need to update the permalinks.

6.) Login as administrator in this URL:

http://codex.tld/wp-admin

7.) Go to “Settings” – > “Permalinks”. And under “Common Settings”, select any desired permalink format, you can still use your old format if you like. The most important thing is to click “Save Changes”.

8.) If you see a message “You should update your .htaccess now”. Copy and paste the provided code on the bottom on that page to your existing .htaccess file .

.htaccess is a hidden file, make sure you enable viewing of hidden files in Ubuntu. After editing the .htaccess, logout as WordPress administrator, clear the browser cache again and re-test the site.

If you don’t see the “You should update your .htaccess now”, you don’t need to update your .htaccess. WordPress automatically perform the changes for you. Go directly to clearing your cache and re-testing your site URLs.

All URLs should be working properly under the new server name “codex.tld”. Example:

Enable WordPress MultiSite Option

1.) Edit your wp-config and add this line after table prefix:

define('WP_ALLOW_MULTISITE', true);

2.) Save the changes and login back to your wordpress admin. Go to Tools – Network Setup.

3.) Select the subdomain option, enter your network site name as well as your email address. Click “Install”. If you see an error relating to DNS, simply ignore it and proceed to the next step.

4.) Follow the rest of the installation details provided by WordPress. They are very easy to follow and self-explanatory. But bear in mind these important things:

a.) Backup your .htaccess and wp-config.php file before editing.
b.) You edit the files using a text editor (gedit) if you are more familiar with this method. You have full permission to your home directory if this configured as a document root to be used by Apache.
c.) The multisite installation will be completed after you have created the folder blogs.dir, updated the wp-config.php and updated the .htaccess.

Clear the browser cache again, login as WordPress admin and you should now see “My Sites” in the header navigation of the admin dashboard. If you mouse over on that, this will show the multisite menus such as this:

This will also give you the privilege to be the network admin of your multisites. If you see this, then you have correctly configured Apache and WordPress to use multisites.

Adding new subdomains for your WordPress projects using multisite as well as further configuration will be discussed in Part 2 of the tutorial.

About the Author