配置laravel开发环境,vscode开发php项目,宝塔搭建web环境

配置laravel开发环境
1、生成 Homestead Vagrant Box
1.1、下载 VirtualBox 并安装

1.2、下载 vagrant 并安装

1.3、获取 laravel settler(如果要安装宝塔,就不能安装laravel环境,有冲突。跳过1.3、1.4、1.5、1.6,执行1.?)

git clone -b v7.0.0 https://github.com/laravel/settler.git laravel-settler

 

1.4、在1.3项目同级目录下获取 chef/bento

git clone -b v201808.24.0 https://github.com/chef/bento.git bento

 

1.5、laravel-settler目录下执行

./bin/copy-to-bento.sh

 

1.6、把 bento/ubuntu/ubuntu-18.04-amd64.json 文件中 scripts/cleanup.sh 替换为 scripts/homestead.sh

 

1.?、安装宝塔(ubuntu18.04安装宝塔有异常,所以用16.04版本)。如果安装 laravel 环境跳过此步骤。

        bento\ubuntu\scripts 目录下创建 bt.sh,内容如下:

#安装宝塔面板
#!/usr/bin/env bash

export DEBIAN_FRONTEND=noninteractive

# Update Package List

apt-get update

# Update System Packages
apt-get -y upgrade

# Force Locale

echo "LC_ALL=en_US.UTF-8" >> /etc/default/locale
locale-gen en_US.UTF-8

sudo su root<<'EOF'
echo -e "root\nroot\n" | sudo passwd
cd /root
wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh
echo "y" | sudo bash install.sh
EOF

  在 bento/ubuntu/ubuntu-16.04-amd64.json 文件中 scripts/cleanup.sh 前添加 scripts/bt.sh


1.7、下载 Packer

1.8、注册 packer(我的是拷贝到C:\Program Files\,使用管理员身份打开 cmd

set path_= %Path%
setx "Path" "C:\Program Files\packer\;%path_%" /m 

 

1.9、修改 bento\ubuntu\http\preseed.cfg 文件
archive.ubuntu.com 修改为国内阿里镜像主机 mirrors.aliyun.com

 

1.10、bento\ubuntu\scripts\homestead.sh文件在apt-add-repository ppa:ondrej/php -y 下面添加

sed -i "s/ppa\.launchpad\.net/launchpad.proxy.ustclug.org/g" /etc/apt/sources.list.d/*.list

 

1.11、bento 目录下执行(如果安装的是宝塔步骤,下面的 ubuntu-18.04-amd64.json 换成 ubuntu-16.04-amd64-bt.json)

cd ubuntu
packer build -only=virtualbox-iso ubuntu-18.04-amd64.json

 

1.12、bento\builds 目录下将生成由1.11执行的ubuntu-18.04.virtualbox.box

参考文件
1、https://github.com/laravel/settler/tree/v7.0.0
2、https://github.com/chef/bento/tree/v201808.24.0

 

 

2、本地方式添加 Homestead Vagrant Box
2.1、在 bento\builds 目录下执行:

vagrant box add laravel/homestead ubuntu-18.04.virtualbox.box

添加成功之后,一定不要急着执行 vagrant up
2.2、修改版本号
2.2.1、打开c:/users/“登录账号”/.vagrant.d/boxes/laravel-VAGRANTSLASH-homestead目录
a)添加一个名为 metadata_url 的文件
里面只写上
https://vagrantcloud.com/laravel/homestead
要注意空格什么的也不能有
b)把看到的 0 或 0.0 这个目录修改为你的 box 版本号,比如这里的就是1.3中的 7.0.0

参考文件
1、https://segmentfault.com/a/1190000008396792

 

 

3、安装 Homestead
3.1、在1.3项目同级目录下执行

git clone https://github.com/laravel/homestead.git homestead
cd homestead
// 克隆期望的发行...
git checkout v7.20.0

3.2、在 homestead 目录中使用 init.bat 命令来创建 Homestead.yaml 配置文件。 
此 Homestead.yaml 文件将被放在 homestead 目录中
3.3、配置 homestead


参考文件
1、https://laravel-china.org/docs/laravel/5.7/homestead/2245#4820f7

 

 

4、启动运行环境
4.1、在3.1中homestead目录下执行 

vagrant up

  

5、创建项目
5.1、进入虚拟机

vagrant ssh

  

5.2、进入你的工作项目目录执行

composer create-project laravel/laravel 新项目名

  

5.3、等待安装完成后,在浏览器打开3.3项目中配置的 url

参考文件
1、https://bbs.csdn.net/topics/392167230

 

 

6、配置远程 debug
6.1、登录虚拟机,查看虚拟机网关
route -n
可看到这里的网关为 10.0.2.2 ,记录下来


6.2、找到 Xdebug 配置文件 xdebug.ini 的路径,注意:请先启用 Xdebug
vagrant@homestead:~$ php --ini | grep 'xdebug'
输出结果根据实际环境定,我的输出结果为:
/etc/php/7.2/cli/conf.d/20-xdebug.ini
修改该配置文件
vagrant@homestead:~$ sudo vim /etc/php/7.2/cli/conf.d/20-xdebug.ini
将原配置替换为如下:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_host=10.0.2.2 #此处填入刚才获得的网关
重启 PHP-FPM 服务
vagrant@homestead:~$ sudo service php7.2-fpm re


6.3、下面操作 vscode
安装并启用 PHP Debug 扩展
打开 Debug 面板,添加配置项,就是那个齿轮,点击后选择 PHP

{
    "version": "0.2.0",
    "configurations": [
        ...
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "pathMappings": {
            "/home/vagrant/Code": "/home/work/Code" // 此处按实际映射路径填写,左边是虚    拟机web路径(如/www/wwwroot/****),右边本机项目路径(可以用 ${workspaceRoot} 代替)
            },
        "port": 9000
        }
    ]
}                

  

6.4、其他插件
6.4.1、PHP Intelephense 
PHP 代码自动提示、补全。安装此插件后,需关闭编辑器本身自带的代码提示,否则会重复显示函数提示。打开“用户设置"(快捷键CMD + ,),将 php.suggest.basic 设置为 false
6.4.2、php cs fixer
PHP 格式化代码。在插件中心搜索php cs fixer,会出现2个同名的插件,我们这里选择安装作者是 junstyle 的php cs fixer。
6.4.3、PHP DocBlocker
代码注释补全,在给类、函数写注释时,能自动补全。

问题:
1、vagrant 免密登录失败
问题:
The private key you're attempting to use with this Vagrant box uses
an unsupported encryption type. The SSH library Vagrant uses does not support
this key type. Please use `ssh-rsa` or `ssh-dss` instead. Note that
sometimes keys in your ssh-agent can interfere with this as well,
so verify the keys are valid there in addition to standard
file paths.
或者
default: Warning: Authentication failure. Retrying...

解决方案:
第一种方式:
在 windows 系统下生产的私钥、公钥跟 linux 是不通的,需要到 linux 虚拟机中去生成。
cd /home/vagrant/.ssh
ssh-keygen -t rsa -C "vagrant"
./id_rsa
会在该目录下生成两个文件id_rsa 和 id_rsa.pub。将 id_rsa.pub 内容复制粘贴到当前目录下的 authorized_keys 文件中,
将 id_rsa 内容复制粘贴到 \你的项目目录\.vagrant\machines\default\virtualbox 目录下的 private_key 文件中。如果
没有 private_key 这个文件,就手动创建一个。
第二种方式:
在Vagrantfile配置文件中添加了两行代码,使用明文用户名密码。
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
保存
vagrant reload
再禁用上面两行配置
保存
vagrant reload

2、宝塔面板中添加 mysql 数据库失败(我的系统是ubuntu,其他系统参考其他网上文档修改)
问题:宝塔面板中数据库管理界面添加数据库提交时提示:pip install MySQL-python,在 linux 命令行中执行又会报以下错误。
_mysql.c:29:20: fatal error: Python.h: No such file or directory
#include "Python.h"
^
compilation terminated.
error: command 'gcc' failed with exit status 1
解决方案:
sudo apt-get install python-dev
sudo pip install MySQL-python

3、

 

其他

目前自己做了几个盒子。通过命令生成的过程中经常会下载失败(我改成国内镜像也是这种情况,不知道是什么原因),所以小伙伴们要有耐心,多尝试几次。

1、ubuntu-16.04.virtualbox.box
ubuntu-16.04系统
2、ubuntu-16.04-bt.virtualbox.box
ubuntu-16.04系统、宝塔
3、ubuntu-16.04-bts.virtualbox.box
ubuntu-16.04系统,宝塔,通过宝塔安装Apache、php、mysql等
4、ubuntu-18.04.virtualbox.box
ubuntu-18.04系统
5、ubuntu-18.04-homestead.virtualbox.box
ubuntu-18.04系统,homestead系列安装

posted @ 2019-02-14 17:18  Sefrid  阅读(1748)  评论(0编辑  收藏  举报