Homestead
这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。
Laravel Homestead
介绍
Laravel 致力于让整个 PHP 开发体验变得愉快,其中也包括你的本地开发环境。Laravel Homestead 是一个官方预打包的 Vagrant box,它为你提供了一个极佳的开发环境,而无需在本地机器上安装 PHP、Web 服务器或任何其他服务器软件。
Vagrant 提供了一种简单、优雅的方式来管理和配置虚拟机。Vagrant box 完全是一次性的。如果出现问题,你可以在几分钟内销毁并重新创建这个 box!
Homestead 可以运行在任何 Windows、macOS 或 Linux 系统上,并且包含 Nginx、PHP、MySQL、PostgreSQL、Redis、Memcached、Node 以及开发出色 Laravel 应用所需的所有其他软件。
[!警告]
如果你使用的是 Windows,可能需要启用硬件虚拟化(VT-x)。这通常可以通过 BIOS 启用。如果你在 UEFI 系统上使用 Hyper-V,则可能还需要禁用 Hyper-V 才能访问 VT-x。
Included Software
- Ubuntu 22.04
- Git
- PHP 8.3
- PHP 8.2
- PHP 8.1
- PHP 8.0
- PHP 7.4
- PHP 7.3
- PHP 7.2
- PHP 7.1
- PHP 7.0
- PHP 5.6
- Nginx
- MySQL 8.0
- lmm
- Sqlite3
- PostgreSQL 15
- Composer
- Docker
- Node (With Yarn, Bower, Grunt, and Gulp)
- Redis
- Memcached
- Beanstalkd
- Mailpit
- avahi
- ngrok
- Xdebug
- XHProf / Tideways / XHGui
- wp-cli
可选软件
- Apache
- Blackfire
- Cassandra
- Chronograf
- CouchDB
- Crystal & Lucky Framework
- Elasticsearch
- EventStoreDB
- Flyway
- Gearman
- Go
- Grafana
- InfluxDB
- Logstash
- MariaDB
- Meilisearch
- MinIO
- MongoDB
- Neo4j
- Oh My Zsh
- Open Resty
- PM2
- Python
- R
- RabbitMQ
- Rust
- RVM (Ruby Version Manager)
- Solr
- TimescaleDB
- Trader (PHP extension)
- Webdriver & Laravel Dusk Utilities
安装与设置
第一步
在启动你的 Homestead 环境之前,你必须先安装 Vagrant,以及以下受支持的提供商之一:
这些软件包都为所有流行的操作系统提供了易于使用的可视化安装程序。
要使用 Parallels provider,你需要安装 Parallels Vagrant 插件。它是免费的。
安装 Homestead
你可以通过将 Homestead 仓库克隆到主机上来安装 Homestead。建议将仓库克隆到你的 用户目录(home 目录)下的 Homestead
文件夹中,因为 Homestead 虚拟机会作为你所有 Laravel 应用的宿主环境。在本文档中,我们会将这个目录称为 Homestead 目录:
git clone https://github.com/laravel/homestead.git ~/Homestead
克隆完 Laravel Homestead 仓库后,你需要切换到 release
分支。这个分支始终包含 Homestead 的最新稳定版本:
cd ~/Homestead
git checkout release
接下来,在 Homestead 目录中执行 bash init.sh
命令来创建 Homestead.yaml
配置文件。Homestead.yaml
文件是你配置 Homestead 安装的地方。该文件会被放置在 Homestead 目录下:
# macOS / Linux...
bash init.sh
# Windows...
init.bat
配置 Homestead
设置 Provider
Homestead.yaml
文件中的 provider
键表示应该使用哪个 Vagrant provider:virtualbox
或 parallels
:
provider: virtualbox
[!警告]
如果你使用的是 Apple Silicon,则必须使用 Parallels provider。
配置共享文件夹
Homestead.yaml
文件中的 folders
属性列出了你希望与 Homestead 环境共享的所有文件夹。 当这些文件夹中的文件发生变化时,它们会在本地机器和 Homestead 虚拟环境之间保持同步。你可以根据需要配置任意多个共享文件夹:
folders:
- map: ~/code/project1
to: /home/vagrant/project1
[!警告]
Windows 用户不应该使用~/
路径语法,而应当使用项目的完整路径,例如:
C:\Users\user\Code\project1
你应该始终将每个应用程序单独映射到它自己的文件夹,而不是把包含所有应用的大目录一次性映射进去。
当你映射一个文件夹时,虚拟机必须追踪该文件夹内 每一个文件 的磁盘 IO。
如果文件夹中文件数量庞大,可能会导致性能下降:
folders:
- map: ~/code/project1
to: /home/vagrant/project1
- map: ~/code/project2
to: /home/vagrant/project2
[!警告]
使用 Homestead 时,绝对不要挂载.
(当前目录)。
这样会导致 Vagrant 不会把当前目录映射到/vagrant
,从而破坏可选功能,并在配置时产生意外结果。
要启用 NFS,你可以在文件夹映射中添加一个 type
选项:
folders:
- map: ~/code/project1
to: /home/vagrant/project1
type: "nfs"
[!警告]
在 Windows 上使用 NFS 时,建议安装 vagrant-winnfsd 插件。
这个插件会确保 Homestead 虚拟机中的文件和目录保持正确的用户/组权限。
你还可以通过在 options
键下列出配置,传递 Vagrant 的 Synced Folders 所支持的任意选项:
folders:
- map: ~/code/project1
to: /home/vagrant/project1
type: "rsync"
options:
rsync__args: ["--verbose", "--archive", "--delete", "-zz"]
rsync__exclude: ["node_modules"]
配置 Nginx 站点
对 Nginx 不熟悉?没关系。
Homestead.yaml
文件中的 sites
属性允许你轻松地将一个“域名”映射到 Homestead 环境中的某个文件夹。
Homestead.yaml
文件中已经包含了一个示例站点配置。
同样地,你可以根据需要在 Homestead 环境中添加任意数量的站点。
Homestead 可以作为一个方便的虚拟化环境,为你正在开发的每个 Laravel 应用提供服务:
sites:
- map: homestead.test
to: /home/vagrant/project1/public
如果你在配置 Homestead 虚拟机之后更改了 sites
属性,你应该在终端中执行 vagrant reload --provision
命令来更新虚拟机上的 Nginx 配置。
[!警告]
Homestead 脚本被构建为尽可能幂等。然而,如果你在配置时遇到问题,你应该通过执行vagrant destroy && vagrant up
命令来销毁并重建虚拟机。
主机名解析
Homestead 使用 mDNS
发布主机名以进行自动主机解析。
如果你在 Homestead.yaml
文件中设置 hostname: homestead
,该主机将在 homestead.local
可用。
macOS、iOS 和 Linux 桌面发行版默认包含 mDNS
支持。
如果你使用 Windows,你必须安装 Bonjour Print Services for Windows。
使用自动主机名在 按项目安装 Homestead 时效果最好。
如果你在单个 Homestead 实例上托管多个站点,你可以将网站的“域名”添加到你机器的 hosts
文件中。
hosts
文件会将对你的 Homestead 站点的请求重定向到你的 Homestead 虚拟机。
-
在 macOS 和 Linux 上,这个文件位于
/etc/hosts
。 -
在 Windows 上,这个文件位于
C:\Windows\System32\drivers\etc\hosts
。
你添加到这个文件的行看起来像这样:
192.168.56.56 homestead.test
确保所列出的 IP 地址是你在 Homestead.yaml
文件中设置的那个。
一旦你将域名添加到 hosts
文件并启动 Vagrant box,你将能够通过浏览器访问该站点:
http://homestead.test
配置服务
Homestead 默认启动若干服务;然而,你可以在配置(provisioning)期间自定义哪些服务被启用或禁用。例如,你可以通过修改 Homestead.yaml
文件中的 services
选项来启用 PostgreSQL 并禁用 MySQL:
services:
- enabled:
- "postgresql"
- disabled:
- "mysql"
指定的服务将根据它们在 enabled
和 disabled
指令中的顺序被启动或停止。
启动 Vagrant Box
一旦你按自己的喜好编辑了 Homestead.yaml
,在你的 Homestead 目录中运行 vagrant up
命令。Vagrant 将启动虚拟机并自动配置你的共享文件夹和 Nginx 站点。
要销毁该机器,你可以使用 vagrant destroy
命令。
按项目安装
你可以选择不在全局安装 Homestead 并在所有项目之间共享相同的 Homestead 虚拟机,而是为你管理的每个项目配置一个 Homestead 实例。如果你希望随项目一起发布 Vagrantfile
,以便其他协作者在克隆项目仓库后能够立即运行 vagrant up
,则按项目安装 Homestead 可能更有益。
你可以使用 Composer 包管理器将 Homestead 安装到你的项目中:
composer require laravel/homestead --dev
一旦 Homestead 被安装,调用 Homestead 的 make
命令为你的项目生成 Vagrantfile
和 Homestead.yaml
文件。这些文件将被放置在你的项目根目录。make
命令会自动在 Homestead.yaml
文件中配置 sites
和 folders
指令:
# macOS / Linux...
php vendor/bin/homestead make
# Windows...
vendor\\bin\\homestead make
Next, run the vagrant up
command in your terminal and access your project at http://homestead.test
in your browser. Remember, you will still need to add an /etc/hosts
file entry for homestead.test
or the domain of your choice if you are not using automatic hostname resolution.
Installing Optional Features
Optional software is installed using the features
option within your Homestead.yaml
file. Most features can be enabled or disabled with a boolean value, while some features allow multiple configuration options:
features:
- blackfire:
server_id: "server_id"
server_token: "server_value"
client_id: "client_id"
client_token: "client_value"
- cassandra: true
- chronograf: true
- couchdb: true
- crystal: true
- dragonflydb: true
- elasticsearch:
version: 7.9.0
- eventstore: true
version: 21.2.0
- flyway: true
- gearman: true
- golang: true
- grafana: true
- influxdb: true
- logstash: true
- mariadb: true
- meilisearch: true
- minio: true
- mongodb: true
- neo4j: true
- ohmyzsh: true
- openresty: true
- pm2: true
- python: true
- r-base: true
- rabbitmq: true
- rustc: true
- rvm: true
- solr: true
- timescaledb: true
- trader: true
- webdriver: true
Elasticsearch
You may specify a supported version of Elasticsearch, which must be an exact version number (major.minor.patch). The default installation will create a cluster named 'homestead'. You should never give Elasticsearch more than half of the operating system's memory, so make sure your Homestead virtual machine has at least twice the Elasticsearch allocation.
[!NOTE]
Check out the Elasticsearch documentation to learn how to customize your configuration.
MariaDB
Enabling MariaDB will remove MySQL and install MariaDB. MariaDB typically serves as a drop-in replacement for MySQL, so you should still use the mysql
database driver in your application's database configuration.
MongoDB
The default MongoDB installation will set the database username to homestead
and the corresponding password to secret
.
Neo4j
The default Neo4j installation will set the database username to homestead
and the corresponding password to secret
. To access the Neo4j browser, visit http://homestead.test:7474
via your web browser. The ports 7687
(Bolt), 7474
(HTTP), and 7473
(HTTPS) are ready to serve requests from the Neo4j client.
Aliases
You may add Bash aliases to your Homestead virtual machine by modifying the aliases
file within your Homestead directory:
alias c='clear'
alias ..='cd ..'
After you have updated the aliases
file, you should re-provision the Homestead virtual machine using the vagrant reload --provision
command. This will ensure that your new aliases are available on the machine.
Updating Homestead
Before you begin updating Homestead you should ensure you have removed your current virtual machine by running the following command in your Homestead directory:
vagrant destroy
Next, you need to update the Homestead source code. If you cloned the repository, you can execute the following commands at the location you originally cloned the repository:
git fetch
git pull origin release
These commands pull the latest Homestead code from the GitHub repository, fetch the latest tags, and then check out the latest tagged release. You can find the latest stable release version on Homestead's GitHub releases page.
If you have installed Homestead via your project's composer.json
file, you should ensure your composer.json
file contains "laravel/homestead": "^12"
and update your dependencies:
composer update
Next, you should update the Vagrant box using the vagrant box update
command:
vagrant box update
After updating the Vagrant box, you should run the bash init.sh
command from the Homestead directory in order to update Homestead's additional configuration files. You will be asked whether you wish to overwrite your existing Homestead.yaml
, after.sh
, and aliases
files:
# macOS / Linux...
bash init.sh
# Windows...
init.bat
Finally, you will need to regenerate your Homestead virtual machine to utilize the latest Vagrant installation:
vagrant up
Daily Usage
Connecting via SSH
You can SSH into your virtual machine by executing the vagrant ssh
terminal command from your Homestead directory.
Adding Additional Sites
Once your Homestead environment is provisioned and running, you may want to add additional Nginx sites for your other Laravel projects. You can run as many Laravel projects as you wish on a single Homestead environment. To add an additional site, add the site to your Homestead.yaml
file.
sites:
- map: homestead.test
to: /home/vagrant/project1/public
- map: another.test
to: /home/vagrant/project2/public
[!WARNING]
You should ensure that you have configured a folder mapping for the project's directory before adding the site.
If Vagrant is not automatically managing your "hosts" file, you may need to add the new site to that file as well. On macOS and Linux, this file is located at /etc/hosts
. On Windows, it is located at C:\Windows\System32\drivers\etc\hosts
:
192.168.56.56 homestead.test
192.168.56.56 another.test
Once the site has been added, execute the vagrant reload --provision
terminal command from your Homestead directory.
Site Types
Homestead supports several "types" of sites which allow you to easily run projects that are not based on Laravel. For example, we may easily add a Statamic application to Homestead using the statamic
site type:
sites:
- map: statamic.test
to: /home/vagrant/my-symfony-project/web
type: "statamic"
The available site types are: apache
, apache-proxy
, apigility
, expressive
, laravel
(the default), proxy
(for nginx), silverstripe
, statamic
, symfony2
, symfony4
, and zf
.
Site Parameters
You may add additional Nginx fastcgi_param
values to your site via the params
site directive:
sites:
- map: homestead.test
to: /home/vagrant/project1/public
params:
- key: FOO
value: BAR
Environment Variables
You can define global environment variables by adding them to your Homestead.yaml
file:
variables:
- key: APP_ENV
value: local
- key: FOO
value: bar
After updating the Homestead.yaml
file, be sure to re-provision the machine by executing the vagrant reload --provision
command. This will update the PHP-FPM configuration for all of the installed PHP versions and also update the environment for the vagrant
user.
Ports
By default, the following ports are forwarded to your Homestead environment:
- HTTP: 8000 → Forwards To 80
- HTTPS: 44300 → Forwards To 443
Forwarding Additional Ports
If you wish, you may forward additional ports to the Vagrant box by defining a ports
configuration entry within your Homestead.yaml
file. After updating the Homestead.yaml
file, be sure to re-provision the machine by executing the vagrant reload --provision
command:
ports:
- send: 50000
to: 5000
- send: 7777
to: 777
protocol: udp
Below is a list of additional Homestead service ports that you may wish to map from your host machine to your Vagrant box:
- SSH: 2222 → To 22
- ngrok UI: 4040 → To 4040
- MySQL: 33060 → To 3306
- PostgreSQL: 54320 → To 5432
- MongoDB: 27017 → To 27017
- Mailpit: 8025 → To 8025
- Minio: 9600 → To 9600
PHP Versions
Homestead supports running multiple versions of PHP on the same virtual machine. You may specify which version of PHP to use for a given site within your Homestead.yaml
file. The available PHP versions are: "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2", and "8.3", (the default):
sites:
- map: homestead.test
to: /home/vagrant/project1/public
php: "7.1"
Within your Homestead virtual machine, you may use any of the supported PHP versions via the CLI:
php5.6 artisan list
php7.0 artisan list
php7.1 artisan list
php7.2 artisan list
php7.3 artisan list
php7.4 artisan list
php8.0 artisan list
php8.1 artisan list
php8.2 artisan list
php8.3 artisan list
You may change the default version of PHP used by the CLI by issuing the following commands from within your Homestead virtual machine:
php56
php70
php71
php72
php73
php74
php80
php81
php82
php83
Connecting to Databases
A homestead
database is configured for both MySQL and PostgreSQL out of the box. To connect to your MySQL or PostgreSQL database from your host machine's database client, you should connect to 127.0.0.1
on port 33060
(MySQL) or 54320
(PostgreSQL). The username and password for both databases is homestead
/ secret
.
[!WARNING]
You should only use these non-standard ports when connecting to the databases from your host machine. You will use the default 3306 and 5432 ports in your Laravel application'sdatabase
configuration file since Laravel is running within the virtual machine.
Database Backups
Homestead can automatically backup your database when your Homestead virtual machine is destroyed. To utilize this feature, you must be using Vagrant 2.1.0 or greater. Or, if you are using an older version of Vagrant, you must install the vagrant-triggers
plug-in. To enable automatic database backups, add the following line to your Homestead.yaml
file:
backup: true
Once configured, Homestead will export your databases to .backup/mysql_backup
and .backup/postgres_backup
directories when the vagrant destroy
command is executed. These directories can be found in the folder where you installed Homestead or in the root of your project if you are using the per project installation method.
Configuring Cron Schedules
Laravel provides a convenient way to schedule cron jobs by scheduling a single schedule:run
Artisan command to run every minute. The schedule:run
command will examine the job schedule defined in your routes/console.php
file to determine which scheduled tasks to run.
If you would like the schedule:run
command to be run for a Homestead site, you may set the schedule
option to true
when defining the site:
sites:
- map: homestead.test
to: /home/vagrant/project1/public
schedule: true
The cron job for the site will be defined in the /etc/cron.d
directory of the Homestead virtual machine.
Configuring Mailpit
Mailpit allows you to intercept your outgoing email and examine it without actually sending the mail to its recipients. To get started, update your application's .env
file to use the following mail settings:
MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Once Mailpit has been configured, you may access the Mailpit dashboard at http://localhost:8025
.
Configuring Minio
Minio is an open source object storage server with an Amazon S3 compatible API. To install Minio, update your Homestead.yaml
file with the following configuration option in the features section:
minio: true
By default, Minio is available on port 9600. You may access the Minio control panel by visiting http://localhost:9600
. The default access key is homestead
, while the default secret key is secretkey
. When accessing Minio, you should always use region us-east-1
.
In order to use Minio, ensure your .env
file has the following options:
AWS_USE_PATH_STYLE_ENDPOINT=true
AWS_ENDPOINT=http://localhost:9600
AWS_ACCESS_KEY_ID=homestead
AWS_SECRET_ACCESS_KEY=secretkey
AWS_DEFAULT_REGION=us-east-1
To provision Minio powered "S3" buckets, add a buckets
directive to your Homestead.yaml
file. After defining your buckets, you should execute the vagrant reload --provision
command in your terminal:
buckets:
- name: your-bucket
policy: public
- name: your-private-bucket
policy: none
Supported policy
values include: none
, download
, upload
, and public
.
Laravel Dusk
In order to run Laravel Dusk tests within Homestead, you should enable the webdriver feature in your Homestead configuration:
features:
- webdriver: true
After enabling the webdriver
feature, you should execute the vagrant reload --provision
command in your terminal.
Sharing Your Environment
Sometimes you may wish to share what you're currently working on with coworkers or a client. Vagrant has built-in support for this via the vagrant share
command; however, this will not work if you have multiple sites configured in your Homestead.yaml
file.
To solve this problem, Homestead includes its own share
command. To get started, SSH into your Homestead virtual machine via vagrant ssh
and execute the share homestead.test
command. This command will share the homestead.test
site from your Homestead.yaml
configuration file. You may substitute any of your other configured sites for homestead.test
:
share homestead.test
After running the command, you will see an Ngrok screen appear which contains the activity log and the publicly accessible URLs for the shared site. If you would like to specify a custom region, subdomain, or other Ngrok runtime option, you may add them to your share
command:
share homestead.test -region=eu -subdomain=laravel
If you need to share content over HTTPS rather than HTTP, using the sshare
command instead of share
will enable you to do so.
[!WARNING]
Remember, Vagrant is inherently insecure and you are exposing your virtual machine to the Internet when running theshare
command.
Debugging and Profiling
Debugging Web Requests With Xdebug
Homestead includes support for step debugging using Xdebug. For example, you can access a page in your browser and PHP will connect to your IDE to allow inspection and modification of the running code.
By default, Xdebug is already running and ready to accept connections. If you need to enable Xdebug on the CLI, execute the sudo phpenmod xdebug
command within your Homestead virtual machine. Next, follow your IDE's instructions to enable debugging. Finally, configure your browser to trigger Xdebug with an extension or bookmarklet.
[!WARNING]
Xdebug causes PHP to run significantly slower. To disable Xdebug, runsudo phpdismod xdebug
within your Homestead virtual machine and restart the FPM service.
Autostarting Xdebug
When debugging functional tests that make requests to the web server, it is easier to autostart debugging rather than modifying tests to pass through a custom header or cookie to trigger debugging. To force Xdebug to start automatically, modify the /etc/php/7.x/fpm/conf.d/20-xdebug.ini
file inside your Homestead virtual machine and add the following configuration:
; If Homestead.yaml contains a different subnet for the IP address, this address may be different...
xdebug.client_host = 192.168.10.1
xdebug.mode = debug
xdebug.start_with_request = yes
Debugging CLI Applications
To debug a PHP CLI application, use the xphp
shell alias inside your Homestead virtual machine:
xphp /path/to/script
Profiling Applications With Blackfire
Blackfire is a service for profiling web requests and CLI applications. It offers an interactive user interface which displays profile data in call-graphs and timelines. It is built for use in development, staging, and production, with no overhead for end users. In addition, Blackfire provides performance, quality, and security checks on code and php.ini
configuration settings.
The Blackfire Player is an open-source Web Crawling, Web Testing, and Web Scraping application which can work jointly with Blackfire in order to script profiling scenarios.
To enable Blackfire, use the "features" setting in your Homestead configuration file:
features:
- blackfire:
server_id: "server_id"
server_token: "server_value"
client_id: "client_id"
client_token: "client_value"
Blackfire server credentials and client credentials require a Blackfire account. Blackfire offers various options to profile an application, including a CLI tool and browser extension. Please review the Blackfire documentation for more details.
Network Interfaces
The networks
property of the Homestead.yaml
file configures network interfaces for your Homestead virtual machine. You may configure as many interfaces as necessary:
networks:
- type: "private_network"
ip: "192.168.10.20"
To enable a bridged interface, configure a bridge
setting for the network and change the network type to public_network
:
networks:
- type: "public_network"
ip: "192.168.10.20"
bridge: "en1: Wi-Fi (AirPort)"
To enable DHCP, just remove the ip
option from your configuration:
networks:
- type: "public_network"
bridge: "en1: Wi-Fi (AirPort)"
To update what device the network is using, you may add a dev
option to the network's configuration. The default dev
value is eth0
:
networks:
- type: "public_network"
ip: "192.168.10.20"
bridge: "en1: Wi-Fi (AirPort)"
dev: "enp2s0"
Extending Homestead
You may extend Homestead using the after.sh
script in the root of your Homestead directory. Within this file, you may add any shell commands that are necessary to properly configure and customize your virtual machine.
When customizing Homestead, Ubuntu may ask you if you would like to keep a package's original configuration or overwrite it with a new configuration file. To avoid this, you should use the following command when installing packages in order to avoid overwriting any configuration previously written by Homestead:
sudo apt-get -y \
-o Dpkg::Options::="--force-confdef" \
-o Dpkg::Options::="--force-confold" \
install package-name
User Customizations
When using Homestead with your team, you may want to tweak Homestead to better fit your personal development style. To accomplish this, you may create a user-customizations.sh
file in the root of your Homestead directory (the same directory containing your Homestead.yaml
file). Within this file, you may make any customization you would like; however, the user-customizations.sh
should not be version controlled.
Provider Specific Settings
VirtualBox
natdnshostresolver
By default, Homestead configures the natdnshostresolver
setting to on
. This allows Homestead to use your host operating system's DNS settings. If you would like to override this behavior, add the following configuration options to your Homestead.yaml
file:
provider: virtualbox
natdnshostresolver: 'off'
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
推荐文章: