Centos5.9下编译安装LAMP(apache 2.4.4 mysql 5.6.10 php 5.4.12)

时间:2022-09-22 13:27:47

安装平台:vmware workstation 9

系统:centos 5.9(内核2.6.18-348.el5)

内存:1024M

所用到的软件包(均为最新稳定版本)

   
  
  
  1. apr-1.4.6.tar.gz

  2. apr-util-1.5.2.tar.gz

  3. httpd-2.4.4.tar.gz

  4. mysql-5.6.10-linux-glibc2.5-i686.tar.gz

  5. php-5.4.12.tar.gz

  6. freetype-2.4.11.tar.gz

  7. libpng-1.6.3.tar.gz

  8. jpegsrc.v9.tar.gz

  9. gd-2.0.33.tar.gz

软件包存放位置:/home/lamp

软件包下载地址:

   
  
  
  1. http://apache.etoak.com//apr/apr-1.4.8.tar.gz

  1. http://mirror.esocc.com/apache//apr/apr-util-1.5.2.tar.gz

  2. http://labs.mop.com/apache-mirror/httpd/httpd-2.4.4.tar.gz

  3. http://cn2.php.net/distributions/php-5.4.12.tar.gz

  4. http://jaist.dl.sourceforge.net/project/mysql.mirror/MySQL 5.6.10/mysql-5.6.10-linux-glibc2.5-i686.tar.gz

  5. http://down1.chinaunix.net/distfiles/gd-2.0.33.tar.gz

  6. http://jaist.dl.sourceforge.net/project/freetype/freetype2/2.4.11/freetype-2.4.11.tar.gz

  7. http://jaist.dl.sourceforge.net/project/libpng/libpng16/1.6.3/libpng-1.6.3.tar.gz

  8. http://www.ijg.org/files/jpegsrc.v9.tar.gz


一、配置安装环境

1、移除系统自带的rpm包的http mysql php,我这里iptables和SElinux都是关闭的,请各位注意下。

    
   
   
  1. #yum remove httpd mysql mysql-server php php-cli php-common php-devel php-gd  -y

2、安装编译所需的开发环境

   
  
  
  1. #yum groupinstall -y 'Development Libraries' 'Development Tools' 'X Software Development'

3、同步系统时间:

   
  
  
  1. # yum -y install ntp

  2. # cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  3. # vim /etc/ntp.conf

 添加下面三行内容(19行下面添加)


server 3.cn.pool.ntp.org

server 3.asia.pool.ntp.org

server 0.asia.pool.ntp.org


   
  
  
  1. #/sbin/service ntpd stop

  2. #/usr/sbin/ntpdate  cn.pool.ntp.org  \\更新系统时间

  3. #/sbin/service ntpd start

  4. #chkconfig ntpd on


二、安装Apache

1、httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行。

(1) 编译安装apr

    
   
   
  1. # tar xf apr-1.4.6.tar.gz

  2. # cd apr-1.4.6

  3. # ./configure --prefix=/usr/local/apr   \\指定安装目录

  4. # make && make install

(2) 编译安装apr-util

    
   
   
  1. # tar xf apr-util-1.5.1.tar.gz

  2. # cd apr-util-1.5.1

  3. # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

  4. # make && make install

(3) httpd-2.4.4编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件包系统光盘自带,因此,找到并安装即可。

   
  
  
  1. #yum -y install pcre-devel

2、编译安装httpd-2.4.4

    
   
   
  1. # tar xf httpd-2.4.4.tar.gz

  2. # cd httpd-2.4.4

  3. # ./configure --prefix=/usr/local/apache \   \\指定安装目录

  4. --sysconfdir=/etc/httpd \                    \\配置文件目录

  5. --enable-so --enable-ssl \                   \\基于ssl的加密传输

  6. --enable-cgi \

  7. --enable-rewrite \                           \\允许URL地址重写

  8. --with-zlib \                                \\支持压缩

  9. --with-pcre \                                \\支持perl的正则表达式

  10. --with-apr=/usr/local/apr \

  11. --with-apr-util=/usr/local/apr-util \        \\关联apr-util的目录

  12. --enable-mpms-shared=all \                   \\实现mpm的动态切换

  13. --with-mpm=event                             \\默认加载的mpm

  14. # make && make install

apache的便宜选项比较多,可以使用configure --help命令查看所支持的编译选项及简要说明,不过都是英文的,想要详细了解每个选项的意义和作用可以查看一下官方的文档,下面的php编译也是一样的,都可以使用该命令查看支持的编译选项,这里就不再详细说明了。

补充:(1)构建MPM为静态模块

在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 ./httpd -l 来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。

(2)构建 MPM 为动态模块

在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。

3、修改httpd的主配置文件,设置其Pid文件的路径

编辑/etc/httpd/httpd.conf,添加如下行即可:

PidFile  "/var/run/httpd.pid"

4、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:

    
   
   
  1. #!/bin/bash

  2. #

  3. # httpd        Startup script for the Apache HTTP Server

  4. #

  5. # chkconfig: - 85 15

  6. # description: Apache is a World Wide Web server.  It is used to serve \

  7. #          HTML files and CGI.

  8. # processname: httpd

  9. # config: /etc/httpd/conf/httpd.conf

  10. # config: /etc/sysconfig/httpd

  11. # pidfile: /var/run/httpd.pid

  12. # Source function library.

  13. . /etc/rc.d/init.d/functions

  14. if [ -f /etc/sysconfig/httpd ]; then

  15.        . /etc/sysconfig/httpd

  16. fi

  17. # Start httpd in the C locale bydefault.

  18. HTTPD_LANG=${HTTPD_LANG-"C"}

  19. # This will prevent initlog from swallowing up a pass-phrase prompt if

  20. # mod_ssl needs a pass-phrase from the user.

  21. INITLOG_ARGS=""

  22. # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

  23. # with the thread-based "worker" MPM; BE WARNED that some modules may not

  24. # work correctly with a thread-based MPM; notably PHP will refuse to start.

  25. # Path to the apachectl script, server binary, and short-form for messages.

  26. apachectl=/usr/local/apache/bin/apachectl

  27. httpd=${HTTPD-/usr/local/apache/bin/httpd}

  28. prog=httpd

  29. pidfile=${PIDFILE-/var/run/httpd.pid}

  30. lockfile=${LOCKFILE-/var/lock/subsys/httpd}

  31. RETVAL=0

  32. start() {

  33.        echo -n $"Starting $prog: "

  34.        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

  35.        RETVAL=$?

  36.        echo

  37.        [ $RETVAL = 0 ] && touch ${lockfile}

  38. return $RETVAL

  39. }

  40. stop() {

  41. echo -n $"Stopping $prog: "

  42. killproc $httpd

  43. RETVAL=$?

  44. echo

  45. [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

  46. }

  47. reload() {

  48.    echo -n $"Reloading $prog: "

  49.    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

  50.        RETVAL=$?

  51.        echo $"not reloading due to configuration syntax error"

  52.        failure $"not reloading $httpd due to configuration syntax error"

  53. else

  54.        killproc -p ${pidfile} $httpd -HUP

  55.        RETVAL=$?

  56.    fi

  57.    echo

  58. }

  59. # See how we were called.

  60. case"$1"in

  61.  start)

  62. start

  63. ;;

  64.  stop)

  65. stop

  66. ;;

  67.  status)

  68.        status -p ${pidfile} $httpd

  69. RETVAL=$?

  70. ;;

  71.  restart)

  72. stop

  73. start

  74. ;;

  75.  condrestart)

  76. if [ -f ${pidfile} ] ; then

  77. stop

  78. start

  79. fi

  80. ;;

  81.  reload)

  82.        reload

  83. ;;

  84.  graceful|help|configtest|fullstatus)

  85. $apachectl $@

  86. RETVAL=$?

  87. ;;

  88.  *)

  89. echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

  90. exit 1

  91. esac

  92. exit $RETVAL

而后为此脚本赋予执行权限:

# chmod +x /etc/rc.d/init.d/httpd

加入服务列表:

# chkconfig --add httpd

为了方便更新PATH,省的每次都绝对路径启动服务,并添加帮助文档

   
  
  
  1. #echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh  //添加PATH路径

  2. # source /etc/profile.d/httpd.sh                //重读配置文件

  3. #vi /etc/man.config                             //添加man文档

Centos5.9下编译安装LAMP(apache 2.4.4 mysql 5.6.10 php 5.4.12)

接下来就可以启动服务进行测试了。

由于在上面的编译过程中把mpm编译成了动态模块,要更换其他模块时只需在配置文件中修改即可。

三、安装mysql-5.6.10

1、准备数据存放的文件系统,这里我使用的是LVM逻辑卷,这个在实际的使用中好处多多,不仅仅是可以动态的更改分区的大小,在使用快照备份数据上也是极为方便的。

新建一个逻辑卷,并将其挂载至特定目录。在这里我准备了两个10G大小的分区sdb1和sdb2用于建立逻辑卷。

   
  
  
  1. # pvcreate /dev/sdb{1,2}

  2. # vgcreate data /dev/sdb1 /dev/sdb2

  3. # lvcreate -L 10G -n mydata data

  4. # mke2fs -j /dev/data/mydata

  5. # mkdir /mydata

  6. # mount /dev/data/mydata /mydata

  7. # mkdir /mydata/data

逻辑卷的挂载目录为/mydata,而后需要创建/mydata/data目录做为mysql数据的存放目录。

2、新建用户以安全方式运行进程

    
   
   
  1. # groupadd -r mysql

  2. # useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

  3. # chown -R mysql:mysql /mydata/data

3、安装并初始化mysql-5.6.10

首先下载平台对应的mysql版本至本地,这里是32位平台,因此,选择的为mysql-5.6.10-linux-glibc2.5-i686.tar.gz

    
   
   
  1. # tar xf  mysql-5.6.10-linux-glibc2.5-i686.tar.gz  -C /usr/local

  2. # cd /usr/local/

  3. # ln -sv  mysql-5.6.10-linux-glibc2.5-i686  mysql

  4. # cd mysql  

  5. # chown -R mysql:mysql  .

  6. # scripts/mysql_install_db --user=mysql--datadir=/mydata/data

  7. # chown -R root  .

4、为mysql提供主配置文件:

    
   
   
  1. # cd /usr/local/mysql

  2. # cp support-files/my-default.cnf  /etc/my.cnf

需要添加如下行指定mysql数据文件的存放位置:

datadir = /mydata/data

5、为mysql提供sysv服务脚本

    
   
   
  1. # cd /usr/local/mysql

  2. # cp support-files/mysql.server  /etc/rc.d/init.d/mysqld

  3. # chmod +x /etc/rc.d/init.d/mysqld

添加至服务列表:

   
  
  
  1. # chkconfig --add mysqld

  2. # chkconfig mysqld on

而后就可以启动服务测试使用了。

为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:

6、输出mysql的man手册至man命令的查找路径:

编辑/etc/man.config,添加如下行即可:

MANPATH  /usr/local/mysql/man

7、输出mysql的头文件至系统头文件路径/usr/include:

这可以通过简单的创建链接实现:

   
  
  
  1. # ln -sv /usr/local/mysql/include  /usr/include/mysql

8、输出mysql的库文件给系统库查找路径:

    
   
   
  1. # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

而后让系统重新载入系统库:

# ldconfig

9、修改PATH环境变量,让系统可以直接使用mysql的相关命令。

   
  
  
  1. #echo "export PATH=$PATH:/usr/local/mysql/bin " >/etc/profile.d/mysql.sh

  2. #source /etc/profile.d/mysql.sh

四、编译安装php-5.4.8

1、解决依赖关系:

如果想让编译的php支持mcrypt扩展,此处还需要下载如下两个rpm包并安装之,这里我直接使用已配置好的yum源进行安装。

libmcrypt-2.5.7-5.el5.i386.rpm

libmcrypt-devel-2.5.7-5.el5.i386.rpm

    
   
   
  1. #yum -y install libmcrypt libmcrypt-devel

2、安装libpng

   
  
  
  1. # tar zxvf libpng-1.6.0.tar.tar

  2. # cd libpng-1.6.0

  3. # ./configure

  4. # make && make install

3、安装freetype

   
  
  
  1. # tar zxvf freetype-2.4.11.tar.gz

  2. # cd freetype-2.4.11

  3. # ./configure

  4. # make && make install

4、安装Jpeg

   
  
  
  1. # tar zxvf jpegsrc.v9.tar.gz

  2. # cd jpeg-9/

  3. # ./configure --enable-shared

  4. # make

  5. # make test

  6. # make install

注意,这里configure一定要带--enable-shared参数,不然,不会生成共享库.

5、安装gd库支持

由于许多php网站程序都需要gd库的支持,在编译php之前要先安装该组件以方便以后的使用。

   
  
  
  1. # tar zxvf gd-2.0.33.tar.gz  

  2. # cd gd-2.0.33

  3. # ./configure --with-png --with-freetype --with-jpeg

  4. # make && make install

6、编译安装php-5.4.12

   
  
  
  1. # tar xf php-5.4.12.tar.gz

  2. # cd php-5.4.12

  3. # ./configure --prefix=/usr/local/php \                   //安装目录

  4. --with-mysql=/usr/local/mysql \                             //指定mysql目录

  5. --with-openssl \                                                    //支持ssl

  6. --with-mysqli=/usr/local/mysql/bin/mysql_config \  //支持mysqli函数

  7. --enable-mbstring \                                              //支持多字符语言

  8. --with-gd

  9. --with-freetype-dir=/usr/local/include/freetype2/freetype \

  10. --with-jpeg-dir=/usr/local/include \                        //支持jpeg

  11. --with-png-dir=/usr/local/include/libpng16 \         //支持png

  12. --with-gettext \

  13. --enable-bcmath \

  14. --with-zlib \                                                         //支持压缩库

  15. --with-libxml-dir=/usr \                                        //支持xml库

  16. --enable-xml  \                                                   //支持xml

  17. --enable-sockets \                                              //支持套接字

  18. --with-apxs2=/usr/local/apache/bin/apxs \           //与Apache的连接

  19. --with-mcrypt  \                                                 //支持加密

  20. --with-config-file-path=/etc \                              //配置文件目录

  21. --with-config-file-scan-dir=/etc/php.d \               //额外会扫描的配置文件目录

  22. --with-bz2  \                                                     //支持bz2

  23. --enable-maintainer-zts                                  //支持apache的worker或event这两个MPM

说明:这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。

    
   
   
  1. # make

  2. # make test

  3. # make intall

为php提供配置文件:

   
  
  
  1. # cp php.ini-production /etc/php.ini

7、 编辑apache配置文件httpd.conf,以apache支持php

   
  
  
  1. # vim /etc/httpd/httpd.conf

添加如下二行

AddType application/x-httpd-php  .php

  AddType application/x-httpd-php-source  .phps

定位至DirectoryIndex index.html

  修改为:

   DirectoryIndex  index.php  index.html

添加index.php为默认主页

而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。

设置php测试页。

    
   
   
  1. # echo -e "<?php\nphpinfo()\n?>" > /usr/local/apache/htdocs/index.php

在浏览器中输入服务器IP地址,别忘了修改一下iptables的设置,开放80端口的访问权限。可以看到php的测试页面

Centos5.9下编译安装LAMP(apache 2.4.4 mysql 5.6.10 php 5.4.12)

至此一个基本的lamp环境搭建完毕。

五、常见问题

1、启动apache出现如下错误

Centos5.9下编译安装LAMP(apache 2.4.4 mysql 5.6.10 php 5.4.12)

错误原因:未关闭SElinux

解决方法:(1)关闭SELINUX

  
 
 
  1. #vi /etc/selinux/config

  2. SELINUX=enforcing 改成SELINUX=disabled  //需要重启

  3. # setenforce 0                           //立即生效

(2)不关闭SELINUX的方法:

   
  
  
  1. #chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache/modules/libphp5.so  

再次启动apache即可。

本文出自 “春夏秋冬又一春” 博客,请务必保留此出处http://zhangjianjian.blog.51cto.com/2944771/1144236