My current web application requires me to use Firebase to authenticate and send push notifications to a mobile app. The best package that I've found that works is Jérôme Gamez's firebase-php and its Laravel package, laravel-firebase. All the other packages I have found either don't work or have not been updated in 5 years.
Unfortunately, in order to access Firebase's database (firestore) with the package, Google's google/cloud-firestore package is required. And that requires the grpc php extension.
The grpc extension is pretty niche, at least when it comes to Laravel applications, so not many hosting options that I have found support it. Or at least first-party options like Laravel Vapor and Laravel Cloud have yet to add support.
Custom servers are the only option I have found that is viable, and luckily, Laravel Forge was recently revamped and added Laravel VPS servers that are pretty neat. Laravel Forge also has a very cool Recipe system to run bash scripts across on the servers it manages. This makes installing gRPC quite easy on Laravel Forge.
At the core, the "easiest" option to install gRPC is by using pecl: sudo pecl install grpc. However, there are a few issues with that. Firstly, it takes a long time to build. A really, really, long time. I'm talking more than an hour in some cases.
Additionally, every time I ran it on a small server (1GB / 2GB RAM), it would always fail. I kept getting the error g++: fatal error: Killed signal terminated program cc1plus, and after a bit of research, I decided to resize my Laravel VPS server to 4GB of ram to try to install gRPC.
It worked, but even after upscaling to a 4GB RAM Laravel VPS server, it took upwards of 75 minutes to install. And the inexperienced me didn't realize I can't downsize a Laravel VPS server.
Caution
You cannot downsize Laravel VPS servers to a smaller specification. You can only resize to a larger server size.
As I am still in the development phase, I'd rather not spend ~$25/mo on a 4GB server. After some more research, I found a thread that discussed the slow build times for gRPC and a few methods to speed it up, specifically in Docker environments.
One comment had a Docker script for a Debian-based environment that I was able to adapt for a server. The recipes that I made are based on this script. I am relatively new to installing things on servers myself, so if there are any issues with my recipes, please let me know so I can update them.
Using this method, I was able to install gRPC in as little as 3 minutes.
Here are the two recipes I use. One to install gRPC, and one to install Protobuf. Protobuf is sometimes needed when using gRPC, but it may not be necessary for everyone.
- Navigate to the "Recipe" tab on the page for your organization.
- Hit the "+ New recipe" button.
- Provide a name, select the user to run the recipe, and enter the script.
- Once the recipe is created, hit the "Run" button on the recipe page.
- Select the servers you want to run the recipe on and then hit "Run".
Name: Install gRPC, Run as: root
# Install all dependencies needed for gRPC compilation
sudo apt-get update && \
apt-get install -y --no-install-recommends \
git \
make \
libgrpc-dev \
libgrpc++-dev \
protobuf-compiler \
build-essential \
pkg-config \
libssl-dev \
zlib1g-dev \
autoconf \
automake \
libtool \
cmake \
&& rm -rf /var/lib/apt/lists/*
# Compile and install gRPC extension
sudo git clone --depth 1 -b v1.63.0 https://github.com/grpc/grpc /tmp/grpc && \
cd /tmp/grpc/src/php/ext/grpc && \
phpize && \
./configure && \
make && \
make install && \
rm -rf /tmp/grpc && \
apt-get autoremove -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
echo "Add 'extension=grpc' to both the PHP CLI and PHP-FPM configurations."
echo "Run 'php -m | grep grpc' in a terminal to check if it is properly set up."Name: Install Protobuf, Run as: root
sudo pecl install protobuf
echo "Add 'extension=protobuf' to both the PHP CLI and PHP-FPM configurations."
echo "Run 'php -m | grep protobuf' in a terminal to check if it is properly set up."Remember to add the extensions to both the PHP CLI and PHP-FPM configurations on your server!
- In Forge, navigate to your server's "PHP" tab.
- On the PHP settings page, hit the "..." button on the PHP version you have installed.
- Select "Edit PHP-FPM configuration".
- Add
extension=grpcand/orextension=protobuf. - Repeat for "Edit PHP CLI configuration".
I hope these scripts can be of use for you!