Getting Started with Laravel: Frameworks, Virtualization, and Homestead
by G. Forrest
Whether you're a veteran in the development world or you're just opening its doors, you should understand the general purpose of software frameworks: to make the entire web development process more efficient and productive. They simplify the majority of development tasks — rendering data, abstracting database access, routing user requests, and allowing manipulation of HTTP requests and responses.
Server-side web frameworks make it easier to write, maintain and scale web applications. They provide tools and libraries that simplify common web development tasks, including routing URLs to appropriate handlers, interacting with databases, supporting sessions and user authorization, formatting output (e.g. HTML, JSON, XML), and improving security against web attacks...While many frameworks come with default libraries or tools that address every problem their developers can think of, many others are more lightweight that require web developers to choose solutions from separate libraries. — Server Side Web Frameworks - MDN
Choosing between the two would depend on the environment, time scheme, or needs of the project. Keep in mind all frameworks can and will be frequently undergoing development, which can result in the deprecation of something you may have spent a sizable amount of time learning. But learning that particular thing is very likely to help you down the road somewhere.
The Rails framework is written in Ruby, and they are typically used together. Python's most popular frameworks are Flask or Django. Common JavaScript frameworks are Node, Ember to name a few. For VB, C, C# — the .NET Framework. For PHP alone, frameworks include Laravel, CodeIgniter, Cake, Symfony, Zend, Aura, Slim, and Yii, and many others.
After building multiple applications using .NET MVC, I found Laravel to be quite similar in approach and methodology. Whether you are a beginner in software or just curious about software languages and frameworks, this will give you a quick run-down of using Laravel.
Laravel is a free, open-source PHP web framework created by Taylor Otwell in 2011. It follows the MVC architectural pattern and is based on Symfony. Otwell created Laravel as an attempt to provide a more advanced alternative to the CodeIgniter framework, which did not provide certain features such as built-in support for user authentication and authorization. — Laravel Wiki
Laravel vs Python
Like Django, Laravel requires a virtual machine environment for production. Laravel Homestead is an official, pre-packaged Vagrant box that provides a wonderful development environment without requiring installation of PHP, a web server, and any other server software on your local machine.
Laravel Homestead uses the Nginx web server by default. However, it can install Apache if it is specified as a site type. Some alternatives to using Vagrant include XAMPP, WampServer, Docker, and Laragon. Many companies have already shifted from virtualizations to containerized applications. — Application Containerization.
What virtualization actually means
Whether you're leveraging a Windows environment on Mac using VirtualBox or running a Linux subsystem with Ubuntu, you're using a virtual machine.
What does that mean? In a nutshell, virtualization is the fundamental technology that powers cloud computing. This software separates computing environments from physical infrastructures, so you can run multiple operating systems and applications simultaneously on the same machine. For example, if you do most of your work on a Mac but use select applications that are exclusive to PCs, you can run Windows on a virtual machine to get access to those applications without having to switch computers.
This concept may also run together with cloud computing if you are not familiar with the two just yet. To clarify:
Virtualization is software that manipulates hardware, while cloud computing refers to a service that results from that manipulation.
If you're looking for a more detailed explanation: A study on virtual machine deployment for application outsourcing in mobile cloud computing.
Virtual machines more efficiently use hardware, which lowers the physical amount of hardware and associated maintenance costs, and reduces power and cooling demand. They also ease management because virtual hardware does not fail.
Cloud computing has brought remarkable benefits to the software world.
So how do you start using Laravel if it requires a virtual environment?
The Laravel documentation can be found here: Laravel Docs
Before you can use a hypervisor (a VMM, virtual machine monitor), you need to download a software package that will support it. The Laravel documentation will prompt you to choose between VirtualBox, Hyper-V, Parallels, or VMware. Follow steps accordingly to install one of these packages.
I recommend VirtualBox, a free and open-source hosted hypervisor for x86 computers. However, any of these programs will allow you to host your virtual machine and develop a project with Laravel.
After completing the hypervisor download, the first thing you should do is create a virtual machine instance. With Vagrant, this means adding a Vagrant box to the list — that is, the list of Vagrant boxes on your machine that can be run by your hypervisor.
You can now check if Composer is installed on your system:
composer --version
Since it's included with installation of Homestead, you may already have Composer. I downloaded it anyway just to be sure I had the latest version. Composer is an application-level package manager for PHP that provides a standard format for managing dependencies and required libraries. Think of it as another type of npm.
By cloning a Git repository or using Composer, you must initialize the Homestead box in your central Homestead directory (the directory into which it was downloaded), to create a Homestead.yaml file. In this file, you can configure the path to your public SSH key, as well as the folders you wish to be shared between your main machine and the Homestead virtual machine.
SSH keys
If you're not familiar with SSH keys, here's what you need to know:
SSH keys come in many sizes, but a popular choice is an RSA 2048-bit encryption, which is comparable to a 617 digit long password. On Windows systems, it is possible to generate your own SSH key pair by downloading and using an SSH client like PuTTY. On Mac and Linux systems, it is possible to generate an SSH key pair using a terminal window. — What Are SSH Keys?
For Windows users, alternate documentation and instructions are available and will not be hard to find. For Mac, the following command is all you need. In your terminal, type the SSH key generator command:
ssh-keygen -t rsa
ssh-keygen is a standard component of the Secure Shell protocol suite found on Unix and Unix-like computer systems used to establish secure shell sessions between remote computers over insecure networks, through the use of various cryptographic techniques. — ssh-keygen
Provisioning the machine
You should now be able to provision your machine. If you're using Vagrant, run this command in your Homestead directory:
vagrant up
Heads up: if you don't run this in your Vagrant box directory (in my case, the Homestead directory), you'll get this message:
A Vagrant environment or target machine is required to run this command.
Run `vagrant init` to create a new Vagrant environment. Or, get an ID of a
target machine from `vagrant global-status` to run this command on. A final
option is to change to a directory with a Vagrantfile and to try again.
Once you have a provisioned machine and you have powered on your Homestead box, you can run:
vagrant ssh
After running vagrant ssh you will be presented with Welcome to Ubuntu! ..., and you will enter the virtual environment secure shell.
A common misconception is that a namespace like
ubunturepresents the canonical space for Ubuntu boxes. This is untrue. Namespaces on Vagrant Cloud behave very similarly to namespaces on GitHub, for example. Just as GitHub's support team is unable to assist with issues in someone's repository, HashiCorp's support team is unable to assist with third-party published boxes. — hashicorp/vagrant, GitHub
Creating the Laravel project
Now that we are using the virtual machine, we can finally create a Laravel project. You will do this using a Composer command:
composer create-project --prefer-dist laravel/laravel ProjectName 5.7
--prefer-dist and --prefer-source are the two options of Composer included in various documentations with a lack of proper explanation.
--prefer-distwould try to download and unzip archives of the dependencies using GitHub or another API when available. This is used for faster downloading of dependencies in most cases.
--prefer-sourcewould try to clone and keep the whole version control repository of the dependencies when available. This is useful when you want to have the original version control repositories cloned in yourvendor/folder.
You're now ready to start production of your new Laravel project — after these last few steps.
Since you're inside a virtual environment, you won't be hosting this project on your localhost but rather on the virtual environment server. With Homestead, your site specifications need to be configured in your Homestead.yaml file. This basically entails adding an IP address (or using an existing one) and name for your server.
You also must modify your /hosts file, which requires permissions (use the sudo command preceding your edit command, i.e. sudo vim or sudo nano, to grant permission). If you're using another web server like XAMPP, you will additionally need to edit your etc/apache2/extra/httpd-vhosts.conf file and add your project specifications.
If you're struggling to understand this process, or why it's necessary, there are plentiful videos and tutorials that can help.
You've done it. You should now be able to visit your new Laravel project at the specified virtual machine server IP in your browser.
If and when errors arise, a useful command to use for a Vagrant box is:
vagrant reload --provision
which must be run after any changes to the configuration files just mentioned, or after any major changes to your project.
A problem I ran into: Intel VT-x
One problem I ran into with virtualization involves enabling Intel VT-x in your system BIOS or UEFI firmware, with which I was previously unfamiliar.
This was part of the error message:
If the provider you're using has a GUI that comes with it, it is often helpful
to open that and watch the machine, since the GUI often has more helpful error
messages than Vagrant can retrieve. For example, if you're using VirtualBox,
run `vagrant up` while the VirtualBox GUI is open.
I found that adding the following lines to my Vagrantfile allowed me to open the VM provider GUI upon starting the machine:
config.vm.provider :virtualbox do |vb|
vb.gui = true
end
Modern CPUs include hardware virtualization features that help accelerate virtual machines created in VirtualBox, VMware, Hyper-V, and other apps. But those features aren't always enabled by default. Within a virtual machine, you can run different operating systems, test apps in a sandbox environment, and experiment with features without worry. In order to work, virtual machine apps need hardware acceleration features built into modern CPUs. For Intel CPUs (my case), this means Intel VT-x hardware acceleration. — How to Enable Intel VT-x in Your Computer's BIOS or UEFI Firmware
The good thing is, all that was needed for me was a restart of my system and a reload for Vagrant and VirtualBox.
I hope I was able to provide a learning opportunity for other developers and tech enthusiasts. Every experience opens new doors and offers new areas of exploration. Happy coding and stay curious!
If you have any comments, please feel free to message me.
Sources
- Server-side web frameworks — MDN
- Laravel Homestead documentation
- A study on virtual machine deployment for application outsourcing in mobile cloud computing
- Virtualization vs Cloud Computing: What's the Difference?
- Virtual machine (VM)
- Laravel vs Python comparison
- Laravel — Wikipedia
- What Are SSH Keys?
- ssh-keygen — Wikipedia
- How to Enable Intel VT-x in Your Computer's BIOS or UEFI Firmware
- Application containerization
About
I'm Gavin, a full-stack developer working mainly in C# / ASP.NET Core / Azure and PHP/Laravel, with SQL, Postgres, Docker and AWS underneath.
I write about the problems that don't have a clean answer online - things I've run into and my debugging paths that actually worked. I hope you find them useful. Thanks for visiting.