1. 程式人生 > >國科大ruby選課原型系統的環境搭建(Ubuntu 16.04)

國科大ruby選課原型系統的環境搭建(Ubuntu 16.04)

步驟:

1.檢視系統版本

[email protected]:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.3 LTS
Release:	16.04
Codename:	xenial

2.更新軟體(可以避免以後遇到的一些問題)

[email protected]:~$ sudo apt-get update

3.安裝curl(如果沒安裝curl的話才安裝)

[email protected]
:~$ sudo apt-get install curl Reading package lists... Done Building dependency tree Reading state information... Done

4.安裝rvm(curl是一種命令列工具,作用是發出網路請求,然後得到和提取資料,顯示在"標準輸出"(stdout)上面。使用-L引數,curl就會跳轉到新的網址。)

[email protected]:~$ curl -L get.rvm.io | bash -s stable
	  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
									 Dload  Upload   Total   Spent    Left  Speed
	100   194  100   194    0     0     77      0  0:00:02  0:00:02 --:--:--    77
	100 24361  100 24361    0     0   5050      0  0:00:04  0:00:04 --:--:-- 15321
	Downloading https://github.com/rvm/rvm/archive/1.29.4.tar.gz

4.1如果公鑰報錯的話執行以下操作

[email protected]:~$ gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
[email protected]:~$ 
[email protected]:~$ curl -sSL https://get.rvm.io | bash -s stable
[email protected]:~$ 
[email protected]:~$ source ~/.bashrc  #這兩個加入之後就能檢測到ruby和rails
[email protected]
:~$ source ~/.bash_profile #這兩個加入之後就能檢測到ruby和rails

5.使用RVM安裝RUBY

[email protected]:~$ echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > ~/.rvm/user/db #輸出重定向到檔案,這個語句似乎並沒有用
[email protected]:~$ rvm list known
[email protected]:~$ rvm install "ruby-2.3.0"

6.安裝rails

[email protected]:~$ gem install rails -v 4.2.6
[email protected]:~$ rails -v

7.安裝git並下載原型系統

[email protected]:~/Desktop$ sudo apt install git
[email protected]:~/Desktop$ git clone https://github.com/PENGZhaoqing/CourseSelect
[email protected]:~/Desktop$ cd CourseSelect/

8.執行bundle install

[email protected]:~/Desktop/CourseSelect$ bundle install#可能的報錯資訊參考本檔案上面部分

9.安裝postgresql和pgadmin3,並建立新使用者dbuser

[email protected]:~/Desktop/CourseSelect$ sudo apt-get install postgresql
[email protected]:~/Desktop/CourseSelect$ sudo adduser dbuser #新建一個Linux新使用者,可以取你想要的名字,這裡為dbuser。
[email protected]:~/Desktop/CourseSelect$ sudo apt-get install pgadmin3

10.切換使用者並啟動postgres

[email protected]:~$ sudo su - postgres
[email protected]:~$ psql

11.建立使用者並設定密碼

postgres=# \password postgres
postgres=# CREATE USER dbuser WITH PASSWORD 'dbuser';
CREATE ROLE

12.新建資料庫並授權

postgres=# CREATE DATABASE dbuser OWNER dbuser;
CREATE DATABASE
postgres=# CREATE DATABASE courseselect_development OWNER dbuser;
CREATE DATABASE

13.給資料庫授權(貌似沒用)

postgres=# GRANT ALL PRIVILEGES ON DATABASE dbuser to dbuser;
GRANT
postgres=# \q

14.切換使用者,並解決ruby -v失效問題

[email protected]:~$ su wsy
Password: 
[email protected]:~$ cd ~/Desktop/CourseSelect/
[email protected]:~/Desktop$ source ~/.bashrc & source ~/.bash_profile

15.執行rake db:migrate和rake db:seed

[email protected]:/var/lib/postgresql$ cd ~/Desktop/CourseSelect/
[email protected]:~/Desktop/CourseSelect$ rake db:migrate
[email protected]:~/Desktop/CourseSelect$ rake db:seed

16.啟動rails server

[email protected]:~/Desktop/CourseSelect$ rails s 

問題彙總

  1. su: Authentication failure 解決:在ubuntu中,root賬戶預設不可用。使用這個獲得root:sudo -i
  2. rake db:migrate報錯:Ruby on Rails , No Rakefile found
    解決: https://stackoverflow.com/questions/18452701/ruby-on-rails-no-rakefile-found-error
  3. postgresql相關知識:
  4. 切換使用者並啟動postgres(3種方法)

法1:

[email protected]:~/Desktop/CourseSelect$ sudo su - postgres #然後,切換到postgres使用者。
[email protected]:/home/wsy/Desktop/CourseSelect$ psql #下一步,使用psql命令登入PostgreSQL控制檯。

法2:

[email protected]:~/Desktop/CourseSelect$ sudo -u postgres psql
psql (9.5.14)
Type "help" for help.

法3:

[email protected]:/etc/postgresql/9.5/main$ psql -h 127.0.0.1 -U postgres
  1. 修改PostgreSQL資料庫預設使用者postgres的密碼(兩種方法)

法1:

postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; #第一件事是使用\password命令,為postgres使用者設定一個密碼。(不要落下分號)

法2:

postgres=# \password postgres
Enter new password: 
Enter it again: 
ALTER ROLE
  1. 執行bundle install報錯:

報錯1: Could not fetch specs from https://gems.ruby-china.org/
詳細資訊:

[email protected]:~/Desktop/CourseSelect$ bundle install
Fetching source index from https://gems.ruby-china.org/
Retrying fetcher due to error (2/4): Bundler::HTTPError Could not fetch specs from https://gems.ruby-china.org/
Retrying fetcher due to error (3/4): Bundler::HTTPError Could not fetch specs from https://gems.ruby-china.org/
Retrying fetcher due to error (4/4): Bundler::HTTPError Could not fetch specs from https://gems.ruby-china.org/
Could not fetch specs from https://gems.ruby-china.org/

解決思路: 將原來的https://gems.ruby-china.org/修改為https://gems.ruby-china.com/

[email protected]:~/Desktop/CourseSelect$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
[email protected]:~/Desktop/CourseSelect$ gem sources -l #檢視修改後的配置
[email protected]:~/Desktop/CourseSelect$ cd CourseSelect
[email protected]:~/Desktop/CourseSelect$ vi Gemfile 然後將第一行改為:source 'https://gems.ruby-china.com'	

報錯2: An error occurred while installing pg (0.18.4)
詳細資訊:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/wsy/.rvm/gems/ruby-2.3.0/gems/pg-0.18.4/ext
/home/wsy/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20181118-99965-1ksvqt7.rb extconf.rb
checking for pg_config... no	No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header   
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.
...
An error occurred while installing pg (0.18.4), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.4' --source 'https://gems.ruby-china.com/'` succeeds before bundling.

解決:

[email protected]:~/Desktop/CourseSelect$ sudo apt-get install libpq-dev
[email protected]:~/Desktop/CourseSelect$ gem install pg
[email protected]:~/Desktop/CourseSelect$ bundle install