• jmessina

    (@jmessina)


    I’m building a site locally for a friend to get the content correct before getting a hosting service like hostgator or godaddy. I’ve created a virtual machine on my laptop using Arch Linux (bridged network) and installed WordPress there. Everything works great when I’m on my home network. I can access the site through the IP address (192.168.1.222) or the hostname provided through the DNS on my router.

    My problems occur when I try to take my laptop off my home network to collaborate with my friend. Obviously, I get a new IP address, so I’ve modified the wp-config.php file to automatically handle the change. Here is the code I added:

    /** Automatically resets the home and site URLs to the current host's IP address */
    $host_ip_addr = shell_exec("ip address show enp0s3 | grep inet | grep -v inet6 | awk {'print $2'} | sed 's/\/.*//'");
    $host_ip_addr = preg_replace('/\s+/','',$host_ip_addr);
    define('WP_HOME', "http://".$host_ip_addr."/wordpress");
    define('WP_SITEURL', "http://".$host_ip_addr."/wordpress");

    I’m sure the code is working right because the URL generated is correct in my home network. But, when I’m anywhere else, the site won’t load and I get a spinning wheel in the browser. From the research I’ve done, I thought all I needed to do was re-point the home and siteurl settings. There must be something else I’m missing. Any help would be much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • abletec

    (@abletec)

    Hi, jmessina, & welcome to the WordPress support forum. Can’t you just access the site using localhost if you’re using your laptop?

    Alternatively, you could perhaps edit your hosts file, but, really–truthfully, I think I’d just put a lamp (or lemp) stack on your laptop & access via localhost.

    Thread Starter jmessina

    (@jmessina)

    Thanks Jackie for your response!

    I had thought about building the LAMP stack directly on the laptop, but for a number of reasons I thought building a VM was the best route. I suppose I could put a GUI on the Linux VM and access the site that way.

    However, I’m wondering if there might be a bug in WordPress or maybe with the installation package in Arch Linux. I may be wrong, but from everything I’ve read, it sounds like defining WP_HOME and WP_SITEURL in the wp-config.php file should override the home and siteurl values in the database. That doesn’t seem to be the case. I’ve discovered that even though those entries are overridden (entries for both grayed out on the admin settings page), the site still attempts to use the database values. Eventually the site will resolve, but it’s pretty much just raw data that comes back – no formatting.

    I have come up with a workaround. I’ve written a new routine that updates the database values instead of overriding them.

    <?php
        $host_ip_addr = shell_exec("ip address show enp0s3 | grep inet | grep -v inet6 | awk {'print $2'} | sed 's/\/.*//'");
        $host_ip_addr = preg_replace('/\s+/','',$host_ip_addr);
        $host_url = "http://".$host_ip_addr."/wordpress";
    
        echo "<pre>Setting WordPress to $host_url</pre>";
        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = 'xxxxx';
        $dbname = 'wordpress';
        $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    
        if ($conn->connect_error)
        {
            die("Connection failed: " . $conn->connect_error);
        }
    
        $sql = "UPDATE wp_options
            SET option_value='$host_url'
            WHERE option_name='siteurl'";
    
        if($conn->query($sql) == TRUE)
        {
            echo "<pre>Updated siterul successfully</pre>";
        }
        else
        {
            die('Could not update siteurl: ' . $conn->error);
        }
    
        $sql = "UPDATE wp_options
            SET option_value='$host_url'
            WHERE option_name='home'";
    
        if($conn->query($sql) == TRUE)
        {
            echo "<pre>Updated home successfully</pre>";
        }
        else
        {
            die('Could not update home: ' . $conn->error);
        }
    
        $conn->close();
    ?>

    But, I would like to confirm if this is either a bug in WordPress, a problem with the package installation in Arch Linux, or my error.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Site not loading after changing host IP address’ is closed to new replies.