<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Clayton Lengel-Zigich&#187; PHP</title>
	<atom:link href="http://www.claytonlz.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.claytonlz.com</link>
	<description>Agile Practitioner, CSP, CSPO, CSM in Phoenix, AZ</description>
	<lastBuildDate>Thu, 26 Jan 2012 06:25:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP Deployment With Capistrano</title>
		<link>http://www.claytonlz.com/2008/08/php-deployment-with-capistrano/</link>
		<comments>http://www.claytonlz.com/2008/08/php-deployment-with-capistrano/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 06:41:38 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[deployment]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=54</guid>
		<description><![CDATA[If you've ever used "Capistrano":http://www.capify.org/ to deploy your Rails application you know how simple the process is. With a little configuration you have a respectable deployment strategy that integrates nicely with your source control system and deployment server. If you're stuck writing your apps in PHP you might think that you are restricted to using rsync, scp or plain old FTP. Fear not! Getting Capistrano deploying your PHP apps is a lot easier than you might think.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever used <a href="http://www.capify.org/">Capistrano</a> to deploy your Rails application you know how simple the process is. With a little configuration you have a respectable deployment strategy that integrates nicely with your source control system and deployment server. If you&#8217;re stuck writing your apps in <span class="caps">PHP </span>you might think that you are restricted to using rsync, scp or plain old <span class="caps">FTP.</span> Fear not! Getting Capistrano deploying your <span class="caps">PHP </span>apps is a lot easier than you might think.</p>

<h3>Assumptions &amp; Requirements</h3>

<p>There are assumptions that I will be making throughout this article, they include:</p>


<ul>
<li>You are using OS X/Linux</li>
<li>You are comfortable using the command line</li>
<li>The code for your <span class="caps">PHP </span>application resides in some type of source control system</li>
<li>The deployment server is running some type of &#042;nix</li>
<li>The deployment server is using a <a href="http://en.wikipedia.org/wiki/LAMP_software_bundle"><span class="caps">LAMP</span></a> setup</li>
<li>You have Ruby and Ruby Gems installed on your system</li>
</ul>



<h3>Installing Capistrano</h3>

<p>I&#8217;m using the latest (as of this post) version which is 2.4.3, it&#8217;s a typical gem install with:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">	gem <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #660033;">-y</span> Capistrano
	...
	Successfully installed Capistrano-2.4.3
	<span style="color: #000000;">1</span> gem installed
	Installing ri documentation <span style="color: #000000; font-weight: bold;">for</span> Capistrano-2.4.3...
	Installing RDoc documentation <span style="color: #000000; font-weight: bold;">for</span> Capistrano-2.4.3...</pre></div></div>




<p>Once you&#8217;ve got Capistrano installed we can move on to configuring our application.</p>

<h3>Application Configuration</h3>

<p>For this example I will be deploying a simple Hello World Application that I wrote in <span class="caps">PHP.</span> The code is stored in my subversion repository and I am deploying to my <a href="https://manage.slicehost.com/customers/new?referrer=454916202">SliceHost Slice</a> running Ubuntu Linux.</p>

<h4>Step 1 &ndash; Create <tt>config</tt> Directory and Capify</h4>

<p>Capistrano places its configuration file, called a recipe, in a directory named <tt>config</tt>. Since my application doesn&#8217;t have a config directory, I will create one.</p>



<pre>clayton$ mkdir config</pre>



<p>Next we will &ldquo;Capify&rdquo; the application. This creates the necessary default files.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">	clayton$ capify .
	<span style="color: #7a0874; font-weight: bold;">&#91;</span>add<span style="color: #7a0874; font-weight: bold;">&#93;</span> writing <span style="color: #000000; font-weight: bold;">`</span>.<span style="color: #000000; font-weight: bold;">/</span>Capfile<span style="color: #ff0000;">'
	[add] writing `./config/deploy.rb'</span>
	<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">done</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> capified<span style="color: #000000; font-weight: bold;">!</span></pre></div></div>




<p>We&#8217;ve got our default recipe in place and our application has been capified!</p>

<h4>Step 2 &ndash; Modifying The Default Recipe (Configuration)</h4>

<p>The default recipe is pretty simple and well(enough) documented. However, we do need to make some changes.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">	<span style="color:#008000; font-style:italic;"># Application Naming</span>
	<span style="color:#008000; font-style:italic;">#</span>
	set <span style="color:#ff3333; font-weight:bold;">:application</span>, <span style="color:#996600;">&quot;set your application name here&quot;</span>
	set <span style="color:#ff3333; font-weight:bold;">:repository</span>,  <span style="color:#996600;">&quot;set your repository location here&quot;</span></pre></div></div>




<p>*<tt>:application</tt><strong>: This is the name of the directory that will be created to hold the files related to your application. I will name mine &#8220;app&#8221;. It&#8217;s important to note that this probably *should not</strong> be your website&#8217;s <tt>DocumentRoot</tt> such as <tt>httpdocs</tt> or <tt>www</tt>.</p>

<p>*<tt>:repository</tt>*: This is the location of your source control repository. I&#8217;m using subversion so I&#8217;ll enter the <span class="caps">URL </span>for my repository.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">	<span style="color:#008000; font-style:italic;"># Application Deployment Location</span>
	<span style="color:#008000; font-style:italic;">#</span>
	<span style="color:#008000; font-style:italic;"># If you aren't deploying to /u/apps/#{application} on the target</span>
	<span style="color:#008000; font-style:italic;"># servers (which is the default), you can specify the actual location</span>
	<span style="color:#008000; font-style:italic;"># via the :deploy_to variable:</span>
	set <span style="color:#ff3333; font-weight:bold;">:deploy_to</span>, <span style="color:#996600;">&quot;/var/www/vhosts/hellowworld.tld/#{application}&quot;</span>
	set <span style="color:#ff3333; font-weight:bold;">:document_root</span>, <span style="color:#996600;">&quot;/var/www/vhosts/hellowworld.tld/httpdocs/current&quot;</span></pre></div></div>




<p>This bit of commented code is important. Chances are this is not how your server is configured and you will need to change the <tt>:deploy_to</tt> variable. You should set this to something reasonable, on my SliceHost server all of my sites are stored in <tt>/var/www/vhosts/_domain_/</tt> so I&#8217;ll set my <tt>:deploy_to</tt> to something like <tt>/var/www/vhosts/hellowworld.tld</tt>.</p>

<p>*<tt>:document_root</tt>*: This isn&#8217;t a built in Capistrano variable, but I like to use to so that I can reference the <tt>DocumentRoot</tt> from my Apache VirtualHost later on in the deployment process.</p>

<p><strong>Note:</strong> The previously set <tt>:application</tt> variable is appended to the end of your <tt>:deploy_to</tt> path so don&#8217;t put your application name in the <tt>:deploy_to</tt> variable.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">	<span style="color:#008000; font-style:italic;"># Source Control Settings</span>
	<span style="color:#008000; font-style:italic;">#</span>
	set <span style="color:#ff3333; font-weight:bold;">:scm_username</span>, <span style="color:#996600;">&quot;cap&quot;</span> <span style="color:#008000; font-style:italic;">#if http</span>
	set <span style="color:#ff3333; font-weight:bold;">:scm_password</span>, <span style="color:#996600;">&quot;fHw1n71d&quot;</span> <span style="color:#008000; font-style:italic;">#if http</span>
	set <span style="color:#ff3333; font-weight:bold;">:scm_checkout</span>, <span style="color:#996600;">&quot;export&quot;</span></pre></div></div>




<p>If you&#8217;re using http to access your subversion repository you&#8217;ll need to add the username and password for a user who has httpauth access.</p>

<p>*<tt>:scm_username</tt>*: Provide the username of a user who has access to your subversion repository.</p>

<p>*<tt>:scm_password</tt>*: Provide the above user&#8217;s e-mail address.</p>

<p>*<tt>:scm_checkout</tt>*: Tell Capistrano what type of checkout to preform, we&#8217;ll use export.</p>


<p>Capistrano uses <span class="caps">SSH </span>to access your deployment server and run the commands required to deploy your application. You&#8217;ll need to tell Capistrano how to access your server.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">	<span style="color:#008000; font-style:italic;"># SSH Settings</span>
	<span style="color:#008000; font-style:italic;">#</span>
	set <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#996600;">&quot;clz&quot;</span>
	set <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#996600;">&quot;password&quot;</span>
	set <span style="color:#ff3333; font-weight:bold;">:use_sudo</span>, <span style="color:#0000FF; font-weight:bold;">false</span>
	set <span style="color:#ff3333; font-weight:bold;">:ssh_options</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:forward_agent <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>




<p>*<tt>:user</tt>*: Set this to a user who has access to the <tt>:deploy_to</tt> directory.</p>

<p>*<tt>:password</tt>*: The above user&#8217;s password</p>

<p>*<tt>:use_sudo</tt>*: If the above user has sudo priviledges you can set this to true. I do not use it because of the way my server is configured. You might need to.</p>

<p>*<tt>:ssh_options</tt>*: This variable accepts a ruby hash, for valid options see <a href="http://github.com/jamis/net-ssh/tree/master/lib/net/ssh.rb">Net-SSH</a>. I&#8217;m using agent forwarding because I want to use my <span class="caps">SSH </span>key and don&#8217;t want to be prompted for a password. For more on agent forwarding see UnixWiz&#8217;s <a href="http://www.unixwiz.net/techtips/ssh-agent-forwarding.html">Illustrated Guide to <span class="caps">SSH</span> Agent Forwarding</a>.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">	<span style="color:#008000; font-style:italic;"># Server Roles</span>
	<span style="color:#008000; font-style:italic;">#</span>
	role <span style="color:#ff3333; font-weight:bold;">:app</span>, <span style="color:#996600;">&quot;your app-server here&quot;</span>
	role <span style="color:#ff3333; font-weight:bold;">:web</span>, <span style="color:#996600;">&quot;your web-server here&quot;</span>
	role <span style="color:#ff3333; font-weight:bold;">:db</span>,  <span style="color:#996600;">&quot;your db-server here&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:primary</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>




<p>When deploying a Rails application it&#8217;s possible that you&#8217;ll have more than one application server and a database on another server, but with <span class="caps">PHP </span>chances are you&#8217;re running a <span class="caps">LAMP </span>setup and have Apache, <span class="caps">PHP </span>and MySQL all side-by-side. In my case I&#8217;m going to set all of these values to my server&#8217;s IP address.</p>

<h4>Step 3 &ndash; Configuring For a <span class="caps">PHP</span> Deployment</h4>

<p>If you were to try and deploy your application right now you&#8217;d run into a number of errors. Remember that Capistrano assumes you&#8217;re deploying a Rails application and because of that it will try and do some things that you don&#8217;t want it to do, restarting servers, creating Rails specific symbolic links etc. To get around these assumptions we will need to bypass or modify some of the existing default tasks.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">	namespace <span style="color:#ff3333; font-weight:bold;">:deploy</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
		task <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#9966CC; font-weight:bold;">do</span>
			transaction <span style="color:#9966CC; font-weight:bold;">do</span>
				update_code
				symlink
			<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
		task <span style="color:#ff3333; font-weight:bold;">:finalize_update</span> <span style="color:#9966CC; font-weight:bold;">do</span>
			transaction <span style="color:#9966CC; font-weight:bold;">do</span>
				run <span style="color:#996600;">&quot;chmod -R g+w #{releases_path}/#{release_name}&quot;</span>
			<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
		task <span style="color:#ff3333; font-weight:bold;">:symlink</span> <span style="color:#9966CC; font-weight:bold;">do</span>
			transaction <span style="color:#9966CC; font-weight:bold;">do</span>
				run <span style="color:#996600;">&quot;ln -nfs #{current_release} #{deploy_to}/#{current_dir}&quot;</span>
				run <span style="color:#996600;">&quot;ln -nfs #{deploy_to}/#{current_dir} #{document_root}&quot;</span>
			<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
		task <span style="color:#ff3333; font-weight:bold;">:migrate</span> <span style="color:#9966CC; font-weight:bold;">do</span>
			<span style="color:#008000; font-style:italic;"># nothing</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
		task <span style="color:#ff3333; font-weight:bold;">:restart</span> <span style="color:#9966CC; font-weight:bold;">do</span>
			<span style="color:#008000; font-style:italic;"># nothing</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p><strong>Update Task</strong>: This is a built in task in the deploy namespace. This is setup to do a few things, among them checkout your code from the repository and restart your server. However, we don&#8217;t need the server restart so we&#8217;ll override it and have it update the code and create symbolic links</p>

<p><strong>Finalize Update</strong>: This was a step that I kept getting stuck on when first trying this out. I did not have this task in my deployment namespace and my file permissions were all goof&#8217;ed up. I found this via the <a href="http://github.com/jamis/Capistrano/tree/master/lib/Capistrano/recipes/deploy.rb">Capistrano GitHub</a> recipe.rb page.</p>

<p><strong>Symlink</strong>: Since we&#8217;re not deploying directly to our site&#8217;s <tt>DocumentRoot</tt> we will need to tell Capistrano to create some symbolic links.  When Capistrano updates the code, it checks out the lastest version of the code from your source control system and sticks it into a directory under <tt><em>deploy_to</em>/releases</tt> inside of a directory named with the current timestamp. Usually Capistrano will create the a symbolic link named <tt>current</tt> that is pointing to the most recent release in <tt><em>deploy_to</em>/releases</tt>, however, we&#8217;ll need to create that on our own.</p>

<p>*<tt>#{current_release}</tt>*: This is a default Capistrano variable for the most recent directory in <tt><em>deploy_to</em>/releases</tt>.</p>

<p>*<tt>#{current_dir}</tt>*: This is a default Capistrano variable for the name of the &ldquo;current&rdquo; directory, by default it is <em>current</em>.</p>

<p><strong>Migrate</strong>: Migrations are a Rails only thing so we&#8217;ll skip them and tell Capistrano to do nothing.</p>

<p><strong>Restart</strong>: Capistrano will try to restart your Rails process or mongrels etc. Needless to say, with <span class="caps">PHP </span>this isn&#8217;t required. Technically you could throw a call to <tt>apache2ctl</tt> if you needed to.</p>

<p><strong>Transactions</strong>: Recent versions of Capistrano allow you to specify transactions. If you encounter any errors while inside of a transaction all of the changes will be rolled back and your application won&#8217;t sit there on the internet broken.</p>

<h4>Step 4 &ndash; The Initial &ldquo;Setup&rdquo; Deploy</h4>

<p>The first step of deploying your <span class="caps">PHP </span>application with Capistrano is to run the setup task. This will create some directories on your deployment server in the correct location.</p>



<pre>
	clayton$ cap deploy:setup
</pre>



<p>Here&#8217;s what I got on my deployment server:</p>


<pre>
	/var/www/vhosts/helloworld.tld/app/shared
		/var/www/vhosts/helloworld.tld/app/shared/log
		/var/www/vhosts/helloworld.tld/app/shared/tmp
		/var/www/vhosts/helloworld.tld/app/shared/pid
	/var/www/vhosts/helloworld.tld/app/releases
</pre>



<p>You can get rid of the <tt>log</tt>, <tt>tmp</tt> and <tt>pid</tt> directories. Those are Rails specific and we won&#8217;t be using them.</p>

<p>Your application is now all ready to be deployed by Capistrano. You&#8217;ve even got a place to store some of your non-code related things like images and other files.</p>

<h4>Step 5 &ndash; Making Use of Capistrano&#8217;s Shared Directories</h4>

<p>My Hello World application has some images that are displayed on the page. However, I&#8217;m not storing my images in my source control system and thus, they are not automatically uploaded when I deploy my app with Capistrano. Luckily we can make use of Capistrano&#8217;s ability to run various commands on the deployment server to create some symbolic links to our images.</p>

<p>First, upload your images to a directory (call it whatever you want) under <tt><em>deploy_to</em>/_application_/shared/</tt>. Mine is located at <tt>/var/www/vhosts/helloworld.tld/app/shared/images</tt>. Now we can update our recipe to create these symbolic links.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
	<span style="color:#008000; font-style:italic;"># namespace :deploy... is up above</span>
&nbsp;
	task <span style="color:#ff3333; font-weight:bold;">:after_symlink</span> <span style="color:#9966CC; font-weight:bold;">do</span>
		transaction <span style="color:#9966CC; font-weight:bold;">do</span>
			run <span style="color:#996600;">&quot;ln -nsf #{shared_path}/images #{document_root}/images&quot;</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p><strong>After Symlink</strong>: We want Capistrano to create our symbolic links <em>after</em> it has already linked up the document root and current release directories. Using <tt>run</tt> we tell Capistrano to execute the command on the deployment server. In this case we want to create a symbolic link in our document root that points to the shared images path.</p>

<p>*<tt>:shared_path_</tt>*: This is the default location and name of the &ldquo;shared&rdquo; directory created by our previous setup command.</p>

<p>By using symbolic links to link up our static non-code-related images, we avoid having to re-upload the images each time we deploy or worry about accidentally wiping out any files.</p>

<h4>Step 6 &ndash; Deploying our <span class="caps">PHP</span> Application For the First Time</h4>

<p>Typically with a Rails application you would run the following to deploy your application for the first time.</p>



<pre>
	clayton$ cap deploy:cold
</pre>



<p>This would checkout the code, rune the migrations and start your server. Since we don&#8217;t need those last two steps, we can just skip deploying &ldquo;cold&rdquo; and use the regular old deploy task.</p>



<pre>
	clayton$ cap deploy
</pre>



<p>At this point you&#8217;ll see a bunch of information running across your terminal window. If there are any errors you will see an error message from capistrano about a failed command on the deployment server or bad password. If everything went well your application will now be deployed on your server!</p>

<h3>Future Deployments</h3>

<p>Now that I&#8217;ve made some changes to my HelloWorld application I&#8217;m ready to deploy again. Capistrano makes this very easy, as long as I follow a simple process.</p>


<ol>
<li>Commit my changes to my source control system</li>
<li>Upload any new static content (images, files etc.) to the shared directory</li>
<li>Run <tt>cap deploy</tt> from my command line</li>
</ol>



<p>You&#8217;ll again see the progress of Capistrano deploying the updated application run accross your terminal window and your new code will be deployed.</p>

<h3>There&#8217;s A Lot to Learn About Capistrano</h3>

<p>Capistrano is a great application, one of the best things about it is its simlicity and easy of use. However, it can also be pretty complex when you start digging into how it&#8217;s working behind the scenes. With that said, I encourage you to do some digging of your own and learn more about the default variables and tasks. There is probably quite a bit I could do above and beyond the defaults to make this <span class="caps">PHP </span>deployment strategy much better, but for now it works and my <span class="caps">PHP </span>deployments are easier than ever.</p>

<p>To learn more about Capistrano&#8217;s deployment recipes read the <a href="http://github.com/jamis/capistrano/tree/master/lib/capistrano/recipes/deploy.rb">code and comments</a>.</p>

<h3>Final Deploy Recipe</h3>

<p><a href="http://pastie.org/261509">View the full example deployment recipe</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/2008/08/php-deployment-with-capistrano/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

