将Rails环境部署在Centos7上
由于经常在虚拟环境中搭建Rails,因此我创建了这份备忘录。
我可以根据其他人提供的步骤进行构建,但如果文章的更新日期较旧,就需要反复确认内容的一致性,这很麻烦。因此,我尽量在查阅官方内容的同时编写了这篇文章。
执行环境
・Centos 7.2:
Centos 7.2深植
・Ruby 2.5.1:
Ruby版本2.5.1
・Rails 5.1.6:
Rails版本5.1.6
・PostgreSQL 9.6:
PostgreSQL版本9.6
安装Git
$ sudo yum update
$ sudo yum install git
安装Ruby
安装构建Ruby所需的软件包
如果没有执行这个步骤而安装Ruby,会发生错误。
$ sudo yum install -y openssl-devel readline-devel zlib-devel
安装rbenv和ruby-build。
安装rbenv
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
在 `~/.bash_profile` 文件中添加 `export PATH=”$HOME/.rbenv/bin:$PATH”` 和 `eval “$(rbenv init -)”`
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ ~/.rbenv/bin/rbenv init
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
将附加内容应用/反映
$ source ~/.bash_profile
安装ruby-build
$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
请使用rbenv-doctor进行确认,检查是否成功设置。
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Checking for `rbenv' in PATH: /home/vagrant/.rbenv/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /home/vagrant/.rbenv/plugins/ruby-build/bin/rbenv-install (ruby-build 20180822)
Counting installed Ruby versions: none
There aren't any Ruby versions installed under `/home/vagrant/.rbenv/versions'.
You can install Ruby versions like so: rbenv install 2.2.4
Checking RubyGems settings: OK
Auditing installed plugins: OK
确认可以安装的Ruby版本
$ rbenv install -l
安装Ruby
$ rbenv install 2.5.1
指定要使用的Ruby版本。
$ rbenv global 2.5.1
确认安装成功
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
安装 Rails
安装Bundler
$ gem install bundler
・安装 Rails(此次指定版本为v5.1.6)。
$ gem install rails -v 5.1.6 --no-rdoc --no-ri
$ rails -v
Rails 5.1.6
安装PostgreSQL
添加RPM存储库
$ sudo yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
安装客户端和服务器。
$ sudo yum install postgresql96 postgresql96-server postgresql-devel
$ psql --version
psql (PostgreSQL) 9.6.10
进行数据库初始化
$ sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
Initializing database ... OK
自动化PostgreSQL的启动
$ sudo systemctl enable postgresql-9.6
$ sudo systemctl start postgresql-9.6
确认PostgreSQL运行正常
$ systemctl list-unit-files -t service | grep postgresql
postgresql-9.6.service enabled
$ systemctl status postgresql-9.6
● postgresql-9.6.service - PostgreSQL 9.6 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)
Active: active (running) since 土 2018-08-25 19:54:15 BST; 21s ago
Docs: https://www.postgresql.org/docs/9.6/static/
Process: 23163 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 23169 (postmaster)
CGroup: /system.slice/postgresql-9.6.service
├─23169 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
├─23171 postgres: logger process
├─23173 postgres: checkpointer process
├─23174 postgres: writer process
├─23175 postgres: wal writer process
├─23176 postgres: autovacuum launcher process
└─23177 postgres: stats collector process
创建一个新的Rails应用
Rails默认的数据库是SQLite3,因此要指定使用PostgreSQL。
$ rails new アプリ名 -d postgresql
请参考以下网站
・Ruby的安装步骤:
https://www.ruby-lang.org/ja/documentation/installation/
・Git的安装步骤:
https://git-scm.com/book/ja/v1/%E4%BD%BF%E3%81%84%E5%A7%8B%E3%82%81%E3%82%8B-Git%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB
・rbenv的安装步骤
https://github.com/rbenv/rbenv
・ruby-build的安装步骤
https://github.com/rbenv/ruby-build#readme
・Ruby on Rails的安装步骤
https://github.com/rails/rails
・PostgreSQL的安装步骤
https://www.postgresql.org/download/linux/redhat/