伺服器和部署
PHP 應用程式可以用各種方式進行部署。
Platform as a Service (PaaS)
PaaS 在 web 上提供了 PHP 應用程式所需的系統和網路架構。意即不需要提供設定檔發佈 PHP 應用程式或框架。
最近 PaaS 成為用以部署,以及擴展不同大小的 PHP 應用程式的一種流行方法。你可以在我們的 資源一覽 找到 PHP PaaS "Platform as a Service" 供應商 的列表。
虛擬或自架伺服器
如果你傾向自己管理系統,或是有興趣學習,虛擬或自己的伺服器環境可以讓你自己控制應用程式的上線環境。
nginx 和 PHP-FPM
PHP 透過內建的 FastCGI Process Manager( FPM ),可以和 nginx 搭配的很好( nginx 是個輕量,高效能的伺服器)。它比 Apache 消耗更少的記憶體,且可以處理更多並行請求。這在沒有太多記憶體空間可用虛擬機器裡尤其重要。
Apache 和 PHP
PHP 和 Apache 的搭配使用已經蠻久了, Apache 可以進行高度客製並且有很多模組 可以擴展功能。對於共用伺服器,或想簡單設定 PHP 框架,以及使用開源程式像 WordPress,它是個流行的選擇。不幸的是, Apache 先天上要比 nginx 耗費更多資源並且無法同時應付更多的使用者。
Apache 有很多設定方式可以執行 PHP 。最普遍且最簡單的設定方式是使用 prefork MPM 加上 mod_php5 。雖然它不是對記憶體最高效的方式,但它是最簡單使用的方式。如果你不想在伺服器系統管理方面太過深入,這可能是最好的選擇。注意如果你使用 mod_php5 ,你也必須使用 prefork MPM。
另外,如果你想讓 Apache 有更好的效能和更多的穩定性,那麼可以使用像是 nginx FPM 的 worker MPM 或是 event MPM 搭配 mod_fastcgi 或 mod_fcgid 。兩者在設定上對於記憶體使用更為高效並且更快,但是設定上需要花更多功夫。
共用伺服器
PHP 相當普遍,很少有伺服器沒有內建 PHP ,但是要確認它是最新的版本。 共用伺服器讓你可以讓你和其他開發者在同一台機器上部署網站。好處是這已經是一種便宜的方式,壞處是 伺服器的負載可能下降或是有安全性漏洞是主要考量。如果預算足夠,你應該要避開它。
建立及部署應用程式
如果你發現自己在(手動)上傳檔案前,需要手動處理資料庫 schema 改變,或是手動執行測試,先想一想!在更新版本時,每個額外的人工部署任務都會增加嚴重錯誤的可能性。當你進行簡單的更新時,或是建構網站,或是持續整合( continuous integration )策略, 自動化部署 會是你的好朋友。
你可能會想要自動化的任務有:
- 相依性管理
- 編譯,最小化 assets
- 執行測試
- 建立文件
- 打包
- 部署
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
-
Chef video tutorial series by Opscode, the makers of chef Further reading:
- Automate your project with Apache Ant
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: