Capistrano series - application vhost creation

Well, we've deployed our application to the Slice and seen Capistrano at work but, perhaps a little frustratingly, we couldn't actually browse the site as we hadn't set up a virtual host.

Let's do that now and, with our previous knowledge of Nginx and vhosts, it won't take much time at all.


vhost

Let's jump straight in and create the vhost. If you need more general information on Nginx and virtual host then you can refer to the Nginx, vhosts and mongrels article which is part of this series.

So, assuming you have read up on the general principles, log into your Slice and create a vhost for your domain:

sudo nano /etc/nginx/sites-available/domain.com

The content is going to be very similar to the example one given in the article shown above:

upstream domain1 {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
    }

server {
            listen   80;
            server_name  www.domain.com;
            rewrite ^/(.*) http://domain.com permanent;
           }


server {
            listen   80;
            server_name domain.com;

            access_log /home/demo/public_html/domain.com/shared/log/access.log;
            error_log /home/demo/public_html/domain.com/shared/log/error.log;

            root   /home/demo/public_html/domain.com/current/public/;
            index  index.html;

            location / {
                          proxy_set_header  X-Real-IP  $remote_addr;
                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                          proxy_set_header Host $http_host;
                          proxy_redirect false;

                          if (-f $request_filename/index.html) {
                                           rewrite (.*) $1/index.html break;
                          }

                          if (-f $request_filename.html) {
                                           rewrite (.*) $1.html break;
                          }

                          if (!-f $request_filename) {
                                           proxy_pass http://domain1;
                                           break;
                          }
            }

}

However, there are some crucial differences such as the location of the logs and the root directory.

Log files

Capistrano uses a directory called 'shared' in which is stored the more permanent aspects of the application such as the logs and pid's.

So adding a direct link to the shared log folder will always link to the correct place - all releases will use the permanent folders in the shared diretory.

Root directory

Remember that the layout Capistrano uses consists of a 'current' directory which is, in fact, a symlink to the latest upload in the 'releases' directory.

As such, the root location we defined:

/home/demo/public_html/domain.com/current/public/

will always point to the latest application version.

Restart Nginx

That's actually all we need to do as we have already set up the mongrel instances when we configured Capistrano.

So stop Nginx and then start it again (a restart does not seem to work):

sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start

Navigate

Now simply navigate to your domain:

http://domain.com

Where you will see the magic Ruby on Rails welcome page:

Ruby on Rails Welcome Page

Yippee!

Excellent news. Now we have all the bits and pieces installed and working perfectly.

All we need to do now is to ensure that any future changes we make on the workstation are mirrored on the site.

So a final set of tweaks (no really, these are the final tweaks) to Capistrano are needed. These will ensure Nginx is restarted after an update, etc.

PickledOnion

Article Comments:

Mick Ryan commented Wed Feb 20 15:54:11 UTC 2008:

Just wanted to let you know, Ive been a linux for a LONG time and very comfortable with the console and terminals but just started working with rails and programming in general, and Ive read thousands of tuts. Yours are the best I've EVER read..you should open your horizons and help the community in other ways! Fantastic job! -Mick

Adam Anderson commented Tue Apr 01 07:06:02 UTC 2008:

I'm going to agree with Mick. These tutorials "just work".

Hari commented Thu May 22 05:22:02 UTC 2008:

When I do sudo /etc/init.d/nginx start I get

Starting nginx: 2008/05/22 05:14:41 [emerg] 26019#0: open() "/home/user/public_html/domain.com/log/access.log" failed (2: No such file or directory)

Can any one see what I missed?

Thanks

leland commented Sun May 25 00:57:11 UTC 2008:

@Hari -- check where you're pointing your access log to in the vhost file. it should point to a file that actually exists.

Daniel Waite commented Tue Aug 26 23:21:31 UTC 2008:

On cap deploy:cold I am told:

* [err :: domain.com] tar: 20080826231704: Cannot mkdir: Permission denied

Tom commented Mon Sep 01 07:03:31 UTC 2008:

Daniel, you need to set the correct permissions for your public_html folder. I had the same error and noticed it was because of that.

Refer to his Vhost Permissions guide under apache, even if you are not using apache you should follow it as it'll set the permissions so the user demo can do those things.

It fixed my problem. Let me know if you have any other problems.

Sofia commented Sun Dec 07 05:34:32 UTC 2008:

Hi

I was following this to use capistrano to deploy a php application. My setup is this: *local workstation with capistrano & local checked out repo *remote workstation (at slicehost) with php (& without ruby) *subversion repository in remote server

So basically the idea is checkout/export the site from svn (without the .svn folders), zip it, transfer to local workstation, upload to slice. But i don't know why but it always copies the .svn folders even though i'm doing an export. Here's my deploy.rb (a bit different from the above but functional):

set :svn_user, "mysvnuser" set :svn_pass, "mysvnpass" ssh_options[:username] = 'admin@domain.com' ssh_options[:password] = 'mysshpass' set :user, "admin" set :password, "mypass"

=============================================================================

REQUIRED VARIABLES

=============================================================================

set :application, "domain.com" set :repository, "--username #{svnuser} --password #{svnpass} http://svn.anotherdomain.com/myapp/trunk"

=============================================================================

OPTIONAL VARIABLES

=============================================================================

set :deployto, "/home/publichtml/sub.domain.com" # defaults to "/u/apps/#{application}" set :deploy_via, :copy

set :checkout, "export"

set :copy_strategy, :checkout set :use_sudo, false set :keep_releases, 3

=============================================================================

ROLES

=============================================================================

role :app, application role :web, application role :db, application , :primary => true

desc "This will deploy the app - overwrites deploy task so it wont run migrate or other rails related tasks" deploy.task :deploy do run "svn --quiet #{checkout} #{repository} #{release_path}" run "ln -nfs #{releasepath} #{currentpath}" run "ln -nfs #{sharedpath}/public #{currentpath}/public" end

Besides not copying the .svn folders to the live site, I would also like to be prompted for the passwords instead of them being hardcoded - i know it's possible but i can't find the article..

If there are better ways to do this please say so. I'm also posting this so it might help other folks with php apps.

PickledOnion - you are the best :D these tutorials really help.

Best,

Sofia

sofia commented Sun Dec 07 05:40:37 UTC 2008:

That doesn't look good without the line breaks. Please check this pastie.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)