Ubuntu 上安装并设置 Vagrant

虚拟机,并不复杂

多年来,开发人员一直使用虚拟机作为其工作流程的一部分,允许他们交换和更改运行软件的环境,这通常是为了防止项目之间的冲突,例如需要 php 5.3 的项目 A 和需要 php 5.4 的项目 B。

并且使用虚拟机意味着你只需要你正在使用的计算机就行,而不需要专用硬件来镜像你的生产环境。

当多个开发人员在一个项目上工作时,它也很方便,他们都可以运行一个包含所有需求的环境,但是维护多台机器并确保所有的需求都具有相同的版本是非常困难的,这时 Vagrant 就能派上用场了。

使用虚拟机的好处

  • 你的虚拟机与主机环境是分开的
  • 你可以根据你代码的要求裁剪一个定制虚拟机
  • 不会影响其他虚拟机
  • 可以运行在你的主机上无法运行的程序,例如在 Ubuntu 中运行一些只能在 Windows 运行的软件

什么是 Vagrant

简而言之,这是一个与虚拟机一起工作的工具,可以让你自动创建和删除虚拟机。

它围绕一个名为 VagrantFile 的配置文件而工作,这个配置文件告诉 Vagrant 你想要安装的操作系统,以及一些其他选项,如 IP 和目录同步。 你还可以在虚拟机上添加一个命令的配置脚本。

通过共享这个 VagrantFile,项目的所有开发人员全可以使用完全相同的虚拟机。

安装要求

安装 VirtualBox

VirtualBox 是运行虚拟机的程序,它可以从 Ubuntu 仓库中安装。

  1. sudoapt-get install virtualbox

安装 Vagrant

对于 Vagrant 本身,你要前往 https://www.vagrantup.com/downloads.html 查看适用于你的操作系统的安装软件包。

安装增强功能

如果你打算与虚拟机共享任何文件夹,则需要安装以下插件。

  1. vagrant plugin install vagrant-vbguest

配置 Vagrant

首先我们需要为 Vagrant 创建一个文件夹。

  1. mkdir~/Vagrant/test-vm
  2. cd~/Vagrant/test-vm

创建 VagrantFile:

  1. vagrant init

开启虚拟机:

  1. vagrant up

登录机器:

  1. vagrant-ssh

此时,你将拥有一个基本的 vagrant 机器,以及一个名为 VagrantFile 的文件。

定制

在上面的步骤中创建的 VagrantFile 看起来类似于以下内容

VagrantFile:

  1. #-*- mode: ruby -*-
  2. #vi:set ft=ruby :
  3. #AllVagrant configuration isdone below.The"2"inVagrant.configure
  4. # configures the configuration version (we support older styles for
  5. # backwards compatibility).Please don't change it unless you know what
  6. # you're doing.
  7. Vagrant.configure("2")do|config|
  8. #The most common configuration options are documented and commented below.
  9. #For a complete reference, please see the online documentation at
  10. # https://docs.vagrantup.com.
  11. #EveryVagrant development environment requires a box.You can search for
  12. # boxes at https://vagrantcloud.com/search.
  13. config.vm.box ="base"
  14. #Disable automatic box update checking.If you disable this,then
  15. # boxes will only be checked for updates when the user runs
  16. #`vagrant box outdated`.Thisisnot recommended.
  17. # config.vm.box_check_update =false
  18. #Create a forwarded port mapping which allows access to a specific port
  19. # within the machine from a port on the host machine.In the example below,
  20. # accessing "localhost:8080" will access port 80 on the guest machine.
  21. # NOTE:This will enable public access to the opened port
  22. # config.vm.network "forwarded_port", guest:80, host:8080
  23. #Create a forwarded port mapping which allows access to a specific port
  24. # within the machine from a port on the host machine and only allow access
  25. # via 127.0.0.1 to disable public access
  26. # config.vm.network "forwarded_port", guest:80, host:8080, host_ip:"127.0.0.1"
  27. #Create a private network, which allows host-only access to the machine
  28. #using a specific IP.
  29. # config.vm.network "private_network",ip:"192.168.33.10"
  30. #Create a public network, which generally matched to bridged network.
  31. #Bridged networks make the machine appear as another physical device on
  32. # your network.
  33. # config.vm.network "public_network"
  34. #Share an additional folder to the guest VM.The first argument is
  35. # the path on the host to the actual folder.The second argument is
  36. # the path on the guest to mount the folder.And the optional third
  37. # argument is a set of non-required options.
  38. # config.vm.synced_folder "../data","/vagrant_data"
  39. #Provider-specific configuration so you can fine-tune various
  40. # backing providers forVagrant.These expose provider-specific options.
  41. #ExampleforVirtualBox:
  42. #
  43. # config.vm.provider "virtualbox"do|vb|
  44. ##Display the VirtualBox GUI when booting the machine
  45. # vb.gui =true
  46. #
  47. ##Customize the amount of memory on the VM:
  48. # vb.memory ="1024"
  49. #end
  50. #
  51. #View the documentation for the provider you are usingformore
  52. # information on available options.
  53. #Enable provisioning with a shell script.Additional provisioners such as
  54. #Puppet,Chef,Ansible,Salt,andDocker are also available.Please see the
  55. # documentation formore information about their specific syntax anduse.
  56. # config.vm.provision "shell",inline:<<-SHELL
  57. #apt-get update
  58. #apt-get install -y apache2
  59. # SHELL
  60. end

现在这个 VagrantFile 将创建基本的虚拟机。但 Vagrant 背后的理念是让虚拟机为我们的特定任务而配置,所以我们删除注释和调整配置。

VagrantFile:

  1. #-*- mode: ruby -*-
  2. #vi:set ft=ruby :
  3. Vagrant.configure("2")do|config|
  4. #Set the LinuxVersion to DebianJessie
  5. config.vm.box ="debian/jessie64"
  6. #Set the IP of the Box
  7. config.vm.network "private_network",ip:"192.168.33.10"
  8. #SyncOurProjectsDirectorywith the WWW directory
  9. config.vm.synced_folder "~/Projects","/var/www/"
  10. #Run the following to Provision
  11. config.vm.provision "shell", path:"install.sh"
  12. end

现在我们有一个简单的 VagrantFile,它将 Linux 版本设置为 debian jessie,设置一个 IP 给我们使用,同步我们感兴趣的文件夹,并最后运行 install.sh,这是我们可以运行 shell 命令的地方。

install.sh:

  1. #!/usr/bin/envbash
  2. #Variables
  3. DBHOST=localhost
  4. DBNAME=dbname
  5. DBUSER=dbuser
  6. DBPASSWD=test123
  7. echo"[ Provisioning machine ]"
  8. echo"1) Update APT..."
  9. apt-get-qq update
  10. echo"1) Install Utilities..."
  11. apt-get install -y tidy pdftk curl xpdf imagemagick openssl vimgit
  12. echo"2) Installing Apache..."
  13. apt-get install -y apache2
  14. echo"3) Installing PHP and packages..."
  15. apt-get install -y php5 libapache2-mod-php5 libssh2-php php-pear php5-cli php5-common php5-curl php5-dev php5-gd php5-imagick php5-imap php5-intl php5-mcrypt php5-memcached php5-mysql php5-pspell php5-xdebug php5-xmlrpc
  16. #php5-suhosin-extension, php5-mysqlnd
  17. echo"4) Installing MySQL..."
  18. debconf-set-selections <<<"mysql-server mysql-server/root_password password secret"
  19. debconf-set-selections <<<"mysql-server mysql-server/root_password_again password secret"
  20. apt-get install -y mysql-server
  21. mysql -uroot -p$DBPASSWD -e "CREATE DATABASE $DBNAME"
  22. mysql -uroot -p$DBPASSWD -e "grant all privileges on $DBNAME.* to '$DBUSER'@'localhost' identified by '$DBPASSWD'"
  23. echo"5) Generating self signed certificate..."
  24. mkdir-p /etc/ssl/localcerts
  25. openssl req -new-x509 -days 365-nodes -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com"-out /etc/ssl/localcerts/apache.pem -keyout /etc/ssl/localcerts/apache.key
  26. chmod600/etc/ssl/localcerts/apache*
  27. echo"6) Setup Apache..."
  28. a2enmod rewrite
  29. >/etc/apache2/sites-enabled/000-default.conf
  30. echo"
  31. <VirtualHost *:80>
  32. ServerAdmin webmaster@localhost
  33. DocumentRoot /var/www/
  34. ErrorLog ${APACHE_LOG_DIR}/error.log
  35. CustomLog ${APACHE_LOG_DIR}/access.log combined
  36. </VirtualHost>
  37. ">>/etc/apache2/sites-enabled/000-default.conf
  38. service apache2 restart
  39. echo"7) Composer Install..."
  40. curl --silent https://getcomposer.org/installer | php
  41. mv composer.phar /usr/local/bin/composer
  42. echo"8) Install NodeJS..."
  43. curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
  44. apt-get-qq update
  45. apt-get-y install nodejs
  46. echo"9) Install NPM Packages..."
  47. npm install -g gulp gulp-cli
  48. echo"Provisioning Completed"

通过上面的步骤,在你的目录中会有 VagrantFileinstall.sh,运行 vagrant 会做下面的事情:

  • 采用 Debian Jessie 来创建虚拟机
  • 将机器的 IP 设置为 192.168.33.10
  • 同步 ~/Projects/var/www/ 目录
  • 安装并设置 Apache、Mysql、PHP、Git、Vim
  • 安装并运行 Composer
  • 安装 Nodejs 和 gulp
  • 创建一个 MySQL 数据库
  • 创建自签名证书

通过与其他人共享 VagrantFileinstall.sh,你可以在两台不同的机器上使用完全相同的环境。

猜你喜欢

转载自www.linuxidc.com/Linux/2018-02/150729.htm