- Hide menu

Tech Tip: Port forwarding WordPress on Raspberry Pi and DNS name resolution

I was following the excellent instructions on how to Build a LAMP Web Server with WordPress on my Pi running on my LAN. Everything worked fine with the internal LAN IP address. I wanted to have the web server accessible from the Internet and I thought all I had to do was setup port forwarding and point an A record on a DNS to the public IP address. But I was wrong.

While I could access the WordPress server using the external IP address, so port forwarding was working correctly, it would not respond when I used a host name set up in the DNS A Record. Turns out the reason is that Apache needs to be set up with a Virtual Host record. And it’s best to setup WordPress via the host name URL rather than the internal IP address. So, here are some modified steps to the above tutorial:

  1. Install Apache as per the tutorial. Stop there, don’t install the database and WordPress yet.
  2. Add an A Record with the host name to your DNS and setup port forwarding so the host name access your Apache server. You should forward ports 80 and 443 for http and https. More on this later.
  3. Now set up a virtual host on Apache. There are good tutorials on doing this here and here. Basically you want Apache to respond correctly when you use a host name rather than an IP Address. Once you’ve done this, test the accessibility by using a web browser with a VPN (or another method such as using your smartphone’s mobile data) to confirm that your web server is accessible from outside of your LAN. Note, some web browsers, eg Apple’s Safari on iOS and iPadOS will not connect to your web server on http. So use Firefox or Chrome to test.
  4. Now set up an SSL certificate so your virtual host is accessible using https. I use the instructions here. Test the connection using the VPN enabled web browser.
  5. Now that you’ve got your web server accessible via http and https from outside your LAN you can finalise the setup of WordPress.
  6. Continue with the installation of the Marian DB and WordPress. When it comes to setting up WordPress, using the VPN enabled web browser with the host name rather than the internal LAN IP address

You’ll now have an externally accessible WordPress server running on a Pi on your LAN. But if you run the Site Health test, it will suggest that the install the following php modules:
sudo apt install php7.4-gd
sudo apt install php7.4-curl
sudo apt install php7.4-xml (for DOM)
sudo apt install php7.4-mbstring
sudo apt install php7.4-zip
sudo apt install php7.4-intl
sudo apt install php-imagick

I also added 127.0.0.1 <host name> to the /etc/hosts file and used certbot to add SSL/https access.