Using Raspberry Pi for Laravel developing

This article can be viewed in portuguese here.

If you are a Laravel developer and can’t afford a Mac or can’t migrate definitely for Linux distro because it lacks an app (Photoshop in my case), chances are high that you already got a bit frustatred in doing so in a Windows. If you also use Laravel Mix for some assets managing, like babel and sass, you have probabably got really frustrated at a point. It doesn’t matter how powerfull your Windows rig is, it’s going to be slow — mainly doing Composer install or handling Node.js.

There are a few options out there though:

  • Git Bash. It is possible to run everything you would do in a Linux distro inside the Git Bash shell that comes with Git for Windows, and it works really nice! Until then, this was my favorite method.
  • Homestead. Homestead is a nice little Ubuntu Vagrant box packed with everything you might need to do some Laravel developing, but according to personal experience and community feedback, it kinda sucks in Windows, in matters of speed mainly.
  • A dedicated Linux for the job. This is the case we will cover here, more specifically using a Raspberry Pi (It’s very cheap). If you have an old computer lying around it is probably worth a try either!

Anyway, what we want:

A Raspberry Pi device running a Laravel 5.5 application with PHP’s built-in server, automatically compiling frontend assets with Laravel Mix and serving all through the local network. We also want the project’s folder to be easily accessible through our local network, making it feel like we are editing the files from Windows inside our Raspberry Pi.

Requirements:

  • Raspberry Pi 3 (it will probably work on older models too though)
  • Local network
  • Lastest Raspbian (currently Stretch)
  • A decent command line in Windows. What I recommend: Git Bash alongside the Cmder or Hyper emulators. If you can’t or don’t want to use Git Bash, Cmder includes Clink which provides a few usefull Unix commands in Window’s command line.

Basic Raspberry Pi setup (OPTIONAL)

Let’s configure remote access to the Pi, expand the filesystem and disable the GUI.

Remote access

With a keyboard and screen conected to your Pi (soon you won’t need them anymore)

  1. Entersudo raspi-config in the CLI.
  2. ChooseInterfacing Options and afterSSH
  3. Pickyes and close the menu

Now execute hostname -Ito find out what is your Pi’s local IP. Back to the computer, we can now access the Pi withssh pi@YOUR-PI-IP Example: ssh pi@192.168.1.38

The default password is “raspberry”. It’s likelly that in the first connection you will have to type yes to confirm you trust the host.

Let’s configure a key so we won’t need to type the password again. If you don’t have a key on your PC, generate one using ssh-keygen -t rsa and confirm all the questions with an enter. The key will be stored in a .ssh folder in your home folder inside two files: id_rsa and id_rsa.pub.

Back to your PC now execute ssh-copy-id -i path/to/id_rsa.pub pi@YOUR-PI-IP Example: ssh-copy-id -i ~/.ssh/id_rsa.pub pi@192.168.1.38 Type the password for the last time and it’s done! 😮

Disabling the GUI and expanding the filesystem

Since we will do all the work in the remote CLI, the GUI and keyboard are no longer needed. We will also expand the filesystem to use the whole SD Card (Raspbian doesn’t do that by default).

In your remote CLI type sudo raspi-config

  • Navigate to Boot options , then Desktop / CLI and selectConsole
  • Back to the main menu, go toAdvanced Options and thenExpand Filesystem. When requested, reboot your Pi.

Sharing the Raspberry files across your local network

Depending on a FTP, SFTP and such here would just blow away all the fun, so let’s setup a folder sharing from your Pi to your Windows. After this, you will be able to open your project’s folder in your editor or IDE naturally like it was inside Windows.

In your Pi, do the following:

  • sudo apt install samba samba-common-bin -y
  • sudo vim /etc/samba/smb.conf (Replace vim with any CLI editor you like or simply install vim with sudo apt install vim -y)
  • Find the following lines and edit them like follows: (The workgroup name can be anything alphanumeric)
workgroup = nicetidygroupy
wins support = yes
  • Add this at the end of the file (In this case we are sharing the whole home folder from the Pi user):
[pihome]
comment= Pi Home
path=/home/pi
browseable=Yes
writeable=Yes
only guest=no
create mask=0777
directory mask=0777
public=no
  • Save smb.conf file and do configure the sharing login with smbpasswd -a pi (Since this a local development server, I recommend using the same password from the Pi user — by default “raspberry”)
  • Restart samba so we can be sure everything will be ok: sudo /etc/init.d/samba restart

If everything went well, it’s likelly your Pi will now appear in your Windows network tab. When asked for a password, use the login “pi” alongside the password you defined in the last step.

Sharing Pi’s home to Windows

If your devices are connected through cable, the experience here will be just perfect, but it works really well in Wi-Fi too.


Installing PHP 7.0 and Composer on Raspberry Pi

In order to install PHP 7.0 and some of Laravel’s mandatory and opcional dependencies, execute the following:

sudo apt install php7.0 php7.0-curl php7.0-gd php7.0-imap php7.0-json php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-xmlrpc php7.0-xml php7.0-fpm php7.0-zip -y

Check if everything is ok by doingphp -v . The output should be something like:

It works!

Installing Composer

Now that we have PHP, do the following to install Composer:

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

Check if went well withcomposer --version

🆒

Installing MariaDB (mysql) on Raspberry Pi

MariaDB is and open source alternative to Oracle’s MySQL.

In order to install it, execute: sudo apt install mariadb-server mariadb-client -y

Once again check if everything worked withmysql --version :

🍻

After installed you can access it withsudo mysql -u root -p .

Considering we are setting up a development only environment here, let’s make things easier by creating a new user and giving it full access to MariaDB. This will discard the need to sudoin MariaDB.

Execute sudo mysql -u root -p to access MariaDB and then do the following queries, replacing what is in bold with what you prefer.

CREATE USER 'user'@'localhost' IDENTIFIED BY 'pass';
GRANT ALL PRIVILEGES ON * . * TO 'user'@'localhost';
FLUSH PRIVILEGES;

After that you can access MariaDB by simply doing mysql -u usuario -p . You can also use these credentials in your application.

Installing Node.js on Raspberry Pi (OPTIONAL)

If you do assets compiling in your project, you will likely need to configure Node.js. Raspbian’s current version comes with Node.js 4.* by default, which doesn’t work nice with most of Laravel Mix dependencies, for example. Instead, let’s configure the 8.* version. Execute the following:

$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
$ sudo apt install nodejs

Check if everything is ok with node -v

Creating a new Laravel app on Raspberry Pi

With all the system dependecies set, let’s create a new project. In case you are cloning an existing one, head down to the bottom of this section.

  • Create a new folder to hold our projects withmkdir ~/projectsand access it withcd ~/projects
  • Execute the following to create a new project based on Laravel’s default skeleton: composer create-project --prefer-dist laravel/laravel the-coolest-project-name

Everything should run fine and the project will be created! If composer complains that something is missing, try searching the package’s name and installing it with sudo apt install .

In this step Composer already installed all of Laravel’s backend dependencies.

Cloning an existing project

In case the project already exists, acess the projects folder created in the last step and do the following:

  • Clone your project using git. Example: git clone git@github.com:roniemeque/nice-little-app.git
  • acess the created folder with cd nice-little-app and install all of Laravel’s backend dependencies with composer install
  • configure your .env file, generate the app’s key, run your migrations and the usual Laravel setup.

Installing the dependencies for assets manipulation with NPM

If you use some kind of compilling, polifylling, minification and such and you have installed Node.js in your Pi, now it’s time to install the project’s dependencies. Whether it’s a new or existing app, execute npm install in its folder.

Important detail: the output of npm’s installs in Raspbian is a lot more verbose and slower than the usual, but don’t worry because after everything is installed it will work like it should, depending on your project depencies, of course. Laravel Mix’s dependencies run perfectly in here so far!


Now that everything is working, let’s head to the final steps:

Running Laravel on Raspberry Pi with PHP’s built-in server

Let’s use artisan to manage the server.

Execute the following: php artisan serve --host=YOUR-PI-IP (If you can’t remember the IP of your Pi, use hostname -I to see it).

Get ready now, it’s coming! In your PC, browse to http://YOUR-PI-IP:8000 and see the result. Example: http://192.168.1.38:8000

Automatically manipulating frontend assets with Laravel Mix on Raspberry Pi

If you don’t use Laravel Mix, oh boy you should. It’s an excellent wrapper around Webpack that makes working with Webpack seems like a breeze. You can even use it in non-laravel projects!

Access your Pi in a new console tab without closing the one running the server and execute npm run dev inside your project’s folder. If all of your Sass, Less, Coffe, Typescript and etcetera compiled correctly, let’s set the compiling to happen automatically everytime a file is saved with npm run watch .

Note on Browsersync

If you use Browsersync alongside your Laravel Mix, you can still use it here, but not directly, of course. Configure your Browsersync so it won’t open any browser by default and you will be able to use it by browsing to the remote host Browsersync provides. (It will pop-up in the first output from npm run watch ) — your online option must be set to true also, but in Laravel Mix it is set by default.

If you would like to disable Browsersync at all everytime you use your Pi for developing but you don’t want to keep changing the webpack.mix.js settings and discarding it everytime, Laravel provides a nice way to solve this:

In your .env file, add something like this:

MIX_BROWSERSYNC=false

Now change your Browsersync call inside you webpack.mix.js file so it is surrounded by the following condition:

if(process.env.MIX_BROWSERSYNC){                                                                                                                                  
mix.browserSync({your options go here});
}

With that you can tell Laravel Mix that you don’t want Browsersync in this environment. Don’t forget to set it to true in the environments you want it enabled.


And that’s it

At this point you probably have a very decent and fine development environment on your Pi. It is also portable! Don’t forget to always check your IP and network settings in case you plan on carrying it around though.

It’s likelly there are tons of ways to accomplish this procedure and it’s possible by the time you read this some things will be outdated. It’s very important to always pay attention to the names of the packages you are installing and the output of the commands you run. If you are doing this it’s quite probable you are used to being frustrated with broken packages or doing a lot of Google to fix tiny hidden errors that will only happen in your system.

I really hope this post will help anyone that tries to accomplish a similar result in using the Raspberry Pi as a development server, regardless of your PHP framework, versions and project requirements.