Setting up a Linux Laravel Virtual Machine on Windows

This is something I do fairly regularly but a little less recently as I have started collating projects into one server to make it a little simpler and reducing space needs.

Environment

This guide is based around a Windows controlled, Linux based virtual machine using Ubuntu 16.04 

Prerequisites

The prerequisites to this guide is that you have a virtual machine controller installed such as Oracle's Virtualbox Once this is installed or a similar system is setup (although I don't have experience with these so will be continuing with VirtualBox for my examples), install Vagrant.

Workspace

To get started you need a working directory to base from. You can set this up anywhere, from your documents, to a network drive location but this is entirely up to you. For the purposes of this guide I will be using E:\Projects\Laravel 

Setup process

To get started open a command prompt window, Start > cmd > Enter

Change directory to the project directory
H:
cd \Projects\Laravel

Note: If your directory is on a network share, right click in explorer while holding shift. You should get the option to open a command window here or a PowerShell window. Either should be fine.

Type in "vagrant init" and press return

A new file will be created in the directory.

Modify this file with notepad and paste in the following:

Vagrant.configure("2") do |config|  config.vm.box = 'bento/ubuntu-16.04'  config.vm.network "forwarded_port", guest: 80, host: 8080  config.vm.network "forwarded_port", guest: 22, host: 2222  config.vm.synced_folder '.', '/var/www'
  config.vm.provider "virtualbox" do |vb|    vb.customize ["modifyvm", :id, "--memory", "1024"]    vb.customize ["modifyvm", :id, "--name", "Laravel 16.04"]  endend

You can get rid of anything else in the file. If you use port 8080 or 2222 for anything, modify them here. Make a note though because you will need them!

Now your ready to start your virtual machine

Type in "vagrant up" and the virtual machine should download and boot.

Once the machine is up, load up an SSH client such as Putty

Fill in hostname as localhost and the port as 2222 or whatever you changed it to, make sure SSH is selected and press open.

You should now have a login screen.

Username: vagrant
Password: vagrant

You now have a virtual machine at your fingertips!

Note: To shut down the virtual machine you can either shutdown from the SSH connection using the shutdown command or type in "vagrant halt" at the command prompt in the working directory.

run the following commands in your SSH terminal, adjust the projectname to the name of your project on the line highlighted in bold. You may be prompted for a password for the MySQL server during installation.

sudo apt-get update 
sudo apt-get install apache2 php7.0 mysql-server libapache2-mod-php7.0 composer curl php7.0-mbstring php7.0-dom php7.0-db php7.0-pdo php7.0-zip php7.0-mysql

cd /var/www

composer global require laravel/installer

composer create-project --prefer-dist laravel/laravel projectname

sudo a2enmod rewrite
Next run the command below to edit the default site the apache configuration is setup for:


sudo nano /etc/apache2/sites-available/000-default.conf

Change or add the following within the file you have just opened, change the public route to whatever you called it above in the bold section
DocumentRoot /var/www/path/to/public
<Directory "/var/www/path/to/public">
    Options All   
    AllowOverride All
</Directory>
Now press CTRL + O and press enter, then press CTRL + X. This saves the file and closes it.

Now run the following commands, adjust the line in bold to match your project directory NOT the public directory:

sudo service apache2 restart
cd /var/www/path/to/project
cp .env.example .env
php artisan key:generate

Visit http://localhost:8080 (or the port you set) in a browser. You should see the Laravel welcome page. Open your project directory on your host system now (not the VM / SSH session). Navigate to the following directory:

app/route/web.php

Copy the existing route and paste it below, add test after the forward slash and then see if this route works in the browser (http://localhost:8080/test). If this does, you are ready to start developing with Laravel!

A little tip. Run composer update to sort out the composer.json file before you try anything complex. It'll really help if you need other injected packages later!

Comments