在ubuntu6.06上安装apache2.2X + mysql + PHP + Ruby on Rails + Mongrel Cluster

到现在已经安装了好几次服务器了。总结出了一些经验,在这里和大家分享一下。

说明
我使用的linux是ubuntu 6.06 server cd版本。选第一项安装最基本的系统文件,不自动安装LAMP服务,因为ubuntu 6.06自带的apache2是2.0X的,不适合做mongrel cluster的前台,安装自带的apache2.0X之后卸载它比较麻烦,所以干脆不要自动安装LAMP服务,而手动安装apache2.2。

ubuntu 6.06 server cd的基本安装只安装系统必要文件,不安装其它服务,甚至连ssh都要手动:
sudo apt-get install ssh
安装,请大家注意要安装上ssh才能远程访问。

安装准备

加本地ubuntu源,加快下载速度
sudo nano /etc/apt/sources.list
加入速度比较快的源,如cn99的:
deb http://ubuntu.cn99.com/ubuntu/ dapper main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ dapper-updates main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ dapper-security main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ dapper-backports main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu-cn/ dapper main restricted universe multiverse

更新系统
sudo apt-get update
sudo apt-get dist-upgrade

准备必要文件
sudo apt-get install build-essential

创建下载源码文件夹

cd ~
mkdir src
cd src

安装zlib
wget http://www.zlib.net/zlib-1.2.3.tar.gz
tar xvfz zlib-1.2.3.tar.gz
cd zlib-1.2.3/
./configure –prefix=/usr/local
make
sudo make install
cd ..

安装Apache2.2
下载安装apche2.2
sudo dpkg –purge apache apache2
wget http://apache.rmplc.co.uk/httpd/httpd-2.2.4.tar.gz
tar xvfz httpd-2.2.4.tar.gz
cd httpd-2.2.4/
./configure –prefix=/usr/local/apache2 –enable-mods-shared=all –enable-deflate –enable-proxy –enable-proxy-balancer –enable-proxy-http
make
sudo make install
cd ..

测试apache2.2
启动apache2.2
sudo /usr/local/apache2/bin/apachectl start
访问测试
http://xxx.xxx.xxx.xxx(服务器IP)
停止apache2.2
sudo /usr/local/apache2/bin/apachectl stop

让apache2.2在系统启动时自动运行
sudo cp /usr/local/apache2/bin/apachectl /etc/init.d/apachectl
sudo chmod +x /etc/init.d/apachectl
sudo nano /etc/init.d/apachectl
修改文件,增加:
#!/bin/sh
#
# chkconfig: – 85 15
# description: Apache is a web server.
sudo /usr/sbin/update-rc.d apachectl defaults

增加apache用户,增强系统安全性
sudo adduser –system apache
sudo nano /usr/local/apache2/conf/httpd.conf
修改文件,将
User daemon
Group daemon
改成:
User apache
Group nogroup

PHP模块安装
如果你不想在服务器中使用php网站,直接跳过本节
下载安装php4(因为我的服务器只是用来运行早点的php程序,php4够用了)
sudo apt-get install flex
wget http://au2.php.net/get/php-4.4.7.tar.gz/from/cn.php.net/mirror
tar xvfz php-4.4.7.tar.gz
cd php-4.4.7
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql –disable-cgi –with-zlib –with-gettext
make
sudo make install
sudo cp php.ini-dist /usr/local/lib/php.ini
cd ..

设置php
sudo nano /usr/local/apache2/conf/httpd.conf
修改文件,
增加(如果已经加上就不用了)
LoadModule php4_module        modules/libphp4.so

AddType application/x-gzip .gz .tgz
之后增加:
AddType application/x-httpd-php .php
修改
DirectoryIndex index.html

DirectoryIndex index.html index.php default.php

测试php
在网站目录(默认/usr/local/apache2/hdocs/)创建test.php文件,内容
<?php
phpinfo();
?>
然后访问:
http://xxx.xxx.xxx.xxx/test.php
应显示系统php模块信息。

安装MySQL
sudo apt-get install mysql-server mysql-client

修改root用户密码
sudo mysqladmin -u root password 新密码
sudo mysqladmin -u root -h localhost password 新密码

如果要进行其它基本设置,修改/etc/mysql/my.cnf文件。

mysql数据备份与恢复
备份
mysqldump -u 用户名 -p 数据库名 > 备份文件名
恢复
mysql -u 用户名 -p 数据库名 < 备份文件名

安装ruby1.8.6
ubuntu 6.06自带的ruby是1.8.4, 因为在使用1.8.4版的ruby时我遇到mongrel提示ruby版本过旧的问题,所以自行编译安装1.8.6版的ruby.

安装ruby1.8.6
wget http://rubyforge.org/frs/download.php/18421/ruby-1.8.6.tar.gz
sudo tar -xvf ruby-1.8.6.tar.gz
cd ruby-1.8.6
./configure
make test
make
sudo make install

安装ruby1.8.6后在运行script/console时遇到了
      /usr/local/lib/ruby/1.8/irb/completion.rb:10:in `require’:
      no such file to load — readline (LoadError)
错误,解决办法是
安装readline

sudo apt-get install libncurses5-dev libreadline5-dev
cd ext/readline
ruby extconf.rb
make
sudo make install
cd ../../..

ruby gems安装
sudo wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
tar -xvzf rubygems-0.9.0.tgz
cd rubygems-0.9.0
sudo ruby setup.rb
sudo gem update –system
cd ..

安装libmysql
sudo apt-get install libmysql-ruby libmysql-ruby1.8 libruby1.8
安装好这个后
irb
irb(main):001:0> require ‘mysql’
还是提示出错,于是我安装mysql gem
sudo gem install mysql
但安装出错。。。。
奇怪的是后来发现RoR网站可以正常运行,没有问题。网友如果解决了这个问题,请留言一下。

安装Rails
sudo gem install rails –include-dependencies
注意linux平台安装过程要选带ruby的rails。

安装Mongrel Cluster
sudo gem install daemons gem_plugin mongrel mongrel_cluster –include-dependencies
同上,linux平台安装过程要选带ruby的程序。

增加一个mongrel用户,增强系统安全性
sudo adduser mongrel
将网站文件归到这个用户
sudo chown -R mongrel:mongrel /www/app
上面/www/app为RoR程序目录

创建mongrel群

sudo mongrel_rails cluster::configure -e production -p 8000 -N 3 -c /www/app -a 127.0.0.1 –user mongrel –group mongrel
其中/www/app为RoR程序目录
8000为mongrel群开始端口
-N 3中的3为mongrel群中的mongrel服务器数量,3表示3个mongrel组成这个群,群使用的端口从8000开始,8000,8001,8002三个。
上面的命令将会在RoR程序/www/app/config目录下生成mongrel_cluster.yml文件。
将新文件归到mongrel用户
sudo chown -R mongrel:mongrel /www/app/config/mongrel_cluster.yml

启动mongrel群测试
在当前RoR程序根目录(如上面的/www/app)下运行
sudo mongrel_rails cluster::start

让mongrel cluster在系统启动时自动运行
sudo mkdir /etc/mongrel_cluster
sudo ln -s /www/app/config/mongrel_cluster.yml /etc/mongrel_cluster/app.yml
sudo cp /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/
上面的/usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/是mongrel gem所在的目录。
修改mongrel_cluster文件
sudo nano /etc/init.d/mongrel_cluster
在CONF_DIR之上加入一行:
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/bin
解决一些情况下因为mongrel找不到程序而不能在系统启动时自动运行的问题。
sudo chmod +x /etc/init.d/mongrel_cluster
sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults
手动修改mongrel cluster的启动顺序,让它在最后运行,解决有可能发生的另一些启动问题。
sudo mv /etc/rc2.d/S20mongrel_cluster /etc/rc2.d/S99mongrel_cluster

让Apache成为mongrel的前台
修改httpd.conf文件(默认在/usr/local/apache2/conf/目录下)
最后加入:
NameVirtualHost 192.168.1.1
<Proxy balancer://cslog_cluster>
  BalancerMember http://127.0.0.1:8000
  BalancerMember http://127.0.0.1:8001
  BalancerMember http://127.0.0.1:8002
</Proxy>

<VirtualHost 192.168.1.1>
    ServerName www.cslog.cn
    DocumentRoot /www/cslog/public
  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /maintenance.html [L]
  # Rewrite index to check for static index.html
  RewriteRule ^/$ /index.html [QSA]
  # Rewrite to check for Rails cached pages with .html extentions
  RewriteRule ^([^.]+)$ $1.html [QSA]
  # All dynamic requests get sent to the cluster
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://cslog_cluster%{REQUEST_URI} [P,QSA,L]
  # Deflate for clients that support it.
  AddOutputFilterByType DEFLATE text/html text/plain text/xml
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0[678] no-gzip
  BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# Error and access logs.
  ErrorLog logs/cslog_error_log
  CustomLog logs/cslog_access_log combined
</VirtualHost>
文件中的cslog_cluster可以自己命名,但要对应。
8000,8001,8002为mongrel群对应的端口。

总结
到这里,ubuntu6.06上的apache2.2, mysql, PHP, Ruby on Rails , Mongrel Cluster安装设置已经完成。下午一次全过程大概用了两个小时,如果写成脚本,应该可以系统自动安装,可以更快完成全过程。

此条目发表在 Ruby on Rails, 站长文档 分类目录。将固定链接加入收藏夹。

在ubuntu6.06上安装apache2.2X + mysql + PHP + Ruby on Rails + Mongrel Cluster》有 3 条评论

  1. http://www.ititbar.com 说:

    很有用,收藏了

  2. aotianlong 说:

    sudo apt-get install libmysql-ruby libmysql-ruby1.8 libruby1.8
    安装好这个后
    irb
    irb(main):001:0> require ‘mysql’
    还是提示出错,于是我安装mysql gem
    sudo gem install mysql
    但安装出错。。。。
    奇怪的是后来发现RoR网站可以正常运行,没有问题。网友如果解决了这个问题,请留言一下。
    —————–
    ruby mysql是ruby写的连接mysql的库
    gem install mysql这个gem是c写的
    所以你可以正常运行,但是在产品环境下。你需要正式的mysql gem

    由于是c写的,所以需要安装ruby-dev跟mysql-dev包,才可以正常安装这个gem

  3. 卯时下雨 说:

    谢谢楼上的信息.有时间测试一下.

http://www.ititbar.com 进行回复 取消回复