Showing your work on the LAN while working with Laravel
You're working on your application using Laravel, the free and open source PHP framework (https://laravel.com/), and you'd like to showcase your work to your colleagues. Using a Vagrant development environment can help keep your work machine clean and allow you to use your usual tools and editors while using an infrastructure close to production.
In this example, we'll deploy a CentOS 7 server, with NGINX, PHP-FPM, and MariaDB, all the PHP dependencies, and install Composer. You can build from this example and others in this book to create an environment that mimics production (three-tier, multiple machines, and other characteristics).
This environment will be available for access to all your coworkers on your network, and the code will be accessible to you locally.
Getting ready
To step through this recipe, you will need the following:
- A working Vagrant installation
- A working VirtualBox or VMware installation
- An Internet connection
How to do it…
Let's start with the simplest Vagrant environment we know:
Vagrant.configure("2") do |config| config.vm.box = "bento/centos-7.2" config.vm.define "srv-1" do |config| config.vm.hostname = "srv-1" end end
A sample NGINX configuration for Laravel
Configuring NGINX for Laravel is out of the scope for this book, but for reference, here's a simple NGINX configuration that will work well for us, listening on HTTP, serving files located on /srv/app/public
, and using PHP-FPM (the file name is nginx.conf
):
events { worker_connections 1024; } http { sendfile off; server { listen 80; server_name _; root /srv/app/public ; try_files $uri $uri/ /index.php?q=$uri&$args; index index.php; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; include fastcgi_params; } } }
Simple shell provisioning
We'll create a provisioning script that we'll name as provision.sh
, which contains all the steps we need to have a fully working Laravel environment. The details are out of the scope of this book, but here are the steps:
- We want Extra Packages for Enterprise Linux (EPEL):
sudo yum install -q -y epel-release
- We want PHP-FPM:
sudo yum install -q -y php-fpm
- We want PHP-FPM to run as the Vagrant user so we have the rights:
sudo sed -i 's/user = apache/user = vagrant/' /etc/php-fpm.d/www.conf
- Install a bunch of PHP dependencies:
sudo yum install -q -y php-pdo php-mcrypt php-mysql php-cli php-mbstring php-dom
- Install Composer:
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer sudo chmod +x /usr/local/bin/composer
- Install and ship a good enough NGINX configuration:
sudo yum install -q -y nginx sudo cp /vagrant/nginx.conf /etc/nginx/nginx.conf
- Install MariaDB Server:
sudo yum install -q -y mariadb-server
- Start all the services:
sudo systemctl enable php-fpm sudo systemctl start php-fpm sudo systemctl enable nginx sudo systemctl start nginx sudo systemctl enable mariadb sudo systemctl start mariadb
Enable provisioning
To enable provisioning using our script, add the following code in the VM definition block:
config.vm.provision :shell, :path => "provision.sh"
Shared folder
To share the src
folder between your host and the Vagrant VM under /srv/app
, you can add the following code:
config.vm.synced_folder "src/", "/srv/app"
Public LAN Networking
The last thing we need to do now is to add a network interface to our Vagrant virtual machine, that will be on the real LAN, so our coworkers will access it easily through the network:
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"
Adapt the name of your network adapter to use (this was on a Mac, as you can guess) to your needs. Another solution is not to specify any adapter name, so you will be presented a list of possible adapters to bridge:
==> srv-1: Available bridged network interfaces: 1) en0: Wi-Fi (AirPort) [...]
Start the Vagrant environment (vagrant up
), and when it's available, you can execute commands such as finding out the network information: vagrant ssh -c "ip addr"
. Your mileage will vary, but in this network, the public IP of this Vagrant box is 192.168.1.106
, so our work is available.
Now you can start coding in the ./src/
folder. This is not a Laravel book, but a way to create a new project in a clean directory is as follows:
cd /srv/app composer create-project --prefer-dist laravel/laravel.
Don't forget to remove all files from the folder beforehand. Navigate to http://local-ip/
and you'll see the default Laravel welcome screen.
To verify the file sharing sync is working correctly, edit the ./resources/views/welcome.blade.php
file and reload your browser to see the change reflected.
There's more…
If you include the Vagrantfile directly with your project's code, coworkers or contributors will only have to run vagrant up
to see it running.
Other Vagrantfile sharing options include Windows Sharing (smb), rsync (useful with remote virtual machines such as on AWS EC2), and even NFS.
A noticeable bug in the sharing feature using VirtualBox leads to corrupted or non-updating files. The workaround is to deactivate in the web server configuration sendfile
, using NGINX:
sendfile off;
Using Apache, it is as follows:
EnableSendfile Off