mac下使用vagrant和homestead配置开发环境
这份教程主要是解决homestead在国内下载速度缓慢的问题,以及碰到问题的总结归纳。
1.下载安装必须的软件 vagrant | virtualbox
- virtualbox下载
- vagrant下载
- iTerm 【mac下这个terminal比较好用】
2.查看vagrant版本,确定已经安装完成
打开iTerm
普通打开:Launchpad-iTerm
快捷键:command+空格 然后输入iTerm,具体操作如下图
#输入命令查看vagrant是否安装成功
vagrant -v
显示了版本,说明安装成功
3.下载laravel/homestead
如果速度太慢,请挂代理下载或者参考【附录1】,导入box教程
在命令行中输入
vagrant box add laravel/homestead
4.安装 Homestead
在命令后中输入
#切换到用户文件夹
cd ~
#克隆homestead项目
git clone https://github.com/laravel/homestead.git Homestead
一旦你克隆完 Homestead 的代码仓库,即可在 Homestead 目录中运行 bash init.sh
命令 来创建 Homesstead.yaml
配置文件。 Homesstead.yaml
文件会被放置在你的 ~/Homestead
目录中:
#切换到homestead目录
cd ~/Homestead
# Mac / Linux...
bash init.sh
5.配置 Homestead
配置共享文件夹
首先在mac下创建code
文件夹
打开命令行
#切换到用户目录
cd ~
#创建文件夹
mkdir -p code/lexing
#切换回homestead目录
cd ~/Homestead
你可以在 Homestead.yaml
文件的 folders
属性里列出所有想与 Homestead 环境共享的文件夹。这些文件夹中的文件若有变更,它们将会在你的本机电脑与 Homestead 环境自动更新同步。你可以在这里设置多个共享文件夹:
folders:
- map: /Users/chrischen/code #mac中的目录
to: /home/vagrant/code #虚拟机中的目录
~~配置 Nginx 站点(不太推荐)~~
使用这个操作是具有破坏性的,当执行
provision
命令,现有的数据库会被摧毁并重新创建。推荐手动配置nginx服务器, 参考【附录2】
对 Nginx 不熟悉吗?没关系。sites
属性可以帮助你可以轻易指定一个 域名
来对应到 homestead 环境中的一个目录上。在 Homestead.yaml
文件中已包含了一个网站设置范本。同样的,你也可以增加多个网站到你的 Homestead 环境中。 Homestead 可以同时为多个 Laravel 应用提供虚拟化环境:
sites:
- map: www.lexing.com
to: /home/vagrant/code/lexing/
- map: another.app
to: /home/vagrant/Code/another/public
如果你在 Homestead box 配置之后更改了 sites
属性,那么应该重新运行 vagrant reload --provision
来更新 Nginx 配置到虚拟机上。
6.关于 Hosts 文件-修改mac本地hosts
下面的步骤是配置一个本地的dns解析,homestead默认的地址是192.168.10.10。如果你想修改这个默认的ip地址,具体你可以在mac下的
~/Homestead/Homestead.yaml
中的ip那栏进行修改。
命令行输入sudo vi /etc/hosts
,如果你不会vi命令跳过看下面 普通编辑
7.使用vi编辑hosts
使用vi编辑文档,增加192.168.10.10 www.lexing.com
普通编辑
使用快捷键:选中finder=>shift+command+G
打开finder =>菜单栏前往=>前往文件夹
输入/etc
回车如图所示,找到hosts
将hosts打开编辑,我这里使用sublime3,增加
192.168.10.10 www.lexing.com
最后保存,并输入密码确认保存
8.启动 Vagrant Box
在mac命令行中输入 cd ~/Homestead
,切换到homestead项目所在到目录
然后输入 vagrant up
,启动画面如下图
9.登陆虚拟机
启动成功之后,输入vagrant ssh
登陆到vagrant虚拟机,如下图所示
附录一:本地导入homestead.box教程
1.下载homestead.box
查看homestead最新版本
打开homestead官网:https://atlas.hashicorp.com/laravel/boxes/homestead
如图所示,最新版本为v1.1.0
,然后修改下面的url资源链接。
#将versions后面的版本改为最新的,这份教程最新的版本为1.1.0
https://atlas.hashicorp.com/laravel/boxes/homestead/versions/1.1.0/providers/virtualbox.box
在浏览器中打开上面的url之后,就会弹出一下下载界面,并将下载文件另存为homestead.box,我这里下载的地址为Downloads
文件夹,等待下载完成。如果下载非常缓慢,请挂代理或者梯子扶墙。
2.手动导入box包
打开命令行
#切换到下载目录
cd ~/Downloads
#使用命令导入下载包
vagrant box add laravel/homestead homestead.box
#使用命令查看导入成功的下载包
vagrant box list
如图所示,输入命令,看到有laravel/homestead
则导入包成功
3.导入包成功之后,根据步骤4安装Homestead继续配置
配置完步骤4之后,因为是手动导入包,还需要多更改几个步骤。
编辑Homestead/scripts/homestead.rb
文件
修改
config.vm.box_version = settings["version"] ||= ">= 1.0.0"
改成
config.vm.box_version = settings["version"] ||= ">= 0
附录二:虚拟机nginx配置多站点
1.登陆到vagrant虚拟机环境
#切换到nginx配置目录
cd /etc/nginx/sites-available
2.拷贝一份配置文件
#拷贝一份配置文件,名字为lexing
cp homestead.app lexing
3.修改配置
#编辑lexing配置,删除里面所有的配置信息
sudo nano lexing
4.复制如下的nginx配置
server {
server_name lexing.com www.lexing.com;
root /home/vagrant/code/lexing;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
5.保存退出
按住control+X
然后按y保存
6.重启服务器
sudo service nginx restart
7.测试服务器
第3步和第4步 是不是弄反了?
没有噢。因为国内不好下载,所以我都是提前下载好包之后再用的