使用Vagrant设置Phalcon(和其他工具)

Matthew Setter's last article on PuPHPet covered the easy GUI-backed creation of Vagrant VMs. PuPHPet is an absolutely awesome tool in getting up and running with a development environment really fast, and offers some very neat default options.

Matthew Setter 在PuPHPet上最后一篇文章介绍了由GUI支持的Vagrant VM的轻松创建。 PuPHPet是一个非常出色的工具,可以非常快速地启动和运行开发环境,并提供一些非常简洁的默认选项。

But what if we want to step outside the defaults? What if we'd, for example, like to install a PHP extension that isn't part of the default distribution and doesn't exist in package managers?

但是,如果我们想超出默认值怎么办? 例如,如果我们想安装一个PHP扩展,该扩展不是默认发行版的一部分,并且不存在于程序包管理器中,该怎么办?

In this tutorial, we'll make sure the Phalcon framework is installed by default when we run vagrant up. Pre-installing software on a Vagrant VM is called provisioning.

在本教程中,我们将确保在运行vagrant up时默认安装Phalcon框架。 在Vagrant VM上预安装软件称为Provisioning

做好准备 (Getting ready)

Please walk through Matthew's article before moving along with this one. Simply make sure you download a ready PHP-enabled VM.

在继续阅读本文之前,请仔细阅读马修的文章 。 只需确保您下载了现成的支持PHP的VM。

In this particular case I used PHP 5.4 with Ubuntu (5.5 won't work with it just yet via PuPHPet), but if you've got your own configuration you're used to, please feel free to use it – just make sure you can get to a good phpinfo screen before continuing. Make certain your PHP configuration works and you can access the VM through the browser and execute a PHP script like

在这种情况下,我将PHP 5.4与Ubuntu一起使用(5.5尚未通过PuPHPet兼容),但是如果您已经习惯了自己的配置,请随时使用它–请确保您已在继续之前可以进入一个良好的phpinfo屏幕。 确保您PHP配置有效,并且您可以通过浏览器访问VM并执行PHP脚本,例如

<?php
phpinfo();

Ideally, your browser should display something like this:

理想情况下,您的浏览器应显示以下内容:

alt

设置的基础 (The basics of Provisioning)

Vagrant offers several ways to provision software into your Virtual Machines. There's Puppet (what PuPHPet is based on), Chef, Docker, and more. What we'll be using is pure good old shell commands – Vagrant can execute a shell file we write if we pass it to its provisioning settings.

Vagrant提供了几种将软件置备到虚拟机中的方法。 有Puppet(PuPHPet所基于的),Chef,Docker等。 我们将使用的是纯好的老式Shell命令–如果将Vagrant传递给它的配置设置,Vagrant可以执行我们编写的Shell文件。

So, let's say you need to install the text editor joe and want to do it through shell. In your PuPHPet folder's shell subfolder, you would create a joe.sh file with the following contents:

因此,假设您需要安装文本编辑器joe并希望通过shell进行操作。 在PuPHPet文件夹的shell子文件夹中,您将创建一个具有以下内容的joe.sh文件:

#!/bin/bash
sudo su
apt-get install joe

This shell script first identifies itself as executable by bash (optional), requests superuser permissions, and then it installs joe through the regular channels. To actually have this executed, we modify the Vagrantfile in the PuPHPet folder by adding the line

该shell脚本首先通过bash(可选)将其自身标识为可执行文件,然后请求超级用户权限,然后通过常规渠道安装joe。 为了实际执行此操作,我们通过添加以下行来修改PuPHPet文件夹中的Vagrantfile

config.vm.provision :shell, :path => "shell/joe.sh"

under the last config.vm.provision... line/block. Now if we run vagrant up in our PuPHPet folder, our VM will have joe installed as soon as it boots.

在最后的config.vm.provision...行/块下。 现在,如果我们在PuPHPet文件夹中运行vagrant up ,则我们的VM将在启动后立即安装joe

安装Phalcon (Installing Phalcon)

As per Phalcon's online installation instructions, the compilation process is straightforward:

根据Phalcon的在线安装说明,编译过程非常简单:

git clone git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install

However, we also need to enter phalcon.so into the PHP configuration, restart the server, and make sure all the prerequisites are installed (they are, PuPHPet took care of that for us). Let's do this step by step.

但是,我们还需要在PHP配置中输入phalcon.so,重新启动服务器,并确保已安装所有先决条件 (PuPHPet已为我们完成了这些工作)。 让我们一步一步地做。

In the shell subfolder again, create the file install_phalcon.sh. Give it the following content:

再次在shell子文件夹中,创建文件install_phalcon.sh 。 提供以下内容:

#!/bin/bash
sudo su
git clone git://github.com/phalcon/cphalcon.git
cd cphalcon/build
./install
cd ..
cd ..
rm -rf cphalcon

Now if you re-run vagrant up after running vagrant destroy, you can see the phalcon.so extension in the PHP extensions folder:

现在,如果你重新运行vagrant up运行后vagrant destroy ,你可以看到在PHP扩展文件夹中的phalcon.so扩展:

alt

However, in order for it to actually be used, phalcon.so needs to be added to the PHP configuration. After adding it, we also need to reload the server in order to load the new configuration. Improve the shell file above by adding the following statements to the bottom:

但是,为了使其实际使用,需要将phalcon.so添加到PHP配置中。 添加后,我们还需要重新加载服务器以加载新配置。 通过在底部添加以下语句来改进上面的shell文件:

echo 'extension=phalcon.so' > /etc/php5/mods-available/phalcon.ini
ln -s /etc/php5/mods-available/phalcon.ini /etc/php5/cli/conf.d/phalcon.ini
ln -s /etc/php5/mods-available/phalcon.ini /etc/php5/apache/conf.d/phalcon.ini
ln -s /etc/php5/mods-available/phalcon.ini /etc/php5/fpm/conf.d/phalcon.ini

service apache2 restart

First, we create a phalcon.ini file in the /etc/php5/mods-available directory. We could echo it directly into php.ini, but this is just cleaner. Then, we symlink that newly created file to all the locations from where PHP can be run: Apache, command line and FPM. That way, all our potential PHP runtimes have Phalcon available. Finally, we restart Apache.

首先,我们在/etc/php5/mods-available目录中创建一个phalcon.ini文件。 我们可以将其直接回显到php.ini中,但这更干净。 然后,我们将新创建的文件符号链接到可以运行PHP的所有位置:Apache,命令行和FPM。 这样,我们所有潜在PHP运行时都可以使用Phalcon。 最后,我们重新启动Apache。

If you try running vagrant up now (after running vagrant destroy), you'll notice the phpinfo screen displaying Phalcon as loaded after you re-visit the VM's IP in your browser.

如果您现在尝试运行vagrant up (在运行vagrant destroy ),您将在重新访问浏览器中的VM IP之后注意到phpinfo屏幕显示Phalcon已加载。

But what if the server process is named differently? For example, if it's "httpd", or just "apache", and not "apache2"? Furthermore, what if we've got a different distro? On CentOS phalcon.ini should go to /etc/php.d/, and on other distros into other locations. Finally, what if we only do a vagrant halt and then run vagrant up but force a re-provision with the --provision flag? Wouldn't it be better to simply ignore the Phalcon installation if it's already installed?

但是,如果服务器进程的名称不同怎么办? 例如,如果它是“ httpd”,或者仅仅是“ apache”,而不是“ apache2”? 此外,如果我们有其他发行版怎么办? 在CentOS上, phalcon.ini应该转到/etc/php.d/ ,而在其他发行版上应转到其他位置。 最后,如果我们只vagrant halt了一个vagrant halt ,然后运行vagrant up但又用--provision标志强行进行了重新配置,该--provision办? 如果已经安装了Phalcon,就不理会它会更好吗?

Luckily, slogsdon took all this into account for us. He built an excellent Phalcon installation shell script, ripe with IF clauses and more, and even created a phalcon-tools installation script.

幸运的是, slogsdon将所有这些考虑在内。 他构建了一个出色的Phalcon安装Shell脚本,其中包含IF子句和更多内容,甚至创建了phalcon-tools安装脚本。

To install Phalcon with all the above taken into account, place install_phalcon.sh into the shell folder of your PuPHPet download, and add it to the provisioner like we did for joe before. If you'd like to install Phalcon tools, do the same with install_phalcon-devtools.sh. That's all you need to do – the script accounts for different servers and setups so all you need to do is have it run.

要考虑以上所有因素来安装Phalcon,请将install_phalcon.sh放入PuPHPet下载的shell文件夹中,然后像以前对joe所做的那样将其添加到预配器中。 如果您想安装Phalcon工具,请使用install_phalcon-devtools.sh进行相同的操作。 这就是您需要做的–脚本说明了不同服务器和设置的情况,因此只需运行它即可。

Now you too can have an out-of-the-box ready Phalcon installation ready for development at the snap of your fingers. Naturally, you'll need to do some additional tweaks to get URL rewriting to work on Nginx (just follow the Phalcon docs), but once the extension is installed and loaded by PHP, the hard part is over. What's best, you can use this approach to install Phalcon on any of the PuPHPet supported environments as well – Digital Ocean, Rackspace or AWS. Just plug slogsdon's shell into the provision settings, and you're ready to go!

现在,您也可以立即使用现成的Phalcon安装,以进行开发。 当然,您需要做一些其他调整才能使URL重写在Nginx上起作用(只需遵循Phalcon docs ),但是一旦PHP安装和加载了扩展,困难的部分就结束了。 最好的是,您也可以使用这种方法在任何PuPHPet支持的环境(Digital Ocean,Rackspace或AWS)上安装Phalcon。 只需将slogsdon的外壳插入配置设置中,就可以开始了!

结论 (Conclusion)

In this tutorial, we covered the basics of provisioning via shell and installed Phalcon (and joe) with a booting VM. Coming soon is a followup article from Matthew Setter which will deal with further customizations of the downloaded provision settings from PuPHPet.

在本教程中,我们介绍了通过Shell进行配置的基本知识,并通过启动VM安装了Phalcon(和joe)。 即将推出的是Matthew Setter的后续文章,该文章将进一步讨论PuPHPet下载的供应设置的自定义设置。

To get familiar with the full workflow of Vagrant and Puppet, please do keep following this series as it's about to get a whole lot more interesting!

要熟悉Vagrant和Puppet的完整工作流程,请继续关注本系列,因为它将变得更加有趣!

If you have any comments, uncertainties, problems or just general feedback, please leave it in the comments below and I'll do my best to address it as soon as possible! If you've got your own setup you'd like to share with the rest of us, do get in touch with me via +BrunoSkvorc and we'll talk.

如果您有任何意见,不确定性,问题或仅是一般性反馈,请保留在下面的评论中,我将尽力尽快解决! 如果您有自己的设置,想与我们其他人分享,请通过+ BrunoSkvorc与我联系 ,我们将进行交流。

翻译自: https://www.sitepoint.com/provisioning-phalcon-tools-vagrant/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值