Explore Vagrant: Understand its purpose, benefits, and implementation

王林
Release: 2023-09-02 13:10:02
Original
1020 people have browsed it

探索 Vagrant:了解其目的、好处和实施

This article will help you understand how to use Vagrant to manage virtual machine instances, and explain how to leverage Puppet to configure various resources, such as PHP and PostgreSQL.


Introduction

Developers have a variety of ways to structure their web development environment.

Developers have a variety of ways to structure their web development environment. You can use "native" options, such as installing pre-built "all-in-one" server stacks such as Zend Server, XAMPP, MAMP, WAMP, etc., or you can install components yourself from source, or through a package management system such as Homebrew , Apt and Yum.

This may add up as you work on various projects using PHP 5.3 and PHP 5.4, MySQL, SQLite, MongoDB, Postgres, PEAR, PHPUnit, Rails 3.1, Memcached, Redis, Gearman, NodeJS, etc. If you upgrade or your computer freezes, you will have to start over.

You can do a "remote" setup using a server with a Samba share on your network or an SSH server installed using a tool like ExpanDrive. The latter option may cause file read/write delays, which can be very annoying. You can SSH Vim for everything, which is fast, but only if you want to use Vim for everything.


Development and Production

While you may be happy with the way it works now, how many of you have heard (or said) "it works great on my computer". This is very common and can happen when there are differences in circumstances, even down to the most trivial details.

It's important to make sure your development environment is the same as your production environment and matches your staging and test servers (if you have those too).

This may sound simple if you are just considering installing a few copies of Apache, PHP, and MySQL, but there are a million factors to consider. If you develop on OSX and deploy to an Ubuntu system, you'll notice interesting issues with file casing. This is common in CodeIgniter when someone has a library with a lowercase first letter. It loads fine on OSX but crashes when deployed to production. Your development process might just be costing you some business, all because of some trivial operating system difference that no one realizes until it's too late.


Development=Production

Forcing developers to use the same operating system can cause problems.

So what is the solution? Forcing all developers to abandon different tools and develop on the exact same model laptop? If your developers all use brand new Macbooks, you probably won't get many complaints, but you'll need to use OSX Server to do everything.

You can do everything with Linux, but you have to argue about which distro to use. Forcing developers to use the same operating system can cause problems, reduce productivity, and promote nerd wars.

Virtualization is the answer, this is nothing new, but when people think of virtualization they often think of performance issues, their fans spinning like crazy while their laptops desperately try to run two operations system.

You still get this when trying to run Windows on a low-power machine, but these days, the average Mac has 4 GB of RAM out of the box, which is more than enough to power an Ubuntu server installation running on Windows. Command line mode and all common development tools (IDE, browser, debugging tools, etc.). There are several options for virtualization, but I prefer Oracle's VirtualBox (free). The software does all the heavy lifting of virtualization, and a tool called Vagrant then manages the instances.


Step 1 - Install VirtualBox

First download VirtualBox and install it. On *nix systems (Mac OSX, Linux, etc.) you need to modify .bash_profile (or .zsh_profile) to expand the $PATH variable:

PATH=$PATH:/Applications/VirtualBox.app/Contents/MacOS/
export PATH
Copy after login

This will let Vagrant know where VirtualBox is installed, of course, different operating systems will vary.


Step 2 - Install Vagrant

You can download the version of vagrant for your operating system, or if it's not available, install it as a gem:

$ gem install vagrant
Copy after login

Step 3 - Create an instance

Give your vagrant setup a place to live:

mkdir -p ~/Vagrant/test
cd ~/Vagrant/test
Copy after login

We will be using Ubuntu 12.04 LTS (Precise Pangolin), which has a "box" already set up.

vagrant box add precise32 http://files.vagrantup.com/precise32.box
Copy after login

You can see the parameter "precise32" here, which is the nickname of the URL. You can now create an instance that will download this .box.

vagrant init precise32
vagrant up
Copy after login

It will run now. simple! If you want to SSH into this instance, use the following command:

vagrant ssh
Copy after login

第 5 步 - 配置

您将有一个名为 Vagrantfile 的文件,其中包含此实例的配置:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant::Config.run do |config|

    config.vm.box = "precise32"
    config.vm.box_url = "http://files.vagrantup.com/precise32.box"

    # Assign this VM to a host-only network IP, allowing you to access it
    # via the IP. Host-only networks can talk to the host machine as well as
    # any other machines on the same network, but cannot be accessed (through this
    # network interface) by any external networks.
    config.vm.network :hostonly, "192.168.33.10"

    # Set the default project share to use nfs
    config.vm.share_folder("v-web", "/vagrant/www", "./www", :nfs => true)
    config.vm.share_folder("v-db", "/vagrant/db", "./db", :nfs => true)

    # Forward a port from the guest to the host, which allows for outside
    # computers to access the VM, whereas host only networking does not.
    config.vm.forward_port 80, 8080

    # Set the Timezone to something useful
    config.vm.provision :shell, :inline => "echo \"Europe/London\" | sudo tee /etc/timezone && dpkg-reconfigure --frontend noninteractive tzdata"

    # Update the server
    config.vm.provision :shell, :inline => "apt-get update --fix-missing"

    # Enable Puppet
    config.vm.provision :puppet do |puppet|
        puppet.facter = { "fqdn" => "local.pyrocms", "hostname" => "www" } 
        puppet.manifests_path = "puppet/manifests"
        puppet.manifest_file  = "ubuntu-apache2-pgsql-php5.pp"
        puppet.module_path  = "puppet/modules"
    end

end
Copy after login

如果您没有注意到,这是 Ruby 语法;这样您就可以对该文件发挥相当大的创意,但这是基础知识。

它将显示要使用的昵称并具有 URL,以防本地未设置昵称(适合共享)。

share_folder 行对于将实例中的文件夹映射到本地文件夹非常有用。使用 nfs => true 实例将能够写入和更改文件的权限,这在您尝试在其中安装 CMS 等情况时非常有用。

端口转发允许您访问 http://localhost:8080 上的实例,当然,如果发生冲突,请将其更改为其他端口。

此配置文件还将时区设置为欧洲/伦敦,然后运行 ​​apt-get update,这将强制您的系统在启动时保持最新状态。如果您跳过此配置项,您可能会发现多个软件包拒绝安装,因为参考现已过时。

当您更改配置时,您可以重新加载实例来使用它:

vagrant reload
Copy after login

现在我们的服务器已经运行并准备就绪,我们需要安装一些软件。我们不仅仅要通过命令行 apt-get install 一堆软件包,我们还要“配置”我们的服务器。


第 4 步 - 配置

服务器配置并不是许多开发人员需要考虑的问题。

服务器配置并不是许多开发人员需要考虑的事情,因为它通常留给系统管理员。这个想法是对服务器上的软件和配置进行一些记录,以便您可以创建新的开发环境、复制生产的新登台服务器,或者创建另一个生产服务器以在两者之间进行负载平衡。

老式配置

系统管理员处理这个问题的方式各不相同,但过去已经使用过各种解决方案 - 从保持命令维基运行(它可能会很快变得庞大和过时)到拥有“多终端”的绝佳方法,您在一个窗口中输入命令,它会同时在另外 7 台服务器上复制相同的命令。 这些方法都很糟糕。

一种解决方案是构建您自己的 .box 文件,或创建 .iso 备份,以便新服务器可以基于此,但维护这些图像会产生大量额外的工作,而且无论多么困难你尝试一下,随着时间的推移,这些开发机器将会变得不同步。

现代配置

目前有两种流行的系统,称为 Puppet 和 Chef。

目前有两种流行的系统,称为 Puppet 和 Chef。两者都已存在多年,但随着 DevOps 开发方法的增加而开始变得更加流行。两者的想法相似,您应该研究这两个系统,但本教程将专门关注 Puppet。

本质上,您不必运行一堆命令并希望一切正常,而是为 Puppet 构建一个清单,解释您需要确保已完成的所有内容。当您在终端中运行命令时,您基本上是在对计算机说:

“安装 Apache”

对于 Puppet,我们会说:

“确保 Apache 已安装”

或者,代替:

“创建一个新文件夹,名为 /var/www 并设置 www-data:www-data 的权限”

对于 Puppet,我们会说:

“确保 /var/www 存在并且具有与 www-data:www-data 匹配的权限”

这里的区别在于,这些清单可以运行多次(在 cron 作业中每小时或每天)以保持最新状态,并且尝试安装两次不会出现意外结果。

它还将帮助您测试一切是否按预期运行,因为任何规则失败都会引发错误,这些错误比 grep 大量 bash 命令结果更容易跟踪。 Puppet 会抛出大红色错误,让您知道 PHP 没有安装,或者无法配置特定模块。

清单和模块

清单一开始有点令人困惑,但过了一段时间,它们就开始有意义了。

查看一个基本示例:

file {'testfile':
  path    => '/tmp/testfile',
  ensure  => present,
  mode    => 0640,
  content => "I'm a test file.",
}
Copy after login

无需解释这里发生了什么,对吧?

此文件稍后可以在清单中称为“testfile”,这意味着它可以被列为其他操作的依赖项。

有关更多示例,我们将参考 GitHub 上的 PyroCMS Puppet 清单。

include apache

$docroot = '/vagrant/www/pyrocms/'
$db_location = "/vagrant/db/pyrocms.sqlite"

# Apache setup
class {'apache::php': }

apache::vhost { 'local.pyrocms':
    priority => '20',
    port => '80',
    docroot => $docroot,
    configure_firewall => false,
}

a2mod { 'rewrite': ensure => present; }
Copy after login

这包括“apache”模块,设置一些变量,在 apache 模块中运行额外的“apache::php”清单,设置虚拟主机,然后确保启用“mod_rewrite”。

所有这些类都在我们包含的 Apache 模块中定义。

继续,我们还想安装 PHP:

include php

php::module { ['xdebug', 'pgsql', 'curl', 'gd'] : 
    notify => [ Service['httpd'], ],
}
php::conf { [ 'pdo', 'pdo_pgsql']:
    require => Package['php5-pgsql'],
    notify  => Service['httpd'],
}
Copy after login

这块清单将安装我们需要的 PHP 扩展,然后 notify 选项会让 Apache 知道您已经安装了新配置,这意味着它将重新启动。

include postgresql

class {'postgresql::server': }

postgresql::db { 'pyrocms':
    owner     => 'pyrocms',
    password => 'password',
}
Copy after login

这将设置一个 postgres 服务器,创建一个名为“pyrocms”的数据库,并确保名为“pyrocms”的用户存在并提供密码。

快完成了!最后一步是确保您正确设置了可写文件和文件夹:

file { $docroot:
    ensure  => 'directory',
}

file { "${docroot}system/cms/config/config.php":
    ensure  => "present",
    mode    => "0666",
    require => File[$docroot],
}

$writeable_dirs = ["${docroot}system/cms/cache/", "${docroot}system/cms/config/", "${docroot}addons/", "${docroot}assets/cache/", "${docroot}uploads/"]

file { $writeable_dirs:
    ensure => "directory",
    mode   => '0777',
    require => File[$docroot],
}
Copy after login

这将确保 Apache 文档根目录存在,配置文件设置为 0666,一些可写文件夹设置为 777。

我们有了!

要运行所有这些,只需重新启动 vagrant 实例,或运行:

vagrant provision
Copy after login

如果一切正常,您应该会看到很多蓝色文本,表明一切都正在安装,但是,如果出现问题,您会看到红色。 Google 搜索这些错误,然后重试。

这里使用的模块是:Apache、Postgres、PHP,您可以通过克隆 PyroCMS Vagrant 存储库来查看整个操作:

git clone --recursive git://github.com/pyrocms/devops-vagrant.git ~/vagrant/pyrocms
cd ~/vagrant/pyrocms
vagrant up
Copy after login

将浏览器指向 http://localhost:8089/,您应该会看到安装程序。很简单的事情,是吧?

注意:这将与 MySQL 一起安装,因为 PyroCMS 的 Postgres 和 SQLite 支持仍在开发中,等待某些 CodeIgniter PDO 功能完成。如果您有兴趣,可以通过更改 Vagrantfile 来尝试使用 ubuntu-apache2-pgsql-php5.pp 清单,销毁实例,然后再次启动它。 Pyrocms 子模块还需要签出到 feature/pdo


摘要

在本文中,我们使用 Vagrant、VirtualBox 和 Puppet 不仅设置了一个可供我们使用的服务器实例,而且还为我们的服务器创建了一个测试套件,以确保一切都正确运行、安装和配置。

我们还创建了一份需求清单,并且将来能够在几分钟而不是几小时内创建任意数量的相同服务器!

The above is the detailed content of Explore Vagrant: Understand its purpose, benefits, and implementation. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template