Homestead
- Installation & Setup
- First Steps
- Install Homestead
- Configuration Homestead
- Configure Nginx site
- Install MongoDB
- Install Elasticsearch
- Install Neo4j
- Alias
- Daily use
- Available globally Homestead
- Mac/Linux
- Windows
- Via SSH Connection
- Add additional sites
- ##Environment variables
- Configuring scheduled schedule
- Configuring Mailhog
- Configuring Minio
- Port
- Share your environment
- Multiple versions PHP ##Web Server
- Extended Homestead
- Updated Homestead
- Provided special settings
- ##VirtualBox
Laravel Homestead
- Introduction
- Installation and setup
- Daily use
- Network Interface
- Extend Homestead
- Update Homestead
- Provider-specific settings
Introduction
Laravel is committed to making the entire PHP development experience more enjoyable, including your local development environment. Vagrant provides a simple and elegant way to manage and configure virtual hosts.
Laravel Homestead is an official pre-packaged Vagrant box, which provides you with a perfect development environment. You do not need to install PHP, web server and other server software on your local machine. You never have to worry about messing up your operating system again! Vagrant boxes are disposable. If something goes wrong, you can destroy and recreate the box in minutes!
Homestead can run on any Windows, Mac, or Linux system, it includes Nginx web server, PHP 7.3, PHP 7.2, PHP 7.1 , MySQL, PostgreSQL, Redis, Memcached, Node and everything you need to develop with Laravel.
{note} If you are using Windows, you may need to enable hardware virtualization (VT-x) through the BIOS. If you are using UEFI on a Hyper-V system you may also need to disable Hyper-V in order to access VT-x.
Built-in software
- Ubuntu 18.04
- Git
- PHP 7.3
- PHP 7.2
- PHP 7.1
- Nginx
- Apache (Optional)
- MySQL
- MariaDB (Optional)
- Sqlite3
- PostgreSQL
- Composer
- Node (With Yarn, Bower, Grunt, and Gulp)
- Redis
- Memcached
- Beanstalkd
- Mailhog
- Neo4j (Optional)
- MongoDB (Optional)
- Elasticsearch (Optional)
- ngrok
- wp-cli
- Zend Z-Ray
- Go
- Minio
laravel/homestead box added to Vagrant installation. It may take you a few minutes to download the box as it depends on the speed of your internet connection:
vagrant box add laravel/homesteadIf this command fails, make sure you have the latest version of Vagrant installed.
Installing Homestead
You can install Homestead by cloning the code. It is recommended to clone the code into the Homestead
folder in your "home" directory, so that the Homestead box can serve as the host for all your Laravel projects:
git clone https://github.com/laravel/homestead.git ~/Homestead
Because of the master branch of Homestead It is not stable, you should use the tagged stable version. You can find the latest stable version on the GitHub releases page::
cd ~/Homestead// 克隆期望的发行版本...git checkout v8.0.1
Once cloning the Homestead code is complete, use the bash init.sh
command in the Homestead directory To create the Homestead.yaml
configuration file. Homestead.yaml
The file will be placed in the Homestead directory:
// Mac / Linux...bash init.sh// Windows...init.bat
Configuring Homestead
Set the provider
Homestead.yaml
The provider
parameter in the file determines which Vagrant provider you use: virtualbox
, vmware_fusion
, vmware_workstation
, parallels
and hyperv
. You can set them up according to your preferences:
provider: virtualbox
Configuring shared folders
The folders attribute of the Homestead.yaml file lists all shared folders with the Homestead environment folder. If the files in these folders change, they remain in sync between the local machine and the Homestead environment. You can configure as many shared folders as needed:
folders: - map: ~/code to: /home/vagrant/code
This general mapping will work just fine if you only create a few websites. However, as the number of sites continues to increase, you may start to experience performance issues. This problem can be very noticeable on low-performance machines or projects with a large number of files. If you encounter this problem, try mapping each project to its own Vagrant folder:
folders: - map: ~/code/project1 to: /home/vagrant/code/project1 - map: ~/code/project2 to: /home/vagrant/code/project2
To enable NFS, you just need to add one to the shared folder configuration Simple flag:
folders: - map: ~/code to: /home/vagrant/code type: "nfs"
{note} When using NFS, you'd better use the vagrant-winnfsd extension. This plugin will handle file or directory permission issues in the Homestead box for you.
You can also do this by listing any of the options supported by Vagrant's Shared Folders
in options:
folders: - map: ~/code to: /home/vagrant/code type: "rsync" options: rsync__args: ["--verbose", "--archive", "--delete", "-zz"] rsync__exclude: ["node_modules"]
Configuring Nginx site
Not familiar with Nginx? It doesn't matter. The sites
feature allows you to easily map a "domain name" to a folder on Homestead. A simple site configuration example is included in the Homestead.yaml
file. Likewise, you can add as many sites to your Homestead environment as you need. Homestead can provide a convenient virtualization environment for each Laravel project you are developing:
sites: - map: homestead.test to: /home/vagrant/code/my-project/public
If you change the sites
option after configuring the Homestead virtual machine, you will need to run it againvagrant reload --provision
Command to update the Nginx configuration on the virtual machine.
Hosts file
You must add the domain name to the Nginx site Add it to your local hosts
file. The hosts
file will redirect requests to your Homestead site to your Homestead virtual machine. This file is located at /etc/hosts
on Mac and Linux, and C:\Windows\System32\drivers\etc\hosts
on Windows. The format for adding a new site is as follows:
192.168.10.10 homestead.test
Make sure the listening IP address is the one you set in the Homestead.yaml
file. Once you've added your domain name to your hosts
file and launched Vagrant Box, you can access the site through your browser:
http://homestead.test
Start Vagrant Box
Edit Homestead.yaml
according to your needs, and run the vagrant up
command in your Homestead folder . Vagrant will start the virtual machine and automatically configure your shared folders and Nginx site.
To delete a virtual machine, just run the vagrant destroy --force
command.
Installation by project
In addition to installing Homestead globally and sharing the same Homestead box in all projects, you can configure a Homestead instance for each project. By creating a Vagrantfile
under the project, other project members can have the same development environment by simply running vagrant up
.
To install Homestead directly into the project, you need to use the Composer command:
composer require laravel/homestead --dev
After Homestead is installed, use the make
command to generate the Vagrantfile in the project root directory
and Homestead.yaml
files. The make
command will automatically configure the sites
and folders
directives in the Homestead.yaml
file.
Mac/Linux:
php vendor/bin/homestead make
Windows:
vendor\bin\homestead make
Next, run the vagrant up
command on the command line and access it in your browser http://homestead.test
. Remember, you still need to add your homestead.test
record or other domain name in the /etc/hosts
file before accessing it.
Install MariaDB
If you prefer to use MariaDB instead of MySQL, you can install it in Homestead.yaml
Add a mariadb
option to the file. This option will remove MySQL and install MariaDB. MariaDB is only a side replacement for MySQL, so you can still use the mysql
database driver in your application's database configuration:
box: laravel/homestead ip: "192.168.10.10"memory: 2048cpus: 4provider: virtualbox mariadb: true
Install MongoDB
If you need to install MongoDB Community Edition, you can modify the configuration in the Homestead.yaml
file to:
mongodb: true
MongoDB default The installation method will set the database username to homestead
and the default password to secret
.
Homestead.yaml Add the
elasticsearch option to the file and specify the supported version number. It can contain only the major version, or it can be a specific version number (major.minor.patch). The default installation creates a cluster named 'homestead'. Be careful to never give Elasticsearch more than half of the operating system's memory, so make sure your Homestead allocates at least twice the memory of Elasticsearch:
box: laravel/homestead ip: "192.168.10.10"memory: 4096cpus: 4provider: virtualbox elasticsearch: 6
{tip} You can check out the Elasticsearch documentation to learn how to do it yourself Define your configuration.
Installing Neo4j
Neo4j is a graph database management system. To install the Neo4j Community Edition, you need to update the Homestead.yaml
configuration items as follows:
neo4j: true
The default installation of Neo4j will set the database username to homestead
and the corresponding password Set to secret
. To view Neo4j you can visit http://homestead.test:7474
in your browser. Neo4j provides three external ports: 7687
(Bolt), 7474
(HTTP), 7473
(HTTPS) for access from the client.
aliases file in the Homestead directory. Add Bash aliases to the machine:
alias c='clear'alias ..='cd ..'After updating the
aliases file, you should use the
vagrant reload --provision command to restart the Homestead virtual machine to ensure that the new alias is in available on the machine.
Sometimes you may want to run the
vagrant up command to start the Homestead virtual machine from any path in the file system. On Mac/Linux systems, you can add Bash functions in the Bash configuration file (~/.bash_profile
). On Windows, you can add "batch" files to PATH
. These scripts allow you to run a Vagrant command from anywhere on your system and automatically point the command to the Homestead installation path:
function homestead() { ( cd ~/Homestead && vagrant $* ) }
Make sure to include this function in The
~/Homestead path is adjusted to the actual Homestead installation path. This way you can run commands like homestead up
or homestead ssh
from anywhere on the system.
Create a batch file
homestead.bat anywhere in the system with the following content:
Certain To adjust the @echo off
set cwd=%cd%
set homesteadVagrant=C:\Homestead
cd /d %homesteadVagrant% && vagrant %*
cd /d %cwd%
set cwd=
set homesteadVagrant=
path in the script to the actual installation path of Homestead. After creating the file, add the file path to PATH
. Then you can run commands such as homestead up
or homestead ssh
anywhere in the system.
You can do this by running
vagrant ssh Terminal in the Homestead directory command to connect to the virtual machine via SSH. However, you may need to connect to the Homestead virtual machine frequently, so please consider adding the above "function" to the host to quickly SSH to the Homestead virtual machine.
Connect to the database
homestead
By default, MySQL and PostgreSQL databases have been configured in the virtual machine. For convenience, the .env
file in Laravel configures the framework to use this database by default.
To connect to MySQL or PostgreSQL from the host's database client, you should connect to 127.0.0.1
, port 33060
(MySQL) or 54320
(PostgreSQL). The username and password are homestead
/ secret
respectively.
{note} These non-standard ports can only be used when connecting to Hoemstead's database locally. Laravel When running in a virtual machine should still use the default 3306 and 5432 ports for database connections.
Database Backup
Homestead can automatically back up your database when your Vagrant box is destroyed . To take advantage of this feature, you must be using Vagrant 2.1.0 or higher. Alternatively, if you use an older version of Vagrant, you must install the vagrant-triggers
plugin. To enable automatic database backups, add the following line to your Homestead.yaml
file:
backup: true
Once configured, Homestead will Will export your database to the mysql_backup
and postgres_backup
directories. If you use the per-project installation method, you can find these directories in your cloned Homestead or in your project root.
Once your Homestead environment is configured and running, you can add additional sites to your Laravel The application adds additional Nginx sites. You may want to run multiple Laravel installations in a single Homestead environment. To add additional sites, add sites to the
Homestead.yaml file:
If Vagrant cannot automatically manage your "hosts" file, you may also need to add new sites to the In the file: sites:
- map: homestead.test
to: /home/vagrant/code/my-project/public
- map: another.test
to: /home/vagrant/code/another/public
192.168.10.10 homestead.test 192.168.10.10 another.test
Once the site is added, run the
vagrant reload --provision command from your Homestead directory.
Homestead supports many types of sites, allowing you to easily run projects that are not based on Laravel. For example, we can easily add a Symfony application to Homestead using the
symfony2 site type:
The available site types are: sites:
- map: symfony2.test
to: /home/vagrant/code/my-symfony-project/web
type: "symfony2"
, apigility
, expressive
, laravel
(default), proxy
, silverstripe
, statamic
, symfony2
, symfony4
, and zf
.
Site Parameters
You can add additional Nginx fastcgi_param
values to your site via the params
site directive. For example, we add a Foo
parameter with a value of BAR
:
sites: - map: homestead.test to: /home/vagrant/code/my-project/public params: - key: FOO value: BAR
Environment Variables
You can set global environment variables by adding the following values to the Homestead.yaml
file:
variables: - key: APP_ENV value: local - key: FOO value: bar
After updating the Homestead.yaml
file , make sure to reconfigure the machine by running the vagrant reload --provision
command. This will update the PHP-FPM configuration for all installed PHP versions and update the environment for vagrant
users.
Configuring a scheduled schedule
Laravel provides a way to schedule scheduled jobs by scheduling a single schedule:run
Artisan command to run every minute. The schedule:run
command will check the schedule you defined in the App\Console\Kernel
class to decide which job to run.
If you want to run the schedule:run
command for a Homestead site, you can set the schedule
option to true## when defining the site. #:
sites: - map: homestead.test to: /home/vagrant/code/my-project/public schedule: trueThe Cron job for the site will be defined in the
/etc/cron.d directory of the virtual machine.
.env file with the following mail settings:
MAIL_DRIVER=smtp MAIL_HOST=localhost MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=nullOnce Mailhog is configured, you can configure it at
http://localhost:8025 Access the Mailhog control panel.
Configuring Minio
Minio is an open source object storage server with an API compatible with Amazon S3. To install Minio, update your Homestead.yaml
file with the following configuration options:
minio: true
By default, Minio is available on port 9600
. You can access the Minio control panel by visiting http://homestead:9600
. The default access key name is homestead
, and the default secret key is secretkey
. When accessing Minio, you should always use us-east-1
.
In order to use Minio, you will need to adjust the S3 disk configuration in your config/filesystems.php
configuration file. You will need to add the use_path_style_endpoint
option to the disk configuration and change the url
key to endpoint
:
's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'endpoint' => env('AWS_URL'), 'use_path_style_endpoint' => true ]
Finally, make sure your ## The #.env file has the following options:
AWS_ACCESS_KEY_ID=homestead AWS_SECRET_ACCESS_KEY=secretkey AWS_DEFAULT_REGION=us-east-1 AWS_URL=http://homestead:9600To configure buckets, add a
buckets directive to the Homestead configuration file:
buckets: - name: your-bucket policy: public - name: your-private-bucket policy: noneSupported
policy Values include:
none,
download,
upload, and
public.
- SSH: 2222 -> Forward to 22
- ngrok UI: 4040 -> Forward to 4040
- HTTP: 8000 -> Forward to 80
- HTTPS: 44300 -> Forward to 443
- MySQL: 33060 - > Forward to 3306
- PostgreSQL: 54320 -> Forward to 5432
- MongoDB: 27017 -> Forward to 27017
- Mailhog:8025 -> Forward to 8025
- Minio:9600 -> Forward to 9600
If you wish, you can forward additional ports to the Vagrant box and also specify their protocols:
ports: - send: 50000 to: 5000 - send: 7777 to: 777 protocol: udp
Share your environment
Sometimes you want to share the content of your current work with colleagues or clients. Vagrant has a built-in way to support this via vagrant share
; however, this will not work if there are multiple site configurations in the Homestead.yaml
file.
To solve this problem, Homestead includes its own share
command. To begin, SSH into your Homestead machine via vagrant ssh
and run share homestead.test
. This will share the homestead.test
site from your Homestead.yaml
configuration file. Of course, you can replace any other configured site with homestead.test
:
share homestead.test
After running this command, you will see an Ngrok screen appear with activity logs and shares The accessible URLs of the site. If you want to specify a custom zone, subdomain, or other Ngrok runtime options, you can add them to your share
command:
share homestead.test -region=eu -subdomain=laravel
{note} Note Live, Vagrant is inherently insecure and exposes the VM to the internet when running the
share
command.
Multiple versions of PHP
Homestead 6 introduces support for multiple PHP on the same virtual machine version support. You can specify the PHP version to use for a given site in your Homestead.yaml
file. The available PHP versions are: 『7.1』, 『7.2』 and 『7.3』 (default):
sites: - map: homestead.test to: /home/vagrant/code/my-project/public php: "7.1"
Alternatively, you can use any supported PHP version through the CLI:
php7.1 artisan list php7.2 artisan list php7.3 artisan list
Web Server
By default, Homestead uses the Nginx web server. However, if apache
is specified as a site type, it can install Apache. While two web servers are installed at the same time, they cannot run at the same time. flip
Script commands can be used to easily handle switching between web servers. flip
The command automatically determines the running web server and shuts it down. Then start other servers. To use this command, SSH into your Homestead machine and run the command in the terminal:
flip##mail Homestead includes the Postfix mail transfer agent, which listens on port
1025 by default. Therefore, you can instruct your application to use the
smtp mail driver on
localhost port
1025. All sent emails will be processed by Postfix and captured by Mailhog. To view sent messages, open
http://localhost:8025 in a web browser.
Network Interfaces
The
networks property in Homestead.yaml
configures the network interface for the Homestead environment. You can configure multiple interfaces as needed:
networks: - type: "private_network" ip: "192.168.10.20"
To enable a bridge interface, configure bridge
Set and change the public_network
network type:
networks: - type: "public_network" ip: "192.168.10.20" bridge: "en1: Wi-Fi (AirPort)"
To enable DHCP, simply remove the ip
option from your configuration:
networks: - type: "public_network" bridge: "en1: Wi-Fi (AirPort)"
Extending Homestead
You can extend Homestead using the after.sh
script in the Homestead root directory. In this file, you can add any shell commands needed to properly configure and customize the virtual machine.
When customizing Homestead, Ubuntu may ask you if you want to keep the package's original configuration or overwrite it with a new configuration file. To avoid this, you should use the following command when installing the package to avoid overwriting any configuration previously written by Homestead:
sudo apt-get -y \ -o Dpkg::Options::="--force-confdef" \ -o Dpkg::Options::="--force-confold" \ install your-package
Updating Homestead
You can update Homestead by following some simple steps. First, you should update the Vagrant box using the vagrant box update
command:
vagrant box update
Next, you need to update the Homestead source code. If you have cloned the repository, you can run the following commands from the location where you originally cloned the repository:
git fetchgit checkout v8.0.1
These commands pull the latest Homestead code from the GitHub repository, get the latest tags, and then check out the latest tag version . You can find the latest stable version on the GitHub releases page.
If you installed Homestead via your project's composer.json
, you should ensure that your composer.json
contains "laravel/homestead": " ^8"
And update your dependencies:
composer update
Finally, you will need to destroy and rebuild your Homestead box to take advantage of the latest Vagrant installation. To accomplish this, run the following command in your Homestead directory:
vagrant destroy vagrant up
Provide special settings
VirtualBox
natdnshostresolver
By default, Homestead will natdnshostresolver
configuration is set to on
. This allows Homestead to use your host operating system's DNS settings. If you do not want to override this behavior, add the following lines to your Homestead.yaml
file:
provider: virtualbox natdnshostresolver: off
Symbolic links on Windows
If symbolic links do not work properly on your Windows machine, you may need to add the following block to the Vagrantfile
file:
config.vm.provider "virtualbox" do |v| v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"] end