LAMP是在Linux操作系统上面,搭配开源软件Apache+Mysql+PHP[Perl/Python]的一个系统平台,它们通常组合在一起用来搭建动态网站或者服务器。而它们本身都是各自独立运行的软件,却因常常被放在一起使用,拥有了越来越高的兼容度,才共同组成了一个强大的Web应用程序平台。并且,随着开源潮流的快速发展,以及投资成本较低的原因,LAMP已经是最好的网站解决方案之一。既然它这么的强大,对我们来说就必须知道如何来搭建LAMP了,下面我们来实现源码编译的方式实现LAMP。
- System:Red Hat Enterprise Linux Server 5.4
- Apache:httpd-2.4.1.tar.bz2 #源码包到http://www.apache.org下载
- Mysql:mysql-5.5.20-linux2.6-i686.tar.gz #源码包到http://www.mysql.com下载
- PHP:php-5.3.10.tar.bz2 #源码包到http://www.php.net下载
在配置好yum源之后,我们可以使用下面命令安装:
- #yum groupinstall "Development Tools" "Development Libraries" "X Software Development"
- 这些rpm包在附件中提供!
- apr-1.4.6-1.i386.rpm
- apr-devel-1.4.6-1.i386.rpm
- apr-util-1.4.1-1.i386.rpm
- apr-util-devel-1.4.1-1.i386.rpm
- libevent-1.4.14b-stable.tar.gz
- libiconv-1.13.1.tar.gz
- libmcrypt-2.5.8.tar.gz
- mhash-0.9.9.9.tar.bz2
下面就开始让我们来编译安装LAMP吧!
一、编译安装Apache
1、解决依赖关系
- apr-1.4.6-1.i386.rpm
- apr-devel-1.4.6-1.i386.rpm
- apr-util-1.4.1-1.i386.rpm
- apr-util-devel-1.4.1-1.i386.rpm
- #rpm –Uvh apr-1.4.6-1.i386.rpm apr-devel-1.4.6-1.i386.rpm
- #rpm -Uvh apr-util-1.4.1-1.i386.rpm apr-util-devel-1.4.1-1.i386.rpm
- #yum install pcre-devel
2、编译安装http-2.4.1
- #tar xf httpd-2.4.1.tar.bz2
- #cd http-2.4.1
- #./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so \
- --enable-ssl --enable-cgi --enable-rewrite --with-zlib
- #make
- #make install
3、配置Apache
- #vim /etc/httpd/httpd.conf #编辑httpd的主配置文件,添加如下内容
- PidFile “/var/run/httpd.pid”
为Apache提供SysV服务脚本/etc/rc.d/init.d/httpd,这样可以使我们可以使用service命令进行对其进行操作,在编译安装Apache时,默认是不提供这个服务脚本的。我们需要手动添加:
- #!/bin/bash
- #
- # httpd Startup script for the Apache HTTP Server
- #
- # chkconfig: - 85 15
- # description: Apache is a World Wide Web server. It is used to serve \
- # HTML files and CGI.
- # processname: httpd
- # config: /etc/httpd/conf/httpd.conf
- # config: /etc/sysconfig/httpd
- # pidfile: /var/run/httpd.pid
- # Source function library.
- . /etc/rc.d/init.d/functions
- if [ -f /etc/sysconfig/httpd ]; then
- . /etc/sysconfig/httpd
- fi
- # Start httpd in the C locale by default.
- HTTPD_LANG=${HTTPD_LANG-"C"}
- # This will prevent initlog from swallowing up a pass-phrase prompt if
- # mod_ssl needs a pass-phrase from the user.
- INITLOG_ARGS=""
- # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
- # with the thread-based "worker" MPM; BE WARNED that some modules may not
- # work correctly with a thread-based MPM; notably PHP will refuse to start.
- # Path to the apachectl script, server binary, and short-form for messages.
- apachectl=/usr/local/apache/bin/apachectl
- httpd=${HTTPD-/usr/local/apache/bin/httpd}
- prog=httpd
- pidfile=${PIDFILE-/var/run/httpd.pid}
- lockfile=${LOCKFILE-/var/lock/subsys/httpd}
- RETVAL=0
- start() {
- echo -n $"Starting $prog: "
- LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && touch ${lockfile}
- return $RETVAL
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc -p ${pidfile} -d 10 $httpd
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
- }
- reload() {
- echo -n $"Reloading $prog: "
- if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
- RETVAL=$?
- echo $"not reloading due to configuration syntax error"
- failure $"not reloading $httpd due to configuration syntax error"
- else
- killproc -p ${pidfile} $httpd -HUP
- RETVAL=$?
- fi
- echo
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status -p ${pidfile} $httpd
- RETVAL=$?
- ;;
- restart)
- stop
- start
- ;;
- condrestart)
- if [ -f ${pidfile} ] ; then
- stop
- start
- fi
- ;;
- reload)
- reload
- ;;
- graceful|help|configtest|fullstatus)
- $apachectl $@
- RETVAL=$?
- ;;
- *)
- echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
- exit 1
- esac
- exit $RETVAL
- #chmod +x /etc/rc.d/init.d/httpd #为此服务脚本赋予执行权限
- #chkconfig –add httpd #将其假如到服务列表
- #chkconfig httpd on #设置为开机启动
- #service httpd configtest #对httpd的配置文件进行检查
- #service httpd start #启动httpd服务
注:每编译安装好一个软件包后最好对其进行测试,保证其正常,避免等到了编译安装好LAMP后出现错误。
- #vim /etc/man.conf #导出帮助文档
- MANPATH /usr/local/apache/man
- #ln –sv /usr/local/apache/include /usr/include/httpd #导出头文件
- #ldconfig
- #vim /etc/profile
- #导出命令
- PATH=$PATH:/usr/local/apache/bin
二、安装Mysql
1、为数据库创建文件系统
- #fdisk /dev/sda #创建磁盘分区,文件系统类型为8e,分区过程不再给出
- #pvcreate /dev/sda5 #创建物理卷
- #vgcreate mysql /dev/sda5 #创建mysql卷组
- #lvcreate –L 5G –n mydata mysql #创建逻辑卷
- #mke2fs –j /dev/mysql/mydata
- #mkdir /mysql #创建挂载点
- #vim /etc/fstab #编辑fstab文件,使其开机自动挂载
- /dev/mysql/mydata /mysql ext3 defaults 0 0
- #mount /dev/mysql/mydata /mysql
- #mkdir /mysql/data #创建mysql数据的存放目录
2、为mysql数据库新建用户
- #groupadd –r mysql
- #groupadd –g mysql –r –s /sbin/nologin –M mysql
- #chown –R mysql:mysql /mysql/data
3、安装mysql-5.5.20
- #tar xf mysql-5.5.20-linux2.6-i686.tar.gz –C /usr/local
- #cd /usr/local/
- #ln –sv mysql-5.5.20-linux2.6-i686 mysql
- #cd mysql
- #chown –R mysql:mysql . #初始化mysql数据库的时候需要将所有文件改为mysql组mysql用户
- #scripts/mysql_install_db –user=mysql –datadir=/mysql/data #初始化mysql数据库
- #chown –R root . #将文件的属主再改为root
4、配置Mysql
- #cd /usr/local/myql
- #cp support-files/my-large.cnf /etc/my.cnf
- #vim /etc/my.cnf
- thread_concurrency = 2
- datadir = /mysql/data #指定mysql数据文件的存放位置
- #cd /usr/local/mysql
- #cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- #chkocnfig –add mysqld
- #chkconfig mysqld on
- #service mysqld on
- #vim /etc/man.config #导出帮助文档
- MANPATH /usr/local/mysql/man
- #ln –sv /usr/local/mysql/include /usr/include/mysql
- #导出头文件
- #echo ‘/usr/local/mysql/lib’ > /etc/ld.so.conf.d/mysql.conf #导出库文件
- #ldconfig
- #vim /etc/profile #导出命令
- PATH=$PATH:/usr/local/mysql/bin
- #mysqladmin -uroot -hlocalhost -p password '123456'
三、编译安装php
1、解决依赖关系
在开始的时候,我们说过编译安装php的时候依赖于"X Software Development"软件包组中的软件包,所以请再确定下是否安装了该软件包组。如果安装过了,就继续下面的步骤吧!
2、编译安装php-5.3.10
- #tar xf php-5.3.10.tar.bz2
- #cd php-5.3.10
- #./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql \
- --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config \
- --enable-mbstring –with-gd --with-freetype-dir --with-jpeg-dir \
- --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml \
- --with-apxs2=/usr/local/apache/bin/apxs
- #make
- #make test #可以省略这一步,检查的时间比较久
- #make install
- 为php提供配置文件
- #cp php.ini-procuction /usr/local/php/lib/php.ini
- 编辑Apache配置文件httpd.conf,使Apache支持php
- #vim /etc/httpd/httpd.conf
- AddType application/x-httpd-php .php
- AddType application/x-httpd-php-source .phps
- 找到DirectoryIndes index.html修改为
- DirectoryIndex index.php index.html
重新启动httpd服务,或重新载入配置文件即可。
4、测试php是否正常工作
- #mv index.html index.php
- #vim index.php
- <?php
- Phpinfo();
- ?>
- #vim index.php<?php
- $link=mysql_connect('localhost','root','');
- if ($link)
- echo "successful";
- else
- echo "failure";
- mysql_close();
- ?>
通过浏览器访问该页面,如果成功,会显示successful,反之显示failure。
附录:
- libmcrypt :提供了系统自带的crypt的函数,增加了更多的加密函数,提供更高级的加密算法;
- mhash :做哈希计算的。做单项加密函数的;
- libiconv :是实现基于一种编码实现国际化的语言的接口。
- libevent-1.4.14b-stable.tar.gz
- libiconv-1.13.1.tar.gz
- libmcrypt-2.5.8.tar.gz
- mhash-0.9.9.9.tar.bz2
然后,我们挨个对其编译安装,下面的编译安装比较简单,只给出步骤:
- #tar zxf libevent-1.4.14b-stable.tar.gz
- #cd libevent-1.4.14b-stable
- #./configure
- #make
- #make install
- #make verify
- #tar zxf libiconv-1.13.1.tar.gz
- #cd libiconv-1.13.1
- #./configure
- #make
- #make install
- #tar zxf libmcrypt-2.5.8.tar.gz
- #cd libmcrypt-2.5.8
- #./configure
- #make
- #make install
- #ldconfig -v
- #cd libltdl
- #./configure --with-gmetad --enable-gexec --enable-ltdl-install
- #make
- #make install
- #tar jxf mhash-0.9.9.9.tar.bz2
- #cd mhash-0.9.9.9
- #./configure
- #make
- #make install
- #ln -sv /usr/local/lib/libmcrypt* /usr/lib/
- #ln -sv /usr/local/lib/libmhash.* /usr/lib/
- #tar xf php-5.3.10.tar.bz2
- #cd php-5.3.10
- #./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql \
- --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config \
- --enable-mbstring –with-gd --with-freetype-dir --with-jpeg-dir \
- --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml \
- --with-apxs2=/usr/local/apache/bin/apxs --with-mhash=/usr/local \
- --with-mcrypt=/usr/local
- #其中--with-mhash与--with-mcryt也可以不用加安装路径,但是如果我们自己定义了目录,就需要
- 指出来了。这里只是给个提示,源码安装与rpm包安装的区别就在这了,是否需要我们自己指定安装目录
- 了!
- #make ZEND_EXTRA_LIBS='-liconv' #如果没有安装装libiconv之类的库,为了避免编译时出错,
- 可以加ZEND这个选项,它的作用是不为php加入iconv模块。
- 可以先直接make试试,如果报错了,先make clean,然后再
- 添加上ZEND_EXTRA_LIBS='-liconv'
- #make test #这一步可以省略,太耗时间了!
- #make install
通过以上步骤就可以搭建出来一个LAMP架构的WEB服务器了!当然上面只是一般性的步骤,具体的实现过程也需要考虑到你的实际情况。希望你也能成功!