linux 自启动服务 默认是以root启动的, 但是我们很多服务都是以普通用户启动的, 如果以root启动 , 这样会到来一些权限问题,例如应用中保存的文件等等.
以普通用户启动的核心指令如下:
su - user -f /path/shell
demo:
/etc/rc.local
[wxianfeng@li390-146 ~]$ cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /bin/sh /usr/local/system/www/shell/linode/init_by_root.sh >> /usr/local/system/logs/init.log 2>&1 su - wxianfeng -f /usr/local/system/www/shell/linode/init_by_wxianfeng.sh >> /usr/local/system/logs/init.log 2>&1
init_by_root.sh 脚本
#!/bin/bash # nginx /usr/local/system/nginx/sbin/nginx # mysql /etc/init.d/mysqld start # svn svnserve -d -r /usr/local/system/vcs
init_by_wxianfeng.sh 脚本
#!/bin/bash source /usr/local/rvm/environments/ruby-1.9.2-p180 cd /usr/local/system/www/project_manager thin start -e production -d -p 5000 source /usr/local/rvm/environments/ruby-1.8.7-p334 thin start -C /etc/thin/thin.yml
最近需要为一个站集成 OAuth 协议 第三方站点账号登录, 目前实现了两个 sina weibo 和 qq, 说下大致步骤 和注意事项,关键用了rails里面经典的gem omniauth.
weibo:
1, 到 http://open.weibo.com 申请应用, 会给你 key和secret
2, 有一步会验证你的 webmaster 权限,按照说明来即可
3, 我使用的gem
gem "omniauth", '1.1.0' gem 'omniauth-weibo-oauth2', '0.2.0'
4, 为 omniauth 配置 weibo provider
config/initliazers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do provider :weibo, '1978113365', '82fa3e1654c905b5b545a16945ahjiyb' end
5, 为了便于调试,修改 hosts 把域名指向本地
/etc/hosts
127.0.0.1 www.abc.com
6, 配置 callback 路由
config/routes.rb
match "/auth/:provider/callback" => "sessions#auth"
7, 访问weibo
www.abc.com:3000/auth/weibo
注意这里不能用 localhost访问,不然会得到:
redirect_uri_mismatch
错误
还有测试的时候,需要到 open.weibo.com 中指定测试账号,这个也需要注意下.
8, 成功后你的callback action中会收到返回给的数据,接下来就是你的事情了,基本的 你可以这样看到
Rails.logger.debug request.env["omniauth.auth"]
QQ:
大致步骤和 weibo 一样,但是不同的是 qq 需要配置 回调地址
我在open qq中配置的回调地址是 abc.com
但是本地调试时, 访问abc.com:3000/auth/qq 总是得到
redirect uri is illegal(100010)!
错误
最后找到的原因是 不能用 3000 端口, 需要使用 80 端口, 最后用 nginx + unicorn 配置80端口访问 解决了.
qq 用的gem是
gem 'omniauth-qq', :git => 'git://github.com/blankyao/omniauth-qq.git'
Ok, That’s ALL!!!