在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. 卯時下雨 說:

    謝謝樓上的信息.有時間測試一下.

aotianlong 進行回復 取消回復