部署PHP应用到线上Web服务器的方式有很多种。

平台即服务(PaaS)

PaaS提供运行PHP Web应用所需的系统和网络环境,对PHP应用和框架只需要做少量的配置即可。

现在PaaS已经成为部署、托管和扩展各种规模的PHP应用的流行方式,可以在 resources部分查看PHP PaaS “平台即服务”供应商列表。

虚拟或独立主机

如果你愿意或想学习系统管理,那么虚拟或独立主机可以让你完全控制自己的运行环境。

nginx和PHP-FPM

PHP通过内置的FastCGI进程管理器(FPM),可以非常高效地和轻量级的高性能Web服务器nginx进行通信。 nginx比Apache消耗更少的内存,能更好的处理并发请求,这在内存限制较多的虚拟主机环境中尤为重要。

  • 阅读更多nginx
  • 阅读更多PHP-FPM
  • 学习如何配置安全的nginx和PHP-FPM

Apache和PHP

PHP和Apache是一个老搭档,历史悠久。Apache有很强的可配置性和大量的扩展模块, 是共享主机中常见的Web服务器,完美支持各种PHP框架和开源应用(如WordPress)。可惜的是,默认情况下,Apache比nginx更耗资源,并发处理能力不强。

Apache有多种方式运行PHP,最常见简单的方式是使用mod_php5的prefork MPM方式, 缺点是它对内存的利用效率不高,如果你不想深入学习服务器的管理,那么这种最简单的方式就是你的最佳选择了。注意,如果你使用mod_php5,最好使用 prefork MPM方式。

如果你想追求高性能和高稳定性,那么也可以为Apache选择与nginx类似的FPM系统worker MPM或 event MPM,它们分别使用mod_fastcgi和mod_fcgid模块。FPM方式可以更高效的利用内存,运行 速度更快,但是配置也相对复杂一些。

  • 阅读更多Apache
  • 深入学习多进程模块
  • 阅读更多mod_fastcgi
  • 阅读更多mod_fcgid

共享主机

PHP非常流行,很少有服务器没有安装PHP的,因而有很多共享主机,不过需要注意服务器上的PHP是否是最新稳定 版本。共享主机允许多个开发者把自己的网站部署在上面,这样的好处是费用非常便宜,坏处是你不知道将和哪些 网站共享主机,因此需要仔细考虑机器负载和安全问题。如果项目预算允许的话,避免使用共享主机是上策。

Building and Deploying your Application

If you find yourself doing manual database schema changes or running your tests manually before updating your files (manually), think twice! With every additional manual task needed to deploy a new version of your app, the chances for potentially fatal mistakes increase. Whether you’re dealing with a simple update, a comprehensive build process or even a continuous integration strategy, build automation is your friend.

Among the tasks you might want to automate are:

  • Dependency management
  • Compilation, minification of your assets
  • Running tests
  • Creation of documentation
  • Packaging
  • Deployment

Build Automation Tools

Build tools can be described as a collection of scripts that handle common tasks of software deployment. The build tool is not a part of your software, it acts on your software from ‘outside’.

There are many open source tools available to help you with build automation, some are written in PHP others aren’t. This shouldn’t hold you back from using them, if they’re better suited for the specific job. Here are a few examples:

Phing is the easiest way to get started with automated deployment in the PHP world. With Phing you can control your packaging, deployment or testing process from within a simple XML build file. Phing (which is based on Apache Ant) provides a rich set of tasks usually needed to install or update a web app and can be extended with additional custom tasks, written in PHP.

Capistrano is a system for intermediate-to-advanced programmers to execute commands in a structured, repeatable way on one or more remote machines. It is pre-configured for deploying Ruby on Rails applications, however people are successfully deploying PHP systems with it. Successful use of Capistrano depends on a working knowledge of Ruby and Rake.

Dave Gardner’s blog post PHP Deployment with Capistrano is a good starting point for PHP developers interested in Capistrano.

Chef is more than a deployment framework, it is a very powerful Ruby based system integration framework that doesn’t just deploy your app but can build your whole server environment or virtual boxes.

Chef resources for PHP developers:

  • Three part blog series about deploying a LAMP application with Chef, Vagrant, and EC2
  • Chef Cookbook which installs and configures PHP 5.3 and the PEAR package management system

Further reading:

  • Automate your project with Apache Ant
  • Maven, a build framework based on Ant and how to use it with PHP

Continuous Integration

Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily — leading to multiple integrations per day. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.

– Martin Fowler

There are different ways to implement continuous integration for PHP. Recently Travis CI has done a great job of making continuous integration a reality even for small projects. Travis CI is a hosted continuous integration service for the open source community. It is integrated with GitHub and offers first class support for many languages including PHP.

Further reading:

  • Continuous Integration with Jenkins
  • Continuous Integration with Teamcity