原文:https://blog.csdn.net/woqianduo/article/details/81091154/
1、簡介
1.1、Homestead是什么
Laravel Homestead 是一個官方預封裝的 Vagrant box,它為你提供了一個完美的開發環境,而無需在本地機器安裝 PHP 、Web 服務器和其他服務器軟件。不用擔心會搞亂你的操作系統!Vagrant boxes 是一次性的。如果出現問題,你可以在幾分鍾內銷毀並創建 Box!
Homestead 可以運行在任何 Windows,Mac,或 Linux 系統,它包括了 Nginx web 服務器, PHP 7.2,PHP 7.1,PHP 7.0,PHP 5.6, MySQL,PostgreSQL,Redis,Memcached, Node,以及開發 Laravel 應用程序所需要的東西。
1.2、Vagrant是什么
Vagrant構建在 虛擬化技術之上 的 虛擬機運行環境管理工具
1.3、安裝前准備
安裝Homestead之前,先自行安裝Git、Virtualbox和Vagrant,並下載homestead.box
2、安裝Homestead
我的Vagrant 安裝目錄選擇在 D:/Vagrant
2.1、打開Git Bash 進入D: /Vagrant
2.2、添加homestead.box到Vagrant
2.2.1、本地無homestead.box執行 (建議使用第二種方式,先下載)
vagrant box add laravel/homestead
2.2.2、本地有homestead.box,將homestead.box復制到D:/Vagrant下(當前目錄)
vagrant box add laravel/homestead homestead.box
2.2.3、查看安裝結果 執行
vagrant box list
//laravel/homestead (virtualbox, 6.1.0)
//見以上結果為安裝成功
2.3、克隆 Laravel Homestead 倉庫到 D:/vagrant/homestead
git clone https://github.com/laravel/homestead.git
cd homestead
2、4.#執行 init.sh(生成Homestead.yaml文件)
bash init.sh
2、5.創建工作目錄
D:/vagrant/www
因為composer要指定php版本,windows如果沒有php環境(有也可以當沒有),因為馬上就要使用homestead的lnmp環境。所以先不在windows安裝laravel,等homestead安裝好后在lnmp環境下安裝composer和laravel即可,可以先建個簡單的目錄 D:/vagrant/www/test/public下建個index.php(echo 'hello laravel')。
2.6、配置秘鑰
cd ~/.ssh
ls
查看是否存在id_rsa和id_rsa.pub連個文件,如果有就不用,再生產,如果沒有執行如下命令
ssh-keygen -t rsa -C "you@homestead.com
2.7、#配置Homestead.yaml 文件 (我的文件在D:/vagrant/homestead目錄下)
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: D:/Vagrant/code
to: /home/vagrant/code
sites:
- map: test.homestead.com
to: /home/vagrant/laravel/public
databases:
- homestead
2.8、配置windows hosts 文件
打開 C:/Windows/System32/drivers/etc/host 添加
192.168.10.10 test.homestead.com
2.9 、如果使用本地box文件安裝,需要找到homestead\scripts\文件夾中,打開homestead.rb文件
config.vm.box_version = settings["version"] ||= ">= 0"
2.10、基本使用
//啟動虛擬機
cd d:/homestead
vagrant up
//進入虛擬機
vagrant ssh
//登錄mysql 密碼為secret (查看mysql版本是否為自己想要的)
mysql -u homestead -p
//查看php版本、nginx版本
php -v
nginx -v
3、安裝完成
3.1、成功
安裝完后在本地瀏覽器,訪問:test.homestead.com
如果輸出前面寫好腳本:hello laravel
那么就到此結束。
3.2、報錯
報錯:502 Bad Gateway
1.查看nginx錯誤日志
/var/log/nginx && ls
//access.log error.log test.laravel.com-error.log
cat test.laravel.com-error.log
//看到錯誤日志:
2019/05/29 16:02:47 [crit] 844#844: *1 connect() to unix:/var/run/php/php7.3-fpm.sock failed (2: No such file or directory) while connecting to upstream, client@@@
//大致意思是php7.3未找到
2.查看php
cd /var/run/php && ls
//php5.6-fpm.sock php7.0-fpm.sock php7.1-fpm.sock php7.2-fpm.sock
//沒有php7.3
3.修改nginx站點配置
cd /etc/nginx/sites-enabled && ls
//test.laravel.com
sudo vim test.laravel.com
/*
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
*/
//將php7.3-fpm.sock修改為:php7.2-fpm.sock,保存退出
4.重啟nginx
sudo nginx -s reload