如何在 Ubuntu 13 上使用 Capistrano 自动执行 PHP 应用程序部署过程

简介


现代网络应用程序开发在很大程度上依赖于框架。与以前不同的是,这些现成的库使得实际编程变得更加容易。它们提供了从身份验证到加密、从 cookie 和会话处理到文件上传的各种工具。

尽管 PHP 编程语言及其众多出色的框架很受欢迎,但仍有一些耗时的挑战,让开发人员无法尽情享受创建网站(或应用程序接口)的乐趣。

在这篇DigitalOcean文章中,继Capistrano自动化工具系列之后,我们将了解如何引入另一个小框架(或工具),这一次是帮助您将代码推送到服务器,而无需处理SFTP文件管理器--自动!

词汇表


卡皮斯特拉诺简介


2.获取 Ruby 解释器和 Capistrano


1.Ruby 解释器 2.卡皮斯特拉诺

3.准备部署服务器


1.创建部署用户和组 2.创建应用程序部署目录 3.设置 PHP 和 Nginx

4.为自动部署准备 PHP 应用程序


1.启动 Git 2.启动 Capistrano 3.配置 Capistrano 部署 4.使用 Capistrano 配置生产 5.部署到生产服务器

5.故障排除


注: 虽然我们将介绍如何下载和设置Capistrano的必要依赖项(如Ruby 2.1.0)以实现自动化部署过程,但本文假定您已经在Ubuntu 13云服务器上在线安装好了可正常运行的网站,并准备好了您的_deployment_ droplet。

卡皮斯特拉诺简介


Capistrano 是一款基于 Ruby 编程语言的开源服务器(或部署)管理工具。使用 Capistrano,可以在虚拟服务器上执行任意功能和程序,而无需直接干预,只需让 Capistrano 执行包含所有指令的脚本(即配方)即可。从一般意义上讲,该工具可被视为开发人员自己的部署助手,从在远程机器上获取代码到启动整个上线过程,几乎无所不能。

Capistrano 3 最初是为帮助 Rails 框架部署而编写的,但在其最新版本中,Capistrano 3 现在几乎可以用于(和用于)包括 PHP 在内的任何东西。

注: 如果您想了解有关 Capistrano 和 Ruby 的更多信息,请查看我们的相关文章:如何使用 Capistrano 自动部署:入门

获取 Ruby 解释器和 Capistrano


Ruby 解释器


要运行 Capistrano,您需要在 PHP 开发机器上安装最新的 Ruby 解释器。下面的说明解释了如何在 Ubuntu VPS 上运行 Ruby,实际上是我们详细教程的快速总结:准备运行 Ruby 2.1.0 的 Ubuntu 13 服务器

 1# Update the software sources list
 2# And upgrade the dated applications:
 3
 4aptitude update
 5aptitude -y upgrade
 6
 7# Download and install the build-essential package:
 8aptitude install -y build-essential
 9
10# And some additional, commonly used tools:
11aptitude install -y cvs subversion git-core libyaml-dev mercurial
12
13# Get the Ruby Version Manager
14curl -L get.rvm.io | bash -s stable
15
16# And to create a system environment with RVM:
17source /etc/profile.d/rvm.sh
18
19# Download and install Ruby using RVM:
20rvm reload
21rvm install 2.1.0

Capistrano


一旦安装了 Ruby,就可以使用默认的 Ruby 软件包管理器 RubyGems 设置 Capistrano。

运行以下命令,使用 gem 下载并安装 Capistrano 3:

1gem install capistrano --no-ri --no-rdoc

准备部署服务器


作为一款成熟的自动化工具,Capistrano 在构建时充分考虑了稳定性和安全性。要使用它来部署 PHP 网络应用程序,我们首先需要在部署服务器上进行一些工作,例如创建一个用户组供 Capistrano 用来连接服务器。

创建部署用户和组


添加新用户组:

1# Usage: sudo addgroup [group name]
2sudo addgroup www

创建一个新用户并将其添加到该组:

 1# Create a new user:
 2# Usage: sudo adducer [user name]
 3sudo adduser deployer
 4
 5# Follow on-screen instructions to user-related
 6# information such as the desired password.
 7
 8# Add the user to an already existing group:
 9# Usage: sudo adducer [user name] [group name]
10sudo adduser deployer www

使用文本编辑器 "nano "编辑"/etc/sudoers",让用户 "deployer "在今后的部署中使用 "sudo":

1nano /etc/sudoers

向下滚动文件,找到定义 root 的位置:

1..
2
3# User privilege specification
4root ALL=(ALL:ALL) ALL
5
6..

root ALL=(ALL) ALL 后面添加以下内容:

1deployer ALL=(ALL:ALL) ALL

现在,/etc/sudoers文件的这一部分应该是这样的:

1..
2
3# User privilege specification
4root ALL=(ALL:ALL) ALL
5deployer ALL=(ALL:ALL) ALL
6
7..

按 CTRL+X 并用 Y 确认保存并退出。

注: 要了解有关SSH和sudo的更多信息,请查看DigitalOcean社区有关Linux基础知识的文章。

创建应用程序部署目录


在部署服务器上,我们还需要定义并创建 PHP 代码库所在的目录,以便网络服务器运行应用程序。

/var 中创建 www 网络应用目录:

1sudo mkdir /var/www

并设置网络服务器(即 Nginx)的访问权限:

1# Set the ownership of the folder to members of `www` group
2sudo chown -R :www  /var/www
3
4# Set folder permissions recursively
5sudo chmod -R g+rwX /var/www
6
7# Ensure permissions will affect future sub-directories etc.
8sudo chmod g+s      /var/www

设置 PHP 和 Nginx


Capistrano 的职责是自动部署。我们仍然需要设置 PHP 和 NGinx(或任何其他网络服务器和解释器组合),以运行我们的网络应用程序。

为了让部署服务器为运行 PHP 网络应用程序做好充分准备,请查看以下文章:

Nginx、PHP 和 MySQL:

如何安装 Linux、nginx、MySQL、PHP (LEMP) 堆栈

phpMyAdmin:

如何在 LEMP 服务器上安装 phpMyAdmin

为自动部署准备 PHP 应用程序


在开发服务器上安装 Ruby 和 Capistrano,并在部署机器上添加部署用户后,我们就可以了解如何 "启动 "Capistrano,开始使用该工具了。

注意: 在本节中,我们假设你的网络应用程序源代码位于 /home/developer1/my_app目录下。以下命令需要在_________________________________中执行。

1# cd /path/to/your/app/on/dev/server
2cd /home/developer1/my_app

启动 Git


Git 是开发人员常用的源代码管理系统和重审工具。Capistrano 通过 Git 资源库控制和管理应用程序的生命周期和部署流程。

在本节中,我们将创建一个可集中访问的 Git 仓库,启动 Git 并将项目上传到该仓库,供 Capistrano 在部署时使用。

注: 要学习本节内容,你需要一个 Github 账户和一个已创建的空仓库。

在应用程序源代码所在的目录(例如 my_app)中执行以下不言自明的命令来启动版本库:

 1# !! These commands are to be executed on
 2#    your development machine, from where you will
 3#    deploy to your server.
 4#    Instructions might vary slightly depending on
 5#    your choice of operating system.
 6
 7# Initiate the repository
 8git init
 9
10# Add all the files to the repository
11git add .
12
13# Commit the changes
14git commit -m "first commit"
15
16# Add your Github repository link 
17# Example: git remote add origin [email protected]:[user name]/[proj. name].git
18git remote add origin git@github.com:user123/my_app.git
19
20# Create an RSA/SSH key
21# Follow the on-screen instructions
22ssh-keygen -t rsa
23
24# View the contents of the key and add it to your Github
25# by copy-and-pasting from the current remote session by
26# visiting: https://github.com/settings/ssh
27# To learn more about the process,
28# visit: https://help.github.com/articles/generating-ssh-keys
29cat /root/.ssh/id_rsa.pub
30
31# Set your Github information
32# Username:
33# Usage: git config --global user.name "[your username]"
34# Email:
35# Usage: git config --global user.email "[your email]"
36git config --global user.name  "user123"    
37git config --global user.email "[email protected]"
38
39# Push the project's source code to your Github account
40git push -u origin master

注: 若想了解更多关于Git的使用方法,请查看DigitalOcean社区页面上的如何有效使用Git教程。

启动卡皮斯特拉诺


在这一步中,我们将让 Capistrano 自动在项目目录中构建配置和部署文件。

运行以下程序启动(即_install_)Capistrano 文件:

1cap install
2
3# mkdir -p config/deploy
4# create config/deploy.rb
5# create config/deploy/staging.rb
6# create config/deploy/production.rb
7# mkdir -p lib/capistrano/tasks
8# Capified

配置 Capistrano 部署


文件 config/deploy.rb 包含与部署服务器相关的参数和设置。在这里,我们将告诉 Capistrano 要连接和部署到哪个(些)服务器。

运行以下程序,使用 nano 文本编辑器编辑文件:

1nano config/deploy.rb

添加以下代码块,并根据自己的设置进行修改:

 1# !! When editing the file (or defining the configurations),
 2#    you can either comment them out or add the new lines.
 3#    Make sure to **not** to have some example settings
 4#    overriding the ones you are appending.
 5
 6# Define the name of the application
 7set :application, 'my_app'
 8
 9# Define where can Capistrano access the source repository
10# set :repo_url, 'https://github.com/[user name]/[application name].git'
11set :scm, :git
12set :repo_url, 'https://github.com/user123/my_app.git'
13
14# Define where to put your application code
15set :deploy_to, "/var/www/my_app"
16
17set :pty, true
18
19set :format, :pretty
20
21# Set your post-deployment settings.
22# For example, you can restart your Nginx process
23# similar to the below example.
24# To learn more about how to work with Capistrano tasks
25# check out the official Capistrano documentation at:
26# http://capistranorb.com/
27
28# namespace :deploy do
29#   desc 'Restart application'
30#   task :restart do
31#     on roles(:app), in: :sequence, wait: 5 do
32#       # Your restart mechanism here, for example:
33#       sudo "service nginx restart"
34#     end
35#   end
36# end

按 CTRL+X 并用 Y 确认保存并退出。

使用 Capistrano 配置生产


注意:config/deploy.rb 类似,您需要对 config/production.rb 文件进行一些修改。最好是修改代码,而不是添加下面的代码块。

使用 nano 文本编辑器运行以下程序编辑文件:

1nano config/deploy/production.rb

输入服务器设置,如下所示:

 1# Define roles, user and IP address of deployment server
 2# role :name, %{[user]@[IP adde.]}
 3role :app, %w{[email protected]}
 4
 5# Define server(s)
 6# Example:
 7# server '[your droplet's IP addr]', user: '[the deployer user]', roles: %w{[role names as defined above]}
 8# server '162.243.74.190', user: 'deployer', roles: %w{app}
 9server '162.243.74.190', user: 'deployer', roles: %w{app}
10
11# SSH Options
12# See the example commented out section in the file
13# for more options.
14set :ssh_options, {
15    forward_agent: false,
16    auth_methods: %w(password),
17    password: 'user_deployers_password',
18    user: 'deployer',
19}

按 CTRL+X 并用 Y 确认保存并退出。

部署到生产服务器


完成设置后,就可以进行部署了。

在开发机器上运行以下代码,将其部署到生产服务器上。根据上述文件的定义,Capistrano 将

  • 连接部署服务器
  • 下载应用程序源代码
  • 执行部署操作

完成所有设置后,您就可以运行以下命令,让 Capistrano 将应用程序源代码从开发服务器部署到部署机器上:

1cap production deploy

就是这样!现在,您可以在线查看 Capistrano 代码,并跟踪您的最新代码库。

疑难解答


使用 Capistrano 并不总是像看起来那么简单。不幸的是,该工具喜欢抱怨而不是指导,而且现阶段的文档也有点有限。

为使一切工作顺利进行,请尽量

  • 匹配目录和存储库名称。
  • 正确输入所有内容。
  • 确保开发和部署服务器包含所有必要工具(如 sqlite3、库等)。
  • 确保在让 Capistrano 执行之前手动测试所有操作和执行。
  • 考虑按照 Capistrano 官方文档实施更安全的身份验证方法。

要进一步了解 Capistrano 及其功能,请阅读 Capistrano 文档

Submitted by: O.S. Tezer
Published At
Categories with 技术
comments powered by Disqus