How to Host a Secure Website on Raspberry Pi

With Cloudflare Tunnel, formerly known as Argo Tunnel, you can securely host a website or blog from your home on a Raspberry Pi. This means you no longer have to buy or pay for web hosting. It’s also secure with a free Cloudflare SSL certificate. Additionally, you can use the various free Cloudflare services to improve your site’s performance, such as load time.

Things you will need

You will need the following to get started.

  • A Raspberry Pi 2, 3, or 4. Optionally, you can also use Raspberry Pi Zero W. We recommend using at least Raspberry Pi 3 or later for decent site performance.
  • An 8 GB or larger microSD card.
  • A card reader.
  • A Windows, Mac or Linux system.

Install Raspberry Pi OS Lite (64-bit)

You will now need to install the operating system on the Raspberry Pi using the Raspberry Pi Imager tool on your Windows, Mac or Linux system. The steps are as follows.

  1. Connect the microSD card to your system and launch the Raspberry Pi Imager tool.
  2. Click on Choose the operating system and select the Raspberry Pi operating system (other) > Raspberry Pi OS Lite (64 bit) option.
  3. Click the gear icon to open the Advanced settings.
  4. Check the Enable SSH option and enter a password for it (be sure to write it down for later).
  5. If you intend to use a Wi-Fi connection instead of Ethernet (see tip below), check the Configure Wireless LAN option and enter the SSID and password of your Wi-Fi router, then select the correct country code from the drop-down list.
  6. When you’re done, click to safeguard to exit advanced settings.
  7. Click on Choose Storage and select the microSD card.
  8. Click on To write. It will take some time to complete depending on your network speed as it downloads the OS file and flashes it.
  9. Once the flash is complete, remove the microSD card, insert it into your Raspberry Pi, and turn it on.

Use an Ethernet cable to connect your Raspberry Pi 2, 3, or 4 for a faster, more reliable connection.

Connect and update Raspberry Pi

On your Windows PC, install putty. Mac and Linux users can use Terminal. For this guide, we are using PuTTY on Windows. The steps are as follows.

  1. Open PuTTY and enter the Raspberry Pi’s IP address. You can find the Raspberry Pi’s IP address using the End app on your Android or iOS device. Make sure your Raspberry Pi and smartphone are connected to the same network.
  2. Click on Open. Accept the prompt, then type ft followed by the SSH password you chose during installation. On Mac or Linux Terminal, type [email protected] and hit it Walk in key. Enter your SSH password to login.
  3. Then run the following commands.
    sudo apt update
    sudo apt upgrade
  4. Hurry Yes and knock Walk in Continue. It will take some time to finish.

Install Apache and PHP

After the update, you can start installing required services such as Apache and PHP on the Raspberry Pi. The steps are as follows.

  1. In the PuTTY or Terminal window, run the following command to install Apache.
    sudo apt install apache2 -y
  2. Once installed, you can enter your Raspberry Pi’s IP address into a web browser. It should load the Apache2 Debian default page.
  3. Now run the following command to install the required PHP packages.
    sudo apt install php7.4 libapache2-mod-php7.4 php7.4-mbstring php7.4-mysql php7.4-curl php7.4-gd php7.4-zip -y

Install and configure MySQL database

To install and configure the MySQL database for WordPress, follow these steps.

  1. In the same PuTTY or Terminal window, run the following command.
    sudo apt install mariadb-server
  2. After installation, connect to MySQL to create a database for the WordPress site.
    sudo mysql -u root -p
  3. Create a database.
  4. Create a user for the database. Replace Your username and Your password with a username and password of your choice.
    CREATE USER 'YourUsername'@'localhost' IDENTIFIED BY 'YourPassword';
  5. Grant all permissions to the user to manage the website database:
    GRANT ALL ON wpsite.* TO 'YourUsername'@'localhost';
  6. You can now type go out or press CTRL+D to quit MySQL.

Install WordPress

We need to download and extract the WordPress package to the /var/www/html phone book. The steps are as follows.

  1. Navigate to the html phone book.
    cd /var/www/html
  2. Remove the iindex.html file in this directory.
    sudo rm index.html
  3. Download the WordPress package.
    sudo wget http:
  4. Extract the WordPress package:
    sudo tar xzf latest.tar.gz
  5. Move all of the snippet wordpress folder at the root of html phone book.
    sudo mv wordpress/* ./
  6. You can now delete the WordPress package.
    sudo rm -rf wordpress latest.tar.gz
  7. Add the ft user at the www-data group and update ownership of all files in /var/www/html to www-data band.
    sudo usermod -a -G www-data pi
    sudo chown -R -f www-data:www-data /var/www/html
  8. Open a web browser and enter your Raspberry Pi’s IP address to complete the WordPress setup.
  9. Choose language and click Continue > let’s go.
  10. You must enter the MySQL Name of the data base, usernameand the password for the database we created earlier. Leave Database host and Array prefix as default settings. Click on Submit.
  11. Follow the wizard to complete the WordPress installation.
  12. Your WordPress site is ready and hosted on a Raspberry Pi. However, the site is currently only accessible locally via the Raspberry Pi’s IP address.

Follow the following steps to use Cloudflare Tunnel to securely host WordPress website on the internet.

Configure the Cloudflare tunnel

Sign up, log in and add your site (domain) to Cloudflare. Once added, access the SSH via PuTTY or Terminal and run the following commands.

  1. Download and install the Cloudflared daemon.
    sudo apt wget https:
    tar -xvzf cloudflared_2022.7.1_arm.tar.gz
    sudo cp ./cloudflared /usr/local/bin
    sudo chmod +x /usr/local/bin/cloudflared
    cloudflared v
    cloudflared login
  2. Copy the displayed URL and open it in the web browser. You need to select the domain (site added) in Cloudflare and click on To allow.
  3. Once authorized, create a secure tunnel by running the following command.
    cloudflared tunnel create mywpsite
  4. Copy the tunnel ID and JSON file path to Notepad.
  5. Create a configuration file.
    sudo nano ~/.cloudflared/config.yml
  6. Copy and paste the following code. Be sure to replace the details with your tunnel UUID, JSON file path, domain, and Raspberry Pi IP address.
    tunnel: a2efc6c1-2c75-45f8-b529d3ee
    credentials-file: /home/pi/.cloudflared/a2efc6cbde49d3ee.json
    - hostname:
    service: http:
    - service: http_status:404
  7. Hurry CTRL+ X followed by Yes so what Walk in. This will save the changes.
  8. Create a DNS entry in Cloudflare (replacing my website and with your own contact details).
    cloudflared tunnel route DNS mywpsite
  9. Finally, you can run the Cloudflare tunnel.
    cloudflared tunnel run mywpsite 

This will start the tunnel. You can now visit to access the website. Make sure to activate the HTTPS and Force HTTPS settings in Cloudflare to enable SSL encryption. You can follow the detailed instructions to set up a free SSL certificate on WordPress.

Also, visit IPAddressOfPi/wp-admin and log in using the username and password you entered when setting up WordPress. Below Settings > Generalupdate the WordPress address and Site address with your domain name as shown below.

At this point, if the Raspberry restarts, your site will crash. To avoid this and ensure that the tunnel starts when Raspberry Pi starts, run the Cloudflare tunnel as a service that starts when Raspberry Pi starts.

  1. Move the configuration file to the /etc/cloudflared phone book.
    sudo mv /home/pi/.cloudflared/config.yml /etc/cloudflared/
  2. Run the tunnel as a service using the following command.
    sudo cloudflared service install

That’s all you need to do to host a WordPress website on a Raspberry Pi securely with free SSL encryption.

Great for launching new websites

We used the free Cloudflare Tunnel service to host a WordPress website on a Raspberry Pi that can be accessed from the internet with secure SSL encryption. However, you should only use it if you are just starting out or want to start a website to provide information to your users about your products and services. You will need to opt for a dedicated hosting provider for better performance and reliability once you start getting decent traffic.

Previous Charity Digital - Topics - How to Write Effective Web Copy
Next Happy 100th birthday to Norman Lear! | Chaz's Diary