Generating a new SSH key
生成秘钥
1
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
把公钥文件id_rsa.pub拷贝到需要登录的服务器上
登录远程服务器,在用户根目录下新建.ssh文件夹(如果不存在),在其中新建authorized_keys文件(如果不存在),把id_rsa.pub添加到authorized_keys文件中
退出当前远程登录,之后就可以不使用密码远程登录了
生成秘钥
1 | $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" |
把公钥文件id_rsa.pub拷贝到需要登录的服务器上
登录远程服务器,在用户根目录下新建.ssh文件夹(如果不存在),在其中新建authorized_keys文件(如果不存在),把id_rsa.pub添加到authorized_keys文件中
退出当前远程登录,之后就可以不使用密码远程登录了
1 | $ who am i # 伪终端的用户名 伪终端序号(pts/0) 打开时间 |
新建一个叫 mars 的用户:
1 | sudo adduser mars # 默认生成的 用户组 和 用户名 相同 |
切换用户到mars1
su -l mars
1 | groups mars |
1 | $ su -l mars # 切换登录用户 |
会提示 mars 不在 sudoers 文件中,意思就是 mars 不在 sudo 用户组中,至于 sudoers 文件(/etc/sudoers)你现在最好不要动它,操作不慎会导致比较麻烦的后果。
使用 usermod
命令可以为用户添加用户组
usermod -g 这条命令编辑/etc/passwd文件,修改相应用户的组ID
usermod -G 这条命令编辑/etc/group文件,增加组成员。
eg: 把mars
用户加入sudo
组: usermod -aG sudo mars # 多个组之间用空格隔开
1
2
3$ groups mars
$ sudo usermod -G sudo mars
$ groups mars
1 | sudo deluser mars --remove-home |
1 | $ sudo chown [新用户名] [文件名] |
1 | $ chmod 700 iphone6 # 现在,其他用户已经不能读这个“ iphone6 ”文件了 |
方式二:加减赋值操作
1 | $ chmod go-rw iphone6 |
g
、o
还有 u
分别表示 group
、others
和 user
,+
和 -
分别表示增加和去掉相应的权限。
1 | cd ~ |
1 | $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" |
rails ngnix配置 参考11
2$ [sudo] apt-get update
$ [sudo] apt-get install nginx
配置文件: (配置文件所在目录:/etc/ngnix/sites-enabled/)1
2$ cd /etc/nginx/conf.d
$ vim nodeChinaClub.conf
使配置生效:1
sudo service nginx reload
1 | sudo service nginx start |
vim /etc/nginx/sites-available/wedesign.conf1
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
29upstream wedesign {
server unix:///mnt/www/wedesign/shared/tmp/sockets/puma.sock;
}
server {
listen 80;
server_name rails.itrydo.com; # change to match your URL
root /mnt/www/wedesign/shared/public/assets; # I assume your app is located at that location
location / {
proxy_pass http://wedesign; # match the name of upstream directive which is defined above
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
creating symlink in /etc/nginx/sites-enabled
1
sudo ln -sf /etc/nginx/sites-available/wedesign.conf /etc/nginx/sites-enabled/wedesign.conf
启动报错可以执行下面命令,会打印出错误信息(个人测试)1
sudo nginx -t -c /etc/nginx/nginx.conf
mina1
2
3bundle exec puma -e production -b unix:///mnt/www/wedesign/shared/tmp/sockets/puma.sock
# 后台启动 -d
bundle exec puma -e production -d -b unix:///mnt/www/wedesign/shared/tmp/sockets/puma.sock
1 | sudo apt-get install git |
1 | sudo apt-get install rbenv ruby-build |
手动安装:
参考11
2
3
4
5
6
7
8
9
10
11
12
13
14git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
~/.rbenv/bin/rbenv init
exec $SHELL
# 安装ruby-build
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
# 通过 gem 命令安装完 gem 后无需手动输入 rbenv rehash 命令, 推荐
git clone git://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
# 通过 rbenv update 命令来更新 rbenv 以及所有插件, 推荐
git clone git://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update
# 使用 Ruby China 的镜像安装 Ruby, 国内用户推荐
git clone git://github.com/AndorChen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror
1 | sudo apt-get install -y libssl-dev libreadline-dev zlib1g-dev |
检查系统中是否已经安装了MySQL1
sudo netstat -tap | grep mysql
如果没有安装,则安装MySQL.1
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
测试安装是否成功:1
sudo netstat -tap | grep mysql
也可通过登录MySQL测试1
mysql -uroot -p
MySQL的一些简单管理:1
2
3
4
5
6
7
8
9# 启动MySQL服务:
sudo service mysql start
# 停止MySQL服务:
sudo service mysql stop
# 修改 MySQL 的管理员密码:
sudo mysqladmin -u root password newpassword
# 设置远程访问(正常情况下,mysql占用的3306端口只是在IP 127.0.0.1上监听,拒绝了其他IP的访问(通过netstat可以查看到)。取消本地监听需要修改 my.cnf 文件:):
sudo vi /etc/mysql/my.cnf
bind-address = 127.0.0.1 //找到此内容并且注释
MySQL安装后的目录结构分析(此结构只针对于使用apt-get install 在线安装情况):1
2
3
4
5
6
7
8# 数据库存放目录:
/var/lib/mysql/
# 相关配置文件存放目录:
/usr/share/mysql
# 相关命令存放目录:
/usr/bin(mysqladmin mysqldump等命令)
# 启动脚步存放目录:
/etc/rc.d/init.d/
图形软件1
2Navicat
Sequel pro
1 | sudo apt-get install nodejs |
首次部署可能无法自动创建数据库1
2
3cd /mnt/www/wedesign/current
# then run
rake db:create RAILS_ENV=production
puma.rb1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#!/usr/bin/env puma
environment "production"
daemonize true
workers 2
threads 2,16
bind "unix:///mnt/www/wedesign/shared/tmp/sockets/puma.sock"
pidfile "/mnt/www/wedesign/shared/tmp/pids/puma.pid"
state_path "/mnt/www/wedesign/shared/tmp/sockets/puma.state"
stdout_redirect '/mnt/www/wedesign/shared/log/stdout', '/mnt/www/wedesign/shared/log/stderr'
directory "/mnt/www/wedesign/current"
activate_control_app 'unix:///mnt/www/wedesign/shared/tmp/sockets/pumactl.sock'
prune_bundler
database.yml1
2
3
4
5
6
7
8
9
10
11
12default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
host: localhost
production:
<<: *default
database: [project_database_name]
username: [database_username]
password: [database_password]
secrets.yml1
2
3
4production:
#secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
# or run rake secret
secret_key_base: [secret_key]
wedesign.conf1
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
29upstream wedesign {
server unix:///mnt/www/wedesign/shared/tmp/sockets/puma.sock;
}
server {
listen 80;
server_name rails.itrydo.com; # change to match your URL
root /mnt/www/wedesign/shared/public; # I assume your app is located at that location
location / {
proxy_pass http://wedesign; # match the name of upstream directive which is defined above
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
then1
$ sudo ln -sf /etc/nginx/sites-available/wedesign.conf /etc/nginx/sites-enabled/wedesign.conf
1 | var vm = new Vue({ |
方法1:
1 | <body> |
方法2:1
2
3Vue.component('custom', {
template: '<h3>我是自定义模板</h3>'
})
方法1:1
2
3
4
5
6
7
8
9
10var Custom = Vue.extend({
template: '<h3>我是自定义模板</h3>'
})
var vm = new Vue({
el: '#box',
components: {
custom: Custom
}
})
方法2:1
2
3
4
5
6
7
8new Vue({
el: '#box',
components: {
custom: {
template: '<h3>我是自定义模板</h3>'
}
}
})
1 | <template id="custom"> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<body>
<div id="box">
<component :is="custom"></component>
</div>
<script>
new Vue({
el: '#box',
data: {
custom: 'a'
},
components: {
a: {
template: '<h1>我是a</h1>'
},
b: {
template: '<h1>我是b</h1>'
}
}
})
</script>
</body>
dev-tools
props
[‘msg’]
{
msg: String,
num: Number
}
1 | <body> |
$emit
: 发送
1 | <body> |
1 | <body> |
1 | <custom> |
block
1 | def foo |
&
and call
1 | def foo(&block) |
proc
可以看做是block
的对象表示,类名为Proc
1
proc = Proc.new {|x| x*2}
lambda的class也是Proc
1
lambda = lambda {|x| x*2}
lambda和proc的区别是: 实例化方式的不同而已,proc
更像对象,lambda
更像方法
1 | ## diff1 |
1 | class Point |
public
- visibility:
in/out
- inheritance:
Yes
- call an obj:
Yes
protected
- visibility:
within
- inheritance:
Yes
- call an obj:
Yes
private
- visibility:
within
- inheritance:
Yes
- call an obj:
No
1 | class Point3D < Point |
1 | module Helper |
方法优先级:singleton中的方法
- 实例方法
- module方法
- 父类实例方法
产生: raise
处理: rescue1
2
3
4
5
6
7
8def foo
begin
# raise "boom in foo"
raise TypeError, "boom in foo", caller
rescue => e
puts e
end
end
Comparable1
2
3
4
5
6
7
8
9
10class People
attr_reader :name
include Comparable
def initialize name
@name = name
end
def <=> other
self.name <=> other.name
end
end
Enumerable1
2
3
4
5
6
7
8
9
10
11
12
13class People
attr_reader :people
include Enumerable
def initialize people
@people = people
end
def each
raise "please provide a block" unless block_given?
people.each do |person|
yield person
end
end
end
生成正则的方法
使用1
2
3"ruby" =~ /ruby/ # 0
"ruby".match(/ru/) # ru
"ruby".gsub!(/r/, i) # iuby
正则编写测试网站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[abc] A single character of: a, b, or c
[^abc] Any single character except: a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary
(...) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a
options:
i case insensitive
m make dot match newlines
x ignore whitespace in regex
o perform #{...} substitutions only once
1 | # 设置环境变量time zone |
1 | def foo |
Thread.new 创建线程
t1.priority = -1
t1.priority = 1 # 设置优先
t1.join 开始执行t1线程,主线程等待t1完成
Thread.current # 当前线程对象
Thread.abort_on_exception = true # 一个线程异常,所有线程都退出
1 | mutex = Mutex.new |
1 | eval "1 + 1" |
1 | String.instance_eval "def foo; puts 'instance_eval foo'; end" |
1 | String.class_eval "def foo; puts 'class_eval foo'; end" |
1 | define_method(:foo) { puts 'abc' } |
1 | def method_missing method_name |
rails中在mysql,postgresql, sqlite中的类型对照表
参考
Rails | mysql | postgresql | sqlite |
---|---|---|---|
:binary | blob | bytea | blob |
:boolean | tinyint(1) | boolean | boolean |
:date | date | date | date |
:datetime | datetime | timestamp | datetime |
:decimal | decimal | decimal | decimal |
:float | float | float | float |
:integer | int(11) | integer | integer |
:string | varchar(255) | * | varchar(255) |
:text | text | text | text |
:time | time | time | datetime |
:timestamp | datetime | timestamp | datetime |
1 | $ rails new wechat |
1 | rails g model User username:string phone:string:index age:integer |
1 | bundle exec rake db:migrate |
1 | rails c |
users
表1 | $ rails g model User phone:string:index email:string age:integer |
假设数据库中的表名为USER
,显式指定User
模型关联USER
数据表,负责默认表名为:users
1
2
3class User < ActiveRecord::Base
self.table_name = "USER"
end
1 | $ rails g controller users index new edit show |
执行迁移文件1
$ bundle exec rake db:migrate
回滚迁移文件1
$ bundle exec rake db:rollback
presence
[必须存在]
1
2
3
4
5
6
7 validates :name, presence: true
# 只在创建时候验证
validates :name, presence: {on: :create}
# 只在更新时验证
validates :name, presence: {on: :update}
# '自定义错误内容'
validates :name, presence: {on: :create, message: '姓名不能为空'}
absence
[必须为空]
1 validates :name, absence: true
length
- maximum 最大长度
length: {maximum: 255}
- minimum 最小长度
length: {minimum: 3}
- in 范围
length: {in: 3..20}
- is 长度必须为多少
length: {is: 5}
confirmation
确认性验证
1 validates :password, confirmation: true #password_confirmation[shouldn't nil]
inclusion 包含性验证[province必须为北京或者上海]
1 validates :province, inclusion: {in: ['北京', '上海']}
exclusion 不包含性验证[不在某个集合之内]
format 正则匹配
1
2
3
4 class User < ActiveRecord::Base
EMAIL = /\A.+@.+\z/i # 常量 正则
validates :email, presence: true, length: {maximum: 255}, format: {with: EMAIL}
end
uniqueness
唯一
1
2
3 class User < ActiveRecord::Base
validates :phone, uniqueness: true
end
1 | class User < ActiveRecord::Base |
自定义验证
validates_with
1 | class MyValidator < ActiveModel::Validator |
validates_each
1 | class NamepresenceValidator < ActiveModel::EachValidator |
1 | class User < ActiveRecord::Base |
1 | u = User.new |
1 | # 创建 |
创建对象回调
更新对象
销毁对象
事务回调
before_*
after_*
eg:1
2
3
4
5
6
7
8
9
10
11
12class User < ActiveRecord::Base
# 仅仅当创建的时候执行AfterCommit
after_commit :AfterCommit, on: :create
# test? 为真, 才会执行BeforeSave
before_save :BeforeSave, if: :test?
def test?
true
end
def BeforeSave
puts 'before_save'
end
end
rvm ruby ..
查看安装的ruby1
rvm list
安装指定版本ruby1
rvm install ruby 2.2.2
使用制定版本ruby1
rvm use 2.2.2
查看所有gem
源1
$ gem sources -l
移除一个gem
源1
$ gem sources --remove https://rubygems.org/
添加一个gem
源1
$ gem source -a https://gems.ruby-china.org
gem安装rails
1
$ gem install rails -v 4.2.3
rails
创建项目1
$ rails new [myPro] [--skip-bundle]
eg:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20# 手动配置posts的show路由 as配置路由名称
# get 'posts/:id', :to => 'posts#show', :as => 'show_post'
# 排除show路由
# resources :posts, :except => :show
resources :posts do
# posts/recent
# get 'recent', :on => :collection
# 集合路由
collection do
# posts/recent
get 'recent'
end
# 成员路由
member do
# posts/:id/recent
get 'recent'
end
end
snippet-key @js function debounce
1 | // underscore v1.7.0相关的源码剖 |
功能和underscore实现一样, 去除了underscore的依赖
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 /**
* 包裹函数 一段时间间隔只执行函数一次
* @param funcRef 函数引用
* @param wait default, 300ms
* 是否立即触发,触发后wait时间内不触发该方法,wait时间后才可以重新触发
* false:wait后触发 没有返回值
* true: wait前触发, 可以有返回值
* @param immediate default false
* return : 返回新的函数,使用:将需要调用funcRef的地方,替换为调用新返回的函数即可
*/
function debounce(funcRef, wait, immediate) {
var context, args, timestamp, timeout;
wait = wait || 300;
return function () {
context = this;
args = arguments;
timestamp = new Date().getTime();
var nowCall = immediate && !timeout;
if(!timeout){
timeout = setTimeout(later, wait)
}
if(nowCall){
var result = funcRef.apply(context, arguments);
context = args = null;
return result;
}
};
function later() {
// 当wait指定的时间间隔期间多次调用_.debounce返回的函数,则会不断更新timestamp的值,导致last < wait && last >= 0一直为true,从而不断启动新的计时器延时执行func
var last = new Date().getTime() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
}else {
timeout = null;
if (!immediate) {
funcRef.apply(context, args);
if (!timeout) context = args = null;
}
}
}
}
1 | #git reset --hard HEAD~1 # 取消当前版本之前的一次提交 |
在Mac OS X中这个操作竟然如此简单。只需在Terminal中输入如下的命令:1
git config --global credential.helper osxkeychain
然后在git操作时只要输入一次用户名与密码,以后就不用输入了。
【参考资料】
currency
(货币处理)1 | {{num | currency : '¥'}} //默认为 '$' |
date
(日期格式化)更详细配置参见这里1 | {{ today | date:'yyyy-MM-dd' }} //2015-01-01 |
filter
(匹配字串)1 | $scope.childrenArray = [ |
json
(对象格式化成json字符串)1 | {{ jsonTest | json}} //没有参数。这东西有什么用呢,我一般也不会在页面上输出一个json串啊,官网说它可以用来进行调试,嗯,是个不错的选择。 |
limitTo
(限制数组长度或者字符串长度)1 | #有点鸡肋,首先只能从数组或字符串的开头进行截取,其次,js原生的函数就可以代替它了 |
lowercase
(小写)uppercase
(大写)number
(格式化数字)1 | //在html中用法 |
orderBy
(排序)1 | //定义filters module |
【参考】
1 | # 希望将redis安装到此目录 |
/etc/init.d/
目录下的脚本就类似与windows中的注册表,在系统启动的时候某些指定脚本将被执行 /usr/local/src/redis-3.0.7/utils/redis_init_script
/etc/init.d/
目录下,并命名为redis1 | $ [sudo] cp /usr/local/src/redis-3.0.7/utils/redis_init_script /etc/init.d/redis |
打开redis脚本文件,并进行编辑1
2
3
4
5
6
7
8
9$ vim /etc/init.d/redis
#修改到正确的路径
#EXEC=/usr/local/bin/redis-server
EXEC=/usr/local/redis/bin/redis-server
#CLIEXEC=/usr/local/bin/redis-cli
CLIEXEC=/usr/local/redis/bin/redis-cli
# 设置配置文件路径
#CONF="/etc/redis/${REDISPORT}.conf"
在上面shell脚本指定的配置目录中创建文件,并进行编辑。更详细配置参考这里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$ [sudo] mkdir /etc/redis/
$ [sudo] vim /etc/redis/6379.conf
写入配置:
#!/bin/sh
# 是否以后台进程运行,默认为no
daemonize yes
# 如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid
pidfile /var/run/redis.pid
# 监听端口,默认为6379
port 6379
#绑定主机IP,默认值为127.0.0.1(注释)
bind 127.0.0.1
# 超时时间,默认为300(秒)
timeout 300
# 日志记录等级,有4个可选值,debug,verbose(默认值),notice,warning
loglevel verbose
#可用数据库数,默认值为16,默认数据库为0
databases 16
#指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。这个可以多个条件配合,比如默认配置文件中的设置,就设置了三个条件。
# 900秒(15分钟)内至少有1个key被改变
save 900 1
# 300秒(5分钟)内至少有10个key被改变
save 300 10
# 存储至本地数据库时是否压缩数据,默认为yes
rdbcompression yes
# 本地数据库文件名,默认值为dump.rdb
dbfilename dump.rdb
# 本地数据库存放路径,默认值为 ./
dir /usr/apps/redis_db
#开启之后,redis将在每100毫秒时使用1毫秒的CPU时间来对redis的hash表进行重新hash,可以降低内存的使用。当你的使 用场景中,有非常严格的实时性需要,不能够接受Redis时不时的对请求有2毫秒的延迟的话,把这项配置为no。如果没有这么严格的实时性要求,可以设置为yes,以便能够尽可能快的释放内存
activerehashing yes
手动启动命令1
$ service redis start
方便redis客户端的使用,可以将Redis的所在目录添加到系统参数PATH中
/etc/profile
export PATH="$PATH:/usr/local/redis/bin"
1 | $ . /etc/profile |