WordPress: PHP Notice: Undefined index: HTTP_HOST in /home/…/ms-settings.php on line 48

You’re running the WP-CLI and get that message, plus the following error:

Error: One or more database tables are unavailable. The database may need to be repaired.

Try running the wp command like this:

HTTP_HOST=yoursitehostname.dom wp

Set the HTTP_HOST environment variable to whatever domain name was used to create the network, before you went multisite. Normally, this is “localhost”, but it might be something different.

I changed mine to “localhost.lo”, because I use the .lo fake domain on my LAN.

To save yourself having to type that long command, add the following to your .bashrc:

[code]
HTTP_HOST=yoursitehostname.dom
export HTTP_HOST
[/code]

Then:

. ~/.bashrc

Now wp should work.

Why Does This Work?

When PHP is called from the command line, all the environment variables are copied into $_SERVER.

Make a file, test.php, with this code to dump the $_SERVER variable:

<?php print_r($_SERVER);

Then run it like this:

php test.php

Instead of the usual $_SERVER variables, you will see your shell’s environment variables!

The –url Global Option and Multisite

If you need to target a specific site for your wp command, use the --url global option. If you have two sites: a.foo.com and b.foo.com, you can run your commands like this:

wp --url=a.foo.com
wp --url=b.foo.com

HTTP_HOST and Multisite

You can also use HTTP_HOST to set which site you’re affecting in your network.

HTTP_HOST=a.foo.com wp
HTTP_HOST=b.foo.com wp