更多内容请点击:

Linux学习从入门到打死也不放弃,完全笔记整理(持续更新,求收藏,求点赞~~~~) 

https://blog.51cto.com/13683480/2095439


第24章,实现LAMP架构

 

本章内容:

               LAMP介绍

               PHP介绍与配置

               module方式实现LAMP

               php-fpm方式实现LAMP

               部署phpmyadmin

               部署wordpress

               部署discuz!

               实现php的  Xcache加速

               源码编译:安装LAMP

               

LAMP介绍:

               LAM(M)P 含义:

                             L:linux

                            A:apache(httpd)

                            M:  mysql,mariadb

                            (M):  memcached

                            P:  php,perl,python

                             

               WEB资源类型:

                             静态资源:原始形式与相应内容一致,在客户端浏览器执行

                             动态资源:原始形式通常为程序文件,需要在服务器端执行之后,将执行结果

                                            返回给客户端

               

               WEB相关术语:

                            客户端技术:html,javascript

                            服务器端技术:php,jsp,python,asp

                             

               CGI:Common Gateway  Interface

                             可以让一个客户端,从网页浏览器通过http服务器向执行在网络服务器上的

                            程序传输数据

                             CGI描述了客户端和服务器程序之间的一种标准

               

               请求流程:

                            Client  --(httpd)--> httpd --(cgi)  -->

                            application  server(program file)--(mysql) --> mysql

                             

               php:

                             脚本编程语言,嵌入到html中的嵌入式web程序语言

                            基于zend编译成opcode(二进制格式的字节码,重复运行,可省略编译环境)

 

 

PHP简介:

               官网:http://www.php.net/

            

               PHP是通用服务器端脚本编程语言,主要用于web开发实现动态web页面,也是最早实现将脚

               本嵌入HTML源码文档中的服务器端脚本语言之一。同时,php还提供了一个命令行接口,因此,

               其也可以在大多数系统上作为一个独立的shell来使用

           

               Rasmus Lerdorf于1994年开始开发PHP,它是初是一组被Rasmus  Lerdorf称作“Personal  

               Home Page Tool” 的Perl脚本,  这些脚本可以用于显示作者的简历并记录用户对其网站的访

               问。后来,Rasmus Lerdorf使用C语言将这些Perl脚本重写为CGI程序,还为其增加了运行Web

               forms的能力以及与数据库交互的特性,并将其重命名为“Personal Home  Page/Forms

               Interpreter”或“PHP/FI”。此时,PHP/FI已经可以用于开发简单的动态web程序了,这即

               PHP1.0。1995年6月,Rasmus Lerdorf把它的PHP发布于

               comp.infosystems.www.authoring.cgi Usenet讨论组,从此PHP开始走进人们的视野。1997

               年,其2.0版本发布。

               

               1997年,两名以色列程序员Zeev  Suraski和Andi  Gutmans重写的PHP的分析器(parser)成为

               PHP发展到3.0的基础,而且从此将PHP重命名为PHP: Hypertext  Preprocessor。此后,这两

               名程序员开始重写整个PHP核心,并于1999年发布了Zend Engine 1.0,这也意味着PHP 4.0的

               诞生。2004年7月,Zend Engine 2.0发布,由此也将PHP带入了PHP 5时代。PHP5包含了许

               多重要的新特性,如增强的面向对象编程的支持、支持PDO(PHP Data  Objects)扩展机制以及

               一系列对PHP性能的改进

 

               Zend Engine是开源的、PHP脚本语言的解释器,它最早是由以色列理工学院

               (Technion)的学生Andi Gutmans和Zeev Suraski所开发,Zend也正是此二人

               名字的合称。后来两人联合创立了Zend Technologies公司

               

               Zend Engine 1.0于1999年随PHP  4发布,由C语言开发且经过高度优化,并能

               够做为PHP的后端模块使用。Zend  Engine为PHP提供了内存和资源管理的功

               能以及其它的一些标准服务,其高性能、可靠性和可扩展性在促进PHP成为一

               种流行的语言方面发挥了重要作用

               

               Zend Engine的出现将PHP代码的处理过程分成了两个阶段:首先是分析PHP

               代码并将其转换为称作Zend opcode的二进制格式(类似Java的字节码),并将

               其存储于内存中;第二阶段是使用Zend Engine去执行这些转换后的Opcode

 

               Opcode是一种PHP脚本编译后的中间语言,类似于Java的ByteCode,或

               者.NET的MSL。PHP执行PHP脚本代码一般会经过如下4个步骤(确切的来说,

               应该是PHP的语言引擎Zend)

                     1、Scanning  词法分析,将PHP代码转换为语言片段(Tokens)

                     2、Parsing 语义分析,将Tokens转换成简单而有意义的表达式

                     3、Compilation   将表达式编译成Opocdes

                     4、Execution 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能

                     

               扫描-->分析-->编译-->执行

 

 

php配置:

               php:脚本语言解释器

               配置文件:

                             /etc/php.ini,/etc/php.d/*.ini

               

               配置文件在php解释器启动时被读取

               对配置文件的修改生效方式:

                             modules:重启httpd服务

                             FastCGI:重启php-fpm服务

                             

               /etc/php.ini 配置文件格式:

                            [foo]:Section  Header

                            directive =  value

                             注释符:较新的版本中,已经完全使用;进行注释

                                   #     纯粹的注释信息

                                   ;      用于注释可启用的directive

                             

               常用配置指令:

                     max_execution_time= 30            最长执行时间30s

                     memory_limit 128M                  生产不够,可调大

                     display_errors off                调试使用,不要打开,否则可能暴露重要信息

                     display_startup_errors  off  建议关闭

                     post_max_size 8M                     最大上传数据大小,生产可能临时要调大,比下面项要大

                     upload_max_filesize  2M   最大上传文件,生产可能要调大

                     max_file_uploads = 20               同时上传最多文件数

                     date.timezone  =Asia/Shanghai  指定时区

                     short_open_tag=on                   开启短标签,如<? phpinfo();?>

 

               php.ini的核心配置选项文档:

                      http://php.net/manual/zh/ini.core.php

                    

               php.ini配置选项列表    

                      http://php.net/manual/zh/ini.list.php

 

               php语言格式:

                             <?php

                            ...php  code

                             ?>

 

               php的加速器:

                             php的加速器:基于PHP的特殊扩展机制如opcode缓存扩展也可以将opcode

                            缓存于php的共享内存中,从而可以让同一段代码的后续重复执行时跳过编译

                             阶段以提高性能。这些加速器并非真正提高了opcode的运行速度,而仅是通过

                            分析opcode后并将它们重新排列以达到快速执行的目的

 

               常见的php加速器有:

                     1、APC (Alternative PHP Cache)

                     遵循PHP License的开源框架,PHP  opcode缓存加速器,目前的版本不适用于

                     PHP  5.4

                     项目地址http://pecl.php.net/package/APC

 

                     2、eAccelerator

                     源于Turck MMCache,早期的版本包含了一个PHP  encoder和PHP loader,

                     目前encoder已经不在支持。项目地址 http://eaccelerator.net/  

 

                     3、XCache

                     快速而且稳定的PHP opcode缓存,经过严格测试且被大量用于生产环境。项

                     目地址:http://xcache.lighttpd.net/,收录EPEL源

 

                     4、Zend Optimizer和Zend Guard  Loader

                     Zend  Optimizer并非一个opcode加速器,它是由Zend  Technologies为

                      PHP5.2及以前的版本提供的一个免费、闭源的PHP扩展,其能够运行由Zend

                     Guard生成的加密的PHP代码或模糊代码。 而Zend Guard  Loader则是专为

                     PHP5.3提供的类似于Zend Optimizer功能的扩展。项目地址

                      http://www.zend.com/en/products/guard/runtime-decoders

                     

                     5、NuSphere PhpExpress

                     NuSphere的一款开源PHP加速器,它支持装载通过NuSphere PHP  Encoder

                     编码的PHP程序文件,并能够实现对常规PHP文件的执行加速。项目地址,

                      http://www.nusphere.com/products/phpexpress.htm

 

 

---------------------------------------------------------------------------               

部署LAMP:

               httpd:接收用户的web请求;静态资源则直接响应;动态资源为php脚本,

                             对此类资源的请求将交由php来运行

               php:运行php程序

               MariaDB:数据管理系统

               

               http与php结合的方式

                            CGI  

                            FastCGI:php-fpm

                            modules:php (将php编译成为httpd的模块,默认方式)

                             MPM:

                                   prefork :  libphp5.so

                                   event,  worker: libphp5-zts.so             

                             

 

               centos7:

                            modules:httpd, php, php-mysql, mariadb-server

                            FastCGI:httpd,php-fpm,php-mysql,mariadb-server

                    

               centos6:

                             modules:httpd,php,php-mysql,mysql-server

                            默认不支持FastCGI

                             

安装LAMP:

               CentOS 6:

                            yum install httpd  php mysql-server php-mysql

                            service httpd  start

                            service  mysqld   start

 

               CentOS 7:

                            yum install httpd  php php-mysql mariadb-server

                            systemctl  start   httpd.service

                            systemctl  start   mariadb.service

               注意:要使用prefork模型

 

测试php是否正常连接:

               vim /var/www/html/index.php

<?php

        echo date("Y/m/d H:i:s");

        phpinfo();

?>    

 

php使用mysql扩展连接数据库

               只能连接mysql数据库

               注意:php7 已启用此函数

               

               1     执行  mysql_secure_installation 设置root 密码

               2     登录mysql 添加授权账号:

                     GRANT ALL ON *.* TO  'user1'@'192.168.65.%' IDENTIFIED BY 'centos';

 

               3     编写测试文件:

                     vim  /var/www/html/mysql.php

                      <?php

                     #$conn = mysql_connect(  'servername','user','password');

                     $conn = mysql_connect(  '127.0.0.1','user1','centos');

                      if($conn)

                                   echo "OK";

                     else

                                   echo  "Failure";

                                   echo  mysql_error();

                      mysql_close();

                     ?>

                    

                     或者

                      <?php

                     $conn = mysql_connect(  '192.168.65.132','user1','centos');

                      if($conn)

                                   echo  "OK";

                     else

                                   echo  "Failure";

                                   echo  mysql_error();

                      mysql_close();

                     ?>

 

               4     浏览器输入  192.168.65.132/mysql.php 访问测试

               

 

php使用mysqli扩展连接数据库

               只能连接mysql数据库

               vim /var/www/html/mysql2.php

                      <?php

                     $mysqli=new  mysqli('192.168.65.155','user1','centos');

                      if(mysqli_connect_errno()){

                                   echo  "Failure";

                                    $mysqli=null;

                                    exit;

                     }

                     echo  "OK";

                      $mysqli->close();

                     ?>

 

 

php使用pdo扩展连接数据库

               可以连接主流各种数据库

               

               代码1      

                     vim  /var/www/html/mysql3.php

                      <?php

                      $dsn='mysql:host=localhost;dbname=mysql';

                      $username='root';

                      $passwd='centos';

                     $dbh=new  PDO($dsn,$username,$passwd);

                      var_dump($dbh);

                     ?>  

               

               代码2      

vim  /var/www/html/mysql4.php

<?php

try {

$user='root';

$pass='centos';

$dbh = new  PDO('mysql:host=localhost;dbname=mysql', $user, $pass);

foreach($dbh->query('SELECT user,host  from user') as $row) {

print_r($row);

}

$dbh = null;

} catch (PDOException $e)  {

print "Error!: " . $e->getMessage() .  "<br/>";

die();

}

?>

 

-----------------------------------------------------------------------------------

常见LAMP应用:

               PhpMyAdmin:

                            是一个以PHP为基础,以Web-Base方式架构在网站主机上的MYSQL的数据库管理工具

                            让管理者可用web接口管理Mysql数据库

                             

               WordPress:

                            是一种使用PHP语言开发的博客平台,用户可以在支持PHP和Mysql数据库的服务器

                             上架设属于自己的网站。也可把Wordpress当作一个内容管理系统(CMS)来使用

                             

               PHPWind:

                            2003年发布了PHPWind的前身版本ofstar,并发展称为包含BBS,CMS,博客,SNS等

                              一系列程序的通用型建站软件,于2008年加入阿里巴巴集团

                             

               Crossday discuz! Board(简称discuz!):

                             是一套通用的社区论坛软件系统。自2001年6月面世以来,是全球成熟度最高,覆盖率

                             最大的论坛软件系统之一。

                            2010年8月23日,与腾讯达成收购协议

                             

               ECShop:

                            是一款B2C独立网店系统,适合企业及个人快速构建个性化网上商店。系统是基于

                            PHP语言及MYSQL数据库架构开发的跨平台开源程序。

                            2006年6月,ECShop推出第一个版本1.0

                             

 

部署phpMyadmin:

               1     安装并启动lamp

                     yum install httpd php  mariadb-server mysql-server

                     yum install  php-mbstring

                     systemctl start httpd  mariadb

               

               2     执行mysql_secure_installation,设置管理员密码

                    

               3     下载phpmyadmin,注意查看支持的php和数据库版本

                     下载地址:

                     4.0版本,PHP 5.2 and MySQL 5. Does

                             https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.zip

                    

                     4.8版本,PHP 5.2 and MySQL 5. Does not support PHP 5.5 or  newer

                             https://files.phpmyadmin.net/phpMyAdmin/4.8.2/phpMyAdmin-4.8.2-all-languages.zip

                             

               4     解压并复制到/var/www/html/目录下

                     unzip  phpMyAdmin-4.3.5-all-languages.zip

                     cp -a  phpMyAdmin-4.3.5-all-languages /var/www/html/mysql

                    

               5     准备配置文件

                     cd  /var/www/html/mysql

                     cp config.sample.inc.php  config.inc.php

 

               6     重启服务

                     systemctl restart  httpd

                    

               7     浏览器输入  server-ip/mysql

                     使用root账号登录

                    

 

部署wordpress:

               1     下载软件:

                     官网:https://cn.wordpress.org/

                            wget  https://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip

 

               2     解压并复制

                     unzip  wordpress-4.9.4-zh_CN.zip

                     cp -a wordpress  /var/www/html/blog

                     chown -R apache:apache  /var/www/html/blog

               

               3     准备数据库和账号

                     mysql>CREATE DATABASE  wordpress

                     mysql>GRANT ALL ON  wordpress.* TO 'wpuser'@'localhost' IDENTIFIED BY  'centos';

 

               4     准备配置文件:

                     cp wp-config-sample.php  wp-config.php

                     vim  wp-config.php

                     数据库登录信息

               

               5     重启httpd

               

               6     server-ip/blog  登录执行初始化

               

               

部署discuz:

               1     下载软件:

                      http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip

 

               2     解压缩并复制的网站目录下      

                            unzip  Discuz_X3.2_SC_UTF8.zip

                            cp upload/*  /data/dz/ -a

                            chown -R  apache:apache /data/dz

               

               3     重启httpd 并测试访问             

 

centos7 编译php-xcache  加速访问:

               

               官网:http://xcache.lighttpd.net/wiki/ReleaseArchive

               安装方法:

                            rpm安装,来自epel源

                             

               编译安装:

                     1     装备环境,安装开发包组development tools

                            yum install  php-devel

                    

                     2     下载并解压缩  xcache-3.2.0.tar.bz2

                            cd  xcache-3.2.0

                             

                     3     phpize 生成编译环境

                    

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

                            make && make  install

                    

                     5     cp xcache-3.2.0/xcache.ini  /etc/php.d/

                            systemctl reload  httpd

                             

                             

------------------------------------------------------------------------------          

fastcgi:

               centos6:

                             PHP-5.3.2之前:默认不支持fpm机制,需要自行打补丁并编译安装

                             httpd2.2:默认不支持fcgi协议,需要自行编译此模块

                             解决方案:编译安装http-2.4,php-5.3.3+

                             

               centos7:

                             httpd-2.4:rpm包默认编译支持fcgi模块

                             php-fpm包:专用于将php运行与fpm模式

 

               

               fcgi服务配置文件:/etc/php-fpm.conf,/etc/php-fpm.d/*.conf

                     连接池:

                            pm =  static|dynamic

                             static:固定数量的子进程;pm.max_children

                             dynamic:子进程数量以动态模式管理

                                           pm.max_children           最大进程数

                                           pm.start_servers     开启启动进程数

                                           pm.min_spare_servers   最小空闲进程

                                           pm.max_spare_servers 最大空闲进程

                                           pm.max_requests = 500       最大处理请求数

                             

                            daemonize = no  //是否将程序运行在后台

                            listen =  127.0.0.1:9000 //FPM 监听地址

                            listen.backlog = -1  //等待队列的长度 -1表示无限制

                             listen.allowed_clients = 127.0.0.1 //仅允许哪些主机访问

                             php_value[session.save_handler] = files

                             php_value[session.save_path] = /var/lib/php/session  

                    

                     确保运行php-fpm 进程的用户对session目录有读写权限

                                   mkdir  /var/lib/php/session

                                   chown  apache.apache /var/lib/php/session                               

                    

 

centos7,yum实现fastfcgi

 

同一台主机:

               1     安装服务并启动服务

                            yum install httpd  mariadb-server php-fpm php-mysql

                            systemctl start  httpd php-fpm mariadb

                             

                    

               2     配置http

                            确认httpd服务器上必须启用proxy_fcgi_module模块,充当php客户端

                                          httpd  -M |grep fcgi

                                          cat  /etc/httpd/conf.modules.d/00-proxy.conf

                             

                            添加/etc/httpd/conf.d/fcgi.conf 配置文件,内容参照

                                           DirectoryIndex index.php

                                      ProxyRequests Off

                                      ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/var/www/html/$1                                      

                    

               3     登录数据库添加授权账号

 

               4     测试连接php和mysql连接书否正常

 

不同主机:

               注意:httpd和php-fpm需为同一主机

               如分别使用不同主机,即使使用相同目录路径,会导致图片信息无识别

               

               数据库最好使用单独主机,只需在应用程序配置地址,账号信息即可

                    

 

----------------------------------------------------------------------------

实现:同一服务器使用虚拟主机配置多应用:

 

php,module方式:

               1     yum install httpd  mariadb-server php php-mysql

                     yum install php-string  安装phpmyadmin需要此包

                    

                     注释掉httpd主配置文件中 documentroot 项

                     添加配置文件

                            vim  /etc/httpd/conf.d/vhosts.conf

                            <virtualhost  *:80>

                                           servername www.blog.com

                                           documentroot "/data/blog/"

                                           errorlog "/var/log/httpd/error_blog_log"

                                           customlog "/var/log/httpd/access_blog_log" conbined

                                           <directory "/data/blog">

                                                         options none

                                                         allowoverride none

                                                         require all granted

                                           </directory>

 

                                          alias  /pma "/data/pma"

                                           <directory "/data/pma">

                                                         options none

                                                         allowoverride none

                                                         require all granted

                                           </directory>

                             </virtualhost>

 

                            <virtualhost  *:80>

                                           servername www.dz.com

                                           documentroot "/data/dz"

                                           errorlog "/var/log/httpd/error_dz_log"

                                           customlog "/var/log/httpd/access_dz_log" conbined

                                           <directory "/data/dz">

                                                         options none

                                                         allowoverride none

                                                         require all granted

                                           </directory>

                             </virtualhost>

               

               2      下载,解压缩应用程序文件,分别复制到相应目录下

                            cp -a wordpress/*  /data/blog/

                            cp -a  phpMyAdmin-4.3.5-all-languages/* /data/pma/

                            cp -a upload/*  /data/dz/

                                           其中dz为discuz解压之后的一个目录,注意只复制此目录即可

                            chown -R  apache:apache /data/blog /data/dz

         

               3     修改配置文件:

                            cp  /data/blog/wp-config-sample.php /data/blog/wp-config.php

                                          vim  /data/blog/wp-config.php

                                           填写数据库登录信息

 

                            cp  /data/pma/config.sample.inc.php /data/pma/config.inc.php

                                          vim  /data/pma/config.inc.php

                                           填写blowfish 中的随机字符

 

               4     数据库授权账号

 

               5     重启服务 并做 页面测试

 

 

fastcgi方式:

               安装程序包时 php换成  php-fpm

               

               特别注意:

                            mkdir  /var/lib/php/session

                            chown -R  apache:apache /var/lib/php/session

                     否则,phpmyadmin无法使用

               

               配置文件中:如下,

               且之后步骤完全和modules方式一致

               

                     vim  /etc/httpd/conf.d/fcgi.conf

                     directoryindex  index.php

                     <virtualhost  *:80>

                                   servername  www.blog.com

                                   documentroot  "/data/blog/"

                                   errorlog  "/var/log/httpd/error_blog_log"

                                   customlog  "/var/log/httpd/access_blog_log" conbined

                             

                                   proxyrequests  off

                                    ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/data/blog/$1

                             

                                   <directory  "/data/blog">

                                                  options none

                                                  allowoverride none

                                                  require all granted

                                    </directory>

                      </virtualhost>

 

                     <virtualhost  *:80>

                                   servername  www.pma.com

                                   documentroot  "/data/pma"

                                   errorlog  "/var/log/httpd/error_pma_log"

                                   customlog  "/var/log/httpd/access_pma_log" combined

 

                                   proxyrequests  off

                                    ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/data/pma/$1

 

                                   <directory  "/data/pma">

                                                  options none

                                                  allowoverride none

                                                  require all granted

                                    </directory>

                      </virtualhost>

 

                     <virtualhost  *:80>

                                   servername  www.dz.com

                                   documentroot  "/data/dz"

                                   errorlog  "/var/log/httpd/error_dz_log"

                                   customlog  "/var/log/httpd/access_dz_log" conbined

                              

                                   proxyrequests  off

                                    ProxyPassMatch ^/(.*\.php)$  fcgi://127.0.0.1:9000/data/dz/$1

                             

                                   <directory  "/data/dz">

                                                  options none

                                                  allowoverride none

                                                  require all granted

                                    </directory>

                      </virtualhost>

 

------------------------------------------------------------------------------------

编译安装LAMP:

 

在centos7编译安装LAMP:

安装程序顺序

               mariadb:通用二进制格式:mariadb-10.2.15

                     下载地址:

                     需提前准备好二进制安装包

               

               httpd:    编译安装,httpd-2.4.33

                     下载地址:

                     官网:http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.33.tar.bz2

                     搜狐:http://mirrors.sohu.com/apache/httpd-2.4.33.tar.bz2

                            需要更新apr,apr-util

                     下载地址:   

                            APR:  http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.gz

                            APR-UTIL:  http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

               

               php7:编译安装,php-7.2.7

                     下载地址:http://mirrors.sohu.com/php/php-7.2.7.tar.bz2

               

               phpmyadmin:安装phpmyadmin4.8.2

                     下载地址:https://files.phpmyadmin.net/phpMyAdmin/4.8.2/phpMyAdmin-4.8.2-all-languages.zip

               xcache:php7 以内置加速功能,不需要安装xcache

 

对比测试:使用yum一件安装脚本安装lamp+wordpress

                     使用ab -c 10 -n 100 http://192.168.65.132/

                     测试结果Requests per second:    5.08  [#/sec] (mean)

                     测试多次大概5个左右 phpmuadmin 大约26个左右

                 yum安装上php-xcache之后测试结果为:还是5个 26个

                

编译安装流程:          

               1     准备开发环境

                            yum groupinstall  "development tools" -y

                            yum install  pcre-devel openssl-devel expat-devel -y

                             

               2     二进制安装mariadb-10.2.15

                            ##如果没有mysql账号,需先创建

                                           groupadd -r -g 306 mysql

                                           useradd -r -u 306 -g 306 -d /data/mysqldb

                            tar -xf  mariadb-10.2.15-linux-x86_64.tar.gz -C /usr/local/

                            cd  /usr/local/

                            ln -sv  mariadb-10.2.15-linux-x86_64 mysql

                            cd  mysql

                            chown -R mysql:mysql  ./*

                            mkdir /data/mysqldb  -p

                            chown -R mysql:mysql  /data/mysqldb

                            mkdir  /etc/mysql

                            cp  support-files/my-huge.cnf /etc/mysql/my.cnf

                             修改配置文件,如果要写成脚本,此项需注意,使用sed    

                                   vim  /etc/mysql/my.cnf

                                           [mysqld]

                                           datadir         =  /data/mysqldb

                                           innodb_file_per_table = on

                                           skip_name_resolve = on

                             ./scripts/mysql_install_db --datadir=/data/mysqldb  --user=mysql

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

                            chkconfig --add  mysqld

                            chkconfig mysqld  on

                            service mysqld  start

 

               3     源码编译httpd-2.4

                             cd

                            wget  http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.33.tar.bz2  

                            wget  http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.gz

                            wget  http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

                            tar -xf  httpd-2.4.33.tar.bz2

                            tar -xf  apr-1.6.3.tar.gz

                            tar -xf  apr-util-1.6.1.tar.gz

                            cp -a apr-1.6.3  httpd-2.4.33/srclib/apr

                            cp -a apr-util-1.6.1  httpd-2.4.33/srclib/apr-util

                             

                            cd  httpd-2.4.33/

./configure --prefix=/app/httpd24  \

--sysconfdir=/etc/httpd24  \

--enable-so --enable-ssl --enable-cgi  \

--enable-rewrite --with-zlib --with-pcre  \

--with-includedapr --enable-modules=most  \

--enable-mpms-shared=all  --with-mpm=prefork

                             

                            make -j 4 &&  make install

                            cd  /app/httpd24/

                            echo  'PATH=/app/httpd24/bin/:$PATH' >  /etc/profile.d/httpd24.sh

                            .  /etc/profile.d/httpd24.sh

                             

                     修改配置文件中的user和group

                            sed -i  '/^User/s/.*/User apache/' /etc/httpd24/httpd.conf

                            sed -i  '/^Group/s/.*/Group apache/' /etc/httpd24/httpd.conf

                     重启服务      

                            apachectl  start

 

               4     源码编译安装PHP:

                             cd

                            yum install  libxml2-devel bzip2-devel libmcrypt-devel -y

                            wget  http://mirrors.sohu.com/php/php-7.2.7.tar.bz2

                    

                     模块方式:

                            tar -xf  php-7.2.7.tar.bz2

                            cd  php-7.2.7/

 

参考编译选项:php5.6

./configure --prefix=/app/php  \

--with-mysql=/usr/local/mysql  \

--with-openssl \

--with-mysqli=/usr/local/mysql/bin/mysql_config  \

--enable-mbstring --with-png-dir  \

--with-jpeg-dir --with-freetype-dir  \

--with-zlib --with-libxml-dir=/usr  \

--enable-xml --enable-sockets  \

--with-apxs2=/appl/httpd24/bin/apxs \ 

--with-mcrypt --with-config-file-path=/etc  \

--with-config-file-scan-dir=/etc/php.d  \

--with-bz2

                    

                    

 

编译选项:php7.0+

./configure --prefix=/app/php  \

--enable-mysqlnd \

--with-mysqli=mysqlnd  \

--with-openssl \

--with-pdo-mysql=mysqlnd  \

--enable-mbstring \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-libxml-dir=/usr  \

--enable-xml  \

--enable-sockets \

--with-apxs2=/app/httpd24/bin/apxs  \

--with-config-file-path=/etc  \

--with-config-file-scan-dir=/etc/php.d  \

--disable-fileinfo \                    

--enable-maintainer-zts           

               

                            注意:    php-7.0以上版本使用--enable-mysqlnd --with-mysqli=mysqlnd ,

                                           原--with-mysql不再支持

                                    --enable-maintainer-zts   

                                                  如果http编译成event或者worker,必须加上此项

                            make -j 4 &&  make install

                    

                     准备配置文件:

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

                     编译apache配置文件httpd.conf,使apache支持php

                            vim  /etc/httpd24/httpd.conf

                            添加2行:

                                   AddType  application/x-httpd-php .php

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

                            定位至: DirectoryIndex index.html

                            修改为: DirectoryIndex index.php index.html

 

                     重复服务:

                            apachectl  restart

                             

 

               5     源码编译安装PHP: fpm方式

                            tar -xf  php-7.2.7.tar.bz2

                            cd  php-7.2.7/

./configure --prefix=/app/php  \

--enable-mysqlnd  \

--with-mysqli=mysqlnd  \

--with-openssl \

--with-pdo-mysql=mysqlnd  \

--enable-mbstring  \

--with-freetype-dir  \

--with-jpeg-dir  \

--with-png-dir  \

--with-zlib   \

--with-libxml-dir=/usr  \

--enable-xml \

--enable-sockets  \

--enable-fpm  \

--with-config-file-path=/etc  \

--with-config-file-scan-dir=/etc/php.d  \

--enable-maintainer-zts  \

--disable-fileinfo                              

 

                            make -j 4 &&  make install

                    

                     安装后配置:

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

                            cp  sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

                            chmod +x  /etc/init.d/php-fpm

                            chkconfig --add  php-fpm

                            chkconfig php-fpm  on

                             

                            cd  /app/php/etc

                            cp  php-fpm.conf.default php-fpm.conf

                            cp  php-fpm.d/www.conf.default php-fpm.d/www.conf

                             

                            vim  php-fpm.d/www.conf

                                          user =  apache

                                          group  = apache  

                            修改php-fpm程序的用户和组,可选项,不修改也可以

                                           

                             

                            rename ini so  /etc/php.d/*

                                           修改/etc/php.d/*.ini ===>  /etc/php.d/*.so

                                           php7.2.7需要,否则启动会有一堆报错

                                           

                            service php-fpm  start

                             

                     配置httpd.conf

                            vim  /etc/http24/httpd.conf

                             取消下面两行的注释:

                                   LoadModule  proxy_module modules/mod_proxy.so

                                     LoadModule proxy_fcgi_module  modules/mod_proxy_fcgi.so

                            定位至:DirectoryIndex index.html

                                    修改为:DirectoryIndex index.php  index.html

                            添加下面四行:

                                   AddType  application/x-httpd-php .php

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

                                   ProxyRequests  Off

                                    ProxyPassMatch  ^/(.*\.php)$  fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1                                

                     重启服务:

                            apachectl  restart

                             

 

编译过程报错记录:

               1     make[1]: Warning: File  `/root/php-7.2.7/ext/date/php_date.c' has

                            modification time  1.9e+06 s in the future

                     问题原因:

                                    系统时间设置不正确,导致安装时间是系统时间未来的时间

                     解决方案:

                                    修改正确系统时间

                                    

               2     PHP Startup: Unable to load  dynamic library 'curl.so' ....

                     解决方案:

                                   rename ini so  /etc/php.d/*

 

 

LAMP 一键安装脚本:

               1     yum安装module方式

               2     yum安装fpm方式

               3     编译安装方式