Build a virtual machine environment through Vagrant
Introduction
Laravel is committed to making PHP development experience more enjoyable and also includes your local development environment. Vagrant provides a simple and elegant way to manage and supply virtual machines.
Laravel Homestead is an officially pre-loaded Vagrant "package" that provides you with a beautiful development environment, you do not need to install PHP, HHVM, web server, or any server software on your local computer. Don't worry about messing up your system! The Vagrant package can handle everything. If something goes wrong, you can quickly destroy and recreate the virtual machine within a few minutes.
Homestead can run on any Windows, Mac, or Linux, it contains Nginx Web servers, PHP 5.6, MySQL, ipvs, Redis, Memcached, and all the software you need to develop brilliant Laravel applications.
Note: If you are a Windows user, you may need to enable hardware Virtualization (VT-x ). You usually need to enable it through BIOS.
Homestead is currently built and tested in Vagrant 1.7.
Built-in software
- Ubuntu 14.04
- Python 5.6
- HHVM
- Nginx
- MySQL
- Postgres
- Node (With Bower, Grunt, and Gulp)
- Redis
- Memcached
- Beanstalkd
- Laravel Envoy
- Blackfire Profiler
Install and configure VirtualBox/VMWare and Vagrant
Before starting your Homestead environment, you must first install VirtualBox and Vagrant. Both sets of software provide easy-to-use visual installation programs on each platform.
VMware
In addition to VirtualBox, Homestead also supports VMware. If you use VMware as the provider, You need to purchase VMware Fusion/Desktop and VMware Vagrant plug-in. VMware to provide faster shared folders with better performance.
Added Vagrant package
After VirtualBox/VMware and Vagrant are installed, run the following command on the terminal to install the 'laravel/homestead 'package into your Vagrant installer. It takes you a little time to download the package. The duration is determined by your network speed:
vagrant box add laravel/homestead
If this command fails, you may install an old version of Vagrant and you need to specify a complete URL:
vagrant box add laravel/homestead https://atlas.hashicorp.com/laravel/boxes/homestead
Install Homestead
You can install Homestead by manually copying the resource library. Copy the resource library to your "home" DirectoryHomestead
As a result, the Homestead package will provide host services to all your Laravel (and PHP) applications:
git clone https://github.com/laravel/homestead.git Homestead
Once you have cloned the Homestead repository, runbash init.sh
Command to createHomestead.yaml
Configuration file:
bash init.sh
ThisHomestead.yaml
Files will be placed in your~/.homestead
Directory.
Configure your Provider
InHomestead.yaml
Fileprovider
Key indicates the Vagrant prodiver to be used:virtualbox
,vmware_fusion
(Mac OS X), orvmware_workstation
(Windows), you can set provider according to your preferences.
provider: virtualbox
Configure your SSH key
Then you need to editHomestead.yaml
. You can configure your SSH Public Key in the file and the shared directory between the main machine and the Homestead virtual machine.
If you do not have an SSH key, you can use the following commands to create an SSH key group on Mac and Linux:
ssh-keygen -t rsa -C "you@homestead"
In Windows, you need to install Git and useGit Bash
To execute the preceding command. You can also use PuTTY and PuTTYgen.
Once you create an SSH key, rememberHomestead.yaml
Fileauthorize
Attribute specifies the Key Path.
Configure your shared folder
Homestead.yaml
Filefolders
Attribute lists all the folders you want to share in the Homestead environment. If the files in these folders change, they will be synchronized to your local machine and Homestead environment. You can configure all the shared folders you need.
To enable NFS, you only needfolders
Add an identifier:
folders: - map: ~/Code to: /home/vagrant/Code type: "nfs"
Configure your Nginx site
Not familiar with Nginx? It doesn't matter.sites
Attribute allows you to easily correspond toDomain Name
To a directory in the homestead environment. The site in one example is configured inHomestead.yaml
File. Similarly, you can add any site you need to your Homestead environment. Homestead provides a convenient virtualization environment for each Laravel application in progress.
You can configurehhvm
The property istrue
To make the virtual site support HHVM:
sites: - map: homestead.app to: /home/vagrant/Code/Laravel/public hhvm: true
Each site will be accessible by HTTP via port 8000 and HTTPS via port 44300.
Bash Aliases
To add Bash aliases to your Homestead package, add the content~/.homestead
Directoryaliases
File.
Start the Vagrant package
When you finish editing according to your preferencesHomestead.yaml
Enter your Homestead folder in the terminal and executevagrant up
Command.
Vagrant will start the VM and automatically configure your shared directory and Nginx site. To remove a virtual machine, you can usevagrant destroy --force
Command.
For your Nginx site, do not forgethosts
Add the "Domain Name" to the file.hosts
File will direct your local domain name site requests to your Homestead environment. In Mac and Linux, this file is stored in/etc/hosts
. In Windows, it is placed inC:\Windows\System32\drivers\etc\hosts
. The content you want to add is similar to the following:
192.168.10.10 homestead.app
Make sure that the IP address matches yourHomestead.yaml
File. Once you add a domain name to yourhosts
File, you can access your site through a web browser.
http://homestead.app
Continue reading, and you will learn how to connect to the database!
Common usage: connect through SSH
To connect to your Homestead environment through SSH, enter your Homestead directory in the terminal and executevagrant ssh
Command.
Because you may often need to access your Homestead virtual machine through SSH, you can consider creating an "alias" on your main machine for fast SSH access to the Homestead Virtual Machine:
alias vm="ssh vagrant@127.0.0.1 -p 2222"
Once you create this alias, you can simply use the "vm" command to access your Homestead vm through SSH no matter which directory you are on the main machine.
You can also usevagrant ssh
Command.
Connect to database
InHomestead
In the package, the MySQL and ipvs databases are pre-configured. For ease, Laravel'slocal
The database configuration has been completed by default.
If you want to connect to MySQL or ipvs database through Navicat or Sequel Pro on the local machine, you can connect127.0.0.1
Port 33060 (MySQL) or 54320 (S ). The account and password arehomestead
/secret
.
Note: from the local end, you should only use these non-standard connection ports to connect to the database. When Laravel runs on a VM, the default port 3306 and port 5432 are configured in the Laravel database configuration file.
Add more sites
After the installation and running in the Homestead environment, you may need to add more Nginx sites for the Laravel application. You can run a lot of Laravel installation programs in a single Homestead environment. There are two ways to achieve this: first, inHomestead.yaml
Add a site to the file and execute it in the Homestead directory.vagrant provision
.
Note: This operation is destructive.provision
Command, your existing database will be destroyed and re-created.
You can also useserve
Command file. To useserve
Command file. first enter the Homestead environment through SSH and execute the following command:
serve domain.app /home/vagrant/Code/path/to/public/directory 80
Note: During executionserve
After the command, do not forget to add the new site to the Local Machinehosts
File.
Connection Port
The following ports will be forwarded to the Homestead environment:
- SSH: 2222 → Forwards To 22
- HTTP: 8000 → Forwards To 80
- HTTPS: 44300 → Forwards To 443
- MySQL: 33060 → Forwards To 3306
- Postgres: 54320 → Forwards To 5432
Add additional port
You can also customize the forwarding to Vagrant box by specifying the Protocol:
ports: - send: 93000 to: 9300 - send: 7777 to: 777 protocol: udp
Blackfire Profiler
Blackfire Profiler is an analysis tool created by SensioLabs. It automatically collects data during code execution, such as RAM, CPU time, and disk I/O. if you use Homestead, it is very easy to use this analysis tool.
The package required by blackfire has been installed in Homestead box.Homestead.yaml
Set the Server ID and token in the file:
blackfire: - id: your-server-id token: your-server-token client-id: your-client-id client-token: your-client-token
When you have set the Blackfire credential information, usevagrant provision
Make the configuration take effect. Of course, you also need to read the Blackfire documentation to learn how to install the Blackfire extension in your browser.
How to Use vagrant to install a Hadoop cluster on a virtual machine
Efficient Puppet module Management in Vagrant
Use Vagrant and Fabric for Integration Testing
Build a development environment using Vagrant
Configure the Vagrant environment in Windows
Build a development environment using Vagrant
This article permanently updates the link address: