LAMP服务器环境搭建

    近日由于PHP开发,需要一台Linux的PHP服务器,于是开始在虚拟机里面安装。无论是红帽还是Ubuntu,使用图形安装方式很轻松就能正常工作。不过鉴于真正的Linux服务器一般是不会安装图形界面的,所以决定命令行下手工搭建LAMP服务器。

    个人感觉,CentServer应该是一个很不错的Linux服务器系统,但是由于我的系统是32位的,无法在虚拟机里面安装64位系统,所以只能放弃。因为以前有过使用Ubuntu 的经历,所以选择了Ubuntu Server 9.04作为系统。

    安装Ubuntu Server没什么要说的,向导很明确,唯一需要注意的是要选择英语作为默认语言,如果选择中文,进系统以后一样显示不了中文,全是乱码,这样非常影响我们观察错误信息,所以需要选择英语。咱英语毕竟也学这么多年了,应该没问题哈[emot]grin[/emot]。另外,为了使用最新的软件包、实现最大化的自定义,我们不使用Ubuntu的apt-get命令一键安装LAMP环境。

    安装过程很快很顺利,完成后重启就能进入到Ubuntu server环境了。

    作为服务器,我们必须配置一个畅通的网络环境,必须的,否则这个系统也就没什么意义了。

    先来配置网卡,键入以下命令配置网卡的基本信息:

sudo vi /etc/network/interfaces

    我们使用VI编辑器来编辑网卡设置文件。Q:什么是VI?A:晕…VI是Linux中最常用的文本编辑器,如果没有接触过,请自己查阅相关资料,这里我只说几点,第一,按i进入插入模式进行编辑,按ESC返回命令模式,第二,在命令模式下按下dd删除光标所在行,第三,在命令模式下输入:wq保存并且退出,键入:q!强行退出,键入:w保存文件

    如果你想要让路由器自动给你分配IP地址(DHCP模式,Vmware中对应的网卡连接方式为NAT),请修改此配置文件的相应部分为

auto eth0
iface eth0 inet dhcp

      设置静态IP地址配置如下

auto eth0
iface eth0 inet static

      保存,退出。接下来,我们设置DNS,如果你选了自动模式(DHCP),那么这一步可以省略了。执行

sudo vi /etc/resolv.conf
    设置DNS服务器为
nameserver 192.168.1.1
      保存,退出,这样网络就配置好了,我们需要重启网络以应用设置
sudo /etc/init.d/networking restart
      为了检查我们的网络设置,可以使用ping命令探测网关、解析域名,用ifconfig查看网络设置
ping 192.168.1.1 -w 4
ping www.baidu.com -w 4
ifconfig

      不出什么意外,网络已经可以正常使用了。下面,我们就要来配置对于Ubuntu特别重要的安装源。执行命令
sudo vi /etc/apt/sources.list

      以下是常用的更新源

For 9.10
deb http://mirrors.163.com/ubuntu/ karmic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ karmic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ karmic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ karmic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ karmic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ karmic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ karmic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ karmic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ karmic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ karmic-backports main restricted universe multiverse

deb http://mirrors.sohu.com/ubuntu/ karmic main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ karmic-security main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ karmic-updates main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ karmic-proposed main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ karmic-backports main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ karmic main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ karmic-security main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ karmic-updates main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ karmic-proposed main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ karmic-backports main restricted universe multiverse

For 9.04
deb http://mirrors.163.com/ubuntu/ jaunty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jaunty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jaunty-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jaunty-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jaunty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jaunty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jaunty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jaunty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jaunty-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jaunty-backports main restricted universe multiverse

For 8.10
deb http://mirrors.163.com/ubuntu/ intrepid main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ intrepid-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ intrepid-backports main restricted universe multiverse

For 8.04
deb http://mirrors.163.com/ubuntu/ hardy main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ hardy-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ hardy-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ hardy-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ hardy-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ hardy main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ hardy-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ hardy-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ hardy-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ hardy-backports main restricted universe multiverse

      获取更多源请访问http://wiki.ubuntu.org.cn/%E6%B7%BB%E5%8A%A0%E5%85%B6%E4%BB%96%E8%BD%AF%E4%BB%B6%E5%BA%93

      Q: 这么多东西,一定要手敲吗?A: 很不幸,你得亲手敲进去,不过,如果你VI足够好,几分钟之内搞定应该没问题的。

    保存,退出。接下来执行刷新
sudo apt-get update

     好了,到目前为止,Ubuntu运行的基本条件已经完全具备了。我们继续。

   安装SSH。为什么要安装SSH?SSH就像Windows的远程桌面连接,服务器一般是没有键盘和显示器的,所以为了我们管理方便,我们需要安装一个SSH。即使你的Ubuntu装在虚拟机里面,建议也安装这个东西,这样,你就可以直接往里面粘贴命令了,呵呵,另外,有了它,我们也就可以很方便的上传下载文件了。

    键入命令
sudo apt-get install openssh-server

      这样,SSH就安装好了,那么至于SSH具体如何配置如何使用,那就不是本文要讲的内容了,请自己去百度。

    再然后,我们安装CURL。这个东西很重要,有了它我们就可以任意地从网上下载东西了。虽然它只是个命令行工具,但它的功能远远不比网际快车这种下载工具少,据说,它有80多个开关……[emot]shuai[/emot]键入以下命令开始安装
sudo apt-get install curl

      终于,我们可以开始配置LAMP环境了~

      因为我们要在自己机器上从源码编译安装软件,所以一定要有开发环境。涉及到的软件一共有build-essential、make、gcc、linux-headers,使用以下命令安装
sudo apt-get install build-essential
sudo apt-get install make
sudo apt-get install gcc

      另外,由于linux-headers需要与系统内核版本一致,所以我们使用
uname -a

      查看版本号,然后使用以下形式的命令安装。XX更换成你的版本号。
sudo apt-get install linux-headers-2.x.x-server
      
      接下来,我们开始下载安装LAMP软件包。需要安装的软件依次如下:

MYSQL
zlib
openssl
apache-httpd
libpng
freetype
jpegsrc
libxml2
GD2
php
ZendOptimizer

     以上软件可以使用百度搜索,去官网下载。我们可以使用以下命令下载这些文件:
while ! curl -C - -O 'url'; do sleep 10; done

     将引号里面的内容替换成相应的网站即可。

    最后,就是繁琐的编译安装过程了……废话不说了,直接上代码

#########################################
# INSTALL mysql
mysqlver=5.1.40
tar -zxf mysql-$mysql.tar.gz
cd mysql-$mysql
./configure --prefix=/usr/local/mysql --sysconfdir=/etc --localstatedir=/var/lib/mysql --with-extra-charsets=utf8
make
make install
groupadd mysql
useradd mysql -d /dev/null -g mysql -s /sbin/nologin
/usr/local/mysql/bin/mysql_install_db
chown -R mysql.mysql /var/lib/mysql
chown -R mysql.mysql /usr/local/mysql/
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
cp ./support-files/mysql.server /etc/init.d/mysqld
ln --symbolic /etc/init.d/mysqld /etc/rc3.d/S19mysqld
chmod 700 /etc/init.d/mysqld
/usr/local/mysql/bin/mysqld_safe --user=mysql &
/etc/init.d/mysqld start
#########################################
# INSTALL zlib-1.2.3
cd ../
tar zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure --prefix=/usr/local/zlib
make
make install
#########################################
# INSTALL openssl
cd ../
tar zxvf openssl0.9.7k.tar.gz
cd openssl0.9.7k
./config --prefix=/usr/local/openssl
make
make test
make install
#########################################
# INSTALL httpd
cd ../
tar zxvf httpd-2.2.14.tar.gz
cd httpd-2.2.14
./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --with-ssl=/usr/local/openssl --enable-track-vars --enable-rewrite  
make
make install
touch /etc/init.d/apache2
echo "#!/bin/bash\n/usr/local/apache/bin/apachectl start" >> /etc/init.d/apache2
ln --symbolic /etc/init.d/apache2 /etc/rc3.d/S19apache2
#########################################
# INSTALL libpng
cd ../
tar zxvf libpng-1.2.40.tar.gz
cd libpng-1.2.40
cp scripts/makefile.linux makefile
make
make install
#########################################
# INSTALL freetype
cd ../
tar -zvxf freetype-2.1.10.tar.gz
cd freetype-2.1.10
mkdir -p /usr/local/freetype
./configure --prefix=/usr/local/freetype
make;make install  
#########################################
# INSTALL jpegsrc
mkdir -p /usr/local/jpeg7
mkdir -p /usr/local/jpeg7/bin
mkdir -p /usr/local/jpeg7/lib
mkdir -p /usr/local/jpeg7/include
mkdir -p /usr/local/jpeg7/man
mkdir -p /usr/local/jpeg7/man1
mkdir -p /usr/local/jpeg7/man/man1
cd ../
tar zxvf jpegsrc.v7.tar.gz
cd jpeg*
./configure --prefix=/usr/local/jpeg7/ --enable-shared --enable-static
make
make install  
make install-lib
#########################################
#INSTALL libxml2
cd ../
tar -zxvf libxml2-2.7.6.tar.gz
cd libxml2-2.7.6
mkdir -p /usr/local/libxml2
./configure --prefix=/usr/local/libxml2
make
make install
cp xml2-config /usr/bin
#########################################
# INSTALL GD
cd /opt
tar -zvxf gd-2.0.35.tar.gz
mkdir -p /usr/local/gd2
cd gd-2.0.35
./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg7/ --with-png=/usr/local/lib/ --with-zlib=/usr/local/zlib/ --with-freetype=/usr/local/freetype/
make
make install
############################################
# INSTALL php
cd ../
tar -zxvf php-5.3.0.tar.gz
mkdir -p /usr/local/php
cd php-5.3.0
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxm-dir=/usr/local/libxml2 --with-png-dir=/usr/local/lib --with-jpeg-dir=/usr/local/jpeg7 --with-zlib=/usr/local/zlib --with-freetype-dir=/usr/local/freetype --with-gd=/usr/local/gd2 --enable-mbstring=all --enable-ftp --with-config-file-path=/usr/local/php --with-pdo-mysql=/usr/local/mysql
make
make install
cp php.ini-dist /usr/local/php/php.ini
#########################################
# INSTALL ZendOptimizer
cd ../
tar -zxvf ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz
cd ZendOptimizer-3.3.0a-linux-glibc21-i386
./install
echo "LINUX+MYSQL+APACHE2+PHP5+ZEND INSTALL OK"
###############################################

      这是一个安装格式,大家可以根据需要修改,也可以写成shell文件批量运行。如果不出什么以外,LAMP环境应该可以正常运行了!Enjoy it!