LAMP+Rails开发环境的搭建全程记录

来源:转载

OK,因为目前我的工作环境仍以windows为主(主要是公司的大部分管理系统都是基于windows的),而windows环境下的Ruby又不是那么好用,所以让我们在虚拟机中安装一个linux系统,并搭建一个全功能的LAMP和Rails环境。

1、准备工作

首先,安装virtualbox 4.1.6。地址:https://www.virtualbox.org/wiki/Downloads ;

然后下载lubuntu 11.10 (选择这个版本是因为其系统资源占用少的缘故)

地址:http://cdimage.ubuntu.com/lubuntu/releases/11.10/release/lubuntu-11.10-desktop-i386.iso;

2、创建一个虚拟机,选ubuntu;安装介质选择刚才下载的lubuntu-11.10-desktop-i386.iso,安装过程中最好选择英语安装,否则会出现乱码;

3、进入lubuntu,安装中文,首选项-语言支持中安装简体中文,然后重启;打开新立得软件包管理器,搜索ibus并安装之,否则

4、在系统工具-更新管理器,设置-ubuntu软件-下载自 选择最佳服务器;

5、升级内核

 sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade

6、安装VirtualBox增强功能

 sudo apt-get install make

sudo apt-get install gcc

cd /media/VBOXADDITIONS_4.1.6_74713

sudo ./VBoxLinuxAdditions.run

创建加载点

sudo mkdir /mnt/share

加载数据空间

sudo mount -t vboxsf ubuntushare /mnt/share

以上两步是创建虚拟主机与宿主机之间的共享数据空间;

7、安装GVim

sudo apt-get install vim-gtk

8、安装MySQL 

sudo apt-get install mysql-server mysql-client

按照提示输入MySQL root用户的密码(123456需要输入2遍),安装的版本是5.1.58

测试:

mysql -u root -p

9、安装Apache2

 sudo apt-get install apache2

安装版本是2.2.20;测试:用浏览器打开 http://127.0.0.1
注意:

apache默认的文档根目录为/var/www
apache配置文件/etc/apache2/apache2.conf
apache配置文件目录/etc/apache2

10、安装PHP5 

sudo apt-get install php5 libapache2-mod-php5

安装的版本是5.3.6;

重启apache2

sudo /etc/init.d/apache2 restart

编辑测试文件

sudo vim /var/www/info.php

内容为

<?php
phpinfo();
?>

测试:用浏览器打开 http://127.0.0.1/info.php

11、安装PHP5的MySQL支持

查看系统中支持的PHP5扩展

sudo apt-cache search php5

安装PHP5模块

sudo apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json

重启apache2

sudo /etc/init.d/apache2 restart

测试:用浏览器打开 http://127.0.0.1/info.php
12、安装phpMyAdmin 

sudo apt-get install phpmyadmin

安装选择如下
Web server to reconfigure automatically: <-- apache2
Configure database for phpmyadmin with dbconfig-common? <-- No

安装的版本是3.4.5
测试:用浏览器打开 http://127.0.0.1/phpmyadmin
13、安装FireFox

sudo apt-get install firefox

然后在firefox中安装firebug
14、安装Git 

sudo apt-get install git

15、安装mysql客户端的开发库

sudo apt-get install libmysqlclient-dev

16、安装curl 

sudo apt-get install curl

17、安装RVM

网站:http://beginrescueend.com/

准备工作:

sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline-gplv2-dev

安装脚本:

bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

安装的位置是:~/.rvm/,安装后的注意事项

View Code
SYSTEM NOTES:

If you do not wish to enable reading of per-project .rvmrc files, simply set:
export rvm_project_rvmrc=0
within either your /etc/rvmrc or $HOME/.rvmrc file, then log out and back in.


In case your shell exits on entering directory with freshly checked out sources
you should update .rvmrc file, and replace any `exit ` with `return `.


You _must_ read 'rvm requirements' for additional OS specific requirements for
various rubies, and native-extension gems. Expect failures until those are met!


You must now complete the install by loading RVM in new shells.

If you wish to use RVM in an interactive fashion in your shells then
Place the following line at the end of your shell's loading files
(.bashrc or .bash_profile for bash and .zshrc for zsh),
after all PATH/variable settings:

[[ -s "/home/yangbing/.rvm/scripts/rvm" ]] && source "/home/yangbing/.rvm/scripts/rvm" # This loads RVM into a shell session.

You only need to add this line the first time you install RVM.

If you are choosing to source RVM into your environment to switch current
shell environments, be sure to close this shell and open a new one so that
the RVM functions load.


Installation of RVM to /home/yangbing/.rvm/ is complete.



Requirements for Linux ( DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=11.10
DISTRIB_CODENAME=oneiric
DISTRIB_DESCRIPTION="Ubuntu 11.10" )

NOTE: 'ruby' represents Matz's Ruby Interpreter (MRI) (1.8.X, 1.9.X)
This is the *original* / standard Ruby Language Interpreter
'ree' represents Ruby Enterprise Edition
'rbx' represents Rubinius

bash >= 4.1 required
curl is required
git is required (>= 1.7 recommended)
patch is required (for ree and some ruby-head's).

To install rbx and/or Ruby 1.9 head (MRI) (eg. 1.9.2-head),
then you must install and use rvm 1.8.7 first.

NOTE: To all Multi-User installers - DO NOT forget to add your users to the 'rvm' group.
The installer does not auto-add any users to the rvm group. Admins must do this.
Group memberships are ONLY evaluated at login time. Log them out, then back in.

Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

# For JRuby, install the following:
jruby: /usr/bin/apt-get install curl g++ openjdk-6-jre-headless
jruby-head: /usr/bin/apt-get install ant openjdk-6-jdk

# For IronRuby, install the following:
ironruby: /usr/bin/apt-get install curl mono-2.0-devel

关闭终端后重新打开终端

rvm -v

安装的RVM的版本是1.9.2;

因为需要安装Ruby 1.9.2,由以上提示得知,准备工作要安装:

sudo apt-get install openssl libreadline6 libreadline6-dev git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

18、RVM安装Ruby

安裝 1.9.2

rvm install 1.9.2

设为默认

rvm 1.9.2 --default

使用项目rvmrc

mkdir -p ~/projects/projecta
cd ~/projects/projecta
rvm --rvmrc --create 1.9.2@projecta
ruby -v
gem env gemdir

19、安装Rails

gem install rails
gem install mysql2
gem install sqlite3-ruby

测试:

rails new testapp
cd testapp
bundle install
rake db:create
rails s

这时会如下报错

.../gems/execjs-1.2.9/lib/execjs/runtimes.rb:47:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

解决方法,在Gemfile中添加两行

gem 'execjs'
gem 'therubyracer'

然后

bundle instal
rails s

访问:http://127.0.0.1:3000
如此,rails3.1.1安装完成。

注意:在安装rails时,有可能会出现类似的如下错误提示

ERROR: Could not find a valid gem 'rails' (>= 0) in any repository
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
Errno::ETIMEDOUT: Connection timed out - connect(2) (http://rubygems.org/latest_specs.4.8.gz)

解决该问题的最好方法
使用google的DNS 8.8.8.8 / 8.8.4.4

20、安裝 Passenger

gem install passenger

安装 CURL with SSL

 sudo apt-get install libcurl4-openssl-dev

在Apache上安装Passenger所需开发包

sudo apt-get install apache2-prefork-dev

在Apache上安装Passenger

passenger-install-apache2-module

编辑Apache的配置文件,加载Passenger

 sudo vim /etc/apache2/mods-enabled/mod_rails.conf

添加内容如下

 

LoadModule passenger_module /home/username/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/passenger-3.0.8/ext/apache2/mod_passenger.so
PassengerRoot /home/username/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/passenger-3.0.8
PassengerRuby /home/username/.rvm/wrappers/ruby-1.9.2-p290@rails3tutorial/ruby

部署RoR应用的例子

View Code
Deploying a Ruby on Rails application: an example

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public # <-- be sure to point to 'public'!
<Directory /somewhere/public>
AllowOverride all # <-- relax Apache security settings
Options -MultiViews # <-- MultiViews must be turned off
</Directory>
</VirtualHost>

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

说明文档位于:
  /home/username/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/passenger-3.0.8/doc/Users guide Apache.html

 部署实战
编辑apache配置文件:

sudo vim /etc/apache2/sites-enabled/forum.local.conf 

内容如下:

<VirtualHost *:80>
ServerName forum.local
DocumentRoot /home/username/projects/forum/public
<Directory /home/username/projects/forum/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>

编辑hosts文件

 sudo vim /etc/hosts

添加如下内容

127.0.0.1 forum.local

然后

 mkdir -p ~/projects/
cd ~/projects/
rails new forum
cd forum
bundle install

 

访问:http://forum.local

另外:

安装rmagick

sudo apt-get remove imagemagick
sudo apt-get install libperl-dev gcc libjpeg62-dev libbz2-dev libtiff4-dev libwmf-dev libz-dev libpng12-dev libx11-dev libxt-dev libxext-dev libxml2-dev libfreetype6-dev liblcms1-dev libexif-dev perl libjasper-dev libltdl3-dev graphviz gs-gpl pkg-config
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar zxvf ImageMagick.tar.gz
cd ImageMagick-*
./configure
make
sudo make install
gem install rmagick

安装Redcar editor

sudo gem install redcar
redcar install

参考文章:

1、Best Practice of Installing Ruby on Rails on Ubuntu 11.04 ( 2011 version ) | Blog.XDite.net
http://wp.xdite.net/?p=2348

2、Installing Ruby on Rails on Linux | 8ª Elite
http://8elite.com/2011/09/instalacion-de-ruby-on-rails-en-linux/

3、java爱好者 - 设置gem源,解决下载慢的问题
http://www.ai-java.com/entry/175

4、Use apt-get To Install, Upgrade, Search, Remove Packages | Lainoox
http://www.lainoox.com/apt-get-packages/

5、RVM: Ruby Version Manager - RVM Best Practices
http://beginrescueend.com/rvm/best-practices/

6、Installing Apache2 With PHP5 And MySQL Support On Ubuntu 10.10 (LAMP) | HowtoForge - Linux Howtos and Tutorials
http://www.howtoforge.com/installing-apache2-with-php5-and-mysql-support-on-ubuntu-10.10-lamp

7、Ubuntu 10.10 安装配置LAMP
http://www.linuxidc.com/Linux/2010-10/29267.htm

8、Linux 下lamp的安装和配置 - 握瑜的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/hekejun19861107/article/details/6194327

...



 

标签: lubuntu, Apache, MySQL, PHP, Ruby, Rails, RVM, Passenger绿色通道:好文要顶关注我收藏该文与我联系


分享给朋友:
------分隔线----------------------------
您可能感兴趣的文章:
随机阅读: