Running Laravel On Lightweight Torch Vagrant Box

Posted on 28 August, 2018
Simple, step by step instructions on how to setup Laravel project on Lightweight Torch Vagrant box

Prerequisites

  • Unix environment: MacOS or any Linux distribution
  • you should be comfortable with terminal
  • basic Git knowledge

What Is Torch Anyway

Torch is lightweight Vagrant box (below 500MB) based on Ubuntu OS, with PHP and Apache as a HTTP server. It was initially created for in-house development. After many hours using it, we decided - why not share it publicly. Hope some of you will find it useful.

You can find more info on official github repository.

Setup Torch Vagrant Box

If you do not have vagrant boxes dir yet, create one in your user home directory

mkdir ~/vagrant-boxes

# ... go to box dir
cd ~/vagrant-boxes

From there, clone Torch GitHub repo

git clone https://github.com/movor/torch.git

# ... and navigate to created directory
cd torch

For this tutorial we'll use Torch version v0.1. It has PHP 7.0.30 preinstalled and it's compatible with Laravel 5.5.

You can find more about version compatibility and Torch project itself on official Torch GitHub Repo Checkout "Compatibility" section and find about versions support and compatibility

Let's switch to branch 0.1 for the corresponding Torch version (v0.1)

git checkout 0.1

Here, you'll need to edit Vagrantfile with your favorite editor, mine is Vim

vim Vagrantfile

Luckily, there is only one line you'll need to uncomment and change

# config.vm.synced_folder "~/Development/web", "/var/www", :mount_options => ["dmode=777", "fmode=777"]

Actually, only "~/Develompent/web" needs to be replaced by your web projects dir.

If you do not have projects dir yet, let's create it with

mkdir ~/webdev

After changing the Vagrantfile accordingly, the line should look like one bellow

config.vm.synced_folder "~/webdev", "/var/www", :mount_options => ["dmode=777", "fmode=777"]

Torch will mount this directory internally to /var/www, so every project you have in there will be accessible from within this box and Apache can serve them all - directly to your browser.

Setup Host Locally

Let's assume that we want to access our Laravel project by typing http://laravel.loc in the browser. For that we'll need to do 2 things.

Append line to /etc/hosts file on local machine with

echo "192.168.11.11 laravel.loc" | sudo tee -a /etc/hosts

This will tell your browser that when you visit http://laravel.locit should look at predefined IP address rather than Internet. In this case this is the IP of our box itself.

Light The Torch

Lets start Torch Vagrant box. This will take some time, as box needs to be downloaded and started.

vagrant up

After box has been started, type

vagrant ssh

and we're in.

Setting Up Laravel 5.5

Navigate to web directory within our Torch box (this will correspond to ~/webdev) directory on our host machine. From here, we need to clone Laravel 5.5.

Cloning Laravel From Github

cd /var/www
git clone --single-branch -b 5.5 https://github.com/laravel/laravel.git
cd laravel

Installing Dependencies

Let's install PHP dependencies using Composer

composer install

Setting Up Environment File

cp .env.example .env

Torch is compatible with default Laravel installation so corresponding user and database homestead has already been pre-set in the box, thus, for now there is no need to edit .env file

Generate key for our application

php artisan key:generate

Browse The Project

Now let's open our favorite browser and navigate to http://laravel.loc

Don't forget to prefix url with http:// or most browsers will take you to the google search page for laravel.loc term.

It should display Laravel project default welcome page.

Interact With The Database

Let's make sure our environment and Laraver project is fully functional and can read and write form the database.

For this purpose we'll generate basic authentication scaffolding provided by Laravel.

Migrations

Before we continue we should run default migrations. They'll create 3 tables: users, password_resets and migrations.

php artisan migrate 

Scaffolding

Finally, run the artisan command which will take care of generating auth scaffolding.

php artisan make:auth

If we visit our project url again - http://larave.loc, in the upper right corner there should be "LOGIN" and "REGISTER" links. Clicking on "REGISTER" will take us to user registration page created by scaffolding command and there we can register our users.

User registration and login should go smoothly. This means that we successfully set up whole Laravel project.