valentin vannay


Installation of a Web Server and Laravel 5 on a Raspberry PI 2


I waited a long time but I finally did it, I bought a Raspberry PI 2. When the mail man knocked at my door this morning, I was like a little boy the day before christmas. I open the tiny box and here it was lying on bubble wrap waiting to be plugged and played with. I actually bought it for a project I will explain in another article. But I always wondered if the PI 2 would be a viable option for a webserver hosting a blog or a small personal site. *spoiler alert* I think the result is very satisfying so I want to share the process of installing an Apache web server with PHP, MySQL and let's be a little crazy here, Laravel 5. If you prefer a video I made a tutorial available here. And before we start, if you want to see how fast a website can be when hosted on a raspberry. I have this one hosted on a RPI2 (in South Korea):    

Raspberry PI setup

The raspberry itself doesn't need much configuration.All you have to do is:
  • Grab a copy of a rasbian image on the raspberry official website
    • Download Jessie if you want to install PHP 5.5 or higher
  • Copy / Install it on your SD card following the instructions given by raspberry
  • Fix an IP address if you which to access your PI a little more easily (optional)
  • Allow the full SD capacity to the rasbian OS

Static IP address

To set a static IP address we have to modify the dhcpdc.conf file. It took me a while to understand that since early 2015 on the new rasbian version we don't configure a static IP address in etc/network/interfaces anymore.. The correct way now is then
sudo nano /etc/dhcpcd.conf
And at the end of the file add
interface eth0
static ip_address=
static routers=
static domain_name_servers=
The ip_address is the ip you want for your raspberry, the routers is the IP of your router (gateway) and the domain_name_servers is your DNS server. ( to use the one from google).

Raspberry basic setting

Something I didn't know you have to do almost immediatley after you install your PI, is to ensure that all the SD card storage is available to the OS. You can do that by entering the raspberry settings.
sudo raspi-config
Select the first option and confirm. You can then if you wish check the available storage on your SD card.
df -h
You should see something like this
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        30G  3.7G   25G  14% /
devtmpfs        459M     0  459M   0% /dev
tmpfs           463M     0  463M   0% /dev/shm
tmpfs           463M  6.3M  457M   2% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           463M     0  463M   0% /sys/fs/cgroup
/dev/mmcblk0p1   60M   20M   41M  34% /boot
tmpfs            93M     0   93M   0% /run/user/1000
Finally update and upgrad apt-get install
sudo apt-get update && sudo apt-get upgrade

Apache setup

If you are familiar with a webserver installation and wish to install everything you need for Laravel in only one command I recommend using
apt-get install mysql-server mysql-client apache2 apache2-utils php5 php5-cli libapache2-mod-php5 php5-mysql php5-curl php5-gd php-pear php5-imagick php5-mcrypt php5-memcache php5-mhash php5-sqlite php5-xmlrpc php5-xsl php5-json php5-dev libpcre3-dev
Source   But allow me here to break it down and explain separatly the installation of Apache, MySQL and PHP. Let's start with Apache2
sudo apt-get install apache2 apache2-utils
Once the installation is done there is a few basic configurations to be able to run Laravel properly.
  • Enable mod-rewrite
  • Allow Override on www
  • Change the document root path (optional)
  • Grant www-data the ownership of the www directory


This module allow urls rewriting. To enable it
sudo a2enmod rewrite

Allow Override

The mod-rewrite wil not work correclty if you don't allow override on the www directory. In older versions of apache the configuration file was called httpd.conf. But now the configurations directives are splitted into different files. All the configuration files are normally located under /etc/apache2/ and the one we are interessted in now is apache2.conf
sudo nano /etc/apache2/apache2.conf
And change AllowOverride None to AllowOverride All
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted

Change the default document root (optional)

The default document root in Apache2 is /var/www/html which I personally don't uderstand. So If you wish to change it to /var/www edit the 000-default.conf file
sudo nano /etc/apache2/sites-available/000-default.conf
And edit the DocumentRoot directive. Some people might want to have their document root somewhere else or use virtual hosts but this won't be covered in this article.

Give www-data the ownership of the document root

I want to warn you that I am not an expert of linux and Apache's permissions and rights. But what I usually do is that I recursivly give the ownership to the default Apache user (www-data) to my www directory. And everytime I create a subdirectory to www I give the ownership of the directory to www-data. To do so
sudo chown -R www-data:www-data /var/www/
sudo chmod g+rw -R /var/www
sudo chmod g+s -R /var/www
You might also want to give permissions to the default user (pi) in order to be able to modify files through SFTP. The easy solution is to simply add the pi user to the www-data group
sudo usermod -a -G www-data pi
  Finally restart your apache
sudo /etc/init.d/apache2 restart

MySQL setup

MySQL installation is very straightforward
sudo apt-get install mysql-server
Then you will be prompt to enter a password for the root user Finish off by installing mySQL client
sudo apt-get install mysql-client

PHP Setup

Last but not least simply install php latest version and a few basic modules with this command
sudo apt-get install php5 php5-cli libapache2-mod-php5 php5-mysql php5-curl php5-gd php-pear php5-imagick php5-mcrypt php5-memcache php5-mhash php5-sqlite php5-xmlrpc php5-xsl php5-json php5-dev libpcre3-dev
Fairly easy ^^  

Laravel 5

Begin by installing composer
sudo curl -sS | php
Then you can simply follow the Laravel Official Documentation and install Laravel with composer
sudo ~/composer.phar global require "laravel/installer"
Note: The installation path of composer is ~/composer.phar So if you didn't register ~/.composer/vendor/bindirectory in your PATH you have to use composer full path to run it
sudo ~/composer.phar global require "laravel/installer"
  Then create a new laravel project with composer (or with the laravel command)
composer create-project --prefer-dist laravel/laravel nameOfYourSite
And most importantly set the correct permissions of the laravel directory ! On the official doc they just say: "After installing Laravel, you may need to configure some permissions. " ... I think it's a little light for an explanation. ^^ So I recommend again, to recursivly give the ownership of the laravel directories to www-data. But I believe that if you only give it to the /storage and /vendor folder it works too. But it depends of the Laravel version. But again I am not a security expert !! Now go to your browser, enter the IP address of your PI and say Hi to Laravel !