LAMP即Linux、Apache、MySQL、PHP四种开源软件的组合。
本例使用的版本为:
Linux:Red Hat Enterprise Linux Server release 5.5 (Tikanga)
Apache:Apache2.4.2,http://mirror.bjtu.edu.cn/apache/httpd/httpd-2.4.2.tar.gz
MySQL:mysql-5.0.67-linux-i686-icc-glibc23.tar.gz,http://downloads.mysql.com/archives/mysql-5.0/mysql-5.0.67-linux-i686-icc-glibc23.tar.gz
PHP:PHP-5.4.3,http://cn.php.net/get/php-5.4.3.tar.gz/from/cn2.php.net/mirror
一、安装Apache
下载的是源码包,需要进行编译安装,编译的时候会提示缺少apr和apr-util两个包,所以在安装前需要先安装这两个包。
apr需要下载比1.3新的版本:http://mirror.bjtu.edu.cn/apache/apr/apr-1.4.6.tar.gz
apr-util:http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.4.1.tar.gz
安装apr:
[root@localhost Desktop ] tar -zxf apr-1.4.6.tar.gz
[root@localhost Desktop ]cd apr-1.4.6
[root@localhost apr-1.4.6]# mkdir /usr/local/src/apr
[root@localhost apr-1.4.6]# ./configure --prefix=/usr/local/src/apr
[root@localhost apr-1.4.6]# make
[root@localhost apr-1.4.6]# make install
安装apr-util:
[root@localhost Desktop ]# tar -zxf apr-util-1.4.1.tar.gz
[root@localhost Desktop]# cd apr-util-1.4.1
[root@localhost apr-util-1.4.1]# mkdir /usr/local/src/apr-util
[root@localhost apr-util-1.4.1]# ./configure --prefix=/usr/local/src/apr-util --with-apr=/usr/local/src/apr
[root@localhost apr-util-1.4.1]# make
[root@localhost apr-util-1.4.1]# make install
安装Apache:
[root@localhost Desktop]# tar -zxf httpd-2.4.2.tar.gz
[root@localhost httpd-2.4.2]# ./configure --prefix=/server/apache --with-apr=/usr/local/src/apr --with-apr-util=/usr/local/src/apr-util
[root@localhost httpd-2.4.2]#make
[root@localhost httpd-2.4.2]#make install
测试Apache:
[root@localhost bin]# vi ~/.bash_profile
修改PATH为:PATH=$PATH:$HOME/bin:/server/apache/bin
保存退出
[root@localhost bin]# . ~/.bash_profile
[root@localhost bin]# apachectl start
[root@localhost bin]# ps -ef | grep apache
root 22069 1 0 17:24 ? 00:00:00 /server/apache/bin/httpd -k start
daemon 22070 22069 0 17:24 ? 00:00:00 /server/apache/bin/httpd -k start
daemon 22072 22069 0 17:24 ? 00:00:00 /server/apache/bin/httpd -k start
daemon 22074 22069 0 17:24 ? 00:00:00 /server/apache/bin/httpd -k start
root 22155 14255 0 17:24 pts/1 00:00:00 grep apache
[root@localhost bin]#
[root@localhost bin]# firefox &
在浏览器栏输入 http://localhost
如果显示 “It works!”,则Apache服务器安装成功。
二、安装MySQL
下载的MySQL是二进制包,解压缩后就可以直接使用。
[root@localhost Desktop]# tar -zxf mysql-5.0.67-linux-i686-icc-glibc23
[root@localhost Desktop]# mkdir /server/mysql
[root@localhost Desktop]# mkdir /server/data
[root@localhost Desktop]# cp -pr mysql-5.0.67-linux-i686-icc-glibc23 /server/mysql/
[root@localhost Desktop]# cd /server/mysql
[root@localhost mysql]# cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
建议cp support-files/my-medium.cnf /etc/my.cnf
[root@localhost mysql]# vi /etc/my.cnf
编辑MySQL的配置文件,在[mysqld]下面添加
basedir=/server/mysql
datadir=/server/data
保存退出。
[root@localhost mysql]#useradd -r mysql
[root@localhost mysql]#chown -R mysql.mysql /server/mysql
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/server/mysql --datadir=/server/data
[root@localhost mysql]#chown -R mysql.mysql /server/data
[root@localhost mysql]# vi ~/.bash_profile
编辑PATH=$PATH:$HOME/bin:/server/apache/bin:/server/mysql/bin
[root@localhost mysql]# . ~/.bash_profile
[root@localhost mysql]# ./bin/mysqld_safe &
mysql>
到此MySQL安装成功。
如果MySQL无法启动
添加MySQL服务,设置为开机自启动:
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysql
[root@localhost mysql]# chmod 755 /etc/init.d/mysql
[root@localhost mysql]# chkconfig --add mysql
[root@localhost mysql]# chkconfig --level 5 mysql on
[root@localhost mysql]# service mysql start
Starting MySQL. [确定]
[root@localhost mysql]
三、设置PHP
[root@localhost Desktop]# tar php-5.4.3.tar.gz
[root@localhost Desktop]# cd php-5.4.3
[root@localhost php-5.4.3]# yum -y install libxml2.i386
[root@localhost php-5.4.3]# yum -y install libxml2-devel.i386
[root@localhost php-5.4.3]# ./configure --prefix=/server/php5 --with-mysql --with-apxs2=/server/apache/bin/apxs --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-mysql=/server/mysql
指定php安装路径/server/php5,--with-mysql指定需要连接MySQL数据库,--with-apxs2指定apxs的路径。
[root@localhost php-5.4.3]# make
[root@localhost php-5.4.3]# make install
[root@localhost php-5.4.3]# cp php.ini-development /server/php5/lib/php.ini
四、连接Apache与PHP
设置PHP页面文件的目录
[root@localhost ~]# mkdir /server/www
[root@localhost ~] vi /server/apache/conf/httpd.conf
执行 :%s/\/server\/apache\/htdocs/\/server\/www/g
或者
手动修改这两行:
DocumentRoot "/server/apache/htdocs"
<Directory "/server/apache/htdocs">
为
DocumentRoot "/server/www"
<Directory "/server/www">
在文件尾加入两行
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php
然后重新启动apache服务,在/server/www目录下新建test.php文件,内容如下
<?php
echo phpinfo();
?>
然后打开浏览器,在地址栏输入http://localhost/test.php
[root@localhost ~]# firefox &
如果显示出PHP Version的信息,则LAMP环境搭建成功。