Matt Crowe, Coder

Just another WordPress site

Ruby Cheatsheet

Kill unicorn daemon. Hat tip.


How to Setup Multiple Ruby on Rails apps on Digital Ocean’s RoR image with Nginx and Unicorn

This is a short tutorial on how to setup multiple Ruby on Rail apps on a single droplet on a Digital Ocean server. I am starting with a droplet that's built on the default image "Ruby on Rails on Ubuntu 14.04 (Nginx + Unicorn)".

I am assuming you have a new droplet that is up and running at http://[your-droplet-ip].

First, let's rename the default RoR app and create a copy, as well:

Now, let's update the home page for each so they are easily distinguished when we are ready to verify our work:

Next we need to create unicorn.rb file for both apps:

Now, we're almost done. Let's create/update the nginx conf here:

Now, let's restart nginx:

Lastly, let's start unicorn for each app:

Now you can test this by going to your browser:

http://[your-droplet-ip] for myapp1 for myapp2 (if necessary, update your hosts file or DNS for your domain )

Additional Reading:

Symfony – Authentication and Role-Based Authorization With Multiple User Models

To jump straight to my shared demo code, click here.

To accomplish authentication / authorization in Symfony can be a complex task.  More so, if you want to treat multiple models differently. Say, you have two entities that implement UserInterface, such as User and Staff, and you want User to be able to access protected areas in your application's frontend, such as that User's profile or invoice records, but Staff can access various administrative areas in the application's backend.

You could throw everyone into a single table and handle these distinctions via your authorization logic, but maybe that just sorta bugs you like it does me. So multiple tables/entities it is.

Anyway, the following doesn't cover each step in full detail, as I will be assuming you already have and/or understand the steps needed to create the relevant models, views, controllers and routes. Furthermore, I don't cover all the code here, as it's spread over quite a few files. To get the full code, please refer to the link at the beginning of this post.

Instead, I will be providing an overview of the less obvious steps.

1. Modify app/config/security.yml

Symfony\Component\Security\Core\User\User: plaintext

id: #User/Bundle/Resources/config/services.xml
id: #User/Bundle/Resources/config/services.xml

pattern: ^/admin
provider: staff
staff_security: true #refers to User/Bundle/DependencyInjection/StaffSecurityFactory::getKey()
pattern: ^/manage
provider: user
user_security: true #refers to User/Bundle/DependencyInjection/UserSecurityFactory::getKey()

2. In your bundle, modify /Resources/config/services.yml

class: User\Bundle\Security\Staff\StaffProvider
arguments: ["@doctrine.orm.default_entity_manager", "@service_container"]
class: User\Bundle\Security\Staff\StaffAuthenticationProvider
arguments: ["@doctrine.orm.default_entity_manager", "@service_container"]
class: User\Bundle\Security\Staff\StaffAuthenticationListener
arguments: ["@doctrine.orm.default_entity_manager", "@service_container", "@security.authentication.manager"]
class: User\Bundle\Security\User\UserProvider
arguments: ["@doctrine.orm.default_entity_manager", "@service_container"]
class: User\Bundle\Security\User\UserAuthenticationProvider
arguments: ["@doctrine.orm.default_entity_manager", "@service_container"]
class: User\Bundle\Security\User\UserAuthenticationListener
arguments: ["@doctrine.orm.default_entity_manager", "@service_container", "@security.authentication.manager"]

3. Update your main bundle file:



$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new \User\Bundle\DependencyInjection\UserSecurityFactory());
$extension->addSecurityListenerFactory(new \User\Bundle\DependencyInjection\StaffSecurityFactory());

4. Add a SecurityFactory to your bundle's DependencyInjection directory for each entity, such as:


5. Add a Security directory to your Bundle and these files:

#Security (Base)

#Security (Staff)

#Security (User)

For more information, please explore the demo code here. If you have any questions or feedback please comment. Thanks.

My Vagrant Cheatsheet

When getting errors like "Failed to create the host-only adapter":

sudo /Library/StartupItems/VirtualBox/VirtualBox restart

Hat Tip

My Symfony2 Cheatsheet

Create database:

php console doctrine:database:create

Drop that database:

php console doctrine:database:drop --force

Create schema:

php console doctrine:schema:create

Load fixtures:

php console doctrine:fixtures:load

Execute database migration:

php console doctrine:migrations:diff
php console doctrine:migrations:migrate

Create/dump assets (via Assetic):

php console assets:install public_html
php console assetic:dump

Run PHPUnit:

cd path/to/bin
phpunit -c ../app --coverage-html phpunit-report #all tests
phpunit -c ../app --coverage-html phpunit-report ../src/My/Bundle/Tests/ #all tests in directory
phpunit -c ../app --coverage-html phpunit-report ../src/My/Bundle/Tests/Entity/MyEntity.php #specific test

Clear cache:

php console cache:clear

Debug routes:

php console router:debug


My MySQL Cheatsheet

Disable foreign key check:

SET foreign_key_checks = 0;
# mysql code here
SET foreign_key_checks = 1;

Create dump file from shell (full database):

mysqldump -u username -ppassword database > filename.sql

Create dump file from shell (specific table):

mysqldump -u username -ppassword database table > filename.sql

Create dump file from shell (more verbose, use gzip & exclude views):

mysqldump --host=localhost --user=username --password=password database --quick --lock-tables --add-drop-table --ignore-table=database.table1 --ignore-table=database.table2 --ignore-table=database.table3  | gzip > /path/to/filename.gzip;

Replace annoying characters:

UPDATE table SET column = REPLACE(column,"Ô","'");
UPDATE table SET column = REPLACE(column,'Ó','"');
UPDATE table SET column = REPLACE(column,'Ò','"');

Dump one database to another:

mysqldump -h localhost -u username -ppassword database1 | mysql -h localhost -u username -ppassword database2


My GIT Cheatsheet

Delete a remove branch:

git push origin :branch-to-delete

Force overwrite of local files with remote files:

git fetch --all
git reset --hard origin/master

Undo last commit:

git reset --soft HEAD^
# make your edits
git commit -a -c ORIG_HEAD

Remove/replace tracked files:

git rm -r --cached .
git add --all
git commit -am "your commit msg"

Exit long-form message screen (on a Mac):

type ":wq"

Ignore changes to file (unsure how useful this really is):

git update-index --assume-unchanged path/to/file

Rename branch

git checkout branch-to-rename
git branch -m <newname>

Revert Remote Merge

git pull origin master #make sure it's current
git checkout -b revert-merge #create new branch that keeps this reversal as part of the git history
git revert -m 1<commit-hash>
git pull origin revert-merge

git checkout master
git merge revert-merge

If you need to recreate branches you deleted

git branch branchname <commit-hash>

My *nix Cheatsheet

Remove file:

rm path/to/file

Remove directory recursively:

rm -rf dir-name/

Find directory:

find / -name 'dir-name'


How I Setup a Multi-Site Dev Environment with Vagrant on a Mac

The following is a list of steps to setup a local environment to develop multiple projects on one Vagrant "box".

Download and install VirtualBox.

Download and install Vagrant.

Create a directory similar to this:


You can follow the documentation on the Vagrant site or start with a pre-made lamp stack. Quite a few exist. I've created my own, as well.

Unfortunately, you can't clone into a non-empty directory. To move the files over:

Now to run a one-time installation of the box:

Create a test file:

Add your dev url to your hosts file (and flush your cache, just in case):

SSH into your Vagrant instance and create and the configuration files for your dev url:

Inside your ssh editor, make sure these lines exist, are not commented and are set as:

The same file needs to be in /etc/apache2/sites-enabled:

Restart Apache:

Test your work, open your site:

Meanwile, some other notes...

If VirtualBox get's upset, you can you restart it:

If you add something new to, update your Vagrant instance with:


© 2017 Matt Crowe, Coder

Theme by Anders NorenUp ↑