温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LAPM(linux+httpd+php+mariadb)环境搭建

发布时间:2020-07-11 23:38:03 来源:网络 阅读:2285 作者:xiangjin1 栏目:web开发

前言

    LAMP(linux+apache+mariadb+php)以下都以源码安装方式,环境为3台虚拟机内存各4G、2核cpu,apache通过fastcgi连接php,数据库用mariadb。

      搭建环境:Centos6.4

    服务器

           IP地址:192.168.146.140

           安装软件:apache2.4.12

           硬盘:20G

      服务器二

           IP地址:192.168.146.141

           安装软件:PHP5.6

           硬盘:20G

      服务器三

           IP地址:192.168.146.139

           安装软件:mariadb-10.0.19

           硬盘一:20G

           硬盘二:20G以lvm方式工作,方便今后扩展和数据快照

      如下图

              LAPM(linux+httpd+php+mariadb)环境搭建


apache2.4.12安装

    httpd程序依赖于apr和apr-util, 

                 apr: apache portable runtime(httpd2.4依赖于1.4以上的apr和apr-util,安装前请查看机器上的apr)      检查命令:

                  rpm -qa | grep apr (如不满足要求,请自行安装高版本apr)

                  apr下载地址:http://apr.apache.org/download.cgi  

    安装新apr包,为了避免和老版本冲突,另指明安装位置,也可以把老版本删除后重新安装,请自行决定安装方式

                   tar xf apr-1.5.2.tar.gz:解压apr

                   cd apr-1.5.2 :进入解压目录

                    ./configure --prefix=/usr/local/apr15:指明apr安装路径

                    make && make install:编译安装


                    tar xf apr-util-1.5.4.tar.gz:解压apr-util

                    cd apr-util-1.5.4 

                    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr15/:指明安装目录和apr工作对象

                    make && make install:编译安装 


                     tar xf httpd-2.4.12.tar.bz2 

               cd httpd-2.4.12

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

                         --sysconfdir:指定配置文件路径
                         --enable-so:支持DSO动态装卸模块
                         --enable-ssl:编译启用ssl模块,但是这项依赖于openssl和openssl-devel
                         --enable-cgi:启用cgi
                         --enable-rewrite:url重写,但是它依赖于pcre
(perl正则表达式分析器)
                         --with-zlib:数据报文发送时的压缩库
                         --with-pcre:如果指明要使用prcr就需要把相关的pcre-devel先安装好
                         --with-apr=/usr/local/apr:指定apr路径。
                         --with-apr-util=/usr/local/apr-util/
                         --enable-mpms-shared=all --with-mpm=event:动态加载mpm并指明用哪个模式运行
                         --enable-modules={all,most}:all编译所有模块或者most常用模块,编译好可以不用。

                     make && make install


      安装后启动httpd,启动脚本在安装目录下/usr/local/apache/bin/apachectl

                     ./apachectl start:启动httpd


      主配置文件在/etc/httpd24/httpd.conf,主配置文件中还包含扩展配置文件,include定义了文件路径

                              编辑/etc/httpd24/httpd.conf,添加如下行即可,规范指定pid路径,:

                                              PidFile  "/var/run/httpd.pid"

                              启用mod_proxy_fcgi.so必须使用代理模块proxy_module,把以下2个注释去掉即可:

                                              LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

                                              LoadModule proxy_module modules/mod_proxy.so

                               启用反向代理:

                                               ProxyRequests Off:关闭正向代理

                                               ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.146.141:9000/$1:所有以.php结尾的网页都以fcgi方式转到后面网页

                               让apache能识别php格式的页面,并支持php格式的主页

                                               AddType application/x-httpd-php  .php

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

                                定位至DirectoryIndex index.html 

                                               DirectoryIndex  index.php  index.html


                     如果想换一种启动方式:注释加载模块,添加相应模块即可

                              注释LoadModule mpm_event_module modules/mod_mpm_event.so

                              添加LoadModule mpm_worker_module modules/mod_mpm_worker.so


                     主程序修改mpm参数

                              Include /etc/httpd24/extra/httpd-mpm.conf


                     http参数详解http://5990383.blog.51cto.com/blog/5980383/1651587

        如需使用虚拟主机,先注释中心主机

                     注释 DocumentRoot 

                     启用  Include /etc/httpd24/extra/httpd-vhosts.conf,在配置文件中按需添加虚拟主机。


        配置启动脚本

                     cd /etc/init.d/

                     cp httpd httpd24:复制原有脚本

                     vim httpd24:修改脚本

                              apachectl=/usr/local/apache/bin/apachectl

                              httpd=${HTTPD-/usr/local/apache/bin/httpd}

                              pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}

                      chkconfig --add httpd24 设置启动项


安装mariadb10.0.19,已把第二块磁盘做成lvm逻辑卷挂载到/mydata/data目录下

        安装前先检查是否已经安装mysql程序

        以二进制方式安装

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

                      vim /usr/local/mariadb-10.0.19-linux-x86_64/INSTALL-BINARY:打开指定文档,按要求安装数据库                                 shell> groupadd mysql

                              shell> useradd -g mysql mysql

                              shell> cd /usr/local

                              shell> gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -

                              shell> ln -s full-path-to-mysql-VERSION-OS mysql

                              shell> cd mysql

                              shell> chown -R mysql .

                              shell> chgrp -R mysql .

                              shell> scripts/mysql_install_db --user=mysql

                              shell> chown -R root .

                              shell> chown -R mysql data

                              shell> bin/mysqld_safe --user=mysql &

                     ln -sv mariadb-10.0.19-linux-x86_64 mysql

                     chown -R root:mysql .

                     mkdir /etc/mysql:为了避免和/etc/my.cnf冲突所以建立个目录单独存放my.cnf,建议备份后直接覆盖

                     cp ./support-files/my-innodb-heavy-4G.cnf /etc/mysql/my.cnf:复制文件

                                     datadir=/mydata/data:添加指定目录数据存储到/data/mydata目录

                     scripts/mysql_install_db --user=mysql --datadir=/mydata/data:指定用户和路径初始化数据库

                     cp support-files/mysql.server /etc/init.d/mysqld:复制启动脚本

                     chkconfig --add mysqld

                     chkconfig --level 2345 mysqld on

                     chkconfig --list mysqld

                      /usr/local/mysql/bin/mysqladmin -u root password 'XXXXXX':

                     service mysqld start


                      create database phpdb;

                      create user phpuser@'192.168.%.%' identified by 'XXXXXX'

                      grant all on phpdb.* to phpuser@'192.168.%.%';

            

编译安装php-5.6.9.tar.bz2

    请配置好yum源(系统安装源及epel源)后执行如下命令:

    # yum -y groupinstall "Desktop Platform Development"

    # yum -y install bzip2-devel libmcrypt-devel      


      tar xf php-5.6.9.tar.bz2 

      cd php-5.6.9        

      ./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 --enable-fpm  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2

                         --prefix=/usr/local/php:安装路径
                         --with-mysql=/usr/local/mysql:编译使用mysql驱动指明路径,如果是rpm包安装的mysql只需写--with-mysql
                         --with-openssl: --with-mysql的加密工具
                         --with-mysqli=/usr/local/mysql/bin/mysql_config:php访问mysql的另外一种机制
                         --enable-mbstring:多字节字串,如果想支持中文这个选项是必须
                         --with-freetype-dir:字体处理工具,php显示网页可以对字体加工。
                         --with-jpeg-dir :jpeg图片生成工具
                         --with-png-dir:png图片生成工具
                         --with-zlib:压缩库
                         --with-libxml-dir=/usr:指明需要使用的xml库所在位置,
                         --enable-xml:启用xml功能
                         --enable-sockets:支持sockets方式进行通信
                         --enable-fpm:以fpm方式运行
                         --with-mcrypt:支持使用加密库
                         --with-config-file-path=/etc:在etc下创建主配置文件php.ini
                         --with-config-file-scan-dir=/etc/php.d:支持主配置文件以外在php.d下的以.ini结尾子配置文件,
                         --with-bz2:支持bz2压缩

      configure: error: Cannot find OpenSSL's <evp.h>:检查环境报错

       解决方式:yum -y install openssl openssl-devel

       configure: error: Please reinstall the BZip2 distribution

       解决方式:yum install bzip2 bzip2-devel

       configure: error: mcrypt.h not found. Please reinstall libmcrypt

       解决方式:去掉--with-mcrypt

       onfigure: error: Cannot find MySQL header files under /usr/local/mysql.

       解决方式修改mysql指定路径

       ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --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 --with-bz2

        

       为php提供配置文件:

                 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

        为启动脚本添加配置文件:

                  cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

        编辑启动脚本配置文件:  配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):

                  vim /usr/local/php/etc/php-fpm.conf

                             pm.max_children = 50:最大并发响应

                             pm.start_servers = 5:启动空闲进程等待响应用户请求

                             pm.min_spare_servers = 2:最小空闲进程

                             pm.max_spare_servers = 8:最大空闲进程

                             pid = /usr/local/php/var/run/php-fpm.pid

                             error_log:启动错误日志

                             listen = 192.168.146.141:9000:修改监听端口默认只监听127.0.0.1


        启动php:

                   service php-fpm start

     

        使用如下命令来验正:

                    ps aux | grep php-fpm


以上为编译安装LAMP环境

          并发数压力测试: ab -n 5000 -c 100 http://192.168.146.140/index.php

               Requests per second:    263.38 [#/sec] (mean)

                              Time per request:       379.675 [ms] (mean)

                              Time per request:       3.797 [ms] (mean, across all concurrent requests)

     

          php加速器xcache:快速而且稳定的PHP opcode缓存,xcache是作为php模块使用的。

                              tar xf xcache-3.2.0.tar.gz

                              cd xcache-3.2.0

                              /usr/local/php/bin/phpize:生成config脚本。

          --with-php-config编译时指明为php模块进行编译,--enable-xcache启用xcache模块

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

                              make && make install

                              mkdir /etc/php.d

                              cp xcache.ini /etc/php.d/

                              vim /etc/php.d/xcache.ini

                                           extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so

                              service php-fpm restar

                               在phpinfo()测试页中可以看到xcache说明已经安装成功。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI