Host your own web app created with Laravel on a RaspberryPi incl. your own Domain, SSL certificate and free dynamic DNS (almost) for free.
Over the years and especially since the introduction of the latest RaspberryPi generation – v4 - the SoC became more and more suitable as a PC or Server replacement.
So powerful even, that the introduction website with tens of millions of visitors was hosted on a cluster of 18 Raspberry Pi 4 (
Source)
This may seem like an overkill for your private website but should give you some feeling of the potentials of this small piece of hardware.
Hosting your own website - a quick journey back in time
There was a time, not long ago actually, where you needed to buy webspace from a provider like GoDaddy, then got some crappy web interface and an outdated php version, no root access and you were not able to install any additional software (or your go to web-framework).
To top it off, it cost you like 10$ per month. If you wanted a vServer, to install your own OS and LAMP stack, probably 30$ per month. Of course, this not necessarily the case anymore, you can get very performant vServers at DigitalOcean for 5$ per month or use AWS Lambda and don’t pay anything for it, unless you are really using CPU time of your instance.
Nevertheless, the solution described in this tutorial is almost free (except for domain registration), you have of course complete root access of your pi (unless you forget your password, which never happened to me!!) and it’s a lot more fun to tinker around - perfect corona quarantine activities so to say.
In this short tutorial, I’ll explain you how to host your own small web application on the PI. Moreover - in Step 2 - we’ll make sure that your app is available from the WWW and is accessible to everybody.
First Steps first
I’ll not go over to steps to actually set up your Pi in your home, install Raspbian, or any other Linux OS and connect to it over SSH. There are literally thousands of tutorials available, just google for one or look at the
official Raspberry documentation.
Given that you are running a Raspbian on your PI and you can ssh into it, you’d first need to install a webserver to host your website – for this we install ye good ole Apache.
// Install apache2 as our webserver
$ apt-get install apache2
Now, you can already access a website in your browser – just key in the IP of your raspberry in the browser and you should be greeted by the default page of apache:
http://ip-of-my-pi
Since we want to host a web app based on Laravel (which is a PHP framework), we need – you guessed it – PHP
$ sudo apt install php
This will install the following packages:
- libapache2-mod-php7.3
- libsodium23
- php-common
- php7.3
- php7.3-cli
- php7.3-common
- php7.3-json
- php7.3-opcache
- php7.3-readline
By the time of this writing, there is only PHP 7.3 available in the official Debian-Repo, but for our web app, this is sufficient.
But we also need some additional PHP packages:
$ sudo apt install php-mcrypt php-mbstring php-xml php-mysql
Now with all dependencies installed, let’s continue with the apache configuration. We want our app to be available via the URL
hello.aboe.eu.
So, we need to create a virtual host for apache to listen to that URL.
Cd in /etc/apache2/sites-available and create the conf-file:
$ cd /etc/apache2/sites-available
$ touch sudo touch hello-word.conf
Paste the following content into the hello-world.conf file:
$ sudo nano hello-world.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName hello.aboe.eu
DocumentRoot /var/www/html/hello-world/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/html/hello-world/public>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the site:
$ sudo a2ensite hello-word.conf
And activate the mod rewrite:
$ sudo a2enmod rewrite
Last but not least – restart the apache2 service:
$ sudo systemctl restart apache2
To actually display a website with our webserver we need – a website – whoop whoop. Don’t know why I did that.
For reasons of convenience, you can go ahead and clone this project onto your PI:
$ cd /var/www/html
$ git clone https://github.com/aborner/hello-world.git
This should give you a very simple "hello-world" Laravel project and clones it directly to your webserver directory.
Give me more dependencies!
To get the Laravel framework to run, we need another dependency – composer.
Composer is a dependency manager for PHP and you need it to download and install the PHP dependencies used by Laravel.
You can download and install it with the following commands:
//Download composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
//Check consistensy of the download
$ php -r "if (hash_file('sha384', 'composer-setup.php') ===
'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a')
{ echo 'Installer verified'; }
else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
// Install the binary
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
// move the binary to your /usr/local/bin directory
sudo mv composer.phar /usr/local/bin/composer
Given /usr/local/bin is in your $PATH, you can now run composer from the command line, e.g. :
$ composer --version
With that done, back to our website and get our cloned laravel project to run:
$ cd var/www/html/hello-world$
$ sudo composer install
$ sudo mv .env.example .env
$ sudo php artisan key:generate
$ sudo chmod -R guo+w storage
This will install all Laravel dependencies via composer, generates an application key and applies the necessary rights to the storage directories. Your Laravel project should now work and you can access it locally in your browser.
But How? I don't have a domain!
To test your vHost set up locally, you can change your host file of your laptop / desktop manually. Mac and Linux users can do it like this:
$ sudo vim /etc/hosts
And add the following line – please make sure that you change the IP to your local IP of your PI and also the domain name.
192.168.1.10 hello.aboe.eu
# 192.168.1.10 needs to replaced by your local IP of your PI.
# Most likely this is not the same address as in this example.
If you now switch to your web browser of choice and key in hello.aboe.eu in the address bar – you should be greeted with this amazing website:
aboe.euIsn’t that great?
In the next post, I’ll go through all the steps to make your website public.
Since this is my first post here on aboe.eu, please let me know what you think of it in the comments down below.
Any feedback is appreciated. If it remotely contains anything negative, I'll delete it anyway – naah just kidding, or will I?
I can only say so much that there was only positive feedback so far. Pretty astonishing, right?