Vagrantfile说明:使用Shell进行设置和配置

In the introduction, we showed you how to create a Vagrant base box, installing the latest Ubuntu 14.04 LTS in the virtual machine to use it as the guest operating system.

简介中 ,我们向您展示了如何创建Vagrant基本框,在虚拟机中安装最新的Ubuntu 14.04 LTS并将其用作客户机操作系统。

In this part you will learn how to setup a development environment using Vagrant, which you can use and reuse in your development. Note that while you can use the box we created in the previous part for the remainder of this post, you don’t have to – this will all work on any Ubuntu based Vagrant box.

在这一部分中,您将学习如何使用Vagrant设置开发环境,您可以在开发中使用和重用该环境。 请注意,尽管您可以使用在上一部分中创建的框来完成本文的其余部分,但您不必这样做-所有这些都可以在任何基于Ubuntu的Vagrant框上使用。

Vagrantfile (The Vagrantfile)

The primary configuration location for any Vagrant development environment is a file called Vagrantfile which you need to place in your project’s folder.

任何Vagrant开发环境的主要配置位置是一个名为Vagrantfile的文件,您需要将其放置在项目的文件夹中。

The configuration syntax of this Vagrantfile is Ruby, but you do not need to be a Ruby programmer or have any knowledge of the programming language to write this configuration file. You’ll mostly do basic variable assignment in the configuration.

这个Vagrantfile的配置语法是Ruby,但是您不必是Ruby程序员,也不需要具备任何编程语言知识即可编写此配置文件。 您将主要在配置中进行基本变量分配。

Every configuration option you will need you can place inside this file.

您将需要的每个配置选项都可以放在此文件中。

Let’s go ahead and create a test folder called vagrant-tutorial and inside this folder create the file named Vagrantfile so your folder structure looks like this:

让我们继续创建一个名为vagrant-tutorial的测试文件夹,并在此文件夹中创建一个名为Vagrantfile的文件,以便您的文件夹结构如下所示:

Vagrantfile

关于预配 (About provisioning)

The primary purpose of Vagrant is to have a base virtual machine and to give you the framework for creating automatic software installations and configurations in the virtual machine.

Vagrant的主要目的是拥有基本的虚拟机,并为您提供在虚拟机中创建自动软件安装和配置的框架。

By letting Vagrant handle the provisioning of software, it also gives you the flexibility in configuration and more importantly, makes this process repeatable and automatic.

通过让Vagrant处理软件的配置,它还为您提供了配置的灵活性,更重要的是,使此过程可重复且自动进行。

Vagrant doesn’t care how you provision the virtual machine, it offers multiple options ranging from basic shell scripts to software automation managers such as Puppet, Chef or Ansible. You can even configure it to use multiple provisioners at the same time.

Vagrant不在乎如何配置虚拟机,它提供了多种选择,从基本的Shell脚本到软件自动化管理器(如Puppet,Chef或Ansible)。 您甚至可以将其配置为同时使用多个配置程序。

Of course there’s always the possibility to vagrant ssh into the base virtual machine and install your required software manually, but that defeats the purpose of Vagrant and all the flexibility it offers when provisioning a box.

当然,总有可能将ssh游荡到基本虚拟机中并手动安装所需的软件,但是这违背了Vagrant的目的以及它在配置盒子时提供的所有灵活性。

设置先决条件 (Provisioning prerequisites)

Before we can start provisioning the base box, we need to set a few required options in our configuration file.

开始配置基本框之前,我们需要在配置文件中设置一些必需的选项。

流浪者API版本 (Vagrant API version)

Vagrant uses API versions for its configuration file, this is how it can stay backwards compatible. So in every Vagrantfile we need to specify which version to use. The current one is version 2 which works with Vagrant 1.1 and up. Let’s write this block in our Vagrantfile.

Vagrant的配置文件使用API​​版本,因此可以保持向后兼容。 因此,在每个Vagrantfile中,我们需要指定要使用的版本。 当前版本是适用于Vagrant 1.1及更高版本的版本2。 让我们在Vagrantfile中编写此块。

Vagrant.configure("2") do |config|
end
指定基本框 (Specifying the base box)

Next, we need to specify the base Vagrant box we created in the introductory post. That base box is hosted on Vagrant Cloud, so we only need to specify the name and Vagrant will automatically get it from there.

接下来,我们需要指定在介绍性帖子中创建的基本Vagrant框。 该基本框托管在Vagrant Cloud上,因此我们只需要指定名称,Vagrant就会自动从那里获取它。

Let’s write this option in the configuration file, inside the block:

让我们将此选项写入配置文件中的块内:

config.vm.box = "primalskill/ubuntu-trusty64"
网络配置 (Network configurations)

After all this is set up, we need to specify the network configurations. Vagrant has multiple ways of allowing us to communicate with the virtual machine from the outside world, such as public or private network and port forwarding.

设置完所有这些之后,我们需要指定网络配置。 Vagrant有多种方法可以使我们与外界的虚拟机进行通信,例如公共或专用网络以及端口转发。

We’re going to set up port forwarding for now. Insert the following line into your configuration file:

我们现在将设置端口转发。 将以下行插入配置文件:

config.vm.network :forwarded_port, guest: 80, host: 8931, auto_correct: true

What the above line defines is that if you have a web server inside the virtual machine listening on port 80, then it can be accessed from the host machine on port 8931, for example by typing localhost:8931 in your browser.

上面的代码行定义的是,如果虚拟机内部有一个Web服务器正在侦听端口80,则可以从主机上的端口8931对其进行访问,例如,通过在浏览器中键入localhost:8931

The auto_correct option set to true tells Vagrant to handle port collisions automatically. There are cases when you have multiple vagrant boxes running with the same port open on the host machine, in these cases Vagrant will automatically resolve the conflicting ports.

auto_correct选项设置为true可以告诉Vagrant自动处理端口冲突。 在某些情况下,主机上打开的多个vagrant框运行时会打开同一端口,在这些情况下,Vagrant会自动解决冲突的端口。

If port collisions occur, Vagrant will output the corrections during the vagrant up boot up process.

如果发生端口冲突,Vagrant将在vagrant up引导过程中输出更正。

同步项目文件 (Syncing project files)

A good practice to follow when you’re using a Vagrant development environment, or as a matter of fact any virtualized development environment, is to share your project files between the host and the guest operating systems, so that your project files are not copied into the virtual machine, because if you delete your VM, the files will be lost with it.

在使用Vagrant开发环境或实际上是任何虚拟化开发环境时,遵循的一个好习惯是在主机和来宾操作系统之间共享您的项目文件,这样就不会将您的项目文件复制到虚拟机,因为如果删除虚拟机,文件将随之丢失。

Sharing folders between the host and the guest operating systems is very easy to do with Vagrant. Just enter the following config into the Vagrantfile:

使用Vagrant非常容易在主机和来宾操作系统之间共享文件夹。 只需在Vagrantfile中输入以下配置:

config.vm.synced_folder "./", "/var/www", create: true, group: "www-data", owner: "www-data"

Let’s go over the arguments. The first argument is the host machine folder to be shared with the VM, in our case to share the current folder where the Vagrantfile was created. The second argument is the target folder inside the virtual machine.

让我们来看一下争论。 第一个参数是要与VM共享的主机文件夹,在本例中为共享创建Vagrantfile的当前文件夹。 第二个参数是虚拟机内部的目标文件夹。

create: true specifies that if the target folder (/var/www) does not exist, then create it automatically.

create:true指定如果目标文件夹( / var / www )不存在,则自动创建它。

group: “www-data” and owner: “www-data” specifies the owner and the group of the shared folder inside the VM. By default most web servers use www-data as the owner accessing the files, it’s a good practice to set the ownership to this user.

group:“ www-data”所有者:“ www-data”指定VM内所有者和共享文件夹的组。 默认情况下,大多数Web服务器使用www-data作为访问文件的所有者,这是将所有权设置为该用户的一种好习惯。

VirtualBox特定的配置 (VirtualBox specific configurations)

Now that we configured the network and synced folders, we should configure the virtual machine itself. Vagrant lets you dynamically modify the VM – you can change the name, memory, etc.

现在我们已经配置了网络并同步了文件夹,我们应该配置虚拟机本身。 Vagrant可让您动态修改VM –您可以更改名称,内存等。

We can do this within the provider block, which in our case is virtualbox. So let’s create the block, set the name and memory:

我们可以在provider块(在我们的例子中是virtualbox)内执行此操作。 因此,让我们创建块,设置名称和内存:

config.vm.provider "virtualbox" do |v|
    v.name = "SitePoint Test Vagrant"
    v.customize ["modifyvm", :id, "--memory", "1024"]
end

Vagrant uses VBoxManage for setting VM specific parameters before booting it up. For more configuration parameters you can visit the VirtualBox VBoxManage documentation.

Vagrant在启动之前使用VBoxManage设置VM特定参数。 有关更多配置参数,您可以访问VirtualBox VBoxManage文档

The :id is the virtual machine’s ID, which needs to be passed to VBoxManage everytime we want to modify something VM specific. Fortunately, Vagrant handles this for us, so we just need to pass this variable.

:id是虚拟机的ID,每次我们要修改特定于VM的东西时,都需要将该ID传递给VBoxManage。 幸运的是,Vagrant为我们处理了这个问题,因此我们只需要传递此变量即可。

Shell脚本配置 (Shell script provisioning)

The easiest way to provision a base box is to use basic shell script commands which then run inside the virtual machine. This also removes the need for learning or installing Puppet, Ansible and similar tools – even though they can sometimes be more effective provisioners. We’ll deal with them in future articles.

设置基本框的最简单方法是使用基本的shell脚本命令,然后在虚拟机内部运行这些命令。 这也消除了学习或安装Puppet,Ansible和类似工具的需要-尽管有时它们可​​以成为更有效的供应商。 我们将在以后的文章中处理它们。

We need to define the provisioning type, which in our case is called shell. Let’s write that inside this block in the configuration file:

我们需要定义配置类型,在我们的例子中称为shell 。 让我们将其写入配置文件中的该块中:

config.vm.provision "shell" do |s|
    s.path "provision/setup.sh"
end

Vagrant has two types of shell provisioning, inline and external. With inline you can write shell commands in the Vagrantfile itself, but let’s focus on external provisioning, which simply means to load and run a shell script from a file (relative to Vagrantfile) or even from a URL.

Vagrant有两种类型的外壳配置, 内联外部 。 使用内联,您可以在Vagrantfile本身中编写Shell命令,但让我们着重于外部配置,这仅意味着从文件(相对于Vagrantfile)甚至从URL加载和运行Shell脚本。

In our case we want to load provision/setup.sh file, let’s create it and write the following in this file:

在本例中,我们要加载Provision / Setup.sh文件,让我们创建它并在该文件中写入以下内容:

#!/bin/bash

echo "Provisioning virtual machine..."

Now, run vagrant up and it will output Provisioning virtual machine… on the screen. Note that Vagrant will provision the virtual machine only once on the first run, any subsequent provisioning must be executed with the --provision flag either vagrant up --provision or vagrant reload --provision. The provisioning will re-run also if you destroy the VM and rebuild it with vagrant destroy and vagrant up.

现在,运行vagrant up ,它将在屏幕上输出Provisioning虚拟机… 。 请注意, Vagrant在第一次运行时仅会配置一次虚拟机,任何后续的配置都必须使用--provision标志执行,即vagrant up --provisionvagrant reload --provision 如果您销毁VM并使用vagrant destroyvagrant up对其进行重建,则配置也将重新运行。

安装基本软件包 (Installing Base Packages)

Let the fun begin! Let’s install the base packages, namely: Git, Nginx, PHP-FPM and MySQL. In the provision/setup.sh file append the following lines:

让乐趣开始! 让我们安装基本软件包,即:Git,Nginx,PHP-FPM和MySQL。 在provision / setup.sh文件中,添加以下行:

echo "Installing Git"
    apt-get install git -y > /dev/null
    
    echo "Installing Nginx"
    apt-get install nginx -y > /dev/null

Simple as that, but while installing Git and Nginx is straightforward, this is not the case with PHP and MySQL, because each has specific configuration options.

如此简单,但是在安装Git和Nginx时很简单,而PHP和MySQL却不是这种情况,因为每个都有特定的配置选项。

安装PHP (Installing PHP)

Unfortunately Ubuntu’s APT (Advanced Packaging Tool) database is not always up-to-date with the latest stable PHP version, therefore we need to switch to a different source when installing it. For this, we need to install a couple of tools before we can actually install PHP itself.

不幸的是,Ubuntu的APT(高级包装工具)数据库并不总是具有最新的稳定PHP版本,因此,在安装它时我们需要切换到其他源。 为此,我们需要先安装一些工具,然后才能实际安装PHP本身。

Add the following commands into the setup.sh file:

将以下命令添加到setup.sh文件中:

echo "Updating PHP repository"
    apt-get install python-software-properties build-essential -y > /dev/null
    add-apt-repository ppa:ondrej/php5 -y > /dev/null
    apt-get update > /dev/null

After this, type in the following lines to install PHP and a couple of essential extensions:

之后,键入以下行以安装PHP和一些基本扩展:

echo "Installing PHP"
    apt-get install php5-common php5-dev php5-cli php5-fpm -y > /dev/null
    
    echo "Installing PHP extensions"
    apt-get install curl php5-curl php5-gd php5-mcrypt php5-mysql -y > /dev/null

安装MySQL (Installing MySQL)

Installing MySQL is even trickier, because the installation process will prompt you for the root password, but Vagrant needs to automate the installation and somehow fill in the password automatically.

安装MySQL更加棘手,因为安装过程会提示您输入root密码,但是Vagrant需要自动执行安装并以某种方式自动填写密码。

For this we need to install a tool called debconf-utils. Go ahead and type in the following lines in setup.sh:

为此,我们需要安装一个名为debconf-utils的工具。 继续并在setup.sh中键入以下行:

apt-get install debconf-utils -y > /dev/null

Now, we can use this tool to tell the MySQL installation process to stop prompting for a password and use the password from the command line instead:

现在,我们可以使用此工具告诉MySQL安装过程停止提示输入密码,而是从命令行使用密码:

debconf-set-selections <<< "mysql-server mysql-server/root_password password 1234"
    
    debconf-set-selections <<< "mysql-server mysql-server/root_password_again password 1234"

In the two commands above 1234 is the actual password we set the root password to.

1234以上的两个命令中,我们将实际密码设置为root密码。

Now we can go ahead and install MySQL without getting the root password prompts:

现在,我们可以继续安装MySQL,而无需获得root密码提示:

apt-get install mysql-server -y > /dev/null

If you entered the commands corectly in the files, it should look like this:

如果您在文件中完全输入了命令,则其外观应如下所示:

Vagrantfile

流浪文件

Vagrantfile config

provision/setup.sh

提供/setup.sh

setup.sh config
笔记 (Notes)
  • At the end of every command, you can see > /dev/null. This simply suppresses the output from the installation processes. If you would like to see the output when provisioning, simply remove it.

    在每个命令的结尾,您可以看到> / dev / null 。 这只是抑制了安装过程中的输出。 如果您希望在配置时看到输出,只需将其删除。

  • When you try to install a package using the apt-get install command, it will always ask for confirmation, the -y flag specifies “yes”, so it won’t prompt you for confirming each installation.

    当您尝试使用apt-get install命令安装软件包时,它将始终要求确认, -y标志指定为“是”,因此它不会提示您确认每个安装。

配置Nginx设置 (Configuring Nginx settings)

Now that we installed the neccessary packages for a PHP development environment, we also need to configure Nginx to actually serve the project files.

现在,我们已经为PHP开发环境安装了必要的软件包,我们还需要配置Nginx以实际为项目文件提供服务。

The easiest way of doing this, is to simply create a file in our synced folder and use that as Nginx’s configuration file.

最简单的方法是在我们的同步文件夹中简单创建一个文件,并将其用作Nginx的配置文件。

Let’s create a file called nginx_vhost (no file extension) in our provision folder inside another folder called config. I.e. the path to the file will be provision/config/nginx_vhost.

让我们在另一个名为config的文件夹内的provision文件夹中创建一个名为nginx_vhost的文件(无文件扩展名)。 即,文件的路径将为provision/config/nginx_vhost

The file will contain an Nginx basic virtual host configuration:

该文件将包含Nginx基本虚拟主机配置:

server {
        listen 80;
        server_name localhost;
        
        root /var/www/src/;
        index index.php index.html;
        
        # Important for VirtualBox
        sendfile off;
        
        location / {
            try_files $uri $uri/ =404;
        }
        
        location ~* \.php {
            include fastcgi_params;
            
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_cache off;
            fastcgi_index index.php;
        }
    }

Insert the following lines into setup.sh to copy this configuration to the Nginx folder:

将以下行插入setup.sh中,以将此配置复制到Nginx文件夹:

echo "Configuring Nginx"
    cp /var/www/provision/config/nginx_vhost /etc/nginx/sites-available/nginx_vhost > /dev/null
    
    ln -s /etc/nginx/sites-available/nginx_vhost /etc/nginx/sites-enabled/
    
    rm -rf /etc/nginx/sites-available/default
    
    service nginx restart > /dev/null

The Nginx virtual host configuration points to /var/www/src/ folder as the document root. Let’s create this folder and an index.php file inside it. Also, let’s write the following “mandatory” code inside this file:

Nginx虚拟主机配置指向/ var / www / src /文件夹作为文档根目录。 让我们创建此文件夹和其中的index.php文件。 另外,让我们在该文件中编写以下“强制性”代码:

<?php echo "Hello World!"; ?>

You final folder structure should look like this:

您最终的文件夹结构应如下所示:

Folder structure

After running “vagrant up” you should be able to access the “Hello World” page by visiting localhost:8931 in your browser.

运行“ vagrant up”后,您应该可以通过在浏览器中访问localhost:8931来访问“ Hello World”页面。

Note: All the configuration files are available at https://github.com/primalskill/vagrant-base-config

注意:所有配置文件均位于https://github.com/primalskill/vagrant-base-config

结论 (Conclusion)

In this article, you learned how to provision a Vagrant base box using a shell script, installing Nginx, PHP, Git, MySQL and how to configure the web server. In future posts, we’ll look at other provisioning methods.

在本文中,您学习了如何使用Shell脚本配置Vagrant基本框,安装Nginx,PHP,Git,MySQL以及如何配置Web服务器。 在以后的文章中,我们将介绍其他配置方法。

Got any feedback? Leave it in the comments below!

有任何意见吗? 留在下面的评论中!

翻译自: https://www.sitepoint.com/vagrantfile-explained-setting-provisioning-shell/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值