Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Infrastructure as Code Cookbook

You're reading from   Infrastructure as Code Cookbook Automate complex infrastructures

Arrow left icon
Product type Paperback
Published in Feb 2017
Publisher Packt
ISBN-13 9781786464910
Length 440 pages
Edition 1st Edition
Arrow right icon
Authors (2):
Arrow left icon
Pierre Pomès Pierre Pomès
Author Profile Icon Pierre Pomès
Pierre Pomès
Stephane Jourdan Stephane Jourdan
Author Profile Icon Stephane Jourdan
Stephane Jourdan
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Vagrant Development Environments FREE CHAPTER 2. Provisioning IaaS with Terraform 3. Going Further with Terraform 4. Automating Complete Infrastructures with Terraform 5. Provisioning the Last Mile with Cloud-Init 6. Fundamentals of Managing Servers with Chef and Puppet 7. Testing and Writing Better Infrastructure Code with Chef and Puppet 8. Maintaining Systems Using Chef and Puppet 9. Working with Docker 10. Maintaining Docker Containers Index

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:

  1. We want Extra Packages for Enterprise Linux (EPEL):
    sudo yum install -q -y epel-release
    
  2. We want PHP-FPM:
    sudo yum install -q -y php-fpm
    
  3. 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
    
  4. Install a bunch of PHP dependencies:
    sudo yum install -q -y php-pdo php-mcrypt php-mysql php-cli php-mbstring php-dom
    
  5. Install Composer:
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    sudo chmod +x /usr/local/bin/composer
    
  6. Install and ship a good enough NGINX configuration:
    sudo yum install -q -y nginx
    sudo cp /vagrant/nginx.conf /etc/nginx/nginx.conf
    
  7. Install MariaDB Server:
    sudo yum install -q -y mariadb-server
    
  8. 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
You have been reading a chapter from
Infrastructure as Code Cookbook
Published in: Feb 2017
Publisher: Packt
ISBN-13: 9781786464910
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image