How I Hosted This Website On A Rasberry Pi !


Hello, so here I’m going to show you how I hosted a website on a Raspberry Pi. I am using the Raspberry Pi 3 model b+.

Here’s a list of pre-requisites for this post:

  • Firstly, a Raspberry Pi or any pc/laptop with a Linux distro works or a virtual machine.
  • An ISO of Raspberry Pi OS Full/lite or any other Linux distro
  • Have a domain
  • Access to your router (I’m using 2 routers for this one)
  • A Cloudflare account(you can create one for free)
  • [Optional] If you are using a Raspberry Pi, you will need an SD memory card with a USB reader.

Since I’m using a Raspberry Pi, I am using Raspberry Pi OS lite as OS also known as raspbian lite. [Next steps are for the Raspberry Pi option]

I’ve downloaded a Raspberry Pi Imager from HERE.

Raspberry Pi Imager

Next, we are going to put the SD memory card in the USB reader and plug it into a computer(I’m using Windows). Quick format the memory card first.
Afterward, install and launch the Raspberry Pi Imager and choose your OS and choose the storage.

Selecting the OS
Selecting the storage

Then click on the “WRITE” button to let the launcher install the OS on the SD card.

Next, put the SD card reader into the Raspberry Pi and install the OS. Put all your info for the installation process and it’s Done.

Since I Raspberry Pi 3 model b+, it can use WIFI. So, here’s the process to connect it to a router via wifi and enable wlan(you will need the SSID and password of the router).

Open a terminal and write :

raspi-config

This will open a menu like this :

raspi menu

Using arrow keys and enter, select the first option like this(if it asks you for your region, select your option):
System Options > Wireless LAN > enter SSID > enter password > finish

write your SSID
write your PASSWD
Finish

You can find out if you’ve received an IP with this command:

ip a

Then, I will suggest editing the DHCP config to add it as a static IP by doing so:

nano /etc/dhcpcd.conf

And add this add the end of the config file with your respective IP addresses:

interface wlan0
static ip_address=192.168.x.x/24
static routers=192.168.x.x
static domain_name_servers=192.168.x.x

Next, you can restart the service:

systemctl restart dhcpcd.service

The next steps are how to install Apache2, PHP, Mysql(MariaDB) and WordPress on the Raspberry Pi. There are a lot of posts on how to do that, so I will leave a link on how to do that.

Here is a genuine tutorial on how to install wordpress by digitalocean.

Before you make your website public, you will need a domain. You can find one on namecheap.com or another website that is cheaper or anything you want.

After that, you will need to create a config file to make your site accessible from outside your local network.

Create a config file for your wordpress, I’ve named mine wordpress.conf. You can name it however you want.

cd /etc/apache2/site-available
nano wordpress.conf

Add and edit it as per your domain name :

<VirtualHost *:80>
        ServerName yourdomain.name
        ServerAlias www.yourdomain.name
        ServerAdmin [email protected]

        <Directory /var/www/html>
                AllowOverride All
        </Directory>

        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
        ServerName yourdomain.name
        ServerAlias www.yourdomain.name
        ServerAdmin [email protected]

        <Directory /var/www/html>
                AllowOverride All
        </Directory>

        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After saving the config file, you need to enable your config file on apache and reload apache by doing so(write the name of your file):

a2ensite wordpress.conf
systemctl reload apache2

Now the routing part.
As I’ve said before, I am using 2 routers but don’t worry, I’ll show you how to do this with only one router.

[ How to set up the 2 routers ]

Step 1, I had to enable dmz on my 1st router. Login on your router and go to the tab that contains forward ports or something like DMZ configuration. Then you need to put the IP address of the second router as Host Address, like this :

enable dmz on your 1st router

Step 2, then on your second router, you need to create a port forwarding rule into your server. So, put the IP address of your Raspberry Pi as source and open the ports 80 and 443, like this :

port forwarding

[ How to set up on 1 router ]

This is easier since it requires only step 2. You only have to create a port forwarding rule and add your server IP address and the ports you need to open(80 and 443).

port forwarding

Finally, I suggest that you register your domain name on Cloudflare. Create an account and on the front page it will ask you if you want to add a site. Follow all the steps that Cloudflare tells you to do(such as add the Cloudflare Nameservers from where you got your domain).

After that you can access your Website with your domain name or your public !

I had some issues with wordpress : ” To many redirect on /wp-admin
Fix : https://wordpress.org/support/topic/to-many-redirect-on-wp-admin/

Hope that this guides you well and if you got any problem, you can always leave a comment or send me a mail.


Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights