本地搭建Linux环境

说明:本工具可以支持Windows/Mac/Linux等环境,但是目前本人仅在Mac上测试。

使用工具

工具 说明 下载
Vagrant 用于配置,管理虚拟机的工具,提供简单易使用的命令行接口。使用模版来描述虚拟机环境,所以可以复用网上的模版。 http://www.vagrantup.com/downloads.html
Virtualbox 跨平台的虚拟机环境,用于运行Vagrant模版。 https://www.virtualbox.org

下载虚拟机

安装完Virtualbox和Vagrant之后,可以直接下载虚拟机的模版。因为我们生产环境使用的linux版本是CentOS6.5,我们在开发环境最好也使用同样的环境. CentOS 6.5的下载地址: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box 更多的Vagrant上的模版可以从以下站点获取: http://www.vagrantbox.es/

使用说明

先将虚拟机加到box list,然后使用该box初始化一个vagrant虚拟机环境。最后使用vagrant up就可以启动虚拟机了。
$ vagrant box add {title} {url}
$ vagrant init {title}
$ vagrant up

如果先将box下载到硬盘,{url} 可以填写为本地路径,例如:
$ vagrant box add centos65 ../centos65-x86_64-20140116.box
$ vagrant init centos65
$ vagrant up

启动虚拟机后,使用vagrant ssh就可以登录到虚拟机上。虚拟机的文件系统是共享本地路径的,所以文件直接copy到制定目录就可以使用了。使用vagrant halt可以关闭该虚拟机。
$ vagrant ssh
[vagrant@vagrant-centos65 ~]$ exit
$ vagrant halt

也可以查看vagrant的命令帮助:
Jings-MacBook-Pro:new-box jingy$ vagrant ?
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     connect         connect to a remotely shared Vagrant environment
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to Vagrant Cloud
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     provision       provisions the vagrant machine
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     share           share your Vagrant environment with anyone in the world
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

分类: web

标签: