How to install and configure ruby on rails + apache + mysql in centos

Source: Internet
Author: User
Tags install openssl mysql in openssl requires sqlite sqlite database install node ruby on rails

The customer's server is Ubuntu + apache2 + ruby on rails. Currently, some pages on the customer's server always report the 500 error. The ruby language is only known and has not been studied in depth. I have never set up a ruby server environment, and it is strange that the customer uses ubuntu as the server. After installing ruby on rails on CentOS, I understand.

Preface: problems after yum is installed.

At the beginning, I used yum to install apache mysql ruby on rails and so on. After installing it, I found many problems. I couldn't install rails with gem.

My centos version.

[Root @ localhost script] # cat/etc/redhat-release
CentOS release 5.9 (Final)

[Root @ localhost ~] # Gem install rails # the following error is reported during installation
ERROR: Error installing rails:
I18n requires RubyGems version> = 1.3.5
The version of rubyGems does not meet the requirements. The version of rubyGems installed in yum is 1.3.2, and there is no updated binary source code in the source. This problem cannot be solved.

[Root @ localhost ~] # Gem update -- system
Updating RubyGems
Updating rubygems-update
ERROR: Error installing rubygems-update:
Rubygems-update requires Ruby version> = 1.8.7
When the ruby version is updated, the ruby version installed in yum is 1.8.5. Think about it, or install it manually. If you want to install ruby on rails on centos, you should first check the errors. The red part below can avoid detours.

1. Install the required software

Ruby: wget
Rubygems: wget
Nodejs: wget
Nodejs is not recommended to be updated because python is updated to python 2.4 in centos5.9 and python is used. To use the latest node, you must install a python26

2. Install apache mysql

# Yum install httpd mysql server libyaml *
Yum install httpd mysql server libyaml *
Note that install the two libyaml packages first. Otherwise, an error will be reported after you install the gem. After the error is reported, the libyaml package will be installed, and ruby will be re-installed, otherwise, the error will always exist.

Installing apache and mysql is not detailed here.

3. Install ruby on rails

1. Install ruby

[Root @ localhost ~] # Tar xzvf ruby-1.9-stable.tar.gz
[Root @ localhost ~] # Cd ruby-1.9.3-p374
[Root @ localhost ruby-1.9.3-p374] # mkdir/usr/local/ruby
[Root @ localhost ruby-1.9.3-p374] #./configure -- prefix =/usr/local/ruby
[Root @ localhost ruby-1.9.3-p374] # make & make install
2. Add environment variables

[Root @ localhost ~] # Export PATH =/usr/local/ruby/bin: $ PATH
Adding environment variables is temporary.

3. Install rubygems

[Root @ localhost ~] # Tar-xvzf rubygems-1.8.25.tgz
[Root @ localhost ~] # Cd rubygems-1.8.25
[Root @ localhost rubygems-1.8.25] # ruby setup. rb
If the following error is reported, the libyaml package is not installed.

[Root @ localhost rubygems-1.8.25] # gem-v
/Usr/local/ruby/lib/ruby/1.9.1/yaml. rb: 56: in '<top (required)> ':
It seems your ruby installation is missing psych (for YAML output ).
To eliminate this warning, please install libyaml and reinstall your ruby.

Yum install libyaml *
Reinstall ruby and rubygems
4. Install rails

1. Change the installation address.

Because it is installed through the network, we need to select a quick installation address.

[Root @ localhost ~] # Gem sources -- remove
Http:// from sources
[Root @ localhost ~] # Gem sources-a
Http:// to sources
[Root @ localhost ~] # Gem source-l
* ** Current sources ***

2. Update the source with gem.

When gem update is used, the following error is reported:

[Root @ localhost ruby-1.9.3-p374] # gem update
ERROR: Loading command: update (LoadError)
Cannot load such file -- zlib
ERROR: While executing gem... (NameError)
Uninitialized constant Gem: Commands: UpdateCommand

[Root @ localhost ~] # Cd/home/zhangy/download/ruby-1.9.3-p374/ext/zlib/
[Root @ localhost zlib] # ruby extconf. rb
[Root @ localhost zlib] # make & make install
I installed zlib package with yum, but ruby does not recognize it and cannot be used. I used zlib in the ruby installation package.

3. Install rails with gem

[Root @ localhost zlib] # gem install rails-V
The following error is reported during installation:

File 'Lib 'not found

[Root @ localhost zlib] # gem install rdoc-V
After the problem is solved, it is said that bundle and passener will be used to create a rails project and passenger will be used to configure apache.

[Root @ localhost zlib] # gem install bundle-V
[Root @ localhost zlib] # gem install passenger-V
Tips: add the-V parameter during installation, so that you can see that if it is not added, nothing will be displayed, and it will feel like a dead end.

4. passenger generates apache configuration

[Root @ localhost bin] # passenger-install-apache2-module
When I installed it, there were about five or six packages missing here. In short, I used yum to install any packages, except openssl. The openssl package should be included in ruby, it is unclear why ruby is like this. The following error is reported: To install OpenSSL support for Ruby:


[Root @ localhost ~] # Cd/home/zhangy/download/ruby-1.9.3-p374/ext/openssl/
[Root @ localhost openssl] # ruby extconf. rb
[Root @ localhost openssl] # make & make install
After installation, in the run, passenger-install-apache2-module, prompt you modify apache configuration, this for beginners, it is very convenient.

The configuration I added in httpd. conf is as follows:

LoadModule passenger_module/usr/local/ruby/lib/ruby/gems/1.9.1/gems/passenger-3.0.19/ext/apache2/

Listen 8080.
NameVirtualHost local host: 8080
<VirtualHost localhost: 8080>
ServerName localhost: 8080
AllowOverride all
Options Indexes-MultiViews
5. Create a directory and restart apache:

[Root @ localhost www] # mkdir/var/www/public
[Root @ localhost www] #/etc/init. d/httpd restart
5. Create a rails project and start the server

1. Create rails

[Root @ localhost www] # rails new public
The following error is reported during creation:

The following error is reported. By default, ruby uses the sqlite database, and the sqlite connection module is missing.

An error occurred while installing sqlite3 (1.3.7), and Bundler cannot continue.
Make sure that 'Gem install sqlite3-v '1. 3.7 ''succeeds before bundling.

[Root @ localhost www] # yum install sqlite-devel
[Root @ localhost www] # gem install sqlite3-V

[Root @ localhost public] # bundle install -- without sqlite
Note that the rails server must be in your web root directory. If you install the bundle separately, it must also be in the web root directory. Otherwise, an error is reported.

2. Start the server of rails

[Root @ localhost public] # rails server
Startup error:

/Usr/local/ruby/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs/runtimes. rb: 51: in 'autodetect ': cocould not find a JavaScript runtime. see for a list of available runtimes. (ExecJS: RuntimeUnavailable)
From/usr/local/ruby/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs. rb: 5: in '<module: ExecJS>'
From/usr/local/ruby/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs. rb: 4: in '<top (required)>'
The solution is as follows:

[Root @ localhost public] # gem install therubyracer-V
[Root @ localhost public] # gem install execjs-V
// Some people say that the cocould not find a JavaScript runtime error will not occur as long as the above two are installed, but I cannot try it. You have to install node
[Root @ localhost ~] # Tar xzvf node-v0.8.18.tar.gz
[Root @ localhost ~] # Cd node-v0.8.18
[Root @ localhost node-v0.8.18] #./configure
./Configure reports the following error:

[Root @ localhost node-v0.8.18] #./configure

1 if options. unsafe_optimizations else 0)
SyntaxError: invalid syntax
The solution is as follows:

[Root @ localhost ~] # Yum install python26
Modify the first line of the configure file of node, and change python to python26.
Modify the fourth line of the node Makefile file, and change python to python26.
After modification, go to./configure, make & make install.

[Root @ localhost script] # rails server
=> Booting WEBrick
=> Rails 3.2.11 application starting in development on http: // 3000
=> Call with-d to detach
=> Ctrl-C to shutdown server
[23:19:48] info WEBrick 1.3.1
[23:19:48] INFO ruby 1.9.3 () [i686-Linux]
[23:19:48] INFO WEBrick: HTTPServer # start: pid = 2784 port = 3000
Started successfully.

3. Generate code in rails

[Root @ localhost public] # rails generate controller welcome index
Now we can access it through url. Localhost: 3000 instead of 8080 configured in apache, it feels strange that the root configuration of php is different from that of perl. The server receives the url request. I am not used to this method. It feels strange. Here ruby on rails is running on apache.

6. Install the ruby mysql connection Library

1. Install

[Root @ localhost www] # gem install mysql
If the following error is reported:

Building native extensions. This cocould take a while...
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/Usr/local/ruby/bin/ruby extconf. rb
Checking for mysql_ssl_set ()... *** extconf. rb failed ***
Cocould not create Makefile due to some reason, probably lack
Necessary libraries and/or headers. Check the mkmf. log file for more
Details. You may need configuration options.

Root @ localhost ~] # Yum install mysql-devel
If the installation is successful, the following message is displayed:

[Root @ localhost www] # gem install mysql
Building native extensions. This cocould take a while...
Successfully installed mysql-2.9.0
1 gem installed
Installing ri documentation for mysql-2.9.0...
Installing RDoc documentation for mysql-2.9.0...
2. test ruby Connection to mysql and create a test. rb file.

Require "mysql"
Dbc = Mysql. real_connect ('localhost', 'root', '', 'test ')
Query_parse = dbc. query ('select * from user ')
While row = query_parse.fetch_row do
Puts "# {row [0] },# {row [1]}"
An error is reported for the first execution. Because one more code is written, it will be normal after modification. After the configuration is complete, the overall feeling is very troublesome. Unlike ubuntu, ruby and rubygem versions are both very high and node blocks are available in the source without manual compilation and installation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.