Skip to content

Instantly share code, notes, and snippets.

@ChengLong
ChengLong / config_ssh.md
Last active August 29, 2015 14:07
[ssh] Disable PermitRootLogin and PasswordAuthentication

To make your server more secure, it's a good idea to disable root login and password authentication.

Open /etc/ssh/sshd_config

  1. Make sure ssh with pub key is enabled
RSAAuthentication yes
PubkeyAuthentication yes
@ChengLong
ChengLong / install_command_t.md
Last active August 29, 2015 14:05
Install Command-T

Install Command-T

Short summary of the official doc

Prerequisite

  1. Vim compiled with Ruby support. To verify this, run :ruby puts RUBY_VERSION. If it does NOT output error message, this vim has ruby support. Remember this version number. It will be used later.
  2. Pathogen or other vim plugin manager.

Installation

@ChengLong
ChengLong / timezone_cron.md
Last active August 29, 2015 14:04
Timezone in Cron

If you find that your cronjobs are not launched at the specified time, chances are it's a timezone issue.

The timezone of cron is specified in /etc/timezone, according to cron's man page:

The daemon will use, if present, the definition from /etc/timezone for the timezone.

If you change the timezone of the machine but has NOT restart cron, cron will still use the old timezone. In order to make your timezone effective, restart cron sudo service cron restart.

@ChengLong
ChengLong / pusher.rb
Last active August 29, 2015 14:04
Push Module
require 'houston'
require 'gcm'
module Pusher
APN = Houston::Client.production
APN.certificate = File.read(File.expand_path('path/to/push_cert.pem', __FILE__))
# Apple has hard limit on the payload of push notification. The maximum size allowed for a notification payload is 256 bytes;
# If you have other fields in the notification, you need to tweak this value to fit your needs. Be careful about this.
MAX_ALERT_LENGTH = 140
@ChengLong
ChengLong / virtual_host.conf
Created May 27, 2014 10:03
virtual host config for Apache
If you see *403 Forbidden* when running your app with Apache, it's because Apache is too strict on permissions. Set the following in virtual host config.
```
<Directory "/home/your_app/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
@ChengLong
ChengLong / user.rb
Last active August 29, 2015 14:01
Simple User Model with bcrypt
require 'bcrypt'
class User < ActiveRecord::Base
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@ChengLong
ChengLong / install_ssl.md
Created April 25, 2014 05:41
Install SSL certificate on Ubuntu for Apache

I found many misleading guides on how to install SSL certificate so that both http and https can work for the same site. Below are the step by step instructions

  1. Enable mod_ssl, sudo a2enmod ssl
  2. Put your certificate in /etc/ssl/certs/example.com.crt. Other directory may work, but I prefer this.
  3. Put your key in /etc/ssl/private/example.com.key. Other directory may work, but I prefer this.
  4. Add a new virtual host for https, e.g.
    <VirtualHost *:443>
        DocumentRoot /path/to/project
    

ServerName example.com

@ChengLong
ChengLong / ios_push.md
Last active August 29, 2015 14:00
Fixing Bad Token Issue when using Grocer

Fixing grocer

If you use grocer for sending push, you may have notificed that one bad token can cause connection to drop and all subsequent tokens are not sent. And it fails silently... More on this here.

One workaround is to use this pull request (it's not merged in yet). After each push, you call

reply = pusher.read_reply

, which will try to get feedback from Apple. And if the connection is already dropped, it will reconnect. This is exactly what we want. e.g.

@ChengLong
ChengLong / ruby_cron.md
Created April 17, 2014 06:34
Run Ruby Script using cron job

If you see this error when cron job runs a ruby script:

'require': cannot load such file ...

And you are using bundler e.g. require 'bundler/setup'. It's probably because the directory where cron runs the script is not correct, resulting in bundler fails to load gems.

Simply changed the directory to fix it, i.e.

* * * * * /usr/local/bin/ruby -C /home/example/scripts example_script.rb >>/home/example/log/cron.log 2>&1

@ChengLong
ChengLong / emoji_mysql.md
Created March 24, 2014 09:54
Store Emoji in Mysql on Rails

I recently came accross this error while working on a rails app

ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value: '\xF0\x9F\x98\x84' for column ...

A bit search shows that this error is famously known. You can see more details from here and here.

In short, this error happens because Mysql uses 3 bytes for utf8 character by default. So when a 4-byte character is sent, it crashes.

Solution: use encoding utf8mb4 and collation utf8mb4_unicode_ci