ljhku 发表于 2015-9-22 08:27:40

CentOS6 编译安装LAMP(Apache,Mariadb,php)

httpd与php结合的方式有三种:1、module:将php编译成httpd的模块,当Apache服务器收到客户端的动态资源请求时,httpd服务自身便可以依靠php模块来处理动态资源。2、cgi:此种方式是当Apache服务器,每次收到客户端发来的动态资源请求时,将调用php生成一个子进程来处理客户端所请求的动态资源,因此n个请求便会在服务器端启动2n个进程,是对服务器资源极大程度的浪费,因此,此种结合方式很少用。3、fpm:此种方式是基于fastcgi协议,php作为一个服务监听在某个套接字上,当Apache客户端解析到客户端所请求的资源是动态资源是,便将请求发送给php服务,由php服务处理动态内容,并响应给httpd服务,再由httpd服务将结果返回给客户端。此时的Apache服务器便是一个代理服务器。httpd2.4版本新增了一个mod_fcgi模块,此模块便是可让httpd与php基于fpm方式结合,httpd2.2版本若想基于fpm方式与httpd结合,需自行安装fcgi模块。
本机版本及IP地址信息:
1
2
3
4
5
6
# uname -r
2.6.32-504.el6.x86_64
# cat /etc/redhat-release
CentOS release 6.6 (Final)
# ip addr show | awk -F'[ /]+' '/inet\>/&&!/127.0.0.1/ {print $3}'
172.16.113.14






安装次序:   1、由于pgp与httpd若与模块方式结合,那么首先得现有httpd才能将php编译进httpd的内核   2、若编译php支持mariadb的驱动,首先需要知道Mariadb的安装位置及其版本,才能编译相应的驱动。因此,编译次序为 httpd---- > MariaDB --- >php ,mariadb和httpd的安装次序不分前后。


a、关闭iptables    //若对iptables规则熟悉,可忽略此条信息b、关闭selinux   
1
2
      # sed -i 's/\(SELINUX=\).\+/\1disabled/' /etc/sysconfig/selinux
      # setenforce 0




c、准备开发环境(需提前配置好yum源)      Development tools      Server Platform Development
A、将php编译进httpd的模块,实现LAMP结合,并搭建phpMyadmin,实现数据库图形借口管理
需要的源码包:
1、httpd-2.4.9.tar.bz2----httpd的源码包,由Apache官网下载http://www.apache.org/2、php-5.6.4.tar.xz    -----php的源码包,有php官网下载      http://www.php.net/3、mariadb-5.5.43-linux-x86_64.tar.gz--- mariadb的二进制版本包,由mariadb官网下载https://downloads.mariadb.org/4、apr-1.5.0.tar.bz2 和apr-util-1.5.3.tar.bz2此为httpd的运行环境,httpd的安装需要依赖于apr(Apache portable Run-time libraries,Apache可移植运行库),实现httpd的跨平台运行。由于htpd2.4需要较高版本的apr及apr-util,所以需将其升级,可通过epel源升级或者源码升级。此处选择源码升级。
http://files.note.sdo.com/Z1AG999C6372B40FD0590D49B86E42F5E1AD9B66

1、展开源码包到/usr/local/src

1
2
3
4
5
# tar xf apr-1.5.0.tar.bz2 -C /usr/local/src/
# tar xf apr-util-1.5.3.tar.bz2 -C /usr/local/src/
# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/src/
# tar xf php-5.6.4.tar.xz -C /usr/local/src/
# unzip phpMyAdmin-4.0.5-all-languages.zip -d /usr/local/src/




2、httpd在url重写时可能需要用到pcre来支持perl语言的正则表达式,安装过程中需要依赖pere-devel包,因此,先装上此包:
1
# yum install -y pcre-devel    //需提前配置好yum源




3、安装apr及apr-util包
1
2
3
4
5
6
# cd /usr/local/src/apr-1.5.0/    //切换至apr解压目录
# ./configure --prefix=/usr/local/apr    //指明apr的安装位置
# make && make install   //编译并安装
# cd /usr/local/src/apr-util-1.5.3/    //切换至apr-util的解压目录
# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr    //指定安装目录及apr路径
# make && make install   //编译并安装





4、安装编译安装httpd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# cd /usr/local/src/httpd-2.4.9/    //切换至httpd的解压目录
# ./configure --help   
//使用此选项查看编译时所支持的参数,由于条目过多,此处指选出需要用到的参数进行讲解
--prefix=/usr/local/apache    //指明安装目录
--sysconfdir=/etc/httpd24    //指定配置文件的安装路径,此处为了避免与rpm包所装的httpd冲
突,将路径名取名为httpd24
--enable-so         //支持httpd的模块化机制
--enable-ssl   //启用支持ssl功能,实现https
--enable-cgi   //支持cgi协议
--enable-rewrite   //支持url重写
--with-zlib   //支持传输压缩
--with-pcre   //支持perl语言的正则表达式
--with-apr=/usr/local/apr    //指明编译升级的apr的路径
--with-apr-util=/usr/local/apr-util //指明编译升级的apr-util路径
--enable-modules=most   //指明启动大多数的模块
--enable-mpms-shared=all   //将所有mpm模块都编译进httpd(httpd2.4版本的新特性)
--with-mpm=event    //默认使用event的MPM工作模型
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --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-mpms-shared=all --with-mpm=event
# make && make install   //编译并安装





5、为编译安装的httpd添加环境变量
1
2
3
4
5
6
# vim /etc/profile.d/httpd24.sh   
//在/etc/profile.d/目录下创建一个以.sh结尾的文件,并编辑
export PATH=/usr/local/apache/bin/:$PATH    //将此信息添加至改文件,并保存退出
# source /etc/profile.d/httpd24.sh   
//通知系统重新读取此文件
现在便可以直接使用编译安装的httpd命令了。




6、编译配置文件,添加pidfile文件路径
1
2
# vim /etc/httpd24/httpd.conf
Pidfile "/var/run/httpd/httpd.pid"      //在配置文件中添加该行,并保存退出




7、为编译安装的httpd提供服务管理脚本,放置于/etc/rc.d/init.d下,名为httpd.24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# chkconfig: - 85 15
# description: The Apache HTTP Server Management script.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
      . /etc/sysconfig/httpd
fi
HTTPD_LANG=${HTTPD_LANG-"C"}
INITLOG_ARGS=""
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
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 ${STOP_TIMEOUT} $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=6                                                                                                                                                                  
      echo $"not reloading due to configuration syntax error"                                                                                                                  
      failure $"not reloading $httpd due to configuration syntax error"                                                                                                         
    else                                                                                                                                                                           
      # Force LSB behaviour from killproc                                                                                                                                       
      LSB=1 killproc -p ${pidfile} $httpd -HUP                                                                                                                                 
      RETVAL=$?                                                                                                                                                                  
      if [ $RETVAL -eq 7 ]; then                                                                                                                                                
            failure $"httpd shutdown"            
      fi                                                                                                                                                                        
    fi                                                                                                                                                                           
    echo                                                                                                                                                                           
}                                                                                                                                                                                 
                                                                                                                                                                                    
# See how we were called.                                                                                                                                                         
case "$1" in                                                                                                                                                                     
start)                                                                                                                                                                           
      start                                                                                                                                                                     
      ;;                                                                                                                                                                        
stop)                                                                                                                                                                           
      stop                                                                                                                                                                     
      ;;                                                                                                                                                                        
status)                                                                                                                                                                        
      status -p ${pidfile} $httpd                                                                                                                                                
      RETVAL=$?                                                                                                                                                                  
      ;;                                                                                                                                                                        
restart)                                                                                                                                                                        
      stop                                                                                                                                                                     
      start                                                                                                                                                                     
      ;;                                                                                                                                                                        
condrestart|try-restart)                                                                                                                                                         
      if status -p ${pidfile} $httpd >&/dev/null; then                                                                                                                           
                stop                                                                                                                                                               
                start                                                                                                                                                            
      fi                                                                                                                                                                        
      ;;                                                                                                                                                                        
force-reload|reload)                                                                                                                                                            
      reload                                                                                                                                                                     
      ;;                                                                                                                                                                        
graceful|help|configtest|fullstatus)                                                                                                                                             
      $apachectl $@                                                                                                                                                            
      RETVAL=$?                                                                                                                                                                  
      ;;                                                                                                                                                                        
*)                                                                                                                                                                              
      echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"                                          
      RETVAL=2                                                                                                                                                                  
esac                                                                                                                                                                                                                                                                                                                                    
exit $RETVAL





8、将httpd24添加至开机自动启动列表,脚本启动httpd,进行测试:
1
2
3
4
5
6
7
8
# chkconfig --add httpd24
# chkconfig --list httpd24
httpd24         0:off   1:off   2:on    3:on    4:on    5:on    6:off
# service httpd24 start
Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message    //此处报错可忽略

# ss -tnl | grep 80
LISTEN   0      128                      :::80                      :::*   //80端口已处于监听状态




9、安装mariadb:此安装过程可查看mysql的展开目录中的INSTALL-BINARY 文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# cd /usr/local/src/       //切换至src的目录
# mv mariadb-5.5.43-linux-x86_64 /usr/local/mysql   
//将mariadb的目录名改为mysql
# groupadd -r -g 306 mysql    //添加mysql组为系统组
# useradd -r -u 306 -g 306 mysql   
//添加mysql用户为系统用户并添加至mysql组
# chown -R mysql:mysql mysql   
//将mysql目录及其子目录改为属主属组都为mysql
# mkdir /mydata/data -pv   
//创建mysql的数据库目录
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
# cd mysql/
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data   
//以mysql用户安装mariaDB并指明数据库目录位置

# chown -R root .       //将mysql目录改为root属主
# mkdir /etc/mysql    //创建mysql配置文件目录
//mysql的配置文件查找顺序为:
/etc/my.cnf -- > /etc/mysql/*.cnf ---> --defaults-file(若编译安装时指定此项)
---> ~/.my.cnf 启动mysql的用户家目录下,最后一个加载的配置参数覆盖前面的配置参数

# cp support-files/my-large.cnf /etc/mysql/my.cnf   
//展开目录下support-files目录中提供有几个配置模板,依照内存需求命令,
可根据需要拷贝为默认的配置文件

# vim /etc/mysql/my.cnf   
//编辑配置文件,在容器中添加如下几个参数
thread_concurrency = 8      //将此项改为自己cpu核心数量的两倍
datadir = /mydata/data    //指明数据库目录
innodb_file_per_table = yes    //开启innodb数据库每个表存储为一个文件
skip_name_resolve = yes    //开启跳过名称自解析
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld   
//提供服务管理脚本,此脚本为软件所提供

# chmod +x /etc/rc.d/init.d/mysqld    //将服务脚本赋予可执行权限
# chkconfig --add mysqld    //将mysqld添加至开机自动启动项
# chkconfig mysqld on    //开启开机自动启动
# chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
# /etc/init.d/mysqld start    //启动mysqld服务
# bin/mysql_secure_installation   
//运行mariadb的安全初始化,以完成为root用户添加密码以及其他的安全操作

至此,mariadb已完成安装





10、编译安装php,将其安装为httpd的模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# yum install -y libmcrypt-devel.x86_64 bzip2-devel    //在epel源中有提供

//解决依赖关系,php可能用到bzip2来进行数据的压缩
# yum install -y libxml2-devel   
//编译安装若启用对xml文档的支持,则需安装此项

# cd /usr/local/src/php-5.6.4/    //切换至php的展开目录
此处依旧可使用./configure --help 查看编译安装时所支持的参数
--prefix=/usr/local/php   //指明安装路径
--with-mysql=/usr/local/mysql   //指定mysql的位置
--with-openssl   //支持openssl
--with-mysqli=/usr/local/mysql/bin/mysql_config   //对mysql进行交互的另外一种接口
--enable-mbstring         //对多字节字符串支持,对中文支持,必须启用此项
--with-freetype-dir         //支持freetype字体格式
--with-jpeg-dir   //开启此功能,可以使用php直接处理jpeg格式的图片   
--with-png-dir   //   开启此功能,可以支持php直接处理png格式的图片
--with-zlib   // 支持zlip压缩,提高数据传输效率
--with-libxml-dir=/usr   //支持处理xml文档
--enable-xml      //启用xml功能
--enable-sockets   //表示支持php支持socket进行通信




--with-apxs2=/usr/local/apache/bin/apxs    //此项表示将php编译成模块 --with-mcrypt          //支持加密解密--with-config-file-path=/etc   //指明php配置文件的放置位置--with-config-file-scan-dir=/etc/php.d   //表示检索此位置下的.ini结尾的文件作为php的配置文件--with-bz2      //支持bzip2格式的压缩--enable-maintainer-zts    //若httpd以线程模式运行(worker或event)模式运行,此项必须!表示将php编译成zts模块#./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 -j 4 && make install    //编译并安装。可使用-j n 指明使用几颗cpu核心进行编译,加速编译过程./configure过程中,若出现错误,一般都是因为所依赖的*-devel包未安装,因此,使用yum install安装即可。

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




//提供php配置文件,此文件无需更改。

1
# vim /etc/httpd24/httpd.conf




//编辑httpd配置文件,找到php的模块,确保是否php已安装成功
添加如下几项:
1
2
3
    AddType application/x-httpd-php .php
    AddType application/x-httpd-source .phps
    将DirectoryIndex index.html   改为    DirectoryIndex index.php index.html




保存退出。执行httpd -t

1
# httpd -t    //检查配置文件是否有误





1
# /etc/rc.d/init.d/httpd24 reload    //重读配置文件





1
# httpd -M |grep php    //检查php的模块是否已加载




AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message php5_module (shared)
编辑一个测试页,测试LAMP是否已结合成功----
1
# vim /usr/local/apache/htdocs/index.php






1
2
3
4
5
6
7
8
<?php
      $conn=mysql_connect('127.0.0.1','root','root');//此处mysql链接账号密码为安装完成Mariadb后,运行安全初始化时设置的。
      if ($conn)
                echo "OK";
      else
                echo "Faulse";
      phpinfo();
?>





11、在浏览器中打开网页进行查看:出现如下页面,即表示搭建成功。
12、搭建基于主机名的虚拟主机安装phpMyadmin,实现mysql(MariaDB)图形化管理:
1
2
3
# vim /etc/httpd24/httpd.conf    //编辑httpd配置文件
1)注释中心主机:#DocumentRoot "/usr/local/apache/htdocs"
2)添加:    Include /etc/httpd24/vhosts.conf




13、创建并编辑虚拟主机配置文件:
# vim /etc/httpd24/vhosts.conf<VirtualHost 172.16.113.14:80>      DocumentRoot /web/vhosts/pma      ServerName pma.su.com      CustomLog "/var/log/httpd/pma_access.log" common      ErrorLog "/var/log/httpd/pma_error.log"</VirtualHost><Directory "/web/vhosts/pma">    //httpd2.4版本必须为网站目录授权才可访问    AllowOverride None    Options None    Require all granted</Directory>
# mkdir -pv /var/log/httpd   //创建日志文件目录# mkdir -pv /web/vhosts/pma    //创建网站根目录mkdir: created directory `/web'mkdir: created directory `/web/vhosts'mkdir: created directory `/web/vhosts/pma'# httpd -t    //检查配置文件是否有误# /etc/rc.d/init.d/httpd24 restart    //重启httpd服务# mv /usr/local/src/phpMyAdmin-4.0.5-all-languages/* /web/vhosts/pma/    //将程序文件移动到网站根目录下//配置phpMyadmin配置文件# cp config.sample.inc.php config.inc.php      
//将配置文件模板复制为配置文件(去掉sample)
# vim config.inc.php       //编辑配置文件$cfg['blowfish_secret'] = 'aadf$#23SDA$%8b7c6d';      //在此项添加一个随机数
编辑测试主机hosts文件(C:\Windows\System32\drivers\etc),添加如下信息,并保存172.16.113.14        pma.su.com
在浏览器输入pma.su.com访问即可。

页: [1]
查看完整版本: CentOS6 编译安装LAMP(Apache,Mariadb,php)