Home » Code » 使用vagrant快速搭建跨平台开发环境

使用vagrant快速搭建跨平台开发环境

在windows下进行编码开发,最终程序部署到Linux服务器下,这应该还是主流,至少在国内是。vagrant就是为了搭建这样的环境而生的。今天也来实践搭建一下。

首先,安装VirtualBox,在win10下,我测试了4.3.28是无法启动的,最终装了最新的5.0.20可以启动。vagrant也是装的最新版本1.8.1。除了这两玩意儿,还得有一个box镜像,在vagrant官网这里可以找到各种Linux发行版的box镜像,我选择了排第一位的下载量最大的Ubuntu 14.04 LTS。进去却找不到下载地址,难道要登陆?登陆页面也找不到注册按钮,莫名其妙。搜索得知,在最终页面URL后面添加/providers/{pD添加/providers/{provider}.box就可以下载了。如;

box地址是:https://atlas.hashicorp.com/ubuntu/boxes/trusty64/versions/20160518.0.0

那么它的下载地址可以通过这个个地址来获得:https://atlas.hashicorp.com/ubuntu/boxes/trusty64/versions/20160518.0.0/providers/virtualbox.box

安装好virtualbox和vagrant(需要重启)后,建立一个目录,比如E:\vagrant,打开cmd命令窗口,进入到这个目录,就可以开始安装了。首先,添加box镜像,执行vagrant box add命令:

vagrant box add ubuntu trusty-server-cloudimg-amd64-vagrant-disk1.box

其中的ubuntu是自定义的名称。接着进行初始化:

vagrant init ubuntu

这时,会在目录下创建一个名为Vagrantfile的配置文件。里边配置的项也不多,关键的几个打开就可以了。

...
config.vm.box = "ubuntu" #添加的名称
...
config.vm.network "forwarded_port", guest: 80, host: 8080 #80是虚拟机监听端口,host是本机监听端口。如果本机80端口已用,记得改不一样的
...
config.vm.synced_folder "./data", "/vagrant_data" #最关键的一个配置,./data是本机目录,这么写也就是E:\vagrant\data,/vagrant_data是对应虚拟机中的目录,二者是同一个
...
  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
      vb.gui = true #是否启动virtualbox可视化界面,在遇到问题无法启动的时候可以打开看看问题是什么
  #
  #   # Customize the amount of memory on the VM:
     vb.memory = "1024" #虚拟机使用的内存
  end

按以上修改后,执行“vagrant up”命令即开始启动。很不幸,我这启动不起来,cmd界面显示卡在等待远程响应的字眼,试了好风次,又换了box镜像,依然无解。然后将vb.gui打开,virtualbox可视化界面也起来报错如下:

VT-x/AMD-V硬件加速功能在您的系统中不可用。您的64位虚拟机无法检测到64位处理器,从而无法启动

确定不是在开玩笑?2013年的笔记本会不支持虚拟化?一顿百度,都说什么在BIOS中把英特尔虚拟化技术打开,进去看了确实是打开的,默认就是打开的。正在郁闷之际,通过谷歌搜索看到一个答案感觉靠谱,那就是win10默认开启了微软自已的Hyper-v虚拟化技术会导致这个问题,也就是这个:

无标题

果断关掉重启,再来执行vateant up,一切都顺利了。

E:\vagrant>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 80 (guest) => 8080 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => E:/vagrant
    default: /vagrant_data => E:/vagrant/data

E:\vagrant>

通过xshell连接127.0.0.1:2222,ssh账户和密码都是vagrant,成功登陆。一阵apt-get install把nginx安装好,访问localhost:8080就能访问到默认页面了。配置一个虚拟域名,root指向到/vagrant_data,就完成了跟windows的共享。windows下的“E:\vagrant\data”跟虚拟机中“/vagrant_data”,其实是同一个目录,可以方便的在windows下修改,即时运行在Linux环境中看效果。而且这种方式box镜像小很多,400多M,添加也很快速,确实是一个不错的方案。

执行apt-get install php5-fpm,启动,配置域名,访问却报502错误,查看日志有如下错误:

connect() failed (111: Connection refused) while connecting to upstream

几经摸索,说是默认情况下php-fpm侦听的是/var/run/php5-fpm.sock,需要让其使用TCP链接,修改/etc/php5/fpm/pool.d/www.conf这个配置,修改如下:

#listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000

感觉就像是连接mysql的时候有时候使用localhost不行,使用127.0.0.1就可以的样子。既然如此在配置nginx虚拟主机的时候将fastcgi_pass指向/var/run/php5-fpm.sock而不是常见的127.0.0.1:9000是否也行呢?暂时不测试了,这样改后502错误没有了,却出现”File not found”。各种搜索出来的答案都是说将“fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name”改为“
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name”。拜托,我一开始就是按照PHP手册配置的就是下边的这一句,很是无奈。整半天发现原来是root定义写错位置了,给写到”location / {}”这个配置项中了,对nginx的配置不熟悉导致。以下是配置是能够正常访问PHP:

server {
        listen 80;
        root /vagrant_data;
        server_name test.vagrant.com;
        location / {
                index index.html index.php;
                try_files $uri $uri/ /index.php$is_args$args;#单入口
        }
        location ~ \.php {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }       
        access_log /var/log/vagrant/access.log;
        error_log /var/log/vagrant/error.log;
}

快照

搞这些环境,就是拿来折腾的,一坏了必须能够快速回滚,因此快照功能必不可少。在vagrant中实现快照需要安装一个插件,如下。

E:\vagrant>vagrant plugin install vagrant-vbox-snapshot
Installing the 'vagrant-vbox-snapshot' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbox-snapshot (0.0.10)'!

E:\vagrant>vagrant snapshot -h
Usage: vagrant snapshot <command> [<args>]

Available subcommands:
     back
     delete
     go
     list
     take

For help on any individual command run `vagrant snapshot <command> -h`

E:\vagrant>vagrant snapshot take 'ok_before_sphinx'
Taking snapshot ok_before_sphinx
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Snapshot taken. UUID: 1cb5ad83-eda7-4d10-948d-20e7a034b2ec

E:\vagrant>vagrant snapshot list
Listing snapshots for 'default':
   Name: ok_before_sphinx (UUID: 1cb5ad83-eda7-4d10-948d-20e7a034b2ec) *

E:\vagrant>

2017-01-19补充:

启动时出现错误:
Failed to upload a file to the guest VM via SCP due to a permissions error. This is normally because the SSH user doesn’t have permission to write to the destination location. Alternately, the user running Vagrant on the host machine may not have permission to read the file.

这是因为启动时候登陆的账户对tmp目录的权限不够所至,它在启动时候需要上传一个文件到tmp目录然后根据文件内容进行磁盘挂载等工作,权限不够就导致挂载只有目录而目录内没有内容。可以切换到root账户执行:chmod -R 777 /tmp,再进行vagrant reload等重启就可以了。变可参考这里的讨论。

参考链接:
http://lovelace.blog.51cto.com/1028430/1423343
https://segmentfault.com/a/1190000000264347
https://blog.smdcn.net/article/1308.html
http://talk.ninghao.net/t/fa-xian-shi-yong-vagrant-chu-xian-zhe-ge-wen-ti-zhuo-mian-qi-dong-xu-ni-ji-chu-xian-vt-x-slash-amd-v-ying-jian-jia-su-zai-nin-de-xi-tong-zhong-bu-ke-yong/1906/7
https://segmentfault.com/a/1190000003033407

One comment

  1. Pingback: Cıvata

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.