How I turned my Raspberry Pi into a webserver.

While looking for a job, I had my presentation website hosted in an amazon ec2 instance (this very website you are reading). Now if you don't understand what i'm talking about follow on, that's really not the point of this story.

 

The point is that, since I am no longer working as a consultant, and I didn't plan on taking any additional clients, I no longer needed to be spending so much money in my website. I decided to turn it into a personal porfolio more than the website of what used to be my company

 

I had an old Raspberry pi model 2 from a couple of years back without any good use. I thought I could host the website myself in my old pi (this is why the website is a bit slow). Mainly for the fun, and mainly because I didn't need it to go smooth and fast again (sorry for the delay, dear reader :(, but this website is being basically hosted off a memory card and a USB2.0 stick )

 

What has changed?

When I thought about creating a post about this, I thought of putting all of the instructions, and letting other people know how I did this, so if anybody wants to do this by themselves, they can reproduce my steps.

 

I would also use this kind of post as SEO, to attract developers to join my old network. Devs would search for my tech articles and join my network.

 

However, what I thought was going to be an incredible challenge was made extremely simple and extremely easy by Chat GPT. I simply asked the AI all of the questions of how to do it and it replied me without much problem or error.

 

I was fascinated, wow!  What previously would have cost a couple hours to search, investigate and develop, took me a couple of minutes simply by asking the A.I.

 

AIs are changing the way we, developers, interact with documentation. More changes are to be expected.

What did I do?

First I installed nginx, php and vsftp to have my own webserver with php and an ftp server to upload files. 

Then, I installed mariaDB and configured an initial database, user and password.

I migrated my Joomla Website from my amazon container to the raspberry pi using Akeeba Backup Manager. Very simple and very powerful tool that makes migrating easy.

 

I updated my dns records to point to my public IP, and then voilá. it worked.

What problems did I encounter?

Some few, at the beginning , I noticed the speed of the website was insanely slow. Using the  hdparm command in my raspberry pi, I realized of how slow the server read and write speeds were (That's what you get for hosting your website in a small sd card). I got an external hdd drive with a nvme disk  and connected it to my old usb 2.0 port . I then moved all of the website and database folders to this external disk drive.

 

Furthermore, I also installed a redis server to act as a fast cache for database queries.

 

Finally, my public IP changes periodically, which is a bummer because this means every time it  changes, I have to change and update my dns records. This is annoying. So I decided to create a small typescript that checked if the public IP had changed with regards to the dns records, and updated the record automatically via the API that my domain hosting service provided me with.

 

The script polls every couple of minutes just to make sure the IP is the same as the ones in the DNS records, and update them when the public ip changes. Here is the code if you want to take a look.

And the future?

If this works and I need more power, maybe I will consider buying a newer version of the Raspberry pi. I do realize this is the maximum speed I probably can get out of it, but to be fair, learning how to optimize the website to fit properly into this old small hardware taught me many things.


Think about it, your phone probably has more than 10 times the computing power of this old raspberry pi. And still it can find some use.

Be the first to comment