CentOS 7 配置Ruby语言开发环境

前端之家收集整理的这篇文章主要介绍了CentOS 7 配置Ruby语言开发环境前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

CentOS 7 配置Ruby语言开发环境

安装Ruby 2.2

CentOS7存储库中的Ruby版本为2.0,但如果需要,可以使用RPM软件包安装2.2

  • 添加CentOS SCLo软件集合存储库
  1. [root@linuxprobe ~]# yum -y install centos-release-scl-rh centos-release-scl
  2. # set [priority=10]
  3. [root@linuxprobe ~]# sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/CentOS-SCLo-scl.repo
  4. [root@linuxprobe ~]# sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
  5. # for another way,change to [enabled=0] and use it only when needed
  6. [root@linuxprobe ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/CentOS-SCLo-scl.repo
  7. [root@linuxprobe ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
  8. # if [enabled=0],input a command to use the repository
  9. [root@linuxprobe ~]# yum --enablerepo=centos-sclo-rh install [Package]
  10. [root@linuxprobe ~]# yum --enablerepo=centos-sclo-sclo install [Package]
  11. # 安装ruby
  12. yum --enablerepo=centos-sclo-rh -y install rh-ruby22
  • 软件集合的软件包安装在/opt目录下, 要使用它,需要加载环境变量,如下所示:
  1. # 加载环境变量
  2. [root@linuxprobe ~]# scl enable rh-ruby22 bash
  3. [root@linuxprobe ~]# ruby -v
  4. ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
  5. [root@linuxprobe ~]# which ruby
  6. /opt/rh/rh-ruby22/root/usr/bin/ruby
  • 设置登录自动启用Ruby 2.2,按如下所示进行配置:
  1. [root@linuxprobe ~]# vim /etc/profile.d/rh-ruby22.sh
  2. #!/bin/bash
  3. source /opt/rh/rh-ruby22/enable
  4. export X_SCLS="`scl enable rh-ruby22 'echo $X_SCLS'`"
  5. export PATH=$PATH:/opt/rh/rh-ruby22/root/usr/local/bin

安装Ruby 2.3

  1. # 安装ruby
  2. yum --enablerepo=centos-sclo-rh -y install rh-ruby23
  • 软件集合的软件包安装在/opt目录下, 要使用它,需要加载环境变量,如下所示:
  1. # 加载环境变量
  2. [root@linuxprobe ~]# scl enable rh-ruby23 bash
  3. [root@linuxprobe ~]# ruby -v
  4. ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
  5. [root@linuxprobe ~]# which ruby
  6. /opt/rh/rh-ruby22/root/usr/bin/ruby
  • 设置登录自动启用Ruby 2.3,按如下所示进行配置:
  1. [root@linuxprobe ~]# vim /etc/profile.d/rh-ruby23.sh
  2. #!/bin/bash
  3. source /opt/rh/rh-ruby23/enable
  4. export X_SCLS="`scl enable rh-ruby23 'echo $X_SCLS'`"
  5. export PATH=$PATH:/opt/rh/rh-ruby23/root/usr/local/bin

安装Ruby on Rails 4来构建Ruby Framework环境

  1. [root@linuxprobe ~]# yum -y install epel-release
  2. # set [priority=5]
  3. [root@linuxprobe ~]# sed -i -e "s/\]$/\]\npriority=5/g" /etc/yum.repos.d/epel.repo
  4. # for another way,change to [enabled=0] and use it only when needed
  5. [root@linuxprobe ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo
  6. # if [enabled=0],input a command to use the repository
  7. [root@linuxprobe ~]# yum --enablerepo=epel install [Package]

Ruby on Rails 4

  • 安装Ruby 2.2,参考上面
  • 安装依赖包(epel+SCLo)
  1. [root@linuxprobe ~]# yum --enablerepo=epel,centos-sclo-rh -y install rh-ruby22-ruby-devel nodejs libuv gcc make libxml2 libxml2-devel mariadb-devel zlib-devel libxslt-devel
  • 安装Rails 4
  1. [root@linuxprobe ~]# gem install bundler
  2. [root@linuxprobe ~]# gem install nokogiri -- --use-system-libraries
  3. [root@linuxprobe ~]# gem install rails --version="~>4.0" --no-ri --no-rdoc
  4. [root@linuxprobe ~]# /opt/rh/rh-ruby22/root/usr/local/bin/rails -v
  5. Rails 4.2.8

Ruby on Rails 5

  • 安装Ruby 2.3,参考上面
  • 安装依赖包(epel+SCLo)
  1. [root@linuxprobe ~]# yum --enablerepo=epel,centos-sclo-rh -y install rh-ruby23-ruby-devel nodejs libuv gcc make libxml2 libxml2-devel mariadb-devel zlib-devel libxslt-devel
  • 安装Rails 5
  1. [root@linuxprobe ~]# gem install bundler
  2. [root@linuxprobe ~]# gem install nokogiri -- --use-system-libraries
  3. [root@linuxprobe ~]# gem install rails --no-ri --no-rdoc
  4. [root@linuxprobe ~]# rails -v
  5. Rails 5.0.2

创建一个示例应用程序,并确保其正常工作

  • 需要安装MariaDB服务
  1. [root@linuxprobe ~]# yum -y install mariadb-server
  2. [root@linuxprobe ~]# vi /etc/my.cnf
  3. # add follows within [MysqLd] section
  4. [MysqLd]
  5. character-set-server=utf8
  6. [root@linuxprobe ~]# systemctl start mariadb
  7. [root@linuxprobe ~]# systemctl enable mariadb
  8. ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
  • 初始化MariaDB
  1. [root@linuxprobe ~]# MysqL_secure_installation
  2.  
  3. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  4. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  5.  
  6. In order to log into MariaDB to secure it,we'll need the current
  7. password for the root user. If you've just installed MariaDB,and
  8. you haven't set the root password yet,the password will be blank,so you should just press enter here.
  9.  
  10. Enter current password for root (enter for none):
  11. OK,successfully used password,moving on...
  12.  
  13. Setting the root password ensures that nobody can log into the MariaDB
  14. root user without the proper authorisation.
  15.  
  16. # set root password
  17. Set root password? [Y/n] y
  18. New password:
  19. Re-enter new password:
  20. Password updated successfully!
  21. Reloading privilege tables..
  22. ... Success!
  23.  
  24. By default,a MariaDB installation has an anonymous user,allowing anyone
  25. to log into MariaDB without having to have a user account created for
  26. them. This is intended only for testing,and to make the installation
  27. go a bit smoother. You should remove them before moving into a
  28. production environment.
  29. # remove anonymous users
  30. Remove anonymous users? [Y/n] y
  31. ... Success!
  32.  
  33. Normally,root should only be allowed to connect from 'localhost'. This
  34. ensures that someone cannot guess at the root password from the network.
  35.  
  36. # disallow root login remotely
  37. Disallow root login remotely? [Y/n] y
  38. ... Success!
  39.  
  40. By default,MariaDB comes with a database named 'test' that anyone can
  41. access. This is also intended only for testing,and should be removed
  42. before moving into a production environment.
  43.  
  44. # remove test database
  45. Remove test database and access to it? [Y/n] y
  46. - Dropping test database...
  47. ... Success!
  48. - Removing privileges on test database...
  49. ... Success!
  50.  
  51. Reloading the privilege tables will ensure that all changes made so far
  52. will take effect immediately.
  53.  
  54. # reload privilege tables
  55. Reload privilege tables now? [Y/n] y
  56. ... Success!
  57.  
  58. Cleaning up...
  59.  
  60. All done! If you've completed all of the above steps,your MariaDB
  61. installation should now be secure.
  62.  
  63. Thanks for using MariaDB!
  64.  
  65. # connect to MariaDB with root
  66. [root@linuxprobe ~]# MysqL -u root -p
  67. Enter password:
  68. Welcome to the MariaDB monitor. Commands end with ; or \g.
  69. Your MariaDB connection id is 11
  70. Server version: 5.5.52-MariaDB MariaDB Server
  71.  
  72. Copyright (c) 2000,2016,Oracle,MariaDB Corporation Ab and others.
  73.  
  74. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  75.  
  76. # show user list
  77. MariaDB [(none)]> select user,host,password from MysqL.user;
  78. +------+-----------+-------------------------------------------+
  79. | user | host | password |
  80. +------+-----------+-------------------------------------------+
  81. | root | localhost | *E2ACEC2F2DA384EE6753673365DFEF35F0C272C9 |
  82. | root | 127.0.0.1 | *E2ACEC2F2DA384EE6753673365DFEF35F0C272C9 |
  83. | root | ::1 | *E2ACEC2F2DA384EE6753673365DFEF35F0C272C9 |
  84. +------+-----------+-------------------------------------------+
  85. 3 rows in set (0.00 sec)
  86.  
  87. # show database list
  88. MariaDB [(none)]> show databases;
  89. +--------------------+
  90. | Database |
  91. +--------------------+
  92. | information_schema |
  93. | MysqL |
  94. | performance_schema |
  95. +--------------------+
  96. 3 rows in set (0.00 sec)
  97.  
  98. MariaDB [(none)]> exit
  99. Bye
  • 创建测试应用程序
  1. [root@linuxprobe ~]# gem install MysqL2 --no-ri --no-rdoc -- --with-MysqL-config=/usr/bin/MysqL_config
  2. [root@linuxprobe ~]# rails new SampleApp -d MysqL
  3. [root@linuxprobe ~]# cd SampleApp
  4. [root@linuxprobe SampleApp]# vi config/database.yml
  5. default: &default
  6. adapter: MysqL2
  7. encoding: utf8
  8. pool: 5
  9. username: root
  10. password: password # MariaDB password
  11. socket: /var/lib/MysqL/MysqL.sock
  12. # create test application
  13. [root@dlp SampleApp]# rails db:create
  14. Created database 'SampleApp_development'
  15. Created database 'SampleApp_test'
  16. [root@dlp SampleApp]# rails generate scaffold testapp name:string title:string body:text
  17. [root@dlp SampleApp]# rails db:migrate
  18. [root@dlp SampleApp]# rails server --binding=0.0.0.0 => Booting Puma => Rails 5.0.2 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options
  19. Puma starting in single mode...
  20. * Version 3.8.2 (ruby 2.3.1-p112),codename: Sassy Salamander
  21. * Min threads: 5,max threads: 5
  22. * Environment: development
  23. * Listening on tcp://0.0.0.0:3000
  24. Use Ctrl-C to stop

猜你在找的CentOS相关文章