操作系統: linux(centos6.5)
為什么選擇postgresql
1. postgresql支持全文檢索;
2. 在創建一些數據庫對象如索引時,不會加表鎖,使得部署和遷移應用更加平滑;
3. 支持過程化編程語言;
4. 數據庫復制模式靈活,異步復制模式下仍可以對特定操作進行同步復制。
安裝postgresql
1. 下載源碼包:9.4.1
wget https://ftp.postgresql.org/pub/source/v9.4.1/postgresql-9.4.1.tar.gz
2. ?安裝系統依賴: ?
yum install openssl openssl-devel libxml2 libxml2-devel?
3. ?編譯安裝源碼包
mkdir -p /opt/postgresql
./configure --prefix=/opt/postgresql
make && make install
4. 添加一個用戶專用于操作postgresql并初始化數據庫
useradd postgres
mkdir -p /home/postgres/data
/opt/postgresql/bin/initdb -D /home/postgres/data
5. ?啟動數據庫
/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項目
git clone project_url
6. 進入項目目錄中,修改gemfile,將如下文本加入。
gem 'pg'
如果需要使用unicorn,則在gemfile中加入如下文本
gem 'unicorn'
然后bundle install安裝項目所有依賴
另外,在安裝依賴時需要配置pgconfig的路徑,因為我們并沒有將postgresql裝到系統默認路徑下。
bundle config pg.build -- --with-pg-config=/opt/postgresql/bin/pg_config
bundle install --path=./vendor
7. 初始化項目的數據庫,首先要創建一個postgresql用戶,然后使用這個用戶連接并創建數據庫。
/opt/postgresql/bin/createuser -d -P 'username'
bundle exec rake db:setup RAILS_ENV=production?
8. 如果是產品環境,則需要編譯壓縮前端文件,這些文件會被生成在public下。
bundle exec rake assets:precompile RAILS_ENV=production
9. 使用unicorn啟動server, -D表示unicorn將作為守護進程運行。
bundle exec unicorn -E production -D?
nginx
1. 安裝依賴, pcre是一個正則表達式的輕量級函數庫,比boost的正則表達式庫小得多,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; 配置靜態文件root路徑;按系統核數配置work進程數。以下是部分配置。
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. 啟動nginx
/opt/nginx/sbin/nginx
自此,服務已可訪問!