ruby on rails + postgresql + nginx的搭建

操作系統(tǒng): linux(centos6.5)

為什么選擇postgresql

1. postgresql支持全文檢索;

2. 在創(chuàng)建一些數(shù)據(jù)庫(kù)對(duì)象如索引時(shí),不會(huì)加表鎖,使得部署和遷移應(yīng)用更加平滑;

3. 支持過(guò)程化編程語(yǔ)言;

4. 數(shù)據(jù)庫(kù)復(fù)制模式靈活,異步復(fù)制模式下仍可以對(duì)特定操作進(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ù)庫(kù)

useradd postgres

mkdir -p /home/postgres/data

/opt/postgresql/bin/initdb -D /home/postgres/data

5. ?啟動(dòng)數(shù)據(jù)庫(kù)

/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)槲覀儾](méi)有將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ù)庫(kù),首先要?jiǎng)?chuàng)建一個(gè)postgresql用戶,然后使用這個(gè)用戶連接并創(chuàng)建數(shù)據(jù)庫(kù)。

/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ù)庫(kù),比boost的正則表達(dá)式庫(kù)小得多,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, 開(kāi)啟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ù)已可訪問(wèn)!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容