Create symbolic links in Ubuntu

Follow this guide to create symbolic links

Depending on your application, you may need to have persisting local storage through multiple deploys by creating a symbolic link to a shared folder.

Cloud 66 uses Capistrano to deploy your application, which will create a new folder for every deployment and create a symbolic link to that folder so that it can be served. This means that folders containing uploaded files for example also need a symbolic link.

There are generally two ways you can accomplish this.

Public folder

If it’s acceptable to serve your files from your public directory, you can re-use the public/system folder which is already auto-symlinked to shared/system on each deploy. The down-side of doing this is that it bypasses your application and your files get served directly by Nginx without security.

Alternatively, you can use deploy hooks to create the symbolic link. Also, you can use $STACK_BASE for your stack base path (eg. $STACK_BASE/shared/uploads) for your deploy hook script.

To create the symbolic link, your deploy hook script could contain this:

mkdir -p $STACK_BASE/shared/uploads
chown nginx:app_writers $STACK_BASE/shared/uploads
rm -rf $STACK_PATH/uploads
ln -nsf $STACK_BASE/shared/uploads $STACK_PATH/uploads

The reason we are doing rm -rf on the $STACK_PATH/uploads directory is due to the way that the ln command works. When you issue the ln command, it places a link to the source directory inside the target directory, so we have to remove the directory before creating the symbolic link.

The deploy hook would look like this:

      - source: /.cloud66/
        destination: /tmp/
        target: rails
        execute: true
        sudo: true
        apply_during: all
        run_on: all_servers


You must be logged in to comment on this article