To host WordPress website on Raspberry Pi, the next steps involved of getting a public-facing IP address, register a domain name for our little Raspberry Pi website, setting up port forwarding and dynamic DNS, and we are going to discuss those in details in this article.
Raspberry Pi colocation
There are two ways you could host your Raspberry Pi WordPress server. One of the most basic service that many data centers in the world offers is “colocation” (a.k.a. colo) service. A colocation service is a data center facility in which a business or individual can rent space for servers and other computing hardware. Typically, a colo data center provides the building, cooling, power, bandwidth and physical security while the customer provides servers and storage.
There are apparently many companies offers Raspberry Pi colocation around the world (just google “Raspberry Pi colocation”). In many cases, you can either send them your own Raspberry Pi (pre-configure based on their requirements with pre-assigned IP and DNS settings) or purchase a Raspberry Pi from them (which normally including the casing, power and SD card with Raspbian). Some of those companies provide backup services, others provide redundant power. Most of the monthly charge is very reasonable partially because Raspberry Pi doesn’t take up a lot of rack space, and consumed very limited power.
Raspberry Pi colo offers a good starting point for ‘learning through tinkering’, you gain the experiences of managing a server, running a web server (as long as it is not mission critical) in the real environment before you push the Raspberry Pi to the limit, by then you probably should consider to have a more powerful server colo or subscribe to a Virtual Private Server (VPS) instance.
Host it yourself
When I started building the Raspberry Pi WordPress web server, I want to build it as close to a real colocation deployment environment as possible, and I want to see how far this little server can go in real deployment, but I’m not ready to go for ‘colo’ yet. So we will discuss the option of host it ourself in the rest part of this article.
Hosting Raspberry Pi using your home broadband internet is technically possible, however, there are a few considerations you will need to know in advance. First, many home broadband ISP’s terms of service disallow hosting a public web site from computers on their network. Secondly, almost all consumer ISP broadband services offers a dynamic IP which means that the IP address of your website domain will have to change accordingly whenever your public IP issued by your broadband ISP changed. Third, depend on your service subscription, the bandwidth and speed of your broadband connection could be very limited when your website gaining attraction.
Setup Raspberry Pi static IP
If you are accessing the Raspberry Pi via SSH, you probably already know your Raspberry Pi local IP or already setup a static IP for the Raspberry Pi for easy access, but in case you have not, it is time to get it done.
Before setting up a static IP for Raspberry Pi, we will need to find out the router IP and DNS first. First, run the following command line to find out the IP address assigned to the Raspberry Pi:
ip -4 addr show eth0 | grep inet
Which will show an inet address similar to this:
inet 192.168.0.101/24 brd 192.168.0.255 scope global eth0
The IP address 192.168.0.101/24 after inet is the current Raspberry Pi address assigned by the router and the range of IP addresses that router could issue.
Next we will need to find out the router IP address:
ip route | grep default
Which show something like this:
default via 192.168.0.1 dev wlan0 metric 303
The IP address following default is the router/gateway address.
We will then find out the DNS address by check the content of /etc/resolv.conf file:
It is likely is the same IP address as the router IP.
# Generated by resolvconf nameserver 192.168.0.1
We now can configure the static IP address on the Raspberry Pi by editing the /etc/dhcpcd.conf file:
sudo nano dhcpcd.conf
Scroll down to the end of the file, and add the following lines:
interface eth0 static ip_address=192.168.0.101/24 static routers=192.168.0.1 static domain_name_servers=192.168.0.1
Press Control-x to save the file.
- ip_address is the static IP we want to assign to the Raspberry Pi. This could be the same as the IP address that currently assigned to the Raspberry Pi by the router, or you could assign a unique IP to the Raspberry Pi as long as it within the range, the /24 means that the first 24-bits of the IP are fixed, which means that you could assign an IP address within any number of the last 8 bits (192.168.0.0-192.168.0.255) except the one used by the router (192.168.0.1) and 192.168.0.255 which usually reserved as broadcast IP.
- routers address should be matching the IP address previous show by
- domain_name_servers is the DNS address from
Reboot for the changes to take effect and login again using the newly assigned static IP address.
So far we are using the local or internal IP address assigned by our router for accessing our Raspberry Pi. In order to access our Raspberry Pi globally from anywhere, we will need a public or external IP address which is assigned by our Broadband ISP. The public/external IP is what the rest of internet sees. To find out the public IP address, type the following command from Terminal:
The IP address return is your exiting public IP address.
In order for the external traffic be able to access the Raspberry Pi, we will need to setup “port forward” on the router to direct the traffic to the local IP assigned to the Raspberry Pi. Access the login panel of router from browser by typing in the IP address of the router (In our case, the router IP address is 192.168.0.1), login with router’s user name and password (this varies from router to router and in general can be find out from user manual or from the label on the router). The actual process of setup port forward varies from vendor to vendor, if you are not sure on how to set up the port forward for your router, go to portforward.com to search for the step-by-step guide for setting up port forward based on the model of the router you use. In my case, which is a cisco router for Small Business, the setting is simply by adding the Raspberry Pi IP address and click on Enable for port 80 as shown below:
What this means is that any external TCP travel coming to port 80 would get forwarded to the internal IP address at port 80. Once the configuration is save, you should be able to access the Raspberry Pi using your public IP via browser.
Domain name registration
There are many reputable domain name registrar and web hosting companies provide domain name registration service, I use Namecheap.com for its reasonable pricing and simple, easy-to-use UI. For every domain registered with Namecheap.com it comes with free Whois Protection which hide your personal information from “whois” search, while some other domain registrar charged for extra for this privacy protection feature.
Once you register and pay for the domain name registration, next step would be setup the DNS record (A record ) to point the domain to our public IP address. If you are using Namecheap.com like me, Namecheap.com provides a good instruction on how to setup the A record and avoid conflict with other records (CNAME and URL Redirect records), in my case, I delete the default URL Redirect record, add an A-record, and change the value of the CNAME record to our domain name (without www) so that it looks like this:
The value of the A record should be the public IP we found out previously. The setting will take effect as soon as a few minutes or up to 48 hours (according to the Namecheap.com website). Launch the browser and type-in the registered domain, we should see our WordPress website serves up from the tiny Raspberry Pi!
The hostname on the Raspberry Pi should be unique but doesn’t require to have any relationship with the site that will be hosted. we can changed the default hostname of raspberrypi by modifying the
sudo nano /etc/hostname
Replace the default raspberrypi with whatever you want, for example, it might be a good idea to name it as yourdomain.com. Press ‘Control-X’ to save the file.
We have one more change to make at
sudo nano /etc/hosts
Replace the default:
Dynamic DNS update
We are now be able to access our Raspberry Pi via domain name, and it seems to work perferctly, except one minor issue. As we mentioned previously that since the Broadband ISP issue an dynamic IP which might change frequently, when that happened, we will have to change the IP address in our DNS records at Namecheap.com. This could be quite a challenge if we need to change it manually.
Most of DNS name servers offer Dynamic DNS service to update the DNS database. The Dynamic DNS service periodically checks for a change to the computer’s IP address, and when a new IP address is discovered, it updates the Dynamic DNS database to reflect that change.
First we need to activate Dynamic DNS on Namecheap.com, login to the Namecheap.com dashboard using our user name and password.
- Click on Manage button under the domain list;
- Click on Advanced DNS tab and scroll down until “Dynamic DNS” section;
- Click on the Status toggle switch to turn it on;
- A Dynamic DNS Password will show up, select the password and copy to the clipboard as we will need it later on.
Now we need to install Dynamic DNS client software
ddclient on Raspberry Pi, SSH into the Raspberry Pi to install ddclient.
sudo apt-get install ddclient
If this is the first time ddclient is installed, a GUI installation script (debconf) will be launched automatically:
- On Dynamic DNS service provider, select other, and type in dynamicdns.park-your-domain.com;
- On Dynamic DNS update protocol, just press enter, we will manually change this later;
- On Username for dynamic DNS service, input the actual domain name of the website;
- On Password for dynamic DNS service, paste the password we save from namecheap.com here;
- On Network interface used for dynamic DNS service, just type in dynamicdns.park-your-domain.com/getip, we will need to modify the network interface later;
- DnDNS fully qualified domain names, type in @.
Press control-x to save the configuration file.
The configuration script does not setup the configuration we expected, so we will have to manually make some changes:
To setup how often it will update the DNS, we add the line:
This means that it will check hourly (3600 seconds) for any change in public IP.
Make the changes on
protocol=dyndns2 line and
use=if, if=… line so that the entire configuration file would looks as this:
# Configuration file for ddclient generated by debconf # # /etc/ddclient.conf daemon=3600 protocol=namecheap use=web, web=dynamicdns.park-your-domain.com/getip server=dynamicdns.park-your-domain.com login=your-actual-domain-name.com password=dynamic_dns_password_assigned_by_namecheap @
Ensure that the configuration is working by running the following command:
sudo ddclient -daemon=0 -debug -verbose -noquiet
Pay attention to see if there is any ‘invalid’ or ‘error’ configuration setting.
We’d want to run ddclient as a daemon so it can check for a change of IP address periodically and notify the dynamic DNS provider (Namecheap.com) if necessary. To ensure that the ddclient is running in the dameon mode, check the
/etc/default/ddclient file is set correctly as:
Run the following command to start the daemon:
sudo service ddclient start
and we can use the following command to check the status:
sudo service ddclient status
We are online now!
We are now all set and officially online, serving website content out from a Raspberry Pi 3! I have a fiber optic broadband with 300mbps on the WAN connection, this is good enough for now.
It’s been quite a journey to setup self hosting on Raspberry Pi!