How to run multiple WordPress sites using a single WordPress install

How to run multiple WordPress sites using a single WordPress install

As a WordPress theme developer, I used to set up a new WordPress install locally using WAMP for each of my clients so that I can easily import and export the database for development and testing purposes. However, as the number of websites and themes grew, so did the number of installs. Having to update all of them and also all the plugins is timewasting and tiring.

Setting up a multisite wasn’t an option because I needed the install to mirror live websites. Running several sites on a single database wasn’t an option either because I wanted to quickly import and export the database without accidentally overwriting the existing database content. I eventually figured out a setup that works for me so that I can run multiple WordPress sites using a single WordPress install.

Warning: I use this set up purely for development purposes. I wouldn’t recommend using it on a live server.

For the purpose of this tutorial I assume you already have WAMP installed on your computer and that you know how to run it.

How to run multiple WordPress sites using a single WordPress install

What I want to do is to have one WordPress install in a subfolder with shared plugins and themes for all my sites but individual databases and uploads folders.

Download a fresh WordPress install

Visit wordpress.org/download website and download a fresh install of WordPress. Extract the folder into WAMPwww folder.

WAMP add wordpress to www folder

How to add a virtual host from WAMP localhost

Creating a virtual host on a new version of WAMP is easily done from the localhost screen.

Start WAMP server. In your System Tray left click on WAMP icon and select Localhost.

WAMP start Localhost

You will see a Tools section in the left bottom corner of the localhost screen. Click on Add a Virtual Host.

WAMP Localhost Tools

In to the Name of the Virtual Host type your domain, for example wptest.dev. For all my sites on localhost I’m using the same name as on a live server but non-existent domain like .dev.

Into the Complete absolute path of the VirtualHost folder type the exact path to your wordpress folder. The best way to get the correct path is to navigate to the folder and copy the path from the address bar. The default is C:\wamp64\www\wordpress.

WAMP copy www folder path

Click the Start the creation of the VirtualHost button. Wamp will create the virtual host for you.

WAMP create virtual host

When you get a message The files have been modified. Virtual host wptest.dev was created, you need to restart a DNS server by right-clicking WAMP icon in the System TrayToolsRestart DNS.

How to add, update or remove a virtual host on Windows 10 manually

To add a new host on localhost manually, go to StartWindows AccessoriesNotepad right click MoreRun as administrator. In the pop up window confirm YES.

Click FileOpen... and navigate to C:\Windows\System32\drivers\etc. In the right bottom corner select All Files (*.*), select hosts from the list and confirm Open.

Windows open hosts file

Copy and paste the last two rows where it says localhost and replace localhost with your new host name. Save changes FileSave.

Windows add new host

Next you need to find a file called httpd-vhosts.conf in the WAMP folder – C:\wamp64\bin\apache\apache2.4.23\conf\extra. Open httpd-vhosts.conf in a text editor, e.g. Notepad and copy and paste the following code at the end of the file.


<VirtualHost *:80>
	ServerName wptest.test
	DocumentRoot "c:/wamp64/www/wordpress"
	<Directory  "c:/wamp64/www/wordpress/">
		Options +Indexes +Includes +FollowSymLinks +MultiViews
		AllowOverride All
		Require local
	</Directory>
</VirtualHost>

Save changes FileSave.

Restart a DNS server. In the System Tray ⇒ right-click WAMP icon ⇒ ToolsRestart DNS.

To remove a virtual host, delete the appropriate lines from hosts and httpd-vhosts.conf files.

How to create a new database in phpMyAdmin

Open phpMyAdmin either from Tools section on a localhost screen or go to System Tray ⇒ left click the WAMP icon phpMyAdmin.

The username is root and the password is empty by default. Click Go.

phpMyAdmin login screen

Click the Databases tab. Into Create database enter the database name, select the collation and click Create. I call all my local databases wamp_ and the host name with underscore instead of the dot because it helps me not to get mixed database exports from local and live server up.

Create a new database in phpMyAdmin

Share the ♥ Join now to get free access to premium content and receive our newsletter. You can unsubscribe anytime.

Registration confirmation will be e-mailed to you.

How to install WordPress on WAMP

Make sure the WAMP server is running. Visit your new host wptest.dev in a web browser and follow the install screens.

You will be prompted to enter database connection details:

WordPress install database details

After the installation is complete, log in and go to SettingsPermalinks, select your desired permalinks structure and click Save Changes.

How to move WordPress to a subfolder

We want to serve WordPress from a subfolder. Go to your WAMP www folder ⇒ wordpress folder and create a new folder inside. I’m going to call mine main.

Create main folder inside wordpress folder

Move everything apart from wp-config.php into the main folder.

Go to the main folder, copy the .htaccess and index.php file and paste them one directory above into the wordpress folder (copy, not move, please!).

Move WordPress files to a subfolder

Open index.php in a text editor and locate the following line:


require( dirname( __FILE__ ) . '/wp-blog-header.php' );

Edit the line by adding main/ into the path.


require( dirname( __FILE__ ) . '/main/wp-blog-header.php' );

wp-config edit wp-blog-header

Save changes FileSave.

Open wp-config.php and add the following code under the database username.


/** Overwrites the database to save keep editing the database */
define('WP_HOME', 'http://wptest.dev');
define('WP_SITEURL', 'http://wptest.dev/main');

wp-config add WP_HOME and WP_SITEURL

Save changes and visit your website. Make sure everything works. If you can’t access your site, double check your code and that you copied not moved .htaccess and index.php.

Moving the uploads folder

If you want each of your sites to have its own uploads folder, add the following code into wp-config.php.


/** Change uploads directory */
define('UPLOADS', 'wp-content/uploads/wptest');

WordPress move uploads folder

Upload an image into Media Library and check that it’s been stored in uploads folder in wptest subfolder.

Automatically activating and deactivating plugins

There’s one more thing I like doing on a localhost. If you import an exported database from a live site, there will be many plugins that you don’t want to run on a localhost because they either interfere with WAMP (many security plugins) or they complain about the domain not being valid (like reCaptcha) or you just don’t need them running like Google Analytics etc. On the other hand there are a bunch of developer plugins that you do want to run on localhost but not on a live site.

Go to wordpressmainwp-content and create a new folder called mu-plugins (unless already exists). Inside mu-plugins folder create a file local-plugins.php and copy and paste the following code:


<?php
/*
 * Activate or deactivate plugin in local installs
 */
function local_plugins() {
    activate_plugins( array(

    ));

    deactivate_plugins( array(

    ));
}
add_action( 'admin_init', 'local_plugins' );

In between brackets add a comma separated list of plugins (in a format of plugin folder slash main php file) you want to deactivate or activate by default on localhost. The last item is always without a comma. Here is an example:


<?php
/*
 * Activate or deactivate plugin in local installs
 */
function local_plugins() {
    activate_plugins( array(
        'debug-bar/debug-bar.php',
        'debug-bar-console/debug-bar-console.php',
        'debug-bar-cron/debug-bar-cron.php',
        'debug-bar-extender/debug-bar-extender.php',
        'developer/developer.php',
        'monster-widget/monster-widget.php',
        'regenerate-thumbnails/regenerate-thumbnails.php',
        'show-current-template/show-current-template.php',
        'simply-show-ids/simply-show-ids.php',
        'theme-check/theme-check.php',
        'user-switching/user-switching.php',
        'wordpress-importer/wordpress-importer.php'
    ));

    deactivate_plugins( array(
        'akismet/akismet.php',
        'backwpup/backwpup.php',
        'wordfence/wordfence.php',
        'google-analytics-dashboard-for-wp/gadwp.php'
    ));
}
add_action( 'admin_init', 'local_plugins' );

Adding configuration files

To run another site on the same WordPress install you need to tell WordPress to use a different database and a different uploads folder based on the virtual host name.

Create a new virtual host (for example myweb.dev) and restart DNS server.

Create a new database in phpMyAdmin (for example wamp_myweb_dev).

Open wp-config.php and replace the following code:


/** The name of the database for WordPress */
define('DB_NAME', 'wamp_wptest_dev');

/** Overwrites the database to save keep editing the database */
define('WP_HOME', 'http://wptest.dev');
define('WP_SITEURL', 'http://wptest.dev/main');

/** Change uploads directory */
define('UPLOADS', 'wp-content/uploads/wptest');

with this code:


$host = getenv('WORDPRESS_HOST') ?: $_SERVER['SERVER_NAME'];
if ( $host && file_exists( __DIR__ . "/conf/{$host}.php" ) ) {
	include ( __DIR__ . "/conf/{$host}.php" );
} else {
	/** The name of the database for WordPress */
	define('DB_NAME', 'wamp_wptest_dev');

	/** Overwrites the database to save keep editing the database */
	define('WP_HOME', 'http://wptest.dev');
	define('WP_SITEURL', 'http://wptest.dev/main');

	/** Change uploads directory */
	define('UPLOADS', 'wp-content/uploads/default');

	/** WordPress Database Table prefix */
	$table_prefix  = 'wp_';
}
unset($host);

This code says, if there is a php file in conf folder that has the same name as the host, use that one, else use the default settings.

Scroll down and delete the table prefix code. We don’t need it anymore because we moved table prefix inside the conditional statement.


/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';

Create a new folder in wordpress folder called conf. Inside conf folder create a new file named after your new virtual host, for example myweb.dev.php. Copy and paste the following code:


<?php
/** The name of the database for WordPress */
define( 'DB_NAME', 'wamp_myweb_dev' );

/** Overwrites the database to save keep editing the database */
define('WP_HOME', 'http://myweb.dev');
define('WP_SITEURL', 'http://myweb.dev/main');

/** Change uploads directory */
define('UPLOADS', 'wp-content/uploads/myweb');

/** WordPress Database Table prefix */
$table_prefix  = 'ah3sp78cru_';

WordPress new configuration file

Visit myweb.dev in a web browser and run WordPress install. This time you won’t be asked for any database details.

If you want to import an existing website:

  • import the database into wamp_myweb_dev via phpMyAdmin
  • in the configuration file change table prefix to to match the table prefix of the site you want to import
  • copy the contents of uploads folder into a subfolder specified in the configuration file.
  • visit myweb.dev and log in with the imported website login details

Conclusion

Running several websites on one WordPress install on WAMP saves lots of time spent doing updates and makes adding new sites easy and fast. All you have to do is to add a virtual host, import a database, create a new configuration file and copy the contents of the uploads folder from the live site into the uploads subfolder. We’re back to 5-minutes install. Hurrah. 🙂

Love, jbWorld logo

Share the ♥ Join now to get free access to premium content and receive our newsletter. You can unsubscribe anytime.

Registration confirmation will be e-mailed to you.

Leave a Reply

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