学习laravel的话,homestead可以说是必备的,而且laravel的作者也推荐使用,国内的社区也都有相关的教程.
但是大多都是在线安装, 但是在线安装最大的缺点就是: 慢, 非常慢, 奇慢无比
...服务器在国外,而且.box
文件本分也有1G多, 要想体验在线安装, 可以晚上执行命令, 然后去睡觉, 运气好的话,就安装成功了
系统环境需要
在安装之前homestead之前, 请确保你的电脑安装好了, virtual box
和 vagrant
- git
- virtual box
- vagrant
- homestead 这个盒子,如果没有,可以到我的云盘中下载
- 链接:https://pan.baidu.com/s/1OVVGb6GVj3X9WmLuHEz0gA
- 提取码:poos
创建 metadata.json
- 下载 .box 文件之后, 在.box文件同级目录下创建一个
metadata.json
文件
{
"name": "laravel/homestead",
"versions": [{
"version": "6.3.0", // 这个是我的homestead版本, 如果不是这个请自行更改
"providers":[
{
"name": "virtualbox",
"url": "file://C:/vagrant/virtualbox.box" // .box 文件存放的绝对路径
}
]
}]
}
执行安装命令
vagrant box add metadata.json
查看是否安装成功
vagrant box list
克隆homestead切换到指定的版本
git clone https://github.com/laravel/homestead
cd homestead
git checkout v6.3.0
初始化
- windows
./init.bat
- macos or linux
./init.sh
- 生成链接秘钥
ssh-keygen -t rsa -C "your email address"
将这里的 your email address
换成你的邮箱, 然后一路回车, 为什么要加个邮箱? 因为github免密提交的秘钥是这样生成的, 如果这样生成的话, 不用再重新配置了
启动 homestead
vagrant up
如何在homestead中运行其他的框架
homestead默认nginx重写规则是只支持laravel, 如果想用其他框架也运行在 homestead 中又不想自己再配置一个环境,这里以ThinkPHP框架为例
在
/homestead/scripts/
目录下新建一个serve-thinkphp.sh
#!/usr/bin/env bash
declare -A params=$6 # Create an associative array
paramsTXT=""
if [ -n "$6" ]; then
for element in "${!params[@]}"
do
paramsTXT="${paramsTXT}
fastcgi_param ${element} ${params[$element]};"
done
fi
# nginx 配置
block="server {
listen ${3:-80};
listen ${4:-443} ssl http2;
server_name .$1;
root \"$2\";
index index.html index.htm index.php;
charset utf-8;
location / {
#try_files \$uri \$uri/ /index.php?\$query_string;
if (!-e \$request_filename) {
rewrite ^(.*)$ /index.php?s=/\$1 last;
#break;
}
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/$1-error.log error;
sendfile off;
client_max_body_size 100m;
location ~* ^(/images|/Static).+.(jpg|jpeg|css|gif|png|ico) {
access_log off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/$1.crt;
ssl_certificate_key /etc/nginx/ssl/$1.key;
}
"
echo "$block" > "/etc/nginx/sites-available/$1"
ln -fs "/etc/nginx/sites-available/$1" "/etc/nginx/sites-enabled/$1"
- 在
/homestead/Homestead.yml
的site
选项中指定一个type
属性
sites:
- map: www.fastadmin.local
to: /home/vagrant/code/fastadmin/public
type: thinkphp
vagrant up 错误解决
-
错误如下:
vagrant up error 问题出现:
在使用git bassh
的时候,./init
tab 提示出来的默认是./init.sh
, 如果是windows环境,不能用 init.sh 来初始化, 只能用./init.bat
来初始化,解决办法:
- 再执行一次
./init.bat
重新初始化 -
覆盖原有的配置文件
override