vagrant打造开发环境

下载安装

1
2
3
4
5
6
VirtualBox 5.1.8
下载地址 https://www.virtualbox.org/wiki/Download_Old_Builds_5_1
Vagrant 1.8.6

下载地址:https://releases.hashicorp.com/vagrant/1.8.6/
切记根据自己的操作系统下载,同时分32位和64位

vagrant常用命令

  • 查看vagrant list
    uiste:box uiste$ vagrant box list

  • 添加vagrant box
    uiste:box uiste$ vagrant box add ubuntu1404 ubuntu1404.box

  • 添加虚拟机

    1
    2
    3
    uiste:local_lamp_dev uiste$ pwd
    /Users/uiste/vagrant/local_lamp_dev
    uiste:local_lamp_dev uiste$ vagrant init ubuntu1404
  • 启动虚拟机
    uiste:local_lamp_dev uiste$ vagrant up

  • 取得链接

    1
    2
    uiste:local_lamp_dev uiste$ vagrant ssh
    vagrant@vagrant-ubuntu-trusty-64:~$
  • 退出、挂起虚拟机

    1
    2
    3
    4
    vagrant@vagrant-ubuntu-trusty-64:~$ exit
    logout
    Connection to 127.0.0.1 closed.
    uiste:local_lamp_dev uiste$ vagrant suspend
  • 重启虚拟机

    1
    uiste:local_lamp_dev uiste$ vagrant reload

对虚拟机进行优化

  • 替换源
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo vim /etc/apt/sources.list #修改源

    deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get update #更新列表

L(A|N)MP软件安装

  • 安装nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install nginx -y
    // 安装成功测试
    vagrant@vagrant-ubuntu-trusty-64:~$ nginx -v
    nginx version: nginx/1.4.6 (Ubuntu)
    vagrant@vagrant-ubuntu-trusty-64:~$ curl -I 'http://127.0.0.1'
    HTTP/1.1 200 OK
    Server: nginx/1.4.6 (Ubuntu)
    Date: Tue, 04 Apr 2017 17:17:02 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
    Connection: keep-alive
    ETag: "5315bd25-264"
    Accept-Ranges: bytes
  • 安装Apache

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install apache2 -y
    // 安装成功测试
    vagrant@vagrant-ubuntu-trusty-64:~$ apache2 -v
    Server version: Apache/2.4.7 (Ubuntu)
    // nginx 服务停止
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo /etc/init.d/nginx stop
    // 启动 apache2 服务
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo /etc/init.d/apache2 start
    * Starting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppress this message
    *
    vagrant@vagrant-ubuntu-trusty-64:~$ curl -I 'http://127.0.0.1'
    HTTP/1.1 200 OK
    Date: Tue, 04 Apr 2017 17:24:09 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Tue, 04 Apr 2017 17:18:42 GMT
    ETag: "2cf6-54c5a77b221fa"
    Accept-Ranges: bytes
    Content-Length: 11510
    Vary: Accept-Encoding
    Content-Type: text/html
    #端口修改-Apache
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo vim /etc/apache2/ports.conf
    Listen 8888
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo /etc/init.d/apache2 restart
    #端口修改-Nginx
    #修改成9000端口 ,默认sock模式
    cd /etc/php5/fpm/pool.d
    sudo vim www.conf # search listen = 127.0.0.1:9000
    sudo /etc/init.d/php5-fpm restart
  • 安装MySQL软件

    1
    2
    3
    4
    5
    6
    7
    8
    #服务器端
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install mysql-server
    安装期间会提示输入为mysql设置root密码,我这边不操作,直接enter 不设置密码
    #客户端
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install mysql-client
    #测试连接库,上面安装服务端没有设置密码,这里直接enter进入
    vagrant@vagrant-ubuntu-trusty-64:~$ mysql -uroot -p
    Enter password:
  • 安装PHP

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #安装php
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install php5-cli -y
    #安装成功测试
    vagrant@vagrant-ubuntu-trusty-64:~$ php -v
    PHP 5.5.9-1ubuntu4.21 (cli) (built: Feb 9 2017 20:54:58)
    #安装php扩展
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install php5-mcrypt php5-mysql php5-gd -y
    #支持apache2的php模块
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install libapache2-mod-php5
    #支持nginx fastcgi 启动php
    vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install php5-cgi php5-fpm
    #开发Apache文件.htaccess重写功能
    vagrant@vagrant-ubuntu-trusty-64:/$ sudo a2enmod rewrite
  • 安装git

    1
    vagrant@vagrant-ubuntu-trusty-64:/home/www/code$ sudo apt-get install git -y

Vagrant高级配置

  • 端口转发

    1
    2
    3
    4
    5
    6
    7
    8
    配置

    config.vm.network "forwarded_port", guest: 80, host: 8888
    config.vm.network "forwarded_port", guest: 8888, host: 8889
    测试效果

    http://127.0.0.1:8888/
    http://127.0.0.1:8889/
  • 网络设置

    1
    2
    3
    4
    5
    6
    7
    配置

    config.vm.network "private_network", ip: "192.168.199.101"
    效果

    http://192.168.199.101/ 或者 http://192.168.199.101:80/
    http://192.168.199.101:8888/

*共享目录

1
2
3
配置

config.vm.synced_folder "/Users/uiste/www/code/", "/home/www"

框架配置

tp5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
git clone https://github.com/top-think/think tp5
然后切换到tp5目录下面,再克隆核心框架仓库:cd tp5 && git clone https://github.com/top-think/framework thinkphp
nginx 配置
server {
server_name tp5.code.com;
root /home/www/code/tp5/public;
index index.php index.html;
location / {
if ( -f $request_filename) {
break;
}

if ( !-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}

location ~ \.php {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
include fastcgi_params;
fastcgi_index index.php?IF_REWRITE=1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $script;
try_files $uri =404;
}

}
针对fasecgi要配置下php-fpm
vim /etc/php5/fpm/pool.d/www.conf
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
无法查看日志 sudo chmod -R 755 /var/log/nginx
apache 配置
<VirtualHost *:8888>
ServerName tp5.imooc.test
DocumentRoot /home/www/code/tp5/public/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
无法查看日志sudo chmod -R 755 /var/log/apache2
Forbidden解决办法
cd /etc/apache2
sudo vim apache2.conf <Directory /> Options FollowSymLinks AllowOverride None #修改成AllowOverride ALL 打开rewrite #Require all denied #把这行屏蔽掉,拒绝一切链接 </Directory>
测试效果
http://tp5.code.com 或者 http://tp5.code.com:8888
做个测试页面 http://tp5.code.com/index/index/test 或者 http://tp5.code.com:8000/index/index/test

yii2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
下载:github.com/yiisoft/yii2/releases/download/2.0.10/yii-basic-app-2.0.10.tgz
nginx 配置
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name yii.code.com;

root /home/www/code/yii2/web;
index index.php;

location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}

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

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
try_files $uri =404;
}

}
apache 配置
<VirtualHost *:8888>
ServerName yii.code.com
DocumentRoot /home/www/code/yii2/web
#<Directory "/home/www/code/yii2/web">
#RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . index.php
#</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
测试效果 * http://yii.code.com 或者 http://yii.code.com:8888 * 做个测试页面 http://yii.code.com/site/test 或者 http://yii.code.com:8000/site/test
问题
runtime/logs": mkdir(): Permission denied
权限不够 chmod -R 777 runtime (如果在虚拟机不能修改文件权限,请使用nfs共享目录)
yii\web\Request::cookieValidationKey must be configured with a secret key.
config/web.php cookieValidationKey 赋值
如果打不开 site/test ,请修改config/web.php 中urlManager 注释打开
'db' => require(__DIR__ . '/db.php'),
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
yii2/web/.htaccess
RewriteEngine on
#If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Otherwise forward it to index.php
RewriteRule . index.php

laravel5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
下载: down.golaravel.com/laravel/laravel-v5.1.11.zip
nginx 配置
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name laravel.code.com;

root /home/www/code/laravel/public;
index index.php;

location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}

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

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
try_files $uri =404;
}

}
apache 配置
<VirtualHost *:8888>
ServerName laravel.code.com
DocumentRoot /home/www/code/laravel/public/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
测试效果
http://laravel.code.com 或者 http://laravel.code.com:8888

vagrant虚拟机配置

虚拟机名称
vb.name = "ubuntu_code"
虚拟机主机名
config.vm.hostname = "code"
配置虚拟机内存和CPU
vb.memory = "1024"
vb.cpus = 2

打包命令

vagrant package --output uiste.box

  • 打包升级
1
2
3
4
5
6
7
8
9
config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
apt-get insatll -y redis-server
SHELL

#执行安装命令
vagrant reload --provision
uiste:box uiste$ vagrant box add new_uiste uiste.box