Marching With Atticus Finch

My father just left to drive from St. Louis to Alabama to spend six weeks volunteering in the Black community of Montgomery, tutoring students and registering voters. While he’s there, I’ll send him…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




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:

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.

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

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

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.

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

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:

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.

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

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

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

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

Check if went well withcomposer --version

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.

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

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:

Check if everything is ok with node -v

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.

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.

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

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:

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).

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 .

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:

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

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.

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.

Add a comment

Related posts:

WHAT I LEARNED FROM NAS DAILY.

Fast forward to a month ago, I see one of my favorite travel vloggers, aka Nas Daily, setting up his first boot camp to go in Ecuador, travel with him, learn from him, work with him. I sent a video…

Could Gene Editing Cure Rare Diseases Within Our Lifetime?

Prime Medicine developed spellcheck and autocorrect for DNA. Their technology could fix 90% of genetic errors which cause disease, within our lifetime.

My Venture As A Golf Company Entrepreneur

As an avid golfer since my college days, golf has been an important part of my life over many years. There was a time when my partners and I owned a golf business. It was my first venture into having…