Deploying with Git

The plan

So I want to deploy my PHP app from my local development machine to a staging test server and then to a production server, without accidentally missing some files or database changes. My PHP app happens to be a Laravel app so I don’t need to worry about database changes too much since php artisan migrate pretty much takes care of that for me. So the first question is …

How to move the files?

All of the files. Every time. Reliably. The answer I’m going with is Git. I have my repo on BitBucket (though GitHub would do just as well, if your project is open source or you’ve paid for a private repo) and every server I want to use has the Git client installed so I just made a new ~/projects directory on each server and cloned my project into there.

How can Apache find the files?

The next issue is that I want the public directory to be visible to the world but the rest of the app’s PHP files to remain completely hidden. And (for my production server at least) I’m using a commercial shared hosting provider and I don’t have access to point Apache to serve a subdirectory of my project. Apache will serve the directory that my provider say it will serve and I can’t change that. (Which is fair enough, really.) So, I have symbolic links in my /var/www directory pointing to index.php and my js and images directories, and everything else in the top level of the public directory. That way I can have the actual files in my home directory but Apache will still find and serve what it needs to serve. So here goes.

One-time setup

When setting up a new server, once it’s got git installed, firstly I make a symbolic link html in my home directory that links to the Apache web directory for convenience – eg if your web root directory is /var/www then you might type ln -s /var/www ~/html. If you’re hosting with (referral link), this step is already done for you.

And if you’re using you might want to add alias php='php-5.6' to your ~/.bash_profile because just running php by itself will give you PHP 5.3 by default.

Then I do mkdir ~/projects and I install composer.phar in ~/projects (see this page).

Next, still in ~/projects, I clone my repo from BitBucket (or GitHub or wherever). You might need to set up a public key first, if it’s a private repo in Bitbucket.

Then I setup the MySQL database (assuming you need one – I usually do).

Lastly, I go in to ~/projects/my_project (or whatever your project is) and copy .env.example to .env.

I then run php ../composer.phar install or if that gives the wrong PHP version then php-7.0 ../composer install so that everything is installed…

…followed by php artisan key:generate to generate a key,

…and then manually set up all the other settings in .env.

…and then I do the first deployment.

Each Deployment

  1. I cd ~/projects/my_project then do
  2. git fetch and then
  3. git checkout release/0.1.0 (or whatever your release branch is).
  4. Then I do php ../composer.phar install (we put composer.phar in ~/projects, remember).
  5. Then, in the main project directory, I do php artisan migrate to update the database.
  6. Then, in case there are any new files or directories in ~/projects/my_project/public, I link to them from the web root using the link I made in the one-time setup. You could do this manually for each file, eg ln -s ~/projects/my_project/public/mynewfile ~/html/mynewfile, but I’ve written a bash script that does them all, which I include in my projects’ root directory and run from there.
  7. Lastly, if this is a Production server, it’s worth doing php artisan config:cache to speed things up. (Don’t do this in development environments as config changes too much.)

…and that’s it.

So that’s the way I do my deployment. This is not the only way it could be done of course, but it’s (relatively) simple and (relatively) reliable, and so far it’s working OK for me.

Deploying with Git

One thought on “Deploying with Git

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s