红联Linux门户
Linux帮助

CentOS 6.4安装配置LAMP环境

发布时间:2015-04-01 21:52:24来源:linux网站作者:linux人

系统环境:CentOS 6.4

软件源码包版本:

httpd-2.4.10
php-5.4.32
mysql-5.5.39


一、编译安装apache2.4

1、下载所需的软件源码包,使用wget命令进行下载:

apr-1.5.1:wget http://mirrors.hust.edu.cn/apache//apr/apr-1.5.1.tar.bz2
apr-util-1.5.3:wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.5.3.tar.bz2
httpd-2.4.10:wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.10.tar.bz2

2、解决依赖关系

(1)使用yum安装系统所需的一些依赖库

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre pcre-devel

(注意:在这次的安装实验中,只需要yum安装openssl,openssl-devel,pcre,pcre-devel即可正常编译完成httpd,而且上面所列出的库与工具,有不少是系统已经安装过了的,这里只是进行一下更新,还有其他的一些库是为下面编译安装mysql与php作的准备)

(2) 先编译安装apr

tar jxf apr-1.5.1.tar.bz2

cd apr-1.5.1

./configure --prefix=/usr/local/apr

make && make install

(3) 再编译安装apr-util

tar jxf apr-util-1.5.3.tar.bz2

cd apr-util-1.5.3

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  #(注意:--with-apr=/usr/local/apr是上面安装apr的路径)

make && make install

(解释:apr,全称为Apache portable Run-time libraries,叫Apache可移植运行库,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库,安装apache都需要先安装这个库的)

3、正式编译安装httpd

tar jxf httpd-2.4.10.tar.bz2

cd httpd-2.4.10

./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event

make && make install

4、配置httpd

(1)为httpd提供sysv服务启动与关闭脚本,把以下内容复制成/etc/init.d/httpd,并且给予此文件执行权限(chmod +x /etc/rc.d/init.d/httpd),就可以直接使用service httpd <start|stop|restart|reload>的命令形式控制httpd服务:

#!/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

(2)把httpd服务添加为开机自启动:

chkconfig --add httpd

chkconfig httpd on

(3)修改httpd主配置文件/etc/httpd/httpd.conf,指定httpd服务的PID文件,直接在配置文件添加如下字段:

PidFile  "/var/run/httpd.pid"

(4)启动httpd服务,并进行访问测试

service httpd start

(注:如果启动的时候出现这个警告:httpd: apr_sockaddr_info_get() failed for longren

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName,解决方法:在/etc/httpd/httpd.conf添加ServerName 127.0.0.1)

访问测试:http://your_ipadress,如果出现It Works,就证明httpd编译安装成功


二、编译安装mysql(注:一般来说是先安装mysql再安装php,因为在php编译时需要指定mysql路径)

1、下载所需软件源码包

mysql-5.5.39:http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.39.tar.gz

2、解决库依赖关系

(1)yum安装依赖库

yum -y install cmake  #mysql5.5是使用cmake进行编译的

yum -y install bison automake zlib* libxml* ncurses-devel libtool-ltdl-devel* mysql-devel

3、添加mysql用户,下面的编译需要用到

groupadd -r mysql

useradd -g mysql -r -s /sbin/nologin mysql

4、正式编译安装mysql

tar zxf mysql-5.5.39.tar.gz

cd mysql-5.5.39

cmake \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_DATADIR=/user/local/mysql/data \

-DSYSCONFDIR=/etc \

-DEXTRA_CHARSETS=all \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \

-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \

-DMYSQL_TCP_PORT=3306 \

-DWITH_DEBUG=0 \

-DMYSQL_USER=mysql \

-DENABLED_LOCAL_INFILE=1

make && make install

5、初始化数据库

chown -R mysql:mysql /usr/local/mysql/*

cd /usr/local/mysql

./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data

6、配置数据库

(1)从源码包中拷贝数据库配置文件

cd mysql-5.5.39

cp support-files/my-medium.cnf /etc/my.cnf

(注:support-files目录里有my-small.cnf、my-medium.cnf、my-large.cnf、my-huge.cnf,使用哪个是根据机器的内存大小进行选择)

(2)mysql提供sysv服务启动与关闭脚本:

cd /usr/local/mysql

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

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

(3)添加至服务列表:

chkconfig --add mysqld

chkconfig mysqld on

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

(1)输出mysql的man手册至man命令的查找路径:

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

MANPATH  /usr/local/mysql/man

(2)输出mysql的头文件至系统头文件路径/usr/include:

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

ln -sv /usr/local/mysql/include  /usr/include/mysql

(3)输出mysql的库文件给系统库查找路径:

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

让系统重新载入系统库:

ldconfig

(4)修改PATH环境变量,让系统可以直接使用mysql的相关命令,添加如下命令后,需要重新连接会话才能生效

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

8、启动mysql服务

service mysqld start


三、源码编译安装php

1、下载所需软件源码包

php-5.4.32:http://cn2.php.net/get/php-5.4.32.tar.bz2/from/this/mirror

libmcrypt-2.5.7:ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

xcache-3.1.0:http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.bz2

mhash-0.9.9:http://pan.baidu.com/s/1hqDtQ2C

2、解决依赖关系

tar zxf libmcrypt-2.5.7.tar.gz

cd libmcrypt-2.5.7

./configure

make && make install

tar jxf mhash-0.9.9.tar.gz

cd mhash-0.9.9

./configure

make && make install

3、正式编译安装php

tar jxf php-5.4.32.tar.bz2

cd php-5.4.32

./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts

make && make install

4、配置php

(1)从源码包拷贝php配置文件:

cd php-5.4.32

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

(2)整合httpd与php:编辑apache配置文件httpd.conf,使apache支持php

vim /etc/httpd/httpd.conf

a、添加如下二行

AddType application/x-httpd-php  .php

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

b、定位至DirectoryIndex index.html修改为:

DirectoryIndex  index.php  index.html

重新启动httpd服务,或让其重新载入配置文件

测试:在/usr/local/apache/htdocs中添加index.php,里面写入php代码

(3)安装xcache,为php加速

tar jxf xcache-3.1.0.tar.bz2

cd xcache-3.1.0

/usr/local/php/bin/phpize

./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

make && make install

编辑php.ini,整合php和xcache:

首先将xcache提供的样例配置导入php.ini

cd xcache-3.1.0

mkdir /etc/php.d

cp xcache.ini /etc/php.d

然后编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:

zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

重启httpd服务并测试xcache是否与php整合成功

service httpd restart

在index.php添加phpinfo()函数,让后访问index.php,查看xcache选项的显示。