操作系統(tǒng): linux(centos6.5)
為什么選擇postgresql
1. postgresql支持全文檢索;
2. 在創(chuàng)建一些數(shù)據(jù)庫對象如索引時(shí)去扣,不會(huì)加表鎖,使得部署和遷移應(yīng)用更加平滑驮瞧;
3. 支持過程化編程語言粪摘;
4. 數(shù)據(jù)庫復(fù)制模式靈活,異步復(fù)制模式下仍可以對特定操作進(jìn)行同步復(fù)制算墨。
安裝postgresql
1. 下載源碼包:9.4.1
wget https://ftp.postgresql.org/pub/source/v9.4.1/postgresql-9.4.1.tar.gz
2. ?安裝系統(tǒng)依賴: ?
yum install openssl openssl-devel libxml2 libxml2-devel?
3. ?編譯安裝源碼包
mkdir -p /opt/postgresql
./configure --prefix=/opt/postgresql
make && make install
4. 添加一個(gè)用戶專用于操作postgresql并初始化數(shù)據(jù)庫
useradd postgres
mkdir -p /home/postgres/data
/opt/postgresql/bin/initdb -D /home/postgres/data
5. ?啟動(dòng)數(shù)據(jù)庫
/opt/postgresql/bin/posgres -D /home/postgres/data
安裝ruby on rails
1. 下載ruby源碼包
wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.1.tar.gz
2. 安裝依賴(可選)
yum install libyaml libyaml-devel
3. 編譯安裝ruby
mkdir -p /opt/ruby
./configure --prefix=/opt/ruby
make && make install
4. 安裝bundler
/opt/ruby/bin/gem install bundler
5. 下載ruby on rails項(xiàng)目
git clone project_url
6. 進(jìn)入項(xiàng)目目錄中宵荒,修改gemfile,將如下文本加入净嘀。
gem 'pg'
如果需要使用unicorn报咳,則在gemfile中加入如下文本
gem 'unicorn'
然后bundle install安裝項(xiàng)目所有依賴
另外,在安裝依賴時(shí)需要配置pgconfig的路徑挖藏,因?yàn)槲覀儾]有將postgresql裝到系統(tǒng)默認(rèn)路徑下暑刃。
bundle config pg.build -- --with-pg-config=/opt/postgresql/bin/pg_config
bundle install --path=./vendor
7. 初始化項(xiàng)目的數(shù)據(jù)庫,首先要?jiǎng)?chuàng)建一個(gè)postgresql用戶膜眠,然后使用這個(gè)用戶連接并創(chuàng)建數(shù)據(jù)庫岩臣。
/opt/postgresql/bin/createuser -d -P 'username'
bundle exec rake db:setup RAILS_ENV=production?
8. 如果是產(chǎn)品環(huán)境袁翁,則需要編譯壓縮前端文件,這些文件會(huì)被生成在public下婿脸。
bundle exec rake assets:precompile RAILS_ENV=production
9. 使用unicorn啟動(dòng)server, -D表示unicorn將作為守護(hù)進(jìn)程運(yùn)行粱胜。
bundle exec unicorn -E production -D?
nginx
1. 安裝依賴, pcre是一個(gè)正則表達(dá)式的輕量級(jí)函數(shù)庫,比boost的正則表達(dá)式庫小得多狐树,pcre的作用在于讓nginx支持rewrite功能焙压。
yum install pcre-devel
2. 編譯安裝(編譯static_gzip模塊)
mkdir -p /opt/nginx
./configure --prefix=/opt/nginx --with-http-gzip-static-module
make && make install
3. 配置nginx.conf, 開啟gzip,gzip_static; 配置靜態(tài)文件root路徑;按系統(tǒng)核數(shù)配置work進(jìn)程數(shù)抑钟。以下是部分配置涯曲。
worker_processes? 4;
gzip? on;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss application/javascript;
location ~ ^/(assets)/? {
root? /opt/virus-backend/manage-backend/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
4. 啟動(dòng)nginx
/opt/nginx/sbin/nginx
自此,服務(wù)已可訪問在塔!