:::: MENU ::::
Posts tagged with: Webfaction

Deploying A Laravel App to Webfaction Using BitBucket

It is not uncommon to use a shared host to run a Laravel application and I have been doing that for a while on my preferred choice of shared host, Webfaction. This post will help you do the same without having to modify much about your application.

The first problem you will face is that default PHP on Webfaction is still 5.2.x. This is not a problem when you are using the webapp itself, as the .htaccess file that is used by Webfaction will ensure that you use PHP 5.5.x, if you chose that as the app profile when you set it up.

The problem arises when you have to run artisan commands either directly or as scripts from Composer. These will pick up the default PHP in /usr/local/bin/php than /usr/local/bin/php55, which is the correct one for our purposes.

To get around this you need to do a couple of things.

First is to symlink /usr/local/bin/php55 to /home/username/bin/php

Second is to download a copy of composer to /home/username/bin/composer

Once these two steps are done, add /home/username/bin to your path.

Now we will install the Laravel app. I usually choose the location for this as /home/username/app since /home/username/webapp is controlled by Webfaction’s setup.

This post won’t cover how to set up a Bitbucket Git repo. There are plenty of great resources that will allow you to that, but I will make the following assumptions:

1. Git will be set up to be accessed using SSH on Bitbucket.

2. You have set-up key-based SSH access on your Webfaction account to Bitbucket.

3. Your Laravel application has been properly pushed into the repo.

These are the last few steps to be followed:

1. Clone your Laravel app into /home/username/apps/appname.

git clone git@bitbucket.org:xxxx/xxxx.git /home/username/apps/appname

2. Run Composer update to pull in all the dependencies.

/home/username/bin/php /home/username/bin/composer update

3. Remove the .htaccess file in /home/username/webapps/webapp_name/

4. Symlink the public folder to the webapp folder.

ln -s /home/username/apps/appname/public/* /home/username/webapps/webapp_name/

5. Edit the .htaccess file in the public folder to add the following lines:

Action php55-cgi /php55.cgi
<FilesMatch \.php$>
SetHandler php55-cgi

You are pretty much good to go after that. One thing you need to keep in mind that if you check in other folders or files into the public folder, you will have to symlink the new files.

For a much more convenient two-step deployment, you can use Envoy to manage everything on the server.

My workflow is mostly:

1. Push code into Bitbucket.

2. Run an Envoy task that pulls the changes into the server, runs composer dump-autoload and gets going from there.

Setting up Fat Free CRM on Webfaction

Fat Free CRM is a lightweight Ruby on Rails application, which is very similar to the Highrise application by 37 Signals. These days I am trying to reduce my dependency on SAAS providers by hosting applications I use on a managed service. Why I am following this particular approach is beyond the scope of this post, but I could not find any instructions online that would get Fat Free CRM running on my hosting provider – Webfaction.

Before you start, I should warn you that I know precious little about Rails other than my usage of Redmine and FatFree. If you were to ask me to help you with any issue you’d face following these instructions, the odds are that I would not be able to help you at all. So proceed at your own risk.

1. Set up a Ruby on Rails application as detailed here.

2. SSH into your host and change to your webapp’s directory
cd ~/webapps/your_webapp_name/

3. Export the environment variables as specified in the Webfaction guide:
export PATH=$PWD/bin:$PATH
export GEM_HOME=$PWD/gems
export RUBYLIB=$PWD/lib

4. Check if the correct Ruby and Gem versions are being called. It should use the app’s versions and not the ones installed by default on the server.
which ruby
which gem

5. Install some gems.

gem install bundler passenger activesupport mysql2 activerecord-mysql2-adapter

6. Get a copy of FatFreeCRM from the Git repo.

git clone https://github.com/fatfreecrm/fat_free_crm.git fatfree

7. Create a new database for the app.

8. Create the database.yml file and change the connection details in it.

cd fatfree/
cp config/database.mysql.yml config/database.yml
vi config/database.yml

9. Edit the Gemfile to uncomment the mysql2 adapter.

vi Gemfile

10. Check the bundler and rake versions, just to be doubly sure.

which bundle
which rake

11. Run db:migrate and db:set-up. 

RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake ffcrm:setup

12. Change to config.assets.compile = true in production.rb

vi config/environments/production.rb

13. Edit nginx configuration to point to ‘fatfree’ from ‘hello_world’

vi ../nginx/conf/nginx.conf

14. Restart nginx