基于Homestead搭建PHP项目开发环境(适合Zend Framework,Laravel,Yii,thinkphp等)

参考: https://framework.zend.com/bl...
参考: https://laravel.com/docs/5.5/...

第一步:软件的下载和安装

软件1:VirtualBox5.2(122M)

下载日期为2018-08-22,选择版本是5.2.8
下载地址: https://www.virtualbox.org/wi...,我下载的是5.2.8[109M]
5.1下载地址: https://www.virtualbox.org/wi... 点击 Windows hosts x86/AMD64 下载 http://download.virtualbox.or...
 安装在: D:\Program Files\Oracle\VirtualBox

软件2: Vagrant

下载日期是2018=08-22,安装版本是2.1.2,大小是221M
下载地址: https://www.vagrantup.com/dow... 点击下载windows bit64下载 https://releases.hashicorp.co...

安装在: D:\Program Files\HashiCorp\Vagrant

软件3: git和svn

官网 https://git-for-windows.githu...
下载(科学上网才行) https://github.com/git-for-wi...
tortoisesvn官网: https://tortoisesvn.net/
并安装了TortoiseSVN D:\Program Files\TortoiseSVN\bin\TortoisePlink.exe

软件4:Homestead

项目地址:https://github.com/laravel/ho...

PS E:\www> git clone https://github.com/laravel/homestead.git
Cloning into 'homestead'...
remote: Counting objects: 3041, done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 3041 (delta 17), reused 28 (delta 15), pack-reused 3002
Receiving objects: 100% (3041/3041), 632.38 KiB | 242.00 KiB/s, done.
Resolving deltas: 100% (1803/1803), done.

软件5: 盒子

参考: https://laravel.com/docs/5.6/...
盒子搜索 https://app.vagrantup.com/box...,找到盒子laravel/homestead 6.3.0,但没有看到下载地址,怎么下载?,使用盒子安装命令 vagrant box add laravel/homestead,会看到盒子地址,如
https://atlas.hashicorp.com/l...
使用迅雷下载下来,文件名是5b64bd3b-eb87-4af4-9b2d-1c1560efca67,很奇怪,不管它,改名为04_homesteadbox

方法一:在线安装(速度蜗牛,不可行)

PS E:\www> vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead'
    box: URL: https://vagrantcloud.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' (v6.3.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/6.3.0/providers/virtualbox.box
    box: Progress: 0% (Rate: 126k/s, Estimated time remaining: 4:33:51)4
    //如果速度慢,在线安装不了,请用迅雷下载上面的链接

方法二:离线安装之命令行(不成功)

盒子的安装命令用法是 vagrant box add -h
PS E:\www> vagrant box add virtualbox20180822 ,virtualbox.box
// virtualbox20180822 是name
// virtualbox.box是path
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'virtualbox20180822' (v0) for provider:
    box: Unpacking necessary files from: file://E:/www/virtualbox.box
    box: Progress: 100% (Rate: 15.2M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'virtualbox20180822' (v0) for 'virtualbox'!
PS E:\www> vagrant box list
virtualbox20180822 (virtualbox, 0)
//显示安装过的盒子时,这里有个问题,盒子的版本是0, 而Homestead对盒子的版本是有要求的,所以安装时得指明版本号,现在重新卸载后再安装
PS E:\www> vagrant box remove virtualbox20180822
Removing box 'virtualbox20180822' (v0) with provider 'virtualbox'...

方法三:使用配置文件json文件安装(成功)

参考 http://bbs.csdn.net/topics/39...

知识点1:配置文件 Vagrantfile
vagrant管理虚拟机,它首先要读取vagrant的配置文件 Vagrantfile,那么它从哪里去读取这个文件呢,官网是有说明 https://www.vagrantup.com/docs/vagrantfile/#lookup-path, 这与你的当前工作目录有关系,如果当前目录是C:\Users\Administrator>,那么得在C:\Users\Administrator>下找Vagrantfile,事实上,Homestead项目里已写好了Vagrantfile文件,所以当前目录为Homestead就最好办,上面步骤安装好了homestead,切换当前工作目录为E:\www\homestead\

  1. 将下载的盒子拷贝到"E:\www\homestead\boxes\virtualbox20180826.box"下(我将盒子重命名了)
  2. 新建原数据(metadata)文件E:\www\homestead/0000.json(任取名),参考https://www.vagrantup.com/docs/boxes/format.html#box-metadata

       {
           "name": "laravel/homestead",
           "versions": [{
               "version": "6.3.0",
               "providers": [{
                   "name": "virtualbox",
                   "url": "file://E:/www/homestead/boxes/virtualbox20180826.box"
               }]
           }]
       }
  3. 新Homestead配置文件Homestead.yaml,双击执行E:\www\homestead\init.bat
  4. 生成私有秘钥

       PS D:\Program Files\Git\usr\bin> ssh-keygen -t rsa
       Generating public/private rsa key pair.
       Enter file in which to save the key (C:\Users\daqi/.ssh/id_rsa):
       Created directory 'C:\Users\daqi/.ssh'.
       Enter passphrase (empty for no passphrase):
       Enter same passphrase again:
       Your identification has been saved in C:\Users\daqi/.ssh/id_rsa.
       Your public key has been saved in C:\Users\daqi/.ssh/id_rsa.pub.
       The key fingerprint is:
       SHA256:4wIBwmk+PF+ZMeU9SqoD6GpCaa2ZiXmxtGy0u6dL1IE daqi@maibenben
       The key's randomart image is:
       +---[RSA 2048]----+
       |o o   ..         |
       | = o o. .        |
       |+ E o =o o       |
       |.= . *o . .      |
       |..B +. .S        |
       |.==o.. . .       |
       |+**B  . .        |
       |=BB o  .         |
       |+o=*             |
       +----[SHA256]-----+
       PS D:\Program Files\Git\usr\bin>
  5. 安装box

       PS E:\www\homestead> vagrant box add 0000.json
       ==> box: Loading metadata for box '0000.json'
           box: URL: file://E:/www/homestead/0000.json
       ==> box: Adding box 'laravel/homestead' (v6.3.0) for provider: virtualbox
           box: Unpacking necessary files from: file://E:/www/homestead/boxes/virtualbox20180826.box
           box: Progress: 100% (Rate: 24.2M/s, Estimated time remaining: --:--:--)
       ==> box: Successfully added box 'laravel/homestead' (v6.3.0) for 'virtualbox'!

问题1: box安装在哪儿了
回答: "C:\Users\daqi\.vagrant.d\boxes\laravel-VAGRANTSLASH-homestead\6.3.0\virtualbox\ubuntu-18.04-amd64-disk001.vmdk"

第二步:虚拟主机配置

参考: https://www.cnblogs.com/ajian...
有关具体配置参考源代码E:\www\homestead\scripts\homestead.rb,分析源代码,知道在E:\www\homestead\Homestead.yaml中应该写哪些选项

项目 内容 解释
provider virtualbox或vmware_fusion或vmware_workstation或hyperv或parallels 默认为virtualbox
name ———— 默认为homestead-7
box ———— 默认为laravel/homestead
version ———— 默认为>= 6.3.0
hostname ———— 默认为homestead
ip autonetwork或指定ip,也可为空 私有ip地址,默认为192.168.10.10
networks ———— 是一个数组,每项包括配置type,ip,bridge,netmask
default_ssh_port ———— ssh端口
ports ———— 端口
default_ports ———— 端口映射,默认为80 => 8000,443 => 44300,3306 => 33060,4040 => 4040, 5432 => 54320,8025 => 8025,7017 => 27017
authorize ———— 认证
keys ———— ————
copy ———— 拷贝文件到虚拟主机
folders ———— 设置共享文件夹
sites ———— 默认站点类型为laravel,也可以为zf,symfony,apigility,expressive,zray
variables ———— 全局变量
databases ———— 数据库
schedule ———— ————
---
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
    - map: E:/www/wangqianjin/jihuizhenghao
      to: /var/www/jihuizhenghao  
sites:
    - map: homestead.test
      to: /home/vagrant/code/public
    - map: store.jihuizhenghao.com
      type: magento  #不同的类型调用不同的serve.类型.sh,默认是laravel
      php: "7.0"     #指定当前网站使用的php版本,默认是7.2,此处要加双引号,否则会报错 * Shell provisioner `args` must be a string or array.
      to: /var/www/jihuizhenghao/store
databases:
    - jihuizhenghao

修改C:\Windows\System32\drivers\etc\hosts

192.168.10.10  store.jihuizhenghao.com
192.168.10.10  api.jihuizhenghao.com

第三步:启动

PS E:\www\homestead> vagrant up
PS E:\www\homestead> vagrant provision //配置文件修改后执行

第四步:ssh连接并增加root用户

远程登录

使用putty或xshell等软件远程连接虚拟机
主机:192.168.10.10,端口:22,用户名vagrant,密码vagrant

开启root用户

ubuntu默认是没有开通root登录的

wang@bogon:~$ sudo passwd //设置root密码
Enter new UNIX password: //这里输入的是root的密码,使用welcome2018
Retype new UNIX password:
passwd: password updated successfully
wang@bogon:~$ su //切换到root
Password:
root@bogon:/home/ubuntu#  //#表示root用户   $表示一般用户

第五步:定制

// ========查看php版本
vagrant@homestead:~$ php -v
PHP 7.2.9-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Aug 19 2018 07:16:54) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.9-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
    with blackfire v1.22.0~linux-x64-non_zts72, https://blackfire.io, by Blackfire

// ========查看mysql版本
vagrant@homestead:~$ mysql --version
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

// ========查看nginx版本
vagrant@homestead:~$ nginx -v
nginx version: nginx/1.15.0

安装php7.0

// 象magento2.0不支持7.1和7.2,得安装>7.06
盒子已安装了php5.6,7.0,7.1,7.2,7.3,具体查看/usr/lib/php

vagrant@homestead:~$ sudo update-alternatives --config php
There are 5 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php7.3   73        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.0   70        manual mode
  3            /usr/bin/php7.1   71        manual mode
* 4            /usr/bin/php7.2   72        manual mode //前面有*表示的是当前使用的php版本
  5            /usr/bin/php7.3   73        manual mode

Press  to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode

// 查看下当前php版本
vagrant@homestead:~$ php -v
PHP 7.0.31-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Jul 25 2018 10:01:10) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.31-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.22.0~linux-x64-non_zts70, https://blackfire.io, by Blackfire

切记
不同的网站可以设置自己的php版本,在Homestead.yaml中指定

第六步:web服务器配置(重点掌握)

01: Homestead.yaml中配置

sites:
    - map: store.jihuizhenghao.com
      type: magento  #不同的类型调用不同的serve.类型.sh,默认是laravel
      php: "7.0"     #指定当前网站使用的php版本,默认是7.2,此处要加双引号,否则会报错 * Shell provisioner `args` must be a string or array.
      to: /var/www/jihuizhenghao/store

02: 网站配置文件

//E:\www\wangqianjin\jihuizhenghao\store\nginx.conf.jihuizhenghao
# 当前文件E:\www\wangqianjin\jihuizhenghao\store\nginx.conf.jihuizhenghao
## 配置样例: 

# root $MAGE_ROOT/pub;
root $MAGE_ROOT;
index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
#add_header "X-UA-Compatible" "IE=Edge";

## location用法
# location [=|~|~*|^~] /uri/ { … }
# = 开头表示精确匹配
# ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
# ~ 开头表示区分大小写的正则匹配
# ~* 开头表示不区分大小写的正则匹配
# !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
# / 通用匹配,任何请求都会匹配到。

location = ^/del.php$ {
    rewrite del.php$ /0000.php last;
}

# 安装应用的PHP入口
# 以setup开头,结尾有/或没有/,location 后面跟两个参数,两个参数之间有一个空格,第1个参数表示匹配的方式,第2个参数才是一个正则或普通字符串
location ~* ^/setup($|/) {
    root $MAGE_ROOT;
    location ~ ^/setup/index.php {
        fastcgi_pass   fastcgi_backend;

        fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
        fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=600";
        fastcgi_read_timeout 600s;
        fastcgi_connect_timeout 600s;

        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ ^/setup/(?!pub/). {
        deny all;
    }

    location ~ ^/setup/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}

# 更新应用的PHP入口
location ~* ^/update($|/) {
    root $MAGE_ROOT;

    location ~ ^/update/index.php {
        fastcgi_split_path_info ^(/update/index.php)(/.+)$;
        fastcgi_pass   fastcgi_backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        include        fastcgi_params;
    }

    # Deny everything but index.php
    location ~ ^/update/(?!pub/). {
        deny all;
    }

    location ~ ^/update/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}


location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

location /pub/ {
    location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
        deny all;
    }
    alias $MAGE_ROOT/pub/;
    add_header X-Frame-Options "SAMEORIGIN";
}

location /static/ {
    # Uncomment the following line in production mode
    # expires max;

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
        rewrite ^/static/(version[^/]+/)?(.*)$ /static/$2 last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;

        if (!-f $request_filename) {
           rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/ {
    try_files $uri $uri/ /get.php$is_args$args;

    location ~ ^/media/theme_customization/.*\.xml {
        deny all;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/customer/ {
    deny all;
}

location /media/downloadable/ {
    deny all;
}

location /media/import/ {
    deny all;
}

# PHP entry point for main application
location ~ (index|get|static|report|404|503|health_check)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 1024 4k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/xml+rss
    image/svg+xml;
gzip_vary on;

# Banned locations (only reached if the earlier PHP entry point regexes don't match)
location ~* (\.php$|\.htaccess$|\.git) {
    deny all;
}

03: web服务器配置文件脚本(自动生成配置文件)

// E:\www\homestead\scripts\serve-magento.sh
#!/usr/bin/env bash
# 针对以magento的web 服务器配置的设置
# 在Homestead.yaml中的类型设为magento
# 请先写好当前网站的配置文件/var/www/jihuizhenghao/storenginx.conf.jihuizhenghao

echo "参数:$*"       #  homestead-7: 参数:store.jihuizhenghao.com /var/www/jihuizhenghao/store 80 443 7.2  false
echo "参数个数$#"    # homestead-7: 参数个数7
block="
upstream fastcgi_backend {
    # 使用 tcp 连接
    # server  127.0.0.1:9000;
    # 或套接字 socket
    # server   unix:/var/run/php5-fpm.sock;
    server   unix:/var/run/php/php$5-fpm.sock;
 }
server {
    listen ${3:-80}; 
    listen ${4:-443} ssl http2;
    server_name $1;
    set \$MAGE_ROOT $2;
    access_log off;
    error_log  /var/log/nginx/$1-error.log error;
    include $2/nginx.conf.jihuizhenghao;
}
"
echo "$block" > "/etc/nginx/sites-available/$1"
ln -fs "/etc/nginx/sites-available/$1" "/etc/nginx/sites-enabled/$1"

如果报错

FastCGI sent in stderr: "Access to the script '/var/www/jihuizhenghao/store/setup/index.php/navigation/header-bar' has been denied (see security.limit_extensions)" 

修改/etc/php/7.0/fpm/php.ini中cgi.fix_pathinfo=1

说明:综合各种php开发环境的搭建,使用Homestead来管理确实是一个不错的选择

你可能感兴趣的:(thinkphp,yii2,laravel,zend-framework,php)