In this tutorial, I will show you how to set up a WordPress website using LAMP, as well as some other necessary configurations to help your site function well.
What I’ll show in this tutorial:
- How to setup LAMP+WordPress
- Port forward to make your site accessible
- Add a domain and hide your IP address with Cloudflare
- Setup Automatic IP address updates
- Disable directory browsing for security
Prerequisites:
- Raspberry Pi (In my opinion, I’d say at least a Pi 4, but I’d recommend using a Pi 5.)
- A USB drive or SSD (SSD is better). (A micro SD card works as well, but there are quite a few reasons not to use one, and a quick search online will tell you why)
- A computer
- A good Internet connection
- A domain
What does LAMP stand for, and what does it do?
L stands for Linux – This is the type of operating system that your web server will run on.
A stands for Apache – This processes web requests and serves web content. In other words, it is what makes your web pages visible to users.
M stands for MySQL – This is the database. This will store wordpress data such as user login details, page data, etc.
P stands for PHP – This is the programming language used on your website.
On top of all that, WordPress makes configuring and setting up your website easy.
Installing an OS onto your Pi
The operating system, in this case, is the Raspberry Pi OS.
To install the OS, we can use the Raspberry Pi Imager
We’ll use the lite version of the OS, as it has no GUI, which you don’t need for a LAMP server.
No GUI means you won’t waste any computing power, leaving more resources for your website.
Flash OS
Insert your storage device into your computer. Make sure there is nothing important saved on it as it will be erased.
If you’re unsure how much storage to use, I’d recommend starting with 256GB, or at least 128GB, however, you can use a higher amount if you want. Once your website grows, you can upgrade.
If you’re using USB storage, make sure your Pi has USB boot enabled.
Open the Raspberry Pi Imager and select your Pi model, then click ‘Choose OS’.
Navigate to ‘Raspberry Pi OS (other)’
And select ‘Raspberry Pi OS Lite (64-bit)’
Choose your storage device and click ‘Next’
Click ‘Edit settings’
Set hostname, username, password, and locale settings.
You can configure Wi-Fi if you want to use it, but I’d highly recommend you use ethernet.
Switch to the ‘Services’ tab and enable SSH
Click ‘Save’ then click ‘Yes’
Click ‘Yes’ again
Booting up the Pi and accessing it via SSH
Once flashing is done, insert your storage device into your Pi, plug in ethernet if using it, and power it on.
Give it some time to fully boot up and connect to the network.
After a few minutes, open terminal on your computer and SSH into the Pi using this command:
ssh [username]@[hostname].local
Replace [username] and [hostname] for their actual values in your case.
When prompted, enter your password.
For example, my username is Mushfik, and my Pi’s hostname is wordpress.
So I would use:
If the [hostname].local method doesn’t work, you’ll need to find the internal IP address of your Pi.
There are many ways to do this, one of which is to open up your routers admin page and find it on the device list. You could also use a keyboard, mouse, and monitor plugged into the pi, and run the hostname -I command to view the IP address.
Once you’ve got the IP address, use:
ssh [username]@[IP_address]
Installing Apache
Now it’s time to install Apache, but first update your Pi:
sudo apt update sudo apt upgrade -y
Now install Apache:
sudo apt install apache2 -y
Test Apache
Once it’s finished installing, if you visit http://[hostname.local] or http://[Pi_IP_address] in a browser on your computer, you should see the Apache default page.
PHP
Install PHP using:
sudo apt install php -y
MariaDB
Now it’s time to install your database.
Install MariaDB and the PHP-MySQL package:
sudo apt-get install mariadb-server php-mysql -y
Once it’s installed, restart Apache:
sudo service apache2 restart
WordPress
We’ll install WordPress in the /var/www/html/ directory, but first we need to empty it.
Switch to the /var/www/html/ directory and empty it to remove the default page created by Apache.
Switch to directory:
cd /var/www/html/
Empty the directory:
sudo rm *
Once you’ve emptied the directory, keep it open as the current directory.
Download WordPress
sudo wget https://wordpress.org/latest.tar.gz
Extract the files:
sudo tar xzf latest.tar.gz
Install WordPress
Move all the extracted files to the current directory:
sudo mv wordpress/* .
Delete the old wordpress directory:
sudo rm -rf wordpress latest.tar.gz
Change the ownership of the WordPress files to Apache:
sudo chown -R www-data: .
Setup MySQL/MariaDB and create the Database
Setup
Run the MySQL installation command:
sudo mysql_secure_installation
Press Enter:
Type ‘n’, then press enter:
Type ‘y’ and press enter:
Set a strong root password:
Type ‘y’ and press enter:
Type ‘y’ and press enter:
Type ‘y’ and press enter:
Type ‘y’ and press enter:
Create the WordPress database
Run MySQL and enter the root password you set earlier:
sudo mysql -uroot -p
Create the database:
create database wordpress;
Grant database privileges to the root user, making sure to change YOURPASSWORD to your own password
Take caution if copying and pasting this command as it might execute by itself when you paste it before you swap your password into the command. I would suggest carefully typing out the command instead. (or copy the command without the semi colon, as it will not execute by itself if you do so)
GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'YOURPASSWORD';
Flush the privileges for the changes to take effect:
FLUSH PRIVILEGES;
Exit MariaDB with Ctrl+D
Reboot your Pi:
sudo reboot
Configuring WordPress
Open the Pi webpage again at either http://[hostname.local] or http://[Pi_IP_address], whichever works for you
You should see the WordPress configuration page.
Choose your language and click continue.
You’ll see the WordPress welcome screen.
Click ‘Let’s go!’
Next, you’ll be at the database connection page.
For the details:
Database Name: wordpress
Username: root
Password: [database password you set earlier]
Database Host: localhost
Table Prefix: wp_
Once you’ve entered the correct details, click ‘Submit’
If your details were correct, you should see:
Click ‘Run the installation’
Enter details for your site, then click ‘Install WordPress’
Once done, click ‘Log in’ to go to the login page, and then log in
WordPress dashboard:
Friendly permalinks
It’s recommended to change your permalink settings to make your URLs more friendly.
In the WordPress dashboard, go to Settings>Permalinks
Select the ‘post name’ option
Then click ‘Save Changes’
In terminal on your computer, SSH into the Pi again.
Enable Apache’s rewrite mod:
sudo a2enmod rewrite
Edit the Apache configuration file for your virtual host:
sudo nano /etc/apache2/sites-available/000-default.conf
Add the following after line 1:
<Directory “/var/www/html”>
AllowOverride All
</Directory>
Like so:
To save, press: Ctrl+x, y, enter
Your WordPress setup is complete!
Your website will be accessible from across your network. But what about outside your network?
Installing required and recommended WordPress PHP modules
For your site to work well, there are some required and some recommended PHP modules you should install.
sudo apt-get install php-curl php-dom php-imagick php-mbstring php-zip php-gd php-intl
Restart Apache:
sudo service apache2 restart
Port forwarding and Static IP address
You’ll need to enable port forwarding from your router to your Pi to make your website accessible from the internet.
You need to forward port 80 and 443. Port 80 is for HTTP and port 443 is for HTTPS, which I’ll show how to configure later on in this tutorial.
Port forwarding is usually done from your routers admin page, which can usually be visited by browsing to your routers IP address. The exact process can be different depending on your router, which is why I can’t give you exact instructions.
It’s not very difficult, but if you’re unable to find the option in your routers settings, you can search how to port forward on your router online.
You should also assign a static internal IP address to your Pi, that way your internal IP for your Pi won’t change. Otherwise, if it changes, it will make your website inaccessible.
This can usually also be done from your routers admin page.
External access
Once you’ve port forwarded, In WordPress, go to settings>general, and change your addresses to your Public IP.
You can find your external IP address by searching ‘My IP’ online.
Your website should now be accessible from your external IP address.
Domain, Cloudflare, and Certbot
Now your website is accessible from the internet with your public IP, but a domain would be better for your website, for many obvious reasons.
If you don’t have a domain, purchasing one online is quite easy, and the price isn’t high, usually around ~£10 a year.
You could also use a free hostname from something like No-IP, but you wont be able to use Cloudflare (shown in the next step) to hide your public IP address.
Cloudflare
You could point your domain directly to your web-server, which will make your public IP address easy to find from a DNS lookup, or you could use Cloudflare to hide your IP.
Cloudflare is a proxy between your web-server and the internet, meaning a DNS lookup of your domain shows Cloudflare IP addresses instead of your actual IP.
Add your domain to cloudflare
Sign up to Cloudflare, then click ‘Add site’ from the menu bar
Enter your domain, select ‘Manually enter DNS records’, and click ‘Continue’
On the next page, scroll down and select the free plan and click ‘Continue’
Cloudflare will give you nameservers, which you will need to point your domain to, in your registrar account. Cloudflare provides instructions on how to change your domain nameservers.
Once you’ve added your domain, go to the DNS page and click ‘Add record’
Select ‘A’ for the type, and for the name enter ‘@’. For the IPv4 address enter your public IP address.
Make sure the ‘Proxy status’ is set to ‘Proxied’
Now, go back to your website admin panel and navigate to settings>general, and change your addresses to your domain.
Make sure you use ‘https’ not ‘http’.
Don’t forget to save changes.
If you search your domain in a browser now, your website won’t show up as HTTPS isn’t configured yet, but after the next step, it should load.
Installing Certbot and configuring HTTPS
For your site to use HTTPS, you’ll need an SSL certificate.
You’ll need to install snapd first
sudo apt install snapd
sudo snap install core
Now install Certbot
sudo snap install --classic certbot
Ensure that the Certbot command can be run
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Have Certbot get and install your certificates
sudo certbot --apache
enter your email address
Agree to the Terms of Service
Decide if you would like to:
Enter your domain and press enter
Now your SSL certificate will be installed, and automatically renewed by Certbot
Test auto-renewal
sudo certbot renew --dry-run
Restart Apache
sudo service apache2 restart
In your Cloudflare account, go to the ‘SSL/TLS’ page
Click ‘Configure’
Select the automatic option
Now, if you enter your domain in the URL bar of your browser, your website should come up.
If it doesn’t work straight away, wait a few minutes to allow the Cloudflare SSL changes to apply.
Automatic IP updates.
Your network will likely have a dynamic public IP address, especially if you are setting this up on a home network. This means that your IP address can change over time.
If your IP address changes, your website will become unavailable as the Cloudflare DNS record will become outdated.
To overcome this, we can use ‘ddclient‘. ddclient runs on your server, connects to your Cloudflare account, and checks for IP changes every 5 minutes. If an IP change is detected, the IP address on your Cloudflare account is changed.
Get Cloudflare API token
First, you’ll need to get an API token for your Cloudflare account to allow ddclient to connect to your Cloudflare account.
Visit the ‘Overview’ page on your Cloudflare account and scroll down until you see ‘Get your API token’ on the right side of the page.
Click on the link to open your API Tokens page
Click ‘View’ next to ‘Global API key’
Enter your password and complete human verification.
Keep the key open for now.
Install ddclient
To install ddclient use:
sudo apt install ddclient -y
You can just keep pressing enter for these steps:
Now open the ddclient configuration file:
sudo nano /etc/ddclient.conf
Remove the configuration text
Add the following:
protocol=cloudflare \
use=web, web=ipify-ipv4 \
login=[your email] \
password='[your Cloudflare API key]’ \
zone=[your domain]
[your domain]
Make sure to change the information in square brackets to your own, and leave no space between lines
Like this:
Press Ctrl+x, y, and enter to save
Now restart ddclient
sudo service ddclient restart
Now your web server will automatically update your domain when there’s an IP change.
Disable directory browsing
Directory browsing allows people to browse your websites files and folders.
Disabling directory browsing is better for your websites security.
Edit your .htaccess file
sudo nano /var/www/html/.htaccess
Add:
Options -Indexes
At the end
like so:
Thats it! Your LAMP server is setup, now it’s time for you to customise your site to how you like it.
You can install a theme suitable for your type of website and customise it to exactly how you want it, as well as install plugins to add functionality.
WordPress is a big learning process, and while there are many tutorials online teaching how to use it, personally, it helps if you just play around and experiment with it, while occasionally using tutorials to figure out how to add things.
Issues
If anything goes wrong while trying this, or you have any questions regarding this tutorial, please leave a comment, and I’ll try to get back to you, or alternatively, leave a post on the forum.
No responses yet