美文网首页
Vagrant 使用

Vagrant 使用

作者: LiuliuZhang | 来源:发表于2018-04-24 18:16 被阅读0次

    1 Vagrant下载

    1)通过官网 https://app.vagrantup.com/boxes/search 查找相应的box,通过vagrant init ubuntu/trusty64 vagrant up 初始化下载
    2)通过下载box文件来加载https://pan.baidu.com/s/1kU8fUlD

    2 Vagrant命令

    加载下载的box,将其命名为ubuntu1404 vagrant box add ubuntu1404 ubuntu-14.04-amd64.box
    $ vagrant init # 初始化
    $ vagrant up # 启动虚拟机
    $ vagrant halt # 关闭虚拟机
    $ vagrant reload # 重启虚拟机
    $ vagrant ssh # SSH 至虚拟机
    $ vagrant suspend # 挂起虚拟机
    $ vagrant resume # 唤醒虚拟机
    $ vagrant status # 查看虚拟机运行状态
    $ vagrant destroy # 销毁当前虚拟机
    box管理命令
    $ vagrant box list # 查看本地box列表
    $ vagrant box add # 添加box到列表
    $ vagrant box remove # 从box列表移除
    $ vagrant package --output xxx.box #虚拟机打包

    3 虚拟机优化

    1)替换源

    修改源 sudo vi /etc/apt/sources.list
    :%d删除文件所有内容,将文件内容替换成如下

    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
    

    shift+zz保存并退出
    sudo apt-get update 更新列表

    2)配置vagrantfile

    配置端口转发/共享目录/主机名等内容

      config.vm.network "forwarded_port", guest: 8080, host: 8088
      config.vm.network "forwarded_port", guest: 80, host: 8082
      config.vm.synced_folder "./downloads", "/home/vagrant/downloads"
      config.vm.hostname = "ubuntu"
    
      config.vm.provider "virtualbox" do |vb|
        vb.memory = "1024"
        vb.name = "ubuntu_14"
        vb.cpus = 2
      end
    

    若出现unknown filesystem type 'vboxsf'错误,进入ssh后,在虚拟机执行sudo apt-get install virtualbox-guest-utils安装

    3)安装nodeJS

    安装应用 sudo apt-get install git vim openssl build-essential libssh-dev wget curl
    安装NVMcurl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
    安装node.js nvm install v8.11.1 / nvm use v8.11.1
    测试: 新建js文件server.js

    const http = require('http');
    
    const hostname = '0.0.0.0';
    const port = 8080;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });
    
    server.listen(port, hostname, () => {
      console.log(`NodeJS run on http://${hostname}:${port}/`);
    });
    

    在host主机浏览器输入http://127.0.0.1:8088 访问


    相关文章

      网友评论

          本文标题:Vagrant 使用

          本文链接:https://www.haomeiwen.com/subject/bjpjlftx.html