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): www.saltcake.com
Raspberry PI setupThe 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 addressTo 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
And at the end of the file add
sudo nano /etc/dhcpcd.conf
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. (126.96.36.199 to use the one from google).
interface eth0 static ip_address=192.168.0.10/24 static routers=192.168.0.1 static domain_name_servers=188.8.131.52
Raspberry basic settingSomething 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.
Select the first option and confirm. You can then if you wish check the available storage on your SD card.
You should see something like this
Finally update and upgrad apt-get install
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
sudo apt-get update && sudo apt-get upgrade
Apache setupIf you are familiar with a webserver installation and wish to install everything you need for Laravel in only one command I recommend using
Source But allow me here to break it down and explain separatly the installation of Apache, MySQL and PHP. Let's start with Apache2
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
Once the installation is done there is a few basic configurations to be able to run Laravel properly.
sudo apt-get install apache2 apache2-utils
- Enable mod-rewrite
- Allow Override on www
- Change the document root path (optional)
- Grant www-data the ownership of the www directory
Mod-RewriteThis module allow urls rewriting. To enable it
sudo a2enmod rewrite
Allow OverrideThe 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
And change AllowOverride None to AllowOverride All
sudo nano /etc/apache2/apache2.conf
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
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
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.
sudo nano /etc/apache2/sites-available/000-default.conf
Give www-data the ownership of the document rootI 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/
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 chmod g+rw -R /var/www sudo chmod g+s -R /var/www
Finally restart your apache
sudo usermod -a -G www-data pi
sudo /etc/init.d/apache2 restart
MySQL setupMySQL installation is very straightforward
Then you will be prompt to enter a password for the root user Finish off by installing mySQL client
sudo apt-get install mysql-server
sudo apt-get install mysql-client
PHP SetupLast but not least simply install php latest version and a few basic modules with this command
Fairly easy ^^
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
Laravel 5Begin by installing composer
Then you can simply follow the Laravel Official Documentation and install Laravel with composer
sudo curl -sS https://getcomposer.org/installer | php
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)
sudo ~/composer.phar global require "laravel/installer"
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 !
composer create-project --prefer-dist laravel/laravel nameOfYourSite