[Deploy]Ubuntu + Nginx + Passenger + PostgreSQL + Backup

软件版本

  • Ubuntu: 12.04
  • Nginx: 1.2.2
  • Passenger: 3.0.15
  • PostgreSQL: 9.1

服务器准备

连接到服务器

ssh root@{new_ip_address}

创建用户

adduser rails

修改 Linux 默认编辑器

sudo update-alternatives --config editor

给新用户添加sudo权限

visudo

# 在 root ALL=(ALL) ALL 下面添加
rails ALL=(ALL) ALL

退出,以 rails 用户登录

ssh rails@{new_ip_address}

更新源、安装各种包

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential automake ssh
sudo apt-get install zlib1g-dev libssl-dev libreadline6-dev libyaml-dev libxml2-dev libxslt-dev
sudo apt-get install libsqlite3-dev libcurl4-openssl-dev

使用 RMagick 的话,执行一下命令

sudo apt-get install imagemagick libmagickwand-dev

设置防火墙

sudo ufw default deny
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 5432/tcp
sudo ufw enable

# 查看状态
sudo ufw status

安装 Ruby

创建 .gemrc 文件,加入如下设置

install: --no-ri --no-rdoc
update: --no-ri --no-rdoc

安装 Rails

gem install rails

安装 Passenger

gem install passenger

安装 Passenger + Nginx

passenger-install-nginx-module

安装 PostgreSQL

sudo apt-get install postgresql

设置 PostgreSQL

添加远程可访问的IP

sudo vim /etc/postgresql/9.1/main/pg_hba.conf

# 在最后一行加上
host   all    all    {server_ip}/24   md5

设置允许远程访问

sudo vim /etc/postgresql/9.1/main/postgresql.conf

# 设置 listen_addresses = 'localhost' 成
listen_addresses = '*'

创建用户

sudo su - postgres
createuser -a {new_user}
createdb -E UTF8 -O {new_user} {project_name}_production -T template0
psql -c "alter user postgres with password 'new pass'"
psql -c "alter user {new_user} with password 'new pass'"
chmod 400 root/postgres_*_password
exit
# 通常,这里的 new_user = project_name

以 {new_user} 身份登录测试

psql -U {new_user} {project_name}_production

退出 PostgreSQL 的终端

q

初始化数据库

# 在项目目录下,如:
/home/rails/sites/{project_name}/
# 执行:
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake db:seed
# 加载省数据(如果必要)
RAILS_ENV=production rails runner script/load_prefectures.rb

备份

安装 Backup

gem install backup

执行生成命令

backup generate:model --trigger my_backup --databases='postgresql' --storages='dropbox,sftp' --compressors='gzip' --notifiers='mail'

配置参数

# encoding: utf-8

##
# Backup Generated: my_backup
# Once configured, you can run the backup with the following command:
#
# $ backup perform -t my_backup [-c <path_to_configuration_file>]
#
Backup::Model.new(:my_backup, 'Description for my_backup') do
  ##
  # Split [Splitter]
  #
  # Split the backup file in to chunks of 250 megabytes
  # if the backup file size exceeds 250 megabytes
  #
  split_into_chunks_of 250

  ##
  # PostgreSQL [Database]
  #
  database PostgreSQL do |db|
    db.name               = "db_name"
    db.username           = "db_username"
    db.password           = "db_password"
    db.host               = "db_host"
    db.port               = 5432
    # db.socket             = "/tmp/pg.sock"
    # db.skip_tables        = ["skip", "these", "tables"]
    # db.only_tables        = ["only", "these" "tables"]
    db.additional_options = ["-xc", "-E=utf8"]
    # Optional: Use to set the location of this utility
    #   if it cannot be found by name in your $PATH
    # db.pg_dump_utility = "/opt/local/bin/pg_dump"
  end

  ##
  # Dropbox File Hosting Service [Storage]
  #
  # Access Type:
  #
  #  - :app_folder (Default)
  #  - :dropbox
  #
  # Note:
  #
  #  Initial backup must be performed manually to authorize
  #  this machine with your Dropbox account.
  #
  store_with Dropbox do |db|
    db.api_key     = "app_key"
    db.api_secret  = "app_secret"
    db.access_type = :app_folder
    db.path        = "/"
    db.keep        = 25
  end

  ##
  # SFTP (Secure File Transfer Protocol) [Storage]
  #
  store_with SFTP do |server|
    server.username   = "rails"
    server.password   = "secret"
    server.ip         = "server_ip"
    server.port       = 22
    server.path       = "~/backups/"
    server.keep       = 5
  end

  ##
  # Gzip [Compressor]
  #
  compress_with Gzip

  ##
  # Mail [Notifier]
  #
  # The default delivery method for Mail Notifiers is 'SMTP'.
  # See the Wiki for other delivery options.
  # https://github.com/meskyanichi/backup/wiki/Notifiers
  #
  notify_by Mail do |mail|
    mail.on_success           = true
    mail.on_warning           = true
    mail.on_failure           = true

    mail.from                 = 'sender@email.com'
    mail.to                   = 'receiver@email.com'
    mail.address              = 'smtp.gmail.com'
    mail.port                 = 587
    mail.domain               = 'your.host.name'
    mail.user_name            = 'sender@email.com'
    mail.password             = 'my_password'
    mail.authentication       = 'plain'
    mail.enable_starttls_auto = true
  end

end

安装 Dropbox,SFTP 相关 Gem

gem install dropbox-sdk -v '~> 1.2.0'
gem install net-ssh -v '~> 2.3.0'

手动测试备份

backup perform -t my_backup

Whenever 自动备份

安装

gem install whenever

生成

cd /apps/my-great-project
wheneverize .

设置

# config/schedule.rb
every 1.day, :at => '10:00pm' do
  command "backup perform --trigger my_backup"
end

执行 whenever

whenever

把这个工作写进 crontab

whenever --update-crontab

查看 crontab 内容

crontab -l

Cron

打开日志服务配置文件

sudo vim /etc/rsyslog.d/50-default.conf

# 去掉这样前面的‘#’
#cron.*                /var/log/cron.log

重启 rsyslog 和 cron 服务

sudo /etc/init.d/rsyslog restart
sudo /etc/init.d/cron restart

查看 cron 的主要配置文件

cat /etc/crontab

※重要说明(CentOS和Ubuntu的不同)※
CentOS(5.7):
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin

Ubutnu(12.04):
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

编辑任务文件

crontab -e

这样就在每天22点自动运行备份了。

相关链接

在CentOS上部署Rails项目(Apache+Passenger)

一、CentOS 5.8 环境准备

查看系统版本

cat /etc/issue

安装各种包

$ yum update
$ yum -y install gcc gcc-c++ autoconf
$ yum -y install openssl-devel zlib-devel readline-devel curl-devel gettext-devel
$ yum -y install httpd-devel sqlite-devel

安装libyaml

$ cd /usr/local/src
$ wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
$ tar xzf yaml-0.1.4.tar.gz
$ cd yaml-0.1.4
$ ./configure
$ make && make install

创建新用户

$ useradd -m cho
$ passwd cho

$ adduser cho

给 cho 用户赋予 sudo 权限

visudo

(
如果提示:
bash: visudo: command not found
就执行下列命令安装:
yum install sudo
)

root ALL=(ALL) ALL 下面加一行:

cho ALL=(ALL) ALL

保存退出。

二、安装 Ruby

% mkdir ~/src
% cd ~/src
% wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz
% tar xzf ruby-1.9.3-p194.tar.gz
% cd ruby-1.9.3-p194
% ./configure
% make
% sudo make install

% which ruby
/usr/local/bin/ruby
% ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]

三、安装 Rails 应用程序

创建 Rails 用户

% sudo useradd -m -d /var/rails rails
% sudo passwd rails
% sudo chmod 750 /var/rails

添加执行 Apache 的用户到 rails 组

% sudo gpasswd -a apache rails

安装 Bundler

% sudo gem install bundler --no-ri --no-rdoc

安装 Rails 应用程序

% sudo su - rails
% git clone ssh://git@123.456.789.0/home/git/my_great_project.git
% cd my_great_project
% bundle install --path=../bundle
% RAILS_ENV=production rake db:setup
% rake assets:precompile

四、安装 Apache 和 Phusion Passenger

切换到 root 用户

su - root

安装 Phusion Passenger

% sudo gem install passenger --no-ri --no-rdoc
% sudo passenger-install-apache2-module

准备 Apache

% sudo -s
$ cd /etc/httpd
$ echo "Include /etc/httpd/sites-enabled/" >> conf/httpd.conf
$ mkdir sites-available
$ mkdir sites-enabled
$ rm conf.d/welcome.conf

编辑 /etc/httpd/conf/httpd.conf,去掉前面的 #

#ServerName www.example.com:80

#NameVirtualHost *:80

把 Phusion Passenger 和 Apache 相连
新建文件 /etc/httpd/conf.d/passenger.conf,加入下列内容

LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.15/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.15
PassengerRuby /usr/local/bin/ruby

添加一个虚拟主机
新建文件 /etc/httpd/sites-available/my_great_project,加入下列内容

<VirtualHost *:80>
  ServerName my_great_project.com
  DocumentRoot /var/rails/my_great_project/public
  RackEnv production
  <Directory /var/rails/my_great_project/public>
    AllowOverride all
    Options -MultiViews
  </Directory>
</VirtualHost>

建立符号连接

$ ln -s /etc/httpd/sites-available/my_great_project /etc/httpd/sites-enabled/001-my_great_project

重启 Apache

$ service httpd start

参考文章

开发Linkedin应用

注意点

1.取得access keys

require "rubygems"
require "linkedin"

client = LinkedIn::Client.new('api_key', 'secret_key')
rtoken = client.request_token.token
rsecret = client.request_token.secret

puts client.request_token.authorize_url

puts "Access this URL get the PIN and paste it here:"
pin = gets.strip

puts client.authorize_from_request(rtoken, rsecret, pin)

2.把得到的access keys粘贴到程序代码中

client = LinkedIn::Client.new('api_key', 'secret_key')
client.authorize_from_access("key1", "key2")
client.profile(:url => 'http://www.linkedin.com/in/netherland')

[Linux]html 和 pdf 文件的互换方法

html to pdf

sudo apt-get install wkhtmltopdf

wkhtmltopdf http://www.webupd8.org webupd8.pdf

pdf to html

sudo apt-get install poppler-utils

pdftohtml -c [filename].pdf [filename].html

Useful Links