Microsoft Azure Open Source Development Support Team Blog

Support for Open Source Technologies on Microsoft Azure App Service

Install Multiple WordPress in One MySQL Database on Azure Website

In the case you need to share one database between multiple WordPress installations on Web App on Azure App Service, here is the example how to do it (The example code is based on WordPress 4.1.1.).

1.  Install 1st WordPress website from the Web App gallery, you have the option of “Create a new MySQL database”,

Installation of this website will create table set on the database with prefix “wp_” by default.

2.  After installation of the 1st website is completed, create a 2nd WordPress website from the Wep App gallery, and choose “Use an existing MYSQL database” in the DATABASE box. Don’t run the WordPress install from your browser at this point, because the table prefix needs to be changed before installation.

3.  Before installation, change the table prefix in wp-config.php (You can find this file at the application root. For a Web App, it is under /site/wwwroot). Here is an example of what a table prefix in wp-config.php looks like. Notice that I’ve changed the prefix from “wp_” to “wp2_” (you can use any distinct name you need to)

$table_prefix  = 'wp2_';

4.  Run the installation of the 2nd WordPress website after the table prefix change. A new table set on the same MySQL database will be created, and the tables in this set start with prefix “wp2_” .  Check your database, and you will see two table sets with prefix “wp_” and “wp2_”. For example, if you have MySQL Workbench, you can connect to the database, and see the tables like this:

                

 

If you want to maintain customer code base separately and share blog contents between the two WordPress websites, make them point to the same content tables. You can choose tables with the prefix “wp_” as the master content tables. To do this, add code in the wp-settings.php file for the 2nd website, and point to the contents in the master content tables. Here is an example:

At the end of wp-settings.php (before do_action(‘wp_loaded’) ), add the following code:

 
// Change Table names, hard code table names to use contents in master tables:
$wpdb->posts = "wp_" . 'posts';
$wpdb->postmeta = "wp_" . 'postmeta';
$wpdb->comments = "wp_" . 'comments';
$wpdb->comments = "wp_" . 'commentmeta';
$wpdb->links = "wp_" . 'links';
$wpdb->links = "wp_" . 'terms';
$wpdb->links = "wp_" . 'term_taxonomy';
$wpdb->links = "wp_" . 'term_relationships';
 
You only want to share contents, so do NOT change the table prefix for users, 
usermeta, and options. That allows website 2 to keep its own settings and users. 
 
5 . To test:
- Publish a post from website 1, you can see it from website 2
- Publish a post from website 2, you should see it from website 1