Pretty Handy Deployment™
Have you ever wished deploying to a shared Apache/PHP host could be as easy as deploying to Heroku? Well, wish no more. Follow this simple guide and your life will instantly get better.
- Environment variables
- Autamatic deployment with Git
- Using Composer for dependency management
This setup has been confirmed to work on DreamHost. Your mileage may vary.
- Make sure you have SSH enabled on your hosting user
- SSH into your hosting account
- CD into your domain directory and make sure it's empty
- Clone this repo (
git clone https://github.com/stephendavis89/PHD.git .
)
Environment variables can be set with putenv
in a root-level PHP file named environment.php
. An example variable is provided. Make sure to configure your domain to use myproject.com/deploy
as its web root.
TIP: If you're using a web framework such as Slim or Laravel, use myproject.com/deploy/public
as the web root instead.
- Create a git directory (
mkdir -p ~/git && cd ~/git
) - Create a project directory (
mkdir -p myproject.git && cd myproject.git
) - Create an empty remote Git repository (
git init --bare
) - Create the post-receive hook
mv hooks/post-receive.sample hooks/post-receive
chmod +x hooks/post-receive
- Edit the post-receive hook (
nano hooks/post-receive
) and insert the following content:
#!/bin/sh
echo "Deploying to production..."
GIT_WORK_TREE=~/myproject.com/deploy git checkout -f
echo "Success!"
- Add the remote repository to your project From your local project directory, run:
git remote add production ssh://user@host/~/git/myproject.git
To set up an SSH key for password-less deployment, follow this excellent guide.
To get Composer working on DreamHost, follow this guide.
To access the composer binary directly, run this from the directory you installed Composer into:
mkdir -p ~/bin
mv composer.phar ~/bin/composer
And add this to your .bashrc
:
export PATH=~/bin:$PATH
Don't forget to add composer installation to your post-receive
hook for maximum good times:
(cd ~/myproject.com/deploy && exec composer install --no-dev)