在 CentOS 8 上安装所需的 Ruby Gems

我们正在安装 mysql2 gem,用于与 MariaDB/MySQL 数据库服务器的 Ruby 连接。

[root@ruby-on-rails ~]# gem install mysql2
Fetching mysql2-0.5.3.gem
Building native extensions. This could take a while...
Successfully installed mysql2-0.5.3
Parsing documentation for mysql2-0.5.3
Installing ri documentation for mysql2-0.5.3
Done installing documentation for mysql2 after 0 seconds
1 gem installed

Bundler 通过跟踪和安装所需的确切 gem 和版本,为 Ruby 项目提供一致的环境。

我们可以使用 gem 命令安装 Bundler。

[root@ruby-on-rails ~]# gem install bundler
Fetching bundler-2.1.4.gem
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Installing ri documentation for bundler-2.1.4
Done installing documentation for bundler after 4 seconds
1 gem installed

我们的 Ruby on Rails 服务器已配置好,现在可以使用了。

在 CentOS 8 上安装 Ruby on Rails 服务器

Ruby on Rails 或者 Rails 是在 MIT 许可下用 Ruby 编写的服务器端 Web 应用程序框架。

在本文中,我们将学习如何在 CentOS 8 上安装 Ruby on Rails 服务器,以及如何在同一 Ruby on Rails 服务器上创建和运行 Ruby Web 应用程序。

在 CentOS 8 上安装 RVM

RVM(Ruby 版本管理器)是一个命令行工具,可让我们轻松安装和管理从解释器到一组 gem 的多个 Ruby 环境的工作。

我们将在 CentOS 8 上安装 RVM,稍后我们将使用 RVM 命令行安装 Ruby。

RVM 可在其官方 yum 存储库中找到。
因此,我们需要在 CentOS 8 中添加 RVM yum 存储库。

在添加 RVM yum 存储库之前,我们需要为 RVM 存储库添加 GPG 密钥,如下所示。

[root@ruby-on-rails ~]# gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: key 105BD0E739499BDB: 8 signatures not checked due to missing keys
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 105BD0E739499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported
gpg: key 3804BB82D39DC0E3: 108 signatures not checked due to missing keys
gpg: key 3804BB82D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 2
gpg:               imported: 2

如果我们在安装 GPG 密钥时遇到问题,那么我们应该参考 RVM 官方网站。

现在,我们可以如下安装 RVM。

[root@ruby-on-rails ~]# curl -sSL https://get.rvm.io | bash -s stable

但是,我们也可以安装最新的稳定版 Ruby 和 Rails 以及 RVM。
但是为了演示,我们分别安装了每个组件。

RVM 环境脚本已复制到 /etc/profile.d/ 目录。
要为当前会话设置 RVM 环境,我们需要执行一次。

[root@ruby-on-rails ~]# source /etc/profile.d/rvm.sh

RVM 已安装在 CentOS 8 上。

在 CentOS 8 上安装 Ruby on Rails

现在是在 CentOS 8 服务器上安装 Rails 的时候了。
我们可以通过使用 gem (Ruby Package Manager) 方便地做到这一点。

[root@ruby-on-rails ~]# gem install rails

验证 Rails 的版本。

[root@ruby-on-rails ~]# rails -v
Rails 6.0.2.2

Ruby on Rails 默认服务端口是 3000/tcp 。
因此,我们需要在 Linux 防火墙中允许该端口。

[root@ruby-on-rails ~]# firewall-cmd --permanent --add-port=3000/tcp
success
[root@ruby-on-rails ~]# firewall-cmd --reload
success
on  It Road.com

在 CentOS 8 上安装 Ruby

使用 rvm 命令检查并解决 Ruby 缺少的任何准备工作。

[root@ruby-on-rails ~]# rvm requirements
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: patch, autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, make, patch, readline-devel, ruby, sqlite-devel, glibc-headers, glibc-devel........................
Requirements installation successful.

检查已知版本的 Ruby 语言列表。

[root@ruby-on-rails ~]# rvm list known

安装所需的 Ruby 版本。
我们正在安装 MRI Ruby 2.7,如下所示。

[root@ruby-on-rails ~]# rvm install 2.7

通过使用 RVM ,我们可以在同一平台上安装多个 Ruby 环境。
因此,我们需要在使用中设置一个默认版本。

设置 Ruby 语言的默认版本。

[root@ruby-on-rails ~]# rvm use 2.7 --default
Using /usr/local/rvm/gems/ruby-2.7.0

验证当前的 Ruby 版本。

[root@ruby-on-rails ~]# ruby --version
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

CentOS 8 上已经安装了 Ruby 语言。

HelloWorld - 第一个 Ruby 项目

为了测试我们的 Ruby on Rails 环境,我们正在创建一个 HelloWorld 项目。

以下命令将使用默认的文件和目录框架创建 HelloWorld 项目目录。

[root@ruby-on-rails ~]# rails new HelloWorld -d mysql

->-> unpipe@1.0.0
->-> utils-merge@1.0.1
->-> wbuf@1.7.3
->-> webpack-dev-middleware@3.7.2
->-> webpack-dev-server@3.10.3
->-> websocket-extensions@0.1.3
->-> ws@6.2.1
->-> yargs-parser@11.1.1
->-> yargs@12.0.5
Done in 11.72s.

在 database.yml 文件中设置 MariaDB 数据库凭据。

[root@ruby-on-rails ~]# cd HelloWorld
[root@ruby-on-rails HelloWorld]# vi config/database.yml

在默认配置下设置 root 用户密码。

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: 123 # Set the MariaDB Password here
  socket: /var/lib/mysql/mysql.sock

使用 rake 命令为我们的 Ruby 项目创建数据库。

[root@ruby-on-rails HelloWorld]# rake db:create
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Created database 'HelloWorld_development'
Created database 'HelloWorld_test'

启动我们的 Ruby on Rails 应用程序。

[root@ruby-on-rails HelloWorld]# rails server -b 0.0.0.0
Warning: the running version of Bundler (2.1.2) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`.
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Warning: the running version of Bundler (2.1.2) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`.
=> Booting Puma
=> Rails 6.0.2.2 application starting in development
=> Run `rails server --help` for more startup options
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Puma starting in single mode...
* Version 4.3.3 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

在 google chrome 浏览器中浏览 URL http://ruby-on-rails.onitroad.com:3000。

我们已经在 CentOS 8 上成功安装了 Ruby on Rails Server。

在 CentOS 8 上安装 Ruby on Rails 必备软件包

Ruby on Rails 需要一些软件包。
但是,大多数依赖项是由 RVM(Ruby 版本管理器)自动解决的,但有些软件包我们必须手动安装。

Ruby on Rails 的第一个依赖项是 Git 。
Git 在标准 yum 存储库中可用,因此我们可以在 CentOS 8 上如下安装 git。

[root@ruby-on-rails ~]# dnf install -y git

Ruby on Rails 的一些组件需要 Node.js,因此我们也需要安装它。

Node.js 在标准 yum 存储库中不可用。
但是,可以从 NodeSource yum 存储库安装 Node.js。

在 CentOS 8 服务器中添加 NodeSource yum 存储库。

[root@ruby-on-rails ~]# curl -sL https://rpm.nodesource.com/setup_12.x | bash

现在,我们可以使用 dnf 命令在 CentOS 8 上安装 Node.js。

[root@ruby-on-rails ~]# dnf install -y nodejs

除了 Node.js ,Ruby on Rails 还需要 YARN (Package Manager) 。
YARN 也由其官方 yum 存储库提供。
因此,我们将在 CentOS 8 中添加 YARN yum 存储库。

[root@ruby-on-rails ~]# curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo
[yarn]
name=Yarn Repository
baseurl=https://dl.yarnpkg.com/rpm/
enabled=1
gpgcheck=1
gpgkey=https://dl.yarnpkg.com/rpm/pubkey.gpg

现在,我们可以使用 dnf 命令轻松安装 yarn。

[root@ruby-on-rails ~]# dnf install -y yarn

在 CentOS 8 上安装 MariaDB 服务器

MariaDB Server 10.3 在标准 yum 存储库中可用,因此,我们使用 dnf 命令安装它。

[root@ruby-on-rails ~]# dnf install -y mariadb-server

启用并启动 MariaDB 服务。

[root@ruby-on-rails ~]# systemctl enable --now mariadb.service
Created symlink /etc/systemd/system/mysql.service -> /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service -> /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service -> /usr/lib/systemd/system/mariadb.service.

使用以下命令配置 MariaDB 服务器并设置 root 用户密码。

[root@ruby-on-rails ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

我们已经在 CentOS 8 上成功安装了 MariaDB 服务器。

日期:2020-09-17 00:10:39 来源:oir作者:oir