--- tags: Linux, Vagrant --- # 使用vagrant打造个人开发环境 Vagrant是[hashicorp](https://www.hashicorp.com/)公司的产品,这家公司很牛,主要做数据中心PAAS和虚拟化,有很多大名鼎鼎的产品,Consul/Vault/Nomad/Terraform。 我们使用Vagrant来管理各种虚拟机环境,它可以兼容市面上几乎所有的主流虚拟机平台,如VirtualBox、VMware、AWS等,好处是你再也不用在虚拟机GUI上进行各种繁琐操作,通过简单的配置文件就可以打造可移植和可复用的虚拟机软件环境。Vagrant不能单独使用,需要借助虚拟软件,例如我现在使用的VirtualBox。 ## 前提条件 :::success 提前安装好这两个软件[VirtualBox](https://www.virtualbox.org/wiki/Downloads)和[Vagrant](https://www.vagrantup.com/downloads),完成后重启电脑。 ::: 重启后,我们先修改一下VirtualBox的默认虚拟机位置,防止把虚拟机安装到系统盘,导致系统盘空间紧张。 ![](https://i.imgur.com/qm29dL4.png) - 不要设置成系统盘 - 最好使用固态硬盘分区,访问速度快些 ## Vagrant 安装一些辅助插件 ```bash= vagrant plugin install vagrant-vbguest vagrant vbguest --do install --no-cleanup ``` ### Vagrantfile Vagrantfile是用来定义vagrant project的,使用ruby语法,不过你不必了解ruby就可以写一个Vagrantfile。 直接看这个例子,也是我正在使用的配置: ```ruby= # -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = "centos/7" config.vbguest.installer_options = { allow_kernel_upgrade: true } # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # 获取和宿主机统一网段的ip地址,相当于一台真实的和宿主机一样的内网服务器了 config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.synced_folder "../code/", "/data", create: true # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # 分配虚拟机系统资源 内存和CPU config.vm.provider "virtualbox" do |vb| vb.memory = "4096" vb.cpus = 2 end # Enable provisioning with a shell script. Additional provisioners such as # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the # documentation for more information about their specific syntax and use. config.vm.provision "shell", inline: <<-SHELL yum install -y epel-release wget curl sysstat iostat telnet tree net-tools iftop lrzsz unzip lsof procps git yum update -y SHELL end ``` :::info **重要配置** `config.vm.synced_folder "../code/", "/data"` 宿主机和虚拟机目录同步配置,这个的好处是在宿主机编辑代码,直接可以在虚拟机里面运行。 - *第一个参数* 是相对vagrant安装目录的地址,我这里的vagrant安装在`D:\HashiCorp`,而我的代码全部在`D:\code`,因此我就配置的`../code/` - *第二个参数* 是虚拟机挂载宿主机目录,一般配置一个你喜欢的绝对地址就好,我使用的是`/data`目录 ::: 一些常用控制服务的命令 :::success 命令的操作前提是当前目录要在Vagrantfile文件所在的目录。 ::: ```bash # 初始化 vagrant init # 启动虚拟机 vagrant up # 登录虚拟机 vagrant ssh # 重启虚拟机 vagrant reload # 初始化虚拟机的环境,前提是有配置 vagrant provision ``` ## 收尾操作 - 使用`vagrant ssh`登录虚拟机后,建议修改虚拟机的登录为账号密码登录。 ```bash # 切换到root用户 sudo su # 给vagrant设置密码 passwd vagrant # 修改sshd配置,开启密码登录 sed 's/^#PasswordAuthentication yes$/PasswordAuthentication yes/g' /etc/ssh/sshd_config sed 's/^PasswordAuthentication no$/#PasswordAuthentication no/g' /etc/ssh/sshd_config # 重启ssh服务 service sshd restart # 查看ip地址 ip addr ``` 此后就可以使用ssh终端登录虚拟机了。 - 如果访问github有问题,可以尝试修改DNS服务器为阿里的 ```bash sudo su sed 's/^nameserver 10.0.2.3$/nameserver 223.5.5.5/g' /etc/resolv.conf ``` - 安装和配置nodejs环境 ```bash # 安装nvm管理node版本 wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm # 使用nvm安装node nvm install --lts # 安装全局库 npm install pm2 yarn @nestjs/cli -g # 配置pm2日志 pm2 install pm2-logrotate pm2 set pm2-logrotate:max_size 100M pm2 set pm2-logrotate:rotateInterval '59 59 23 * * *' pm2 set pm2-logrotate:retain 30 ``` - 安装redis ```bash yum install redis -y systemctl start redis ``` [如何使用ReJson库?](https://github.com/RedisJSON/RedisJSON) - 安装rabbitmq [官方教程](https://www.rabbitmq.com/install-rpm.html) - 安装mongodb [官方教程](https://docs.mongodb.com/v4.0/tutorial/install-mongodb-on-red-hat/) - 编译安装nginx 参考[centos初始化脚本](http://192.168.1.80:8888/linli/ops/blob/master/script/os/init_centos_7.sh) ## 参考 - [Vagrant](https://vagrant.ninghao.net/)