舞命小丢

舞命小丢的技术Blog

Ubuntu Php Down to 5.4 From 5.5

| Comments

http://superuser.com/questions/662824/downgrade-php-from-5-5-3-to-5-4-x-in-ubuntu-13-10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# upgrade system, so you can add to ignore all updates later
sudo apt-get update
sudo apt-get upgrade

# remove your php, apache, etc
sudo apt-get purge apache2 php5 libapache2-mod-php5 # add here your server packages

# change repositories to raring  (with backup)
sudo sed -i.bak "s/saucy/raring/g" /etc/apt/sources.list

# update and install server packages
sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5 phpmyadmin #add here packages you need and make sure you install php5.4 and apache2.2

# change repositories back to saucy
sudo sed -i "s/raring/saucy/g" /etc/apt/sources.list

# ignore all current upgrades (package hold)
sudo apt-mark hold `aptitude -F%p --disable-columns search ~U` 

Install Rmagick Fail on Macbook Lion

| Comments

错误信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ gem install rmagick
Buildingnative extensions.  This could take a while...ERROR:  Error installing rmagick:        ERROR:Failed to build gem native extension.        /Users/dhiemstra/.rvm/rubies/ruby-1.9.3-head/bin/ruby extconf.rb
checking forRuby version >=1.8.5... yes
extconf.rb:128:UseRbConfig instead of obsolete and deprecated Config.checking for clang... yes
checking forMagick-config... yes
checking forImageMagick version >=6.4.9... yes
checking for HDRI disabled version of ImageMagick... yes
checking for stdint.h...*** extconf.rb failed ***Couldnot create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.Provided configuration options:        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.        --curdir
        --ruby=/Users/dhiemstra/.rvm/rubies/ruby-1.9.3-head/bin/ruby
/Users/dhiemstra/.rvm/rubies/ruby-1.9.3-head/lib/ruby/1.9.1/mkmf.rb:381:in`try_do': The compiler failed to generate an executable file. (RuntimeError)

需要卸载了,重新安装

1
2
brew uninstall imagemagick
brew install imagemagick --disable-openmp

Use Devise on Legacy Database Width Md5

| Comments

建议使用deivse 2.1,并且添加devise-encryptable

1
2
3
# Gemfile
gem 'devise', '~> 2.1'
gem 'devise-encryptable'

实现自己的加密方式,我是使用的是md5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# lib/devise/encryptable/encryptors/md5.rb
require 'digest/md5'

module Devise
  module Encryptable
    module Encryptors
      class Md5 < Base
        def self.digest(password, stretches, salt, pepper)
          str = [password, salt].flatten.compact.join
          Digest::MD5.hexdigest(str)
        end
      end
    end
  end
end

如果Devise版本低于2.1

1
2
3
4
5
6
7
8
9
10
11
12
13
# lib/devise/encryptors/md5.rb
require 'digest/md5'

module Devise
  module Encryptors
    class Md5 < Base
      def self.digest(password, stretches, salt, pepper)
        str = [password, salt].flatten.compact.join
        Digest::MD5.hexdigest(str)
      end
    end
  end
end

在config/initializers/devise.rb中加载md5加密

1
require Rails.root.join('lib', 'devise', 'encryptable', 'encryptors', 'md5')

在config/initializers/devise.rb中修改加密方式为md5

1
config.encryptor = :md5

添加 :encryptable 到User model.

如果password_salt为空的话,要在model中加入如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# for devise
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable
  devise :encryptable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :username, :password, :password_confirmation, :remember_me

  # for devise
  def password_salt=(password_salt)
  end

  def password_salt
  end

  protected

    def password_digest(password)
        Devise::Encryptable::Encryptors::Md5::digest(password, self.class.stretches, authenticatable_salt, self.class.pepper)
    end
end

这里必须覆盖password_digest方法,因为password_digest中监测了password_salt是否为空

https://github.com/plataformatec/devise-encryptable/blob/master/lib/devise/encryptable/model.rb%E4%B8%AD56%E8%A1%8C

1
2
3
4
5
6
# Digests the password using the configured encryptor.
      def password_digest(password)
        if password_salt.present?
          encryptor_class.digest(password, self.class.stretches, authenticatable_salt, self.class.pepper)
        end
      end

参考信息:

How-To:-Create-a-custom-encryptor

/tmp/目录没有空间

| Comments

这是错误信息,查了很多都没有查到问题所在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[ pid=14193 thr=71528440 file=utils.rb:176 time=2012-05-03 21:15:35.137 ]: *** Exception Errno::ENOENT in application (No such file or directory - /tmp/RackRewindableInput20120503-14193-1w1x37q.lock) (process 14193, thread #<Thread:0x0000000886dff0>):
  from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/tempfile.rb:346:in `rmdir'
  from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/tempfile.rb:346:in `rmdir'
  from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/tempfile.rb:338:in `ensure in locking'
  from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/tempfile.rb:338:in `locking'
  from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/tempfile.rb:144:in `block in initialize'
  from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/tmpdir.rb:133:in `create'
  from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/tempfile.rb:134:in `initialize'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/utils/rewindable_input.rb:86:in `new'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/utils/rewindable_input.rb:86:in `make_rewindable'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/utils/rewindable_input.rb:30:in `read'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/request.rb:202:in `POST'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:26:in `method_override'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:14:in `call'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/runtime.rb:17:in `call'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/lock.rb:15:in `call'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:136:in `forward'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:143:in `pass'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:155:in `invalidate'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:71:in `call!'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:51:in `call'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/railties-3.2.2/lib/rails/engine.rb:479:in `call'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/railties-3.2.2/lib/rails/application.rb:220:in `call'
  from /opt/www/memebox/shared/bundle/ruby/1.9.1/gems/railties-3.2.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_request_handler.rb:513:in `accept_and_process_next_request'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/rack/application_spawner.rb:206:in `start_request_handler'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/rack/application_spawner.rb:171:in `block in handle_spawn_application'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/utils.rb:479:in `safe_fork'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/rack/application_spawner.rb:166:in `handle_spawn_application'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server.rb:180:in `start'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/rack/application_spawner.rb:129:in `start'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
  from <internal:prelude>:10:in `synchronize'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
  from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/helper-scripts/passenger-spawn-server:99:in `<main>'

运行下面命令,查看磁盘空间是否够用

1
dh -h

我们的服务器系统只有16G,其他都挂载了/opt,前两天转移数据放了一个大文件7个G的文件,把16G占满了,当然写不进去了。终于解决了。呜呜,吸取教训了。

Attr Attr_accessor Attr_accessible Attr_protected

| Comments

attr_accessor是Ruby提供的方法,相当于帮你生产读写方法,同时兼备 attr_reader attr_writer 的作用

attr_reader 相当于生产读方法

attr_writer 相当于生产写方法

attr 只能带一个符号参数,第二个参数是一个 bool 参数,用于指示是否为头一个符号参数产生写方法。它的默认值是 false , 不产生写方法。

attr_accessible是rals的提供的方法,指的是能某些字段可以通过前端来赋值

attr_protected是rals的提供的方法,作用恰好和attr_accessible相反,指定某些字段不能够前端来赋值

Git Revert and Reset

| Comments

git revert 是撤销某次操作,此次操作之前的commit都会被保留

git reset 是撤销某次提交,但是此次之后的修改都会被退回到暂存区

Git的一些常用的撤销提交版本的的命令:

1
2
3
4
git revert HEAD                #撤销前一次 commit
git revert HEAD^               #撤销前前一次 commit
#撤销指定的版本,撤销也会作为一次提交进行保存。
git revert commit 7de3c367b8ee77e27371f8f6761b8f5909fcce43

相关内容

Git Community Book

非常好的git – 简易指南

Git revert 撤销某个提交版本

Use Capistrano to Deploy

| Comments

一直在寻找一个好的部署工具,以前用java的时候,经常过maven,ant,更多是用来构建,而不是部署。

前一段使用cakephp一直也想找一个这样的工具,也是没有找到。

开始学习RoR的开发,在ruby-china的代码中发现了capistrano 这个工具,看了一下介绍,感觉就是我理想中工具

支持多服务器,服务器重启,自定义任务,版本切换,版本管理工具支持,可以连接ssh,对服务器操作

而且还有很多第三方扩展:

cakephp扩展-capcake

play扩展

1
2
3
4
ssh-keygen -p
cd ~/.ssh
cp id_rsa.pub authorized_keys
scp -p ~/.ssh/authorized_keys hrothgar:.ssh/

First Post

| Comments

以前在上大学那会比较清闲,使用wordpress写了一些blog,毕业之后就荒废了,一直想重新开Blog。

一方面感觉太忙了,另一方面,感觉又要假设一个空间,wordpress添加很多插件,而且它对代码显示的支持我一直认为不够好

看到很多人开始github上写blog,感觉很方面,可以想保存代码那样保存每个版本,感觉很不错啊。

而且发现了Octopress,对代码显示的支持非常好,使用github管理,之间发布,非常方便。

参考: * 技术博客利器——Octopress