Localhost is Unreachable with Wordpress

I've attempted to setup my server with a domain name but am getting a 301 error when typing the URL in the browser. ( works fine, but yields a 301 error).

I have the DNS servers pointed to the correct public IP, and the router set to port-forward incoming 80, to local 80, to the correct private IP.

apache2.conf has been modified as follows

#<Directory /var/www/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
<Directory /srv/> Options FollowSymLinks AllowOverride None Require all granted
</Directory>

This is for sake of Wordpress being installed at the suggested /srv/www/wordpress/ and not needing the other default Apache location for a website....

wordpress.conf in the /etc/apache2/sites-enabled/ folder is like the following:

<VirtualHost *:80> ServerName example.com ServerAlias *.example.com DocumentRoot /srv/www/wordpress <Directory /srv/www/wordpress> Options FollowSymLinks AllowOverride Limit Options FileInfo DirectoryIndex index.php Require all granted </Directory> <Directory /srv/www/wordpress/wp-content> Options FollowSymLinks Require all granted </Directory>
</VirtualHost>

I'm using Ubuntu 20.04, Apache 2.4.41, Wordpress 5.8.1

Update Sept 23, 2021

Edited the title of this question from "Getting 301 Error on Apache Server" to "Localhost is Unreachable with Wordpress". Previously, I had always gotten a mere "timed out" response when entering my domain () from my local network upon which the server runs. The 301 error came when another, at that time, accessed my domain externally. Now that I've found a way to access my domain externally, the error is a consistent: "Localhost is unreachable". This by the way, only seems to happen on the Wordpress site; when I access the default "It Works" site, it is fine (I've uncommented the apache2.conf lines I highlighted earlier). Is this perhaps due to Wordpress using a database, and the default hostname for the user being "localhost"?

[For those trying to enter the domain pointing to their server: if you're doing this from the same local network upon which the server sits, it will not work (search "NAT loopback" for more on this). That's not the discussion here, but I mention it because it sunk a lot of time; localhost works, modifying /etc/hosts can do some cheats--still doens't simulate an external request from my knowledge--, but ultimately, using the domain name will not work.]

7

1 Answer

So the fix was that the Wordpress database needed to be updated. To do this:

With access to your Wordpress account, merely change the Wordpress URL and Site URL in Settings -> General:

WordPress Address (URL): <site_URL_here>

Site Address (URL): <site_URL_here>

OR

With access to you Wordpress database

  1. login to mysql with the username of your wordpress site (if you forget your username, it's in wp-config.php in the root directory of your wordpress site; the corresponding password is also there)
mysql -u <user_name_here> -p
  1. select the database for your site (again, wp-config.php has you covered if you forget)
use <database_name_here>
  1. KEY STEP: update "home" and "siteurl" records (wordpress official doc notes you should omit any trailing '/' on your URL)
UPDATE wp_options SET option_value="" WHERE option_name="home";
UPDATE wp_options SET option_value="" WHERE option_name="siteurl";
  1. check the database records were just updated
SELECT * FROM wp_options WHERE option_name = "home" OR option_name = "siteurl";

That's it for that issue.

As per being able to access your site at the new address:

If the wordpress site is being hosted on your own local network, as noted, you will NOT be able to access it INTERNALLY, that is, from a device on the same local network (including the server); whether via localhost or even the URL you just entered in the steps above, it will not work. You can ONLY access the site EXTERNALLY (ex. from a phone using data, from some computer on a friend's network, from a computer at work, etc.). Because that's a pain, the two options I found so you can still access the site INTERNALLY are: i) you have a router that can handle NAT loopback ii) you use a Virtual Private Network

I have not tested (i), but I know that (ii) works because I do just that. When the VPN is on, navigate to your URL and you'll be able to see the site.

I think this point should really be made more known for tutorials about setting up and creating a Wordpress site on a local network

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like