简体   繁体   English

PHP 代码部署技巧

[英]PHP Code Deployment Tips

In the past, I have been developing in a very amateurish fashion, meaning I had a local machine where I developed and tested code and a production machine to which I copied the code when I was done.过去,我一直在以一种非常业余的方式进行开发,这意味着我有一台本地机器,用于开发和测试代码,还有一台生产机器,完成后我将代码复制到该机器上。 Recently I modified this slightly to where I developed locally, checked the code into SVN and then updated the production machine through SVN.最近我在本地开发的地方稍微修改了一下,将代码签入SVN,然后通过SVN更新生产机。

Now I would like to start a new project and improve my workflow.现在我想开始一个新项目并改进我的工作流程。 Ideally I had the following in mind:理想情况下,我有以下想法:

  • Have one or more local dev environments拥有一个或多个本地开发环境
  • Develop and test on local machine(s)在本地机器上开发和测试
  • Use SVN (or Git) as code repository使用 SVN(或 Git)作为代码存储库
  • Use a build tool to set up new environments (either dev, staging or production) and deploy code使用构建工具设置新环境(开发、登台或生产)并部署代码

Since I am not very familiar with this process, I am looking for suggestions on how to best set this idea up and the tools to use, especially when it comes to the build tools.由于我对这个过程不是很熟悉,我正在寻找关于如何最好地设置这个想法和使用工具的建议,特别是在构建工具方面。 I was looking into Ant and Phing (possibly make), but I am so new to this that I would really like to get some guidance.我正在研究 Ant 和 Phing(可能是制造),但我对此很陌生,我真的很想得到一些指导。 Are there any good tutorials or books about PHP deployment, especially for beginners?有没有关于 PHP 部署的好教程或书籍,特别适合初学者? What I am especially interested in are the following topics:我特别感兴趣的是以下主题:

  • Deployment to different types of servers with different settings (eg dev uses different db, db passwords, PHP error reporting than production or staging).部署到具有不同设置的不同类型的服务器(例如,与生产或登台相比,开发人员使用不同的数据库、数据库密码、PHP 错误报告)。
  • Deployment that automatically pulls code from SVN.自动从 SVN 提取代码的部署。
  • Deployment that temporarily sets a "Maintenance" page for production environment.为生产环境临时设置“维护”页面的部署。
  • Once I mastered the above, maybe even do some testing in the build process.一旦我掌握了上述内容,甚至可以在构建过程中进行一些测试。

I know my question might sound quite confused... I admit, I am new to this and might be a little off the target in what I really need.我知道我的问题可能听起来很困惑......我承认,我对此并不陌生,并且可能有点偏离我真正需要的目标。 That's why any help is greatly appreciated.这就是为什么任何帮助都非常感谢。

I would suggest making your testing deployment strategy a production-ready install-script -- since you're going to need one of those anyway eventually.我建议将您的测试部署策略设置为生产就绪的安装脚本——因为您最终还是需要其中一个。

A few tips that may seem obvious to some, but are worth pointing out:一些对某些人来说似乎很明显但值得指出的技巧:

  • Your config file saved in your VCS should be a template, and should be named differently from the file that will eventually contain the actual settings.保存在 VCS 中的配置文件应该是一个模板,并且应该与最终包含实际设置的文件命名不同。 Eg config-dist.php or config-sample.conf or sample/config-mysql.php or something along those lines.例如config-dist.phpconfig-sample.confsample/config-mysql.php或类似的东西。 Otherwise you will end up accidentally checking in a server-specific configuration file over your template.否则,您最终会意外地通过模板签入特定于服务器的配置文件。
  • For PHP deployment, anticipate that some users will not be able to run server-side scripts through any mechanism other than the web server itself.对于 PHP 部署,预计某些用户将无法通过 web 服务器本身以外的任何机制运行服务器端脚本。 A PHP-based installer is almost non-negotiable.基于 PHP 的安装程序几乎是不可协商的。
  • You should include a consumer-friendly update mechanism, and for that, wordpress is a great example of a project to emulate.您应该包括一个消费者友好的更新机制,为此,wordpress 是一个很好的模拟项目示例。 A PHP script can (a) download the latest build, (b) use the ftp functions to update your application's files, and (c) execute an update script which makes the appropriate changes to the database, etc. PHP 脚本可以 (a) 下载最新版本,(b) 使用ftp函数更新应用程序的文件,以及 (c) 执行更新脚本,对数据库进行适当的更改等。
  • For heaven's sake don't do like [ redacted ] and make your users download and install separate patches for each point release.看在上帝的份上,不要像 [ redacted ] 那样让您的用户为每个点版本下载和安装单独的补丁。 Have them download the latest (final) release which contains all the updates to date, and applies the correct ALTER TABLE functions in sequence.让他们下载包含迄今为止所有更新的最新(最终)版本,并按顺序应用正确的ALTER TABLE功能。

Whether the files are deployed via SVN or through FTP, the install/update mechanism should be the same: get the latest files, run the update script.无论文件是通过 SVN 还是通过 FTP 部署,安装/更新机制应该是相同的:获取最新文件,运行更新脚本。 The updater uses the version listed in the PHP script and the version listed in the DB, and uses that knowledge to apply the appropriate DB patches in order.更新程序使用 PHP 脚本中列出的版本和 DB 中列出的版本,并使用该知识按顺序应用适当的 DB 补丁。 As for how to generate those patches, there are other questions here that you can refer to for more info.至于如何生成这些补丁,这里还有其他问题,您可以参考以获取更多信息。

As for the "Maintenance" page, just use the version trick mentioned above to trigger it (compare the version in the DB against the version in the PHP code).至于“维护”页面,只需使用上面提到的版本技巧来触发它(将 DB 中的版本与 PHP 代码中的版本进行比较)。 It's also useful to be able to mark a site as "down" to the public but make it visible to admins (like Joomla does), which you can trigger through database or filesystem flags.能够将站点标记为对公众“关闭”但使其对管理员可见(如 Joomla 所做的)也很有用,您可以通过数据库或文件系统标志触发它。

As for automatically pulling code from SVN, I'd say you're better off with either a cron script or with commit triggers than working that into your application, since it wouldn't be relevant to end users.至于自动从 SVN 中提取代码,我想说你最好使用 cron 脚本或提交触发器而不是在你的应用程序中使用它,因为它与最终用户无关。

This isn't exactly part of your question, but it's relevant:这不完全是您问题的一部分,但它是相关的:

If you go into distributing code intended for a wide audience, I would advise you to go with building and distributing OpenSSL-signed PHAR packages.如果您将 go 用于分发面向广大受众的代码,我建议您使用 go 构建和分发 OpenSSL 签名的 PHAR 包。 You can distribute them over HTTP without a problem, and because they're OpenSSL-signed, you're also mitigating the risk of man-in-the-middle attacks and protecting end-users/customers/clients from someone injecting code if you want to setup an automatic or one-click update.您可以毫无问题地将它们分发到 HTTP 上,并且由于它们是 OpenSSL 签名的,因此您还可以降低中间人攻击的风险,并保护最终用户/客户/客户免受有人注入代码的影响。想要设置自动或一键更新。

There's a set of tools I've contributed to in the past that work great for this, but you'll either need PHP 5.3, or you'll need PHP 5.2 with PHAR installed via PECL.我过去贡献了一组工具,它们对此非常有用,但您要么需要 PHP 5.3,要么需要 PHP 5.2 和通过 PECL 安装的 PHAR。 https://github.com/koto/phar-util https://github.com/koto/phar-util

As far as testing goes, PHPUnit is the de facto standard.就测试而言,PHPUnit 是事实上的标准。

If you are interested in using Git then you should check out this build system from CodeMeme.如果您对使用 Git 感兴趣,那么您应该从 CodeMeme 查看这个构建系统。 From what you described it sounds like it would be a good fit.从你描述的情况来看,这听起来很合适。 You can add it to any project as a submodule and with the included code you can tailor a build script that will deploy to different multiple servers in multiple environments.您可以将其作为子模块添加到任何项目中,并使用包含的代码定制构建脚本,该脚本将部署到多个环境中的不同多个服务器。 It uses Git to build the code for deployment but unfortunately SVN is not supported.它使用 Git 构建部署代码,但不幸的是 SVN 不受支持。

https://github.com/CodeMeme/Phingistrano https://github.com/CodeMeme/Phingistrano

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM