您的位置:首页 > 运维架构 > Apache

LAMP:apache+mysql+php的编译安装 推荐

2012-10-28 14:16 567 查看
LAMP组合: httpd 2.4.2 + mysql-5.5.24 + php-5.3.14编译安装过程:Red Hat Enterprise Linux Server release 5.8

php的安装是基于httpd和mysql的,所以,安装php之前首先要先安装httpd和mysql,这两者先后顺序不作限定。

一、首先我们先安装mysql:这里我们使用mysql-5.5.28版本
mysql是一个客户端工具,服务器端是mysqld
客户端用SQL语句通过连接发送至服务器端进行执行,并将结果取回在本地显示。我们只用一台主机做演示,所以客户端和服务器端在同一台主机上。

1、先准备mysql数据库中的数据存放的位置即文件系统,在磁盘上新建一个分区,创建出一个逻辑卷,并将其挂载至特定目录下/data下即可。
创建新的分区/dev/sda5,指定类型为8e,大小为20G
partprobe /dev/sda
在/dev/sda5上创建逻辑卷

pvcreate /dev/sda5
vgcreate myvg /dev/sda5
lvcreate -L 10G -n mydata myvg

格式化该逻辑卷
mke2fs -j /dev/myvg/mydata

新建挂载的目录
mkdir /data
vim /etc/fstab  #开机自动挂载
/dev/myvg/mydata    /data   ext3    defaults 0 0
mount -a #自动挂载一下

2、新建用户以安全方式运行进程;假设创建目录/data/mydata作为mysql数据的存放位置
groupadd -r mysql
useradd -r -g mysql mysql
chown -R mysql.mysql /data/mydata

3、从ftp://172.16.0.1/pub/Sources/mysql-5.5。下载mysql-5.5.28-linux2.6-i686.tar.gz这个压缩包,解压至指定的路径,这里将它解压到/usr/local目录下,进行安装并初始化。
tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
cd /usr/local
ln -sv mysql-5.5.28-linux2.6-i686 mysql
cd mysql

chown -R mysql.mysql .  #将当前目录中的所有文件属主属组改为mysql别少了最后的'.'哦

初始化mysql:
scripts/mysql_install_db --user=mysql --datadir=/data/mydata  #指定路径为/data/mydata 用户为mysql
chown -R root .  #将其属主再更改为root

4、为mysql提供配置文件
cp support-files/my-large.cnf /etc/my.cnf

修改thread_concurrency的值为你的cpu的个数的2倍,不并添加行数据库的路径
thread_concurrency =2   #我的cpu个数为1
datadir = /data/mydata

5、为mysql 提供服务脚本
cd /usr/local/mysql
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld

将该服务添加至服务列表,并启动
chkconfig --add mysqld
chkconfig mysqld on

至此,mysql的简单安装就完成了,可以启动服务测试使用了。

为了使mysql更加规范化,并将其组件导出给系统使用,还可以添加下面一些步骤哦

1、修改mysql的路径和man命令的查找路径
vim /etc/mysql.sh
PATH=$PATH:/usr/local/mysql/bin
export PATH

vim /etc/man.config找到MANPATH 添加如下行:
MANPATH /usr/local/mysql/man

2、输出mysql头文件至系统头文件路径/usr/include,这里可以通过简单的链接可以实现
ln -sv /usr/loal/mysql/include /usr/include/mysql

3、输出mysql库文件的路径:
vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib

添加完成后,让系统重新载入系统库
ldconfig

之后系统便可以使用mysql命令直接进入数据库进行管理了,与数据库操作相关的命令在内部也可以直接使用了。。

二、apache的编译安装

1、先安装好开发环境,这里需要三个包组,"Development Libraries" "Development Tools" "X Software Development"
另外httpd的安装还要依赖于pcre-devel的软件包,这四项可以通过yum直接安装,这里不做演示。

2、我们要安装的是httpd-2.4.3的版本,apache的安装还需要更新的apr和apr-util 两个软件包,系统自带的apr和apr-util版本较老,我们这里需要手动去升级,升级的方式有2种,一种是直接升级rpm包,还有一种可以通过源码来进行编译,我们这里使用源码编译进行安装。下载地址是:ftp://172.16.0.1/pub/Sources/new_lamp。

1),编译安装apr
将两个软件包下载至本地,
tar xf apr-1.4.6.tar.bz2
cd apr-1.4.6
./congfigure --prefix=/usr/local/apr  #指定安装路径
make && make install


2),编译安装apr-util

tar xf apr-uitl-1.4.1.tar.bz2
cd apr-util-1.4.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  #apr-util的安装依赖于apr,所以为其指定apr的路径
make && make install

3、编译安装httpd-2.4.3
配置好httpd所需要的环境,接下来就可以对httpd进行安装了,首先下载httpd-2.4.3到本地,路径为:ftp://172.16.0.1/pub/Sources/new_lamp。
tar xf httpd-2.4.3.tar.bz2
cd httpd-2.4.3
./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-mpms-shared=all --with-mpm=prefork

这里解释下各选项代表的意义:

--prefix=/usr/local/apache #指定httpd的安装路径
--sysconfdir=/etc/httpd #指定安装后配置文件的路径
--enable-so #这项是关键:支持动态模块装卸载,如果不写的话,php没办法以模块的方式编译成apache的模块了
--enable-ssl #支持https
--enable-cgi #支持cgi动态执行
--enable-rewrite #支持url重写
--with-zlib #支持使用zlib库将数据发送到客户端之前进行压缩
--with-pcre #和依赖的pcre软件包相关
--with-apr=/usr/local/apr #依赖于apr告知apr的安装路径
--with-apr-util=/usr/local/apr-util #依赖于apr-util,告知其安装路径
--enable-mpms-shared=all #构建MPM为动态模块,将所有的模块都设为动态共享,可以在系统运行时加载
--with-mpm=prefork #指定安装的模块式prefork,如果安装后想要更改,可以再编辑LoadModule指令内容选择不同的模块。

make  &&   make install


4、设置httpd的主配置文件

vim  /etc/httpd/httpd.conf
PidFile "/var/run/httpd.pid"    #指定Pid文件的路径


5、安装好的httpd支持一个指令:apachectl可以启动httpd服务
可以修改PATH 的环境变量,让系统支持apachectl的相关操作。
vim /etc/profile.d/apachectl
PATH=$PATH:/usr/local/apache/bin
export PATH


运用apachectl start 就可以启动服务了。
(为httpd提供服务脚本后,这项可以不用设置的,我们可以通过httpd相关指令可以直接控制服务的开启与关闭)

6、为httpd提供服务脚本。(这里要确定httpd服务是关闭状态)

vim /etc/rc.d/init.d/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


这里可以自己根据原有httpd2.2 的版本样例来进行修改

而后为脚本赋予权限:
chmod +x /etc/rc.d/init.d/httpd

将该服务添加至服务列表:
chkconfig --add httpd

接下来就可以通过httpd来进行服务的测试了。

三、编译安装php-5.4.8

1、安装php-5.4.8依赖于libmcrypt-2.5.7-5.el5.i386.rpm
libmcrypt-devel-2.5.7-5.el5.i386.rpm 两个rpm包,可以让php支持mcrypt加密解密扩展库,下载地址:ftp://172.16.0.1/pub/Sources/ngnix目录中,另外开发环境已经在安装httpd的时候安装过了,这里不多做详解。

2、安装:
先安装两个rpm包:
rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm
rpm -ivh libmcrypt-devel-2.5.7-5.el5.i386.rpm


编译安装php,将源码包下载至本地,下载地址:ftp://172.16.0.1/pub/Sources/new_lamp。

tar xf php-5.4.8.tar.bz2
cd php-5.4.8
./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


各选项的意义:
--prefix=/usr/local/php #指定安装路径
--with-mysql=/usr/local/mysql #指定mysql的安装路径
--with-openssl #支持加密功能
--with-mysqli=/usr/local/mysql/bin/mysql_config #指定mysql的接口
--enable-mbstring #支持多字节的字符
--with-freetype-dir #支持多种字体文件库
--with-jpeg-dir #支持jpeg图片格式
--with-png-dir #支持pnf图片格式
--with-zlib #支持使用zlib库将数据发送到客户端之前进行压缩
--with-libxml-dir=/usr
#指定扩展标记语言库文件路径
--enable-xml #支持扩展标记语言
--enable-sockets #php可以基于sockets进行通信
--with-apxs2=/usr/local/apache/bin/apxs #指将php安装成apache的动态模块
--with-mcypt #支持加密解密功能
-with-config-file-path=/etc #指定配置文件的路径,主配置文件php.ini放在这里)
--with-config-file-scan-dir=/etc/php.d #指定主配置文件的从配置文件路径,这里放置一些以.ini 结尾的文件
--with-bz2 #支持bz2的压缩格式

make
make test   #进行安装测试 (建议在生成环境中使用)
make install


3、提供配置文件
在当前目录下:

cp php.ini-production  /etc/php.ini     #我们是生产环境中使用,所以拷贝production的配置文件即可


4、编辑apache的配置文件httpd.conf 以使apache支持php

vim /etc/httpd/httpd.conf
1),在末尾处添加如下2行:指定所能够识别php的类型
AddType application/x-httpd-php  .php
AddType application/x-httpd-php-source  .phps

2),定位至DirectoryIndex index.html
修改:
DirectoryIndex index.php index.html


之后重新启动httpd,或者让它重新载入配置文件就可以测试是否已经可以正常使用了。
尝试配置页面,httpd安装的默认网页在/usr/local/apache/htdos下

cd /usr/local/apache/htdocs
mv index.html index.php
vim index.php
添加:
<?php
phpinfo();
?>


重新启动httpd服务器,再次访问172.16.9.2就会出现以下页面了。




至此,LAMP各组件已经全部安装完成!!

补充(编译安装httpd选项):

(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。

四、扩展:在httpd中设置虚拟主机,安装xcache以及创建论坛

1,httpd虚拟主机配置:
首先注释掉中心主机

vim /etc/httpd/httpd.conf
#DocumentRoot "/usr/local/apache/htdocs"  在前面加'#'将其注释掉

Include /etc/httpd/extra/httpd-vhosts.conf      #将虚拟主机这项启用起来


编辑虚拟主机配置文件
<VirtualHost *:80>
DocumentRoot "/web/test"
ServerName www.test.com
<Directory "/web/test">
AllowOverride none
Options none
Require all granted
</Directory>
</VirtualHost>

在/web/test下编辑一个html文档,重启服务,即可进行访问。




2、安装xcache:为php加速的一个工具
1),安装xcache:下载地址ftp:// 172.16.0.1/pub/Sources/new_lamp.

tar xf xcache-2.0.0.tar.gz
cd xcache-2.0.0
/usr/local/php/bin/phpize       #生成php扩展模块
./configure --enable-xchahe --with-php-config=/usr/local/php-config  #为其指定php的配置文件路径
make && make install


2)、编辑php主配置文件php.ini

整合php和xcache
将xcache提供的配置文件按导入php.ini

mkdir /etc/php.d
cp xcache.ini  /etc/php.d       #xcache.ini 在当前目录下

编辑xcache.ini,找到zend_extension开头的行进行修改

vim /etc/php.d/xcache.ini
zend_extension =/usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so


注:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。
如下,在访问页面中就可以看到新增xcache.ini所在路径了





3、论坛的创建
下载Discuz_7.2_FULL_SC_GBK.zip,下载地址:ftp://172.16.0.1/pub/Sources/LAMP

touch tmp       #将解压缩后的文件放在该文件中
unzip Discuz_7.2_FULL_SC_GBK.zip > tmp

在当前目录下生成一个目录upload,切换到该目录

cd upload
mv * /web/test   #将该目录下的文件全部移到/web/test下

访问该网页之前编辑php配置文件,以便安装论坛时出错。

vim /etc/php.ini
short_open_tag = On     #这一项默认为off,将其启动

访问www.test.com/install #第一次进入安装论坛页面要加上install
会出现以下页面




按照安装步骤进行安装:





文件权限出现错误,根据提示改变其权限,如下:
(这一步是赋予启动httpd进程的用户以所有权限,这里的用户deamon是根据httpd的配置文件中的User的定义来决定的,你的可能会不一样哦)

[root@www ~]# cd /web/test
[root@www test]# setfacl -m u:daemon:rw- config.inc.php
[root@www test]# setfacl -m u:daemon:rwx attachments/
[root@www test]# setfacl -m u:daemon:rwx forumdata/
[root@www test]# setfacl -m u:daemon:rwx forumdata/cache/
[root@www test]# setfacl -m u:daemon:rwx forumdata/templates/
[root@www test]# setfacl -m u:daemon:rwx forumdata/threadcaches/
[root@www test]# setfacl -m u:daemon:rwx forumdata/logs/
[root@www test]# setfacl -m u:daemon:rwx uc_client/data/cache/

刷新该页面;





进入下一步:





这里填写的数据库和用户名最好是存在的,密码最好在创建数据库中指定用户时设定好(这里不多做详解,有关mysql可以参考前面的文章^_^)





一个简易的论坛就创建完成了,你可以在里面进行操作了!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  安装 Linux mysql release