Linux Administration: Troubleshooting Apache Web Server and Name-based Virtual Hosting with WordPress

Background:

I use a locally hosted WordPress site draft my GitHub pages articles and then convert to static pages using Simply Static. During the process I ran into the following issues with WordPress:

  • WordPress Site Health check has three critical issues:
    • The required module, gd, is not installed, or has been disabled.
    • The REST API encountered an error.
    • Your site could not complete a loopback request.
  • Updating failed. The response is not a valid JSON response. when creating and updating posts.
  • Simply Static’s failed diagnostics:
    • Checking if WordPress can make requests to itself from YOUR_IP FAIL
    • Checking for cURL support FAIL

Related article: Linux Administration: Configure Apache Web Server and Name-based Virtual Hosting

Solutions:

Issue 1: Updating failed. The response is not a valid JSON response.

I started getting this error only after setting the Permalinks setting to anything else other than the default setting Plain. When Permalinks is not set to Plain, the URLs need to be rewritten, and that’s when this error occurs. It turned out to be that I forgot to add the <Directory> tag in the virtual host configuration file. As you can see in this post, my original configuration file looks like this:

<VirtualHost *:80>
    ServerAdmin webmaster@example1.mydomain.com
    ServerName example1.mydomain.com
    DocumentRoot /var/www/html/example1.mydomain.com
    DirectoryIndex index.php
    ErrorLog ${APACHE_LOG_DIR}/example1.mydomain.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example1.mydomain.com_access.log combined
</VirtualHost>

But the correct configs for rewrite to work need to look like this:

<VirtualHost *:80>
    ServerAdmin webmaster@example1.mydomain.com
    ServerName example1.mydomain.com
    DocumentRoot /var/www/html/example1.mydomain.com

        <Directory /var/www/html/example1.mydomain.com>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
                DirectoryIndex index.php
        </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example1.mydomain.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example1.mydomain.com_access.log combined
</VirtualHost>

Without the <Directory> element, the .htaccess file is unable to override the rewrite rules.

Issue 2: WordPress Site Health Critical Issues

1. The required module, gd, is not installed, or has been disabled.

# Install the php8.1-gd module

apt-get install php8.1-gd -y
service apache2 restart

2. The REST API encountered an error.

The REST API is one way that WordPress and other applications communicate with the server. For example, the block editor screen relies on the REST API to display and save your posts and pages.

When testing the REST API, an error was encountered:

REST API Endpoint: http://example1.mydomain.com/wp-json/wp/v2/types/post?context=edit
REST API Response: (http_request_failed) cURL error 6: Could not resolve host: example1.mydomain.com

Or:

The REST API is one way that WordPress and other applications communicate with the server. For example, the block editor screen relies on the REST API to display and save your posts and pages.

When testing the REST API, an unexpected result was returned:

REST API Endpoint: http://example1.mydomain.com/wp-json/wp/v2/types/post?context=edit
REST API Response: (404) Not Found

3. Your site could not complete a loopback request.

Loopback requests are used to run scheduled events, and are also used by the built-in editors for themes and plugins to verify code stability.

The loopback request to your site failed, this means features relying on them are not currently working as expected.
Error: cURL error 6: Could not resolve host: example1.mydomain (http_request_failed)

To resolve issues 2 and 3, edit the /etc/hosts file:

nano /etc/hosts

# Add a new line: IP hostname. e.g. 192.168.0.4 website1.example.com

Additionally, you might need to install the curl package and double check to see if the rewrite module is properly enabled:

# Install curl:

apt-get install php8.1-curl curl -y

# Verify curl version:

curl --version

# Verify curl is enabled in php.ini

nano /etc/php/8.1/apache2/php.ini

# Uncomment this line:

extension=curl

# Enable the rewrite module.

a2enmod rewrite
service restart apache2

This also resolves Simply Static’s "Checking if WordPress can make requests to itself from your_IP" FAIL issue.