最近正好用着Vagrant搭建开发环境,写一篇文章记录一下。
Vagrant目前是国内互联网公司应用最多的内部开发环境工具。
Mac、 Windows搭建是一样的,我是在Mac下搭建的环境。
vagrant 搭建开发环境
基础环境
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
启动虚拟机
box下载
地址1:https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
地址2: https://github.com/CommanderK5/packer-centos-template/releases/
下载之后请确保文件名为centos6.7.box
- 添加box
- vagrant box add centos67 centos6.7.box
- 初始化box
- vagrant init centos67
- vagrant ssh
对虚拟机优化
-
替换源
- sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份
- sudo vim /etc/apt/sources.list #修改源
- 将文件内容替换成以下内容
-
1 deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse 2 deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse 3 deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse 4 deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse 5 deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse 6 deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse 7 deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse 8 deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse 9 deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse 10 deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
- sudo apt-get update #更新列表
L(A|N)MP
Ngnix
sudo apt-get install nginx
nginx -v
nginx version: nginx/1.4.6
Apache
sudo apt-get install apache2
apache2 -v
Server version: Apache/2.4.7
更改端口,将端口设置为8888
- 修改 ports.conf 文件
- curl -I 'http://127.0.0.1:8888'
-
Mysql
- sudo apt-get install mysql-server #服务器端
- 安装期间会提示输入为mysql设置root密码,我这边不操作,直接enter 不设置密码
- sudo apt-get install mysql-client #客户端
- mysql -uroot -p #测试连接库,上面安装服务端没有设置密码,这里直接enter进入
- sudo apt-get install mysql-server #服务器端
-
php
-
安装php
- sudo apt-get install php5-cli
- php -v
- sudo apt-get install php5-cli
-
PHP扩展
- php5-mcrypt
- php5-mysql
- php5-gd
-
支持apache2的php模块
- sudo apt-get install libapache2-mod-php5
- 开启rewrite功能
- sudo a2enmod rewrite
-
支持nginx fastcgi
- sudo apt-get install php5-cgi php5-fpm
- 修改成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
-
Vagrant高级配置
-
端口转发
- 配置
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/
-
网络设置
-
配置
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/
-
-
共享目录
-
配置
config.vm.synced_folder "/Users/code/", "/home/www"
-
框架配置
-
tp5
- git clone https://github.com/top-think/think tp5
- 然后切换到tp5目录下面,再克隆核心框架仓库:cd tp5 && git clone https://github.com/top-think/frameworkthinkphp
- nginx 配置
- 无法查看日志 sudo chmod -R 755 /var/log/nginx
- apache 配置
- 无法查看日志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.test 或者 http://tp5.test:8888
- 做个测试页面 http://tp5.test/index/index/test 或者 http://tp5.test:8000/index/index/test
-
yii2
- 下载:github.com/yiisoft/yii2/releases/download/2.0.10/yii-basic-app-2.0.10.tgz
- nginx 配置
- apache 配置
- 测试效果 * http://yii.test 或者 http://yii.test:8888 * 做个测试页面 http://yii.test/site/test 或者 http://yii.test:8000/site/test
- 问题
- runtime/logs": mkdir(): Permission denied
- 权限不够 chmod -R 777 runtime (如果在虚拟机不能修改文件权限,请使用nfs共享目录)
- yiiwebRequest::cookieValidationKey must be configured with a secret key.
- config/web.php cookieValidationKey 赋值
- 如果打不开 site/test ,请修改config/web.php 中urlManager 注释打开
- runtime/logs": mkdir(): Permission denied
-
laravel5
- 下载: down.golaravel.com/laravel/laravel-v5.1.11.zip
- nginx 配置
- apache 配置
- 测试效果
- http://laravel.test 或者 http://laravel.test:8888
优化
- 虚拟机名称
- vb.name = "vagrant_centos"
- 虚拟机主机名
- config.vm.hostname = "centos"
- 配置虚拟机内存和CPU
- vb.memory = "1024"
- vb.cpus = 2
打包命令
- vagrant package --output xxx.box
vagrant 常用命令
vagrant init 初始化配置
vagrant up 启动虚拟机
vagrant ssh ssh登录虚拟机
vagrant suspend 挂起虚拟机
vagrant reload 重启虚拟机
vagrant halt 关闭虚拟机
vagrant status 查看虚拟机状态
vagrant destroy 删除虚拟机