Score:1

Localhost is Unreachable with Wordpress

nl flag

I've attempted to setup my server with a domain name but am getting a 301 error when typing the URL in the browser. (http://localhost works fine, but http://example.com 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 (http://example.com) 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.]

in flag
Two questions ... well ... three, but I'll stop at two: (1) Does Apache own or have permission to access the files in `/srv/www/wordpress` (2) Is AppArmor running and, if so, is it configured to allow Apache to access the contents of `/srv/www/wordpress`?
jelt avatar
nl flag
(1) is a great suggestion; I did setup Wordpress under my user ID, not the default of www-data (which Ubuntu's Apache apparently does also). I did a recursive `chown -R www-data:www-data /srv/www` to rectify that, but it didn't solve the problem. (2) I'm not sure how to check if AppArmor is configured to allow Apache, but it was running and so I stopped it briefly to check, but it also didn't yield a result.
in flag
If AppArmor was running, then there may also be modules loaded for Apache that need to be disabled: `sudo a2dismod apparmor` then `sudo service apache2 restart` may give you what you need. Ideally, though, there should not be any reason to have Apache files outside of `/var/www`, as a lot of things by default just expect Apache to live in there ...
jelt avatar
nl flag
AppArmor is not an enabled mod on my setup; the AppArmor service however, was running, then I stopped it temporarily for testing as noted. The ubuntu.com/tutorials for installing WordPress I followed stated the /srv location.
in flag
Let's look at things from another angle, then. You can access the server from `localhost`, but not externally. A `301` is a permanent redirect. Did you install WordPress via `http://localhost` or with the domain you want to use? If `localhost` was used, then there may be a couple of things you'll need to change in the WordPress config
jelt avatar
nl flag
If you want to see the fairly concise tutorial I followed at any point (https://ubuntu.com/tutorials/install-and-configure-wordpress#1-overview) --only difference other than dependencies, was I replaced `www-data` with my user ID each instance throughout. As per your question, I guess I installed for `localhost` first, then added the `ServerName` option to the directive in `wordpress.conf` later
jelt avatar
nl flag
What I mean is I set `ServerName example.com`....
Score:0
nl flag

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="http://<site_URL_here>" WHERE option_name="home";
UPDATE wp_options SET option_value="http://<site_URL_here>" 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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.