WordPress Blog on AWS: Avoid 5 EC2 and RDS Errors

Putting a WordPress blog on AWS with EC2 for the web server and RDS for MySQL is straightforward until the page says "Error establishing a database connection". That one sentence covers a missing network rule, a wrong host name, a wrong password and a TLS requirement.

I rebuilt the two tiers locally to separate those causes. The web tier is the amazonlinux:2023 container with Apache and PHP, and a MySQL 8.4.13 container with require_secure_transport=ON stands in for RDS. No AWS account was used, so the AWS commands below come from the documentation, and the error output comes from the lab.

Create the AWS pieces for the WordPress blog on AWS

Keep the database private and let only the web server's security group reach port 3306. With the AWS CLI, the shape is as follows. Replace the identifiers with your own:

aws rds create-db-instance --db-instance-identifier blog-db \
  --engine mysql --db-instance-class db.t4g.micro --allocated-storage 20 \
  --master-username admin --manage-master-user-password \
  --vpc-security-group-ids sg-DATABASE --no-publicly-accessible

aws ec2 authorize-security-group-ingress --group-id sg-DATABASE \
  --protocol tcp --port 3306 --source-group sg-WEBSERVER

Check the instance class and storage against current pricing and your free tier before running this. See the create-db-instance reference for every flag. Using a source security group means the rule keeps working when the web server gets a new address.

The TLS default depends on the MySQL version

According to the RDS documentation on requiring SSL/TLS, require_secure_transport is on by default for MySQL 9.7 and off by default for MySQL 8.4 and lower. An upgrade or a new instance can therefore break a site whose configuration worked before.

Database and user for the WordPress blog on AWS

Do not give WordPress the master account. The lab used its own database and a user limited to it:

$ cat /tmp/wp.sql; mysql -uroot -plab-root-only < /tmp/wp.sql 2>/dev/null && echo created
CREATE DATABASE wordpress CHARACTER SET utf8mb4;
CREATE USER 'wpuser'@'%' IDENTIFIED BY 'lab-wp-only' REQUIRE SSL;
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'%';
created
[exit 0]

A client without TLS fails in two different ways. A user created without REQUIRE SSL gets a clear message from the server setting:

$ mysql -h db -u plain -plab-plain-only --skip-ssl -e "select 1"
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
[exit 1]

The WordPress user has REQUIRE SSL, and for it the same mistake looks like a bad password:

$ mysql -h db -u wpuser -plab-wp-only --skip-ssl wordpress -e "select 1"
ERROR 1045 (28000): Access denied for user 'wpuser'@'10.89.2.3' (using password: YES)
[exit 1]

With TLS, the connection works and the cipher is visible:

$ mysql -h db -u wpuser -plab-wp-only --ssl wordpress -e "show status like \"Ssl_cipher\"; show grants"
Variable_name	Value
Ssl_cipher	TLS_AES_256_GCM_SHA384
Grants for wpuser@%
GRANT USAGE ON *.* TO `wpuser`@`%`
GRANT ALL PRIVILEGES ON `wordpress`.* TO `wpuser`@`%`
[exit 0]

Error 1: PHP has no MySQL extension

On the Amazon Linux image, installing php alone did not include the MySQL driver. WordPress 7.1.5 said so:

$ php -m | grep -ci mysql; sh /root/probe.sh
0
HTTP 500
Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
[exit 0]

Install php-mysqlnd and restart PHP-FPM. The message then changes to the generic one:

$ php -m | grep -i mysql; sh /root/probe.sh
mysqli
mysqlnd
pdo_mysql
HTTP 500
Error establishing a database connection
[exit 0]

Error 2: the real reason behind a database connection failure

WordPress hides the cause of a database connection failure on purpose. This short script makes the same kind of connection and prints it. Run it from the web server and delete it afterwards:

$ cat /root/dbtest.php
<?php // same connection WordPress makes, but print the real reason
mysqli_report(MYSQLI_REPORT_OFF);
[$host, $flags] = [$argv[1], ($argv[2] ?? '') === 'ssl' ? MYSQLI_CLIENT_SSL : 0];
$db = mysqli_init(); $db->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
$start = microtime(true);
$ok = @$db->real_connect($host, 'wpuser', $argv[3] ?? 'lab-wp-only', 'wordpress', 3306, null, $flags);
printf("%s after %.1f s: %s\n", $ok ? "connected" : "failed [" . mysqli_connect_errno() . "]", microtime(true) - $start, $ok ? "ok" : mysqli_connect_error());
[exit 0]

Without TLS and with TLS:

$ php /root/dbtest.php db
failed [1045] after 0.0 s: Access denied for user 'wpuser'@'10.89.2.3' (using password: YES)
[exit 0]
$ php /root/dbtest.php db ssl
connected after 0.0 s: ok
[exit 0]

Tell WordPress to use TLS

Add one constant to wp-config.php:

$ cd /var/www/html && sed -i "/DB_COLLATE/a define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL );" wp-config.php && grep -nE "DB_HOST|DB_COLLATE|MYSQL_CLIENT_FLAGS" wp-config.php
32:define( 'DB_HOST', 'db' );
38:define( 'DB_COLLATE', '' );
39:define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL );
[exit 0]

Then something odd happened. The first request still failed, and the same request three seconds later succeeded:

$ sh /root/probe.sh; sleep 3; sh /root/probe.sh; sh /root/probe.sh wp-admin/install.php
HTTP 500
Error establishing a database connection
HTTP 302
HTTP 200
WordPress &rsaquo; Installation
Welcome
Welcome to the famous five-minute WordPress installation process! Just fill in the information below and you&#8217;ll be on your way to using the most extendable and powerful perso
[exit 0]
$ php -r "echo \"opcache.revalidate_freq=\", ini_get(\"opcache.revalidate_freq\"), PHP_EOL;"
opcache.revalidate_freq=2
[exit 0]

PHP's opcode cache rechecks changed files only after opcache.revalidate_freq seconds, 2 here. Wait a moment before deciding that a fix did not work. This flag requests an encrypted connection; the test did not verify the server certificate. AWS publishes a certificate bundle for clients that do, described in using SSL/TLS with RDS.

Errors 3 to 5: password, host name and network

The error number and the waiting time separate the remaining cases. A wrong password answers immediately with 1045:

$ php /root/dbtest.php db ssl wrong-password
failed [1045] after 0.0 s: Access denied for user 'wpuser'@'10.89.2.3' (using password: YES)
[exit 0]

A misspelled endpoint fails in name resolution:

$ php /root/dbtest.php db-typo.lab ssl
failed [2002] after 0.0 s: php_network_getaddresses: getaddrinfo for db-typo.lab failed: Name or service not known
[exit 0]

An address that drops packets, which is how a missing security group rule typically behaves, runs into the timeout:

$ php /root/dbtest.php 10.255.255.1 ssl
failed [2002] after 5.0 s: Connection timed out
[exit 0]

A host that answers but has nothing listening refuses at once:

$ php /root/dbtest.php 127.0.0.1 ssl
failed [2002] after 0.0 s: Connection refused
[exit 0]
WordPress blog on AWS database connection errors for wrong password, bad host and timeout
Three causes, three different messages.

Finish and protect the install

Replace the sample secret keys with values from the WordPress salt service, then run the installer. In the lab the installer reported success and the database held 12 tables:

$ mysql -uroot -plab-root-only -e "select count(*) as wp_tables from information_schema.tables where table_schema=\"wordpress\"" 2>/dev/null
wp_tables
12
[exit 0]

Restrict the configuration file so only root can change it and the web server group can read it:

$ cd /var/www/html && chown root:apache wp-config.php && chmod 640 wp-config.php && ls -l wp-config.php && sh /root/probe.sh | head -1
-rw-r-----. 1 root apache 3637 Nov 29 10:13 wp-config.php
HTTP 200
[exit 0]

Requesting the file over HTTP returned an empty body, because PHP executed it. That protection disappears if PHP stops handling .php files, so block the path in the web server too.

$ curl -s -w " HTTP %{http_code}\n" -o /dev/null http://localhost/wp-config.php; curl -s http://localhost/wp-config.php | wc -c
 HTTP 200
0
[exit 0]

Finally I stopped the database. The visitor sees the same generic database connection message again:

# database stopped

$ sh /root/probe.sh
HTTP 500
Error establishing a database connection

Checklist for a WordPress blog on AWS

SymptomLook at
Missing MySQL extensionphp-mysqlnd
1045, fastPassword, user host, TLS requirement
getaddrinfo failureThe RDS endpoint in DB_HOST
TimeoutSecurity groups, subnets, routes
RefusedWrong host or port, nothing listening
3159Server requires TLS

Before a WordPress blog on AWS gets its first real visitor, add HTTPS, automated RDS backups and a tested restore. If you would rather run both tiers on one machine first, my WordPress on Nginx guide covers the single-server layout. A WordPress blog on AWS is only as dependable as the restore you have rehearsed.

Comments

Popular posts from this blog

Linux Desktop Frozen? 5 Proven Fixes Before a Hard Reboot

Minecraft Bedrock on Linux: 4 Proven Ways to Play in 2026

DXVK in Wine on Linux: 7 Easy Setup Steps, Tested