5imobi 发表于 2018-11-21 07:05:50

CentOS6 编译安装LAMP(Apache




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地址信息:
# 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
      # sed -i 's/\(SELINUX=\).\+/\1disabled/' /etc/sysconfig/selinux
      # setenforce 0c、准备开发环境(需提前配置好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源升级或者源码升级。此处选择源码升级。






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

# 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包,因此,先装上此包:
# yum install -y pcre-devel    //需提前配置好yum源3、安装apr及apr-util包
# 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
# 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添加环境变量
# 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文件路径
# vim /etc/httpd24/httpd.conf
Pidfile "/var/run/httpd/httpd.pid"      //在配置文件中添加该行,并保存退出7、为编译安装的httpd提供服务管理脚本,放置于/etc/rc.d/init.d下,名为httpd.24
# 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,进行测试:
# 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 文档

# 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的模块
# 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安装即可。


# cp php.ini-production /etc/php.ini //提供php配置文件,此文件无需更改。

# vim /etc/httpd24/httpd.conf//编辑httpd配置文件,找到php的模块,确保是否php已安装成功

添加如下几项:
    AddType application/x-httpd-php .php
    AddType application/x-httpd-source .phps
    将DirectoryIndex index.html   改为    DirectoryIndex index.php index.html保存退出。执行httpd -t

# httpd -t    //检查配置文件是否有误# /etc/rc.d/init.d/httpd24 reload    //重读配置文件# 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是否已结合成功----
# vim /usr/local/apache/htdocs/index.php


11、在浏览器中打开网页进行查看:出现如下页面,即表示搭建成功。



12、搭建基于主机名的虚拟主机安装phpMyadmin,实现mysql(MariaDB)图形化管理:
# vim /etc/httpd24/httpd.conf    //编辑httpd配置文件
1)注释中心主机:#DocumentRoot "/usr/local/apache/htdocs"
2)添加:    Include /etc/httpd24/vhosts.conf13、创建并编辑虚拟主机配置文件:

# vim /etc/httpd24/vhosts.conf

      DocumentRoot /web/vhosts/pma
      ServerName pma.su.com
      CustomLog "/var/log/httpd/pma_access.log" common
      ErrorLog "/var/log/httpd/pma_error.log"

    //httpd2.4版本必须为网站目录授权才可访问
    AllowOverride None
    Options None
    Require all granted



# 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.14pma.su.com


在浏览器输入pma.su.com访问即可。


14、为pma.su.com提供https服务:
a>搭建ca服务器:
IP地址为172.16.113.101
# ls    //保证/etc/pki/CA目录下有这些文件及目录,并在serial中的第一行写入01


# (umask 077;openssl genrsa -out private/cakey.pem -out cacert.pem -days 7300)
    //生成CA的私钥


# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 7300   
    //生成CA的自签证书
b>在httpd服务器生成私钥,并生成证书申请请求:
# mkdir /etc/httpd24/ssl
# cd /etc/httpd24/ssl
# (umask 077;openssl genrsa -out httpd.key 2048)
//证书申请请求需填写国家等信息,需要注意的是,下面此项必须和网站的FQDN完全一致

Common Name (eg, your name or your server's hostname) []:pma.su.com
# scp httpd.csr root@172.16.113.101:/tmp/    //将证书申请请求发送至CA的tmp目录下
c>在CA服务器签发证书,并发送给httpd服务器
# openssl ca -in /tmp/httpd.csr -out certs/pma.su.com.crt -days 365    //CA签发证书
# scp certs/pma.su.com.crt root@172.16.113.14:/etc/httpd24/ssl




d>配置
# vim /etc/httpd24/extra/httpd-ssl.conf


ServerName pma.su.com
ServerAdmin admin@su.com
ErrorLog "/var/logs/pma_error_log"
TransferLog "/var/logs/pma_access_log"
SSLCertificateFile "/etc/httpd24/ssl/pma.su.com.crt"
SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key"


e>编辑Apache配置文件,将下面几项注释去掉                  
Include /etc/httpd24/extra/httpd-ssl.conf
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so


f>将CA的证书导出至测试机的浏览器进行测试即可






15、安装PHP加速器:
      原理:php的执行需要两步:1、编译 2、执行 ,其中编译得过程又分为三个步骤:1、扫描(查看是否存在语法错误);2、分析(分析哪些是php代码,哪些是数据);3、编译(编译成opcode);PHP加速器的原理是将php代码编译后的opcode缓存至内存中,并共享给其他请求使用,这样节省了php代码的执行步骤,从而实现了加速功能。


       测试1:

安装前,可使用ab命令测试httpd服务器每秒可以相应多少个请求:
模仿20个用户,1000个并发请求

此时,httpd服务器每秒种可响应33个请求。
      安装:

# cd /usr/local/src/xcache-3.2.0/    //切换至xcache的展开目录
# /usr/local/php/bin/phpize   //生成configure文件
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
// 指明编译所用到的php配置文件的位置

# make && make install
# mkdir /etc/php.d    //创建php的配置扫描路径
# cp xcache.ini /etc/php.d/    //拷贝xcache源码包中的配置文件到php.d目录
# vim /etc/php.d/xcache.ini   将其中的extension项的库文件指向,指向安装xcache最后显示的路径
例如:/usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
# service httpd24 restart    //重启httpd


    测试:模仿20个用户,1000个并发请求。


由此可见,加速效果已达到。




16、再次创建一个机遇域名的虚拟主机,搭建wordpress论坛:

# vim /etc/httpd24/vhosts.conf   //编辑配置文件

      DocumentRoot /web/vhosts/wp
      ServerName wp.su.com
      CustomLog "/var/log/httpd/wp_access.log" common
      ErrorLog "/var/log/httpd/wp_error.log"



    AllowOverride None
    Options None
    Require all granted




# mkdir /web/vhosts/wp
# unzip wordpress-4.3.1-zh_CN.zip -d /web/vhosts/wp/    //将WordPress解压至wp.su.com的网站根目录中
# cd /web/vhosts/wp
#mv wordpress/* ./      //将文件全部移动出来
# cp wp-config-sample.php wp-config.php   //将配置模板复制为配置文件
# vim wp-config.php    //编辑配置文件

# /usr/local/mysql/bin/mysql -uroot -hlocalhost -p    //登录数据库
MariaDB [(none)]> create database wpdb;    //创建数据库‘wpdb’
在测试主机的hosts文件添加:
172.16.113.14wp.su.com


在浏览器输入主机名访问进行配置即可。
  

  




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