使用PuPHPet轻松构建虚拟机-第2部分

In part one of this series, we considered the problem of how to create and manage development environments in a way which was simple, repeatable, and (even) efficient. It's a problem which I'm confident we've all run in to from time to time. The solution was a relatively new tool called PuPHPet.

在本系列的第一部分中,我们考虑了如何以简单,可重复和(甚至)高效的方式创建和管理开发环境的问题。 我有信心我们经常会遇到这个问题。 该解决方案是一个名为PuPHPet的相对较新的工具。

If you missed part one, PuPHPet creates a Vagrant + Puppet configuration based on five key areas:

如果您错过了第一部分,PuPHPet将基于五个关键区域创建Vagrant + Puppet配置:

  1. Deploy Target (memory, ip address, port forwarding)

    部署目标(内存,IP地址,端口转发)
  2. Server Basics

    服务器基础
  3. PHP (modules, libraries, config settings)

    PHP(模块,库,配置设置)
  4. Database (PostgreSQL, MySQL)

    数据库(PostgreSQL,MySQL)
  5. Webserver (Apache 2, Nginx)

    网络服务器(Apache 2,Nginx)

We looked at how to configure most of the options and how to use the generated configuration, with some basic vagrant commands. But that's as far as we went.

我们通过一些基本的vagrant命令研究了如何配置大多数选项以及如何使用生成的配置。 但这就是我们所做的。

So in this second part of the series, we're going further. Specifically, we're going to be looking at the two core files used: common.yaml, and Vagrantfile. We'll be making some changes to them, then provisioning the virtual machines to reflect the configuration changes.

因此,在本系列的第二部分中,我们将走得更远。 具体来说,我们将研究使用的两个核心文件: common.yamlVagrantfile 。 我们将对其进行一些更改,然后配置虚拟机以反映配置更改。

This series won't make you a master of either Puppet or Vagrant, but by the end of it, you'll have sufficient knowledge to make changes to the configuration generated by PuPHPet, so you can adapt your virtual machine, as your needs change.

本系列文章将不会使您成为PuppetVagrant的高手,但到最后,您将具有足够的知识来更改PuPHPet生成的配置,因此您可以根据需求的变化来适应您的虚拟机。 。

NOTE: please be aware that there have been some important changes in Puppet and Vagrant, which affect PuPHPet. Please pay particular attention to the two notices at the top of the PuPHPet site – especially if you've been experiencing any difficulties following part one of this series. If the notices are gone, the problems are, too.

注意:请注意,Puppet和Vagrant中发生了一些重要更改,这些更改会影响PuPHPet。 请特别注意PuPHPet网站顶部的两个注意事项-特别是如果您在阅读本系列文章的第一部分时遇到任何困难。 如果这些通知不见了,问题也随之而来。

设置变更 (Provisioning Changes)

For our purposes today, there is one key command which you need to know, vagrant provision. Quoting the manual:

就我们今天的目的而言,您需要了解一个关键命令,即无规提供 。 引用手册:

This command is a great way to quickly test any provisioners, and is especially useful for incremental development of shell scripts, Chef cookbooks, or Puppet modules. You can just make simple modifications to the provisioning scripts on your machine, run a vagrant provision, and check for the desired results. Rinse and repeat.

此命令是快速测试任何供应者的好方法,对于增量开发Shell脚本,Chef Cookbook或Puppet模块特别有用。 您可以对计算机上的配置脚本进行简单的修改,运行无用的配置,然后检查所需的结果。 冲洗并重复。

We'll be using it to roll out our environment changes.

我们将使用它来发布环境更改。

更改共享文件夹配置 (Altering Shared Folder Configuration)

One of the things that you'll likely want to do first is to adjust the configuration of the shared folders. Assuming the original configuration from the first part in the series, here's what the relevant section of Vagrantfile looks like.

您可能首先要做的一件事就是调整共享文件夹的配置。 假设该系列第一部分的原始配置,这是Vagrantfile的相关部分的样子。

config.vm.synced_folder "/Users/matthewsetter/Documents/workspace/my-great-app", "/var/www/my-great-app", id: "vagrant-root", :nfs => false

You see that I've set up a shared folder in my workspace directory with the same name under /var/www/ in the virtual machine. Recently I was setting up a new Vagrant VM for a client's project. I found that my Zend Framework 2 application couldn't write to the cache directory. Looking at the directory structure of the project, in the virtual machine, all of the directories were owned by the user vagrant; but the webserver user (and group) was www-data. So it wasn't able to read. I saw two options:

您会看到我已经在虚拟机中的/var/www/下的工作空间目录中设置了一个相同名称的共享文件夹。 最近,我正在为客户的项目设置新的Vagrant VM。 我发现我的Zend Framework 2应用程序无法写入缓存目录。 查看项目的目录结构,在虚拟机中,所有目录均由用户无业游民拥有; 但网络服务器用户(和组)是www-data。 因此它无法阅读。 我看到了两个选择:

  1. Change the permissions to 777

    将权限更改为777
  2. Change the owner and group

    更改所有者和组

To me the second was the more intelligent choice. But how to do that? As it turns out, it is quite simple. At the end of the configuration above, we need to specify the owner and group as www-data. I've done this in the updated configuration below.

对我来说,第二个是更明智的选择。 但是该怎么做呢? 事实证明,这非常简单。 在上述配置的最后,我们需要将所有者和组指定为www-data 。 我已经在下面的更新配置中完成了此操作。

config.vm.synced_folder "/Users/matthewsetter/Documents/workspace/my-great-app", "/var/www/my-great-app", id: "vagrant-root", :nfs => false, owner: "www-data", group: "www-data"

To roll out the change, update the file Vagrantfile in your Vagrant configuration, save the file, then from the command line, run the command: vagrant provision. All being well, you'll see output similar to the first time you called vagrant up, but much shorter, as only a few changes are being made – not the entire VM being provisioned. When it's done, ssh in to the VM with vagrant ssh, then have a look at the permissions on the project directory. They should look like the output below:

要推出更改,请在您的Vagrant配置中更新文件Vagrantfile,保存文件,然后从命令行运行命令: vagrant provision 。 一切都很好,您会看到与第一次调用vagrant up时类似的输出,但输出会更短,因为仅进行了少量更改–并未配置整个VM。 完成后,使用vagrant ssh进入虚拟机,然后查看项目目录上的权限。 它们看起来应该像下面的输出:

$ ls -lahrt /var/www/my-great-app/
    total 3.1M
    drwxrwxrwx 1 www-data www-data  102 Oct 15 05:00 data/
    -rw-rw-rw- 1 www-data www-data 856K Oct 15 05:00 composer.phar
    -rw-rw-rw- 1 www-data www-data  607 Oct 15 05:00 README.md
    -rw-rw-rw- 1 www-data www-data  259 Oct 15 05:00 .gitmodules
    -rw-rw-rw- 1 www-data www-data  338 Oct 15 05:00 .gitignore
    -rw-rw-rw- 1 www-data www-data 1.8K Oct 15 05:00 init_autoloader.php
    drwxrwxrwx 1 www-data www-data  340 Oct 15 05:00 db/
-rw-rw-rw- 1 www-data www-data 2.2M Oct 15 05:00 zftool.phar

改变PHP的配置 (Altering PHP’s Configuration)

So far, so good. Let's look at the PHP configuration and make some changes. Below is the original configuration generated by PuPHPet in part one. You'll find it in vagrant/puppet/hieradata/common.yaml. It shows the PHP version is 5.5, composer's enabled, the php modules, ini settings and the default timezone.

到目前为止,一切都很好。 让我们看一下PHP配置并进行一些更改。 以下是第一部分由PuPHPet生成的原始配置。 您可以在vagrant/puppet/hieradata/common.yaml找到它。 它显示PHP版本为5.5,启用了作曲家,php模块,ini设置和默认时区。

php:
    version: '55'
    composer: '1'
    modules:
        php:
            - cli
            - intl
            - cgi
            - curl
            - mcrypt
            - memcache
            - memcached
            - pspell
            - tidy
            - sqlite
        pear: {  }
        pecl:
            - pecl_http
    ini:
        display_errors: On
        error_reporting: 'E_ALL & ~E_STRICT'
        apc.enabled: '1'
    timezone: Europe/Berlin

Let's make some changes. Let's change:

让我们进行一些更改。 让我们改变:

  • the timezone to ’Europe/London’

    时区为“欧洲/伦敦”
  • the session save_path

    会话save_path
  • some other runtime options:

    其他一些运行时选项:

Specifically, update from ini: down with the following:

具体来说,从ini:向下更新以下内容:

ini:
        display_errors: On
        error_reporting: 'E_ALL & ~E_STRICT'
        apc.enabled: '1'
        session.save_path: /tmp
        allow_url_fopen: '1'
        allow_url_include: '1'
        error_log: syslog
        file_uploads: '1'
    timezone: Europe/London

You'll likely recognize the ini directives from the PHP Manual so I won't rehash them here. But you can see that they're as easy to configure here as they are in the PHP ini file. Actually, let's go out on a limb and make a decent change.

您可能会认识到PHP手册中的ini指令,因此在这里不再赘述。 但是您可以看到,它们在此处的配置与在PHP ini文件中一样容易。 其实,让我们出去做一个体面的改变。

After doing so, here's the new PHP settings:

这样做之后,这是新PHP设置:

  • session.save_path => /tmp => /tmp

    session.save_path => / tmp => / tmp
  • allow_url_fopen => On => On

    allow_url_fopen =>开=>开
  • allow_url_include => On => On

    allow_url_include =>开=>开
  • file_uploads => On => On

    file_uploads =>开=>开
  • error_log => syslog => syslog

    错误日志=>系统日志=>系统日志
  • Default timezone => Europe/London

    默认时区=>欧洲/伦敦
  • date.timezone => Europe/London => Europe/London

    date.timezone =>欧洲/伦敦=>欧洲/伦敦

Note: If you'd like to pick a different timezone, here's the list supported by PHP.

注意:如果您想选择其他时区,则这是 PHP支持的列表

Now I'm sure you'll agree that being able to change a PHP configuration, just by changing a text file, is powerful, very powerful. Imagine the amount of work you've likely done in the past to achieve this.

现在,我确定您会同意,仅通过更改文本文件即可更改PHP配置的功能非常强大。 想象一下您过去为实现这一目标而要做的工作。

更改XDebug配置 (Altering XDebug Configuration)

If you've been using PHP for any amount of time, you've likely been using Derek Rethan's XDebug package. In part one, we took the basic setup offered in PuPHPet. Let's make some modest changes to it and again provision the virtual machine to enable them. Below is the original configuration in common.yaml.

如果您一直在使用PHP,那么您可能一直在使用Derek Rethan的XDebug软件包 。 在第一部分中,我们采用了PuPHPet中提供的基本设置。 让我们对其进行一些适度的更改,然后再次配置虚拟机以启用它们。 以下是common.yaml中的原始配置。

xdebug:
    install: '1'
    settings:
        xdebug.default_enable: '1'
        xdebug.remote_autostart: '0'
        xdebug.remote_connect_back: '1'
        xdebug.remote_enable: '1'
        xdebug.remote_handler: dbgp
        xdebug.remote_port: '9000'

Add in the following options to it, remembering to respect the indentation, otherwise you'll have problems.

在其中添加以下选项,切记要遵守缩进,否则会遇到问题。

xdebug.auto_trace = 1
    xdebug.idekey = "SITEPOINTPHP"
    xdebug.max_nesting_level
    xdebug.profiler_enable = 1
    xdebug.profiler_output_dir = "/tmp/xdebug.profiler"

There's no specific reason for these configuration options over any other. They've been randomly picked just to play with XDebug. So feel free to choose other options if you wish.

这些配置选项没有其他任何特定的原因。 他们被随机挑选出来只是为了与XDebug一起玩。 因此,如果您愿意,可以随时选择其他选项。

As before, call vagrant provision, wait a bit for the provisioning process to complete. Then we can check that the process' been successful by again looking at the running PHP configuration with:

和以前一样,调用vagrant provision ,稍等片刻以完成设置过程。 然后,通过再次查看正在运行PHP配置,可以检查该过程是否成功:

php -i | grep -i xdebug
  • xdebug.auto_trace => On => On

    xdebug.auto_trace =>开=>开
  • xdebug.idekey => SITEPOINTPHP => SITEPOINTPHP

    xdebug.idekey => SITEPOINTPHP => SITEPOINTPHP
  • xdebug.max_nesting_level => 100 => 100

    xdebug.max_nesting_level => 100 => 100
  • xdebug.profiler_enable => On => On

    xdebug.profiler_enable =>开=>开
  • xdebug.profiler_output_dir => /tmp/xdebug.profiler => /tmp/xdebug.profiler

    xdebug.profiler_output_dir => /tmp/xdebug.profiler => /tmp/xdebug.profiler

改变MySQL的配置 (Altering MySQL’s Configuration)

Ok, one last one. Let's make changes to MySQL and have a bit of fun. We’ll change the user account name and password, install and enable phpmyadmin. We’ll finish by making the user's privileges a bit more restrictive. Not a bad list of tasks. No?

好,倒数第一。 让我们对MySQL进行更改并获得一些乐趣。 我们将更改用户帐户名和密码,安装并启用phpmyadmin 。 最后,我们将限制用户的权限。 不错的任务清单。 没有?

Think for a moment just how long that'd take if you did it all by hand. No, seriously. maybe you already have provisioning scripts for this; maybe you do it all by hand; but either way it takes time.

想一想如果您手动完成所有操作需要花费多长时间。 不,认真 也许您已经有配置脚本了; 也许你全手工做; 但无论哪种方式都需要时间。

Note: I'll be honest, I'm not a MySQL admin; so there's tools I won't be aware of. If you are, please mention some in the comments.

注意:老实说,我不是MySQL管理员; 所以有些工具我不会知道。 如果您是,请在评论中提及一些内容。

As before, below is the originally generated configuration so you know what it looks like.

和以前一样,下面是原始生成的配置,因此您知道它的外观。

mysql:
    root_password: password
    phpmyadmin: 0
    databases:
        njmOqAFAsG3i:
            grant:
                - ALL
            name: testdb
            host: localhost
            user: testdb_couser
            password: testdb_password
            sql_file: ''

Below is the updated version, showing what it looks like with the changes I mentioned above. You see that the username and password have changed, I've set 1 next to phpMyAdmin to enable & install it.

下面是更新的版本,显示了我上面提到的更改的外观。 您会看到用户名和密码已更改,我在phpMyAdmin旁边设置了1以启用并安装它。

mysql:
    root_password: password
    phpmyadmin: 1
    databases:
        njmOqAFAsG3i:
            grant:
                - INSERT
                - SELECT
                - 'SHOW DATABASES'
                - UPDATE
                - USAGE
                - INDEX
                - DELETE
                - CREATE
                - ALTER
            name: testdb
            host: localhost
            user: testdb_username
            password: testdb_P@$sw0rd
            sql_file: ''

Let's provision the virtual machine again and see what's changed. After running vagrant provision, I'm able to login with the new username and password, and phpMyAdmin is available at http://192.168.56.101/phpmyadmin/.

让我们再次配置虚拟机,看看有什么变化。 运行vagrant provision ,我可以使用新的用户名和密码登录,并且可以从http://192.168.56.101/phpmyadmin/获得phpMyAdmin。

结论 (Conclusion)

So, what do you think? Do PuPHPet, Vagrant, and Puppet make managing a development environment really simple? So many things which may have been done manually, or with hand written bash scripts don't need to be any longer.

所以你怎么看? PuPHPet,Vagrant和Puppet使管理开发环境真的很简单吗? 如此多的事情可以手动完成,也可以不再需要手写bash脚本。

As you followed along, did your provisioning run as smoothly as mine did? Did you encounter any issues that you need a hand with? If so, feel free to email me directly.

在您遵循的过程中,您的配置是否像我的一样顺利进行? 您是否遇到任何需要解决的问题? 如果是这样,请随时直接给我发送电子邮件。

Otherwise, I hope that you've been bitten by the bug that is Vagrant, Puppet, and PuPHPet. I also hope you've seen just how easy it is to adjust your environments, on the fly, to match your changing needs, through nothing more than 2 version controlled text files and a handful of commands.

否则,我希望您被Vagrant,Puppet和PuPHPet的错误所困扰。 我也希望您已经看到,通过2个版本控制的文本文件和少量命令,即时调整环境以适应不断变化的需求是多么容易。

Please share your thoughts in the comments.

请在评论中分享您的想法。

翻译自: https://www.sitepoint.com/build-virtual-machines-easily-puphpet-part-2/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值