Laravel的Homestead开发环境部署

Vagrant脚本安装

Vagrant命令只能识别Vagrantfile,通过Homestead/Vagrantfile加载Homestead/scripts目录下的homestead.rb

> git clone https://github.com/laravel/homestead.git Homestead  //使用Git Bash执行,使用CMD有点问题

> vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead'
    box: URL: https://atlas.hashicorp.com/laravel/homestead
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) parallels
3) virtualbox
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'laravel/homestead' (v2.1.0) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/laravel/boxes/homestead/versions/2.1.0/providers/virtualbox.box
    box: Progress: 100% (Rate: 359k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'laravel/homestead' (v2.1.0) for 'virtualbox'!

如果速度很慢,直接Ctrl+C中断,使用迅雷下载:https://atlas.hashicorp.com/laravel/boxes/homestead/versions/2.1.0/providers/virtualbox.box

> vagrant box add laravel/homestead E:/VagrantBox/homestead-virtualbox.box

> vagrant box list

> cd Homestead

> init.bat

> vagrant up

如遇到id_rsa/id_rsa.pub等SSH问题,则在Git Bash下执行命令:

ssh-keygen -t rsa -C "yezhou@yezhou.org"

如果vagrant up出现问题:

C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.3/lib/vagrant/util/is_port_open.rb:21:in `initialize': The requested address is not valid in its context. - connect(2) for "0.0.0.0" port 8000 (Errno::EADDRNOTAVAIL)

则修改Homestead/scripts/homestead.rbhost_ip改为 "127.0.0.1"

# Use Default Port Forwarding Unless Overridden
default_ports.each do |guest, host|
  unless settings["ports"].any? { |mapping| mapping["guest"] == guest }
    # config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
    config.vm.network "forwarded_port", guest: guest, host: host, host_ip: "127.0.0.1", auto_correct: true
  end
end

# Add Custom Ports From Configuration
if settings.has_key?("ports")
  settings["ports"].each do |port|
    # config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], protocol: port["protocol"], auto_correct: true
    config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], protocol: port["protocol"], host_ip: "127.0.0.1", auto_correct: true
  end
end

全局安装

> composer global require "laravel/homestead=~2.0"
Changed current directory to C:/Users/yezhou/AppData/Roaming/Composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing laravel/homestead (v2.2.2)
    Downloading: 100%

Writing lock file
Generating autoload files

> homestead -V
Laravel Homestead version 2.2.2

> homestead up

如果 homestead up 出现问题:

C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.3/lib/vagrant/util/is_port_open.rb:21:in `initialize': The requested address is not valid in its context. - connect(2) for "0.0.0.0" port 8000 (Errno::EADDRNOTAVAIL)

则修改C:\Users\yezhou\AppData\Roaming\Composer\vendor\laravel\homestead\scripts\homestead.rbhost_ip改为 "127.0.0.1"

# Use Default Port Forwarding Unless Overridden
default_ports.each do |guest, host|
  unless settings["ports"].any? { |mapping| mapping["guest"] == guest }
    # config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
    config.vm.network "forwarded_port", guest: guest, host: host, host_ip: "127.0.0.1", auto_correct: true
  end
end

# Add Custom Ports From Configuration
if settings.has_key?("ports")
  settings["ports"].each do |port|
    # config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], protocol: port["protocol"], auto_correct: true
    config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], protocol: port["protocol"], host_ip: "127.0.0.1", auto_correct: true
  end
end

Homestead测试

查看Vagrant Box

> vagrant box list
CentOS72          (virtualbox, 0)
laravel/homestead (virtualbox, 0.3.3)
laravel/homestead (virtualbox, 2.1.0)

注意:使用Homestead全局安装的默认版本并不是最新的

Homestead.yaml

注意:使用脚本启动默认启动名称为homestead-7的虚拟机;使用全局启动默认启动名称为homestead的虚拟机。

无论是脚本启动还是全局启动,Homestead的配置文件均为C:\Users\yezhou\.homestead\Homestead.yaml

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Code
      to: /home/vagrant/Code

sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public

databases:
    - homestead

修改hosts

192.168.10.10       yz.app
192.168.10.10       homestead.app

SSH 登录

启动成功信息:

> vagrant up
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Checking if box 'laravel/homestead' is up to date...
==> homestead-7: Clearing any previously set forwarded ports...
==> homestead-7: Clearing any previously set network interfaces...
==> homestead-7: Preparing network interfaces based on configuration...
    homestead-7: Adapter 1: nat
    homestead-7: Adapter 2: hostonly
==> homestead-7: Forwarding ports...
    homestead-7: 80 (guest) => 8000 (host) (adapter 1)
    homestead-7: 443 (guest) => 44300 (host) (adapter 1)
    homestead-7: 3306 (guest) => 33060 (host) (adapter 1)
    homestead-7: 5432 (guest) => 54320 (host) (adapter 1)
    homestead-7: 27017 (guest) => 27017 (host) (adapter 1)
    homestead-7: 22 (guest) => 2222 (host) (adapter 1)
==> homestead-7: Running 'pre-boot' VM customizations...
==> homestead-7: Booting VM...
==> homestead-7: Waiting for machine to boot. This may take a few minutes...
    homestead-7: SSH address: 127.0.0.1:2222
    homestead-7: SSH username: vagrant
    homestead-7: SSH auth method: private key
==> homestead-7: Machine booted and ready!
==> homestead-7: Checking for guest additions in VM...
==> homestead-7: Setting hostname...
==> homestead-7: Configuring and enabling network interfaces...
==> homestead-7: Mounting shared folders...
    homestead-7: /vagrant => D:/Laravel/Homestead
    homestead-7: /home/vagrant/Code => C:/Users/yezhou/Code
==> homestead-7: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> homestead-7: flag to force provisioning. Provisioners marked to run always will still run.

> homestead up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'laravel/homestead' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 (guest) => 8000 (host) (adapter 1)
    default: 443 (guest) => 44300 (host) (adapter 1)
    default: 3306 (guest) => 33060 (host) (adapter 1)
    default: 5432 (guest) => 54320 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 5.0.4
    default: VirtualBox Version: 5.1
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/yezhou/AppData/Roaming/Composer/vendor/laravel/homestead
    default: /home/vagrant/Code => C:/Users/yezhou/Code
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

默认账户和密码:

  • SSH address: 127.0.0.1:2222
  • SSH username: vagrant
  • SSH password: vagrant

环境测试

vagrant@homestead:~$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.4.1 2017-03-10 09:29:45

vagrant@homestead:~$ nginx -v
nginx version: nginx/1.11.9

vagrant@homestead:~$ php -v
PHP 7.1.3-3+deb.sury.org~xenial+1 (cli) (built: Mar 25 2017 14:00:03) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.3-3+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.16.0~linux-x64-non_zts71, https://blackfire.io, by Blackfireio Inc.

vagrant@homestead:~$ mysql -V
mysql  Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using  EditLine wrapper
vagrant@homestead:~$ mysql -uroot -p
Enter password:     //默认密码:secret
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17-0ubuntu0.16.04.2 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

vagrant@homestead:~$ psql -V
psql (PostgreSQL) 9.5.6
vagrant@homestead:~$ psql -U homestead -h localhost
Password for user homestead:     //默认密码:secret
psql (9.5.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

homestead=# \l
                                  List of databases
   Name    |   Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+-----------+----------+-------------+-------------+-----------------------
 homestead | homestead | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |           |          |             |             | postgres=CTc/postgres
 template1 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |           |          |             |             | postgres=CTc/postgres
(4 rows)

homestead=# 

vagrant@homestead:~$ redis-server -v
Redis server v=3.2.8 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=11aa79fd2425bed9
vagrant@homestead:~$ redis-cli -v
redis-cli 3.2.8

vagrant@homestead:~$ memcached -i
memcached 1.4.25

部署测试

http://yz.app/

http://homestead.app/

http://192.168.10.10/

http://localhost:8000/ (仅在全局启动有效)

修改Homestead.yaml并重新加载

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: D:/Laravel/www
      to: /home/vagrant/www

sites:
    - map: homestead.app
      to: /home/vagrant/www/index
    - map: yz.app
      to: /home/vagrant/www/yezhou/public

databases:
    - homestead

重新加载配置

> vagrant provision
> vagrant reload  //某些情况下需要重启虚拟机
> vagrant reload --provision  //或者加载更新重启

安装Laravel

vagrant@homestead:~$ composer config -g repo.packagist composer https://packagist.phpcomposer.com
vagrant@homestead:~$ composer global require "laravel/installer"
vagrant@homestead:~$ laravel -V
Laravel Installer 1.3.5
vagrant@homestead:~$ cd www/
vagrant@homestead:~/www$ laravel new yezhou
vagrant@homestead:~/www$ cd yezhou/
vagrant@homestead:~/www/yezhou$ php artisan -V
Laravel Framework 5.4.18

Laravel测试

此时再打开:http://yz.app/

即可看到熟悉的Laravel页面~

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/25/laravel-homestead-development-environment-deployment/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Laravel的Homestead开发环境部署
Vagrant脚本安装 Vagrant命令只能识别Vagrantfile,通过Homestead/Vagrantfile加载Homestead/scripts目录下的homestead.rb > git clone https://github.com/l……
<<上一篇
下一篇>>
文章目录
关闭
目 录