erfsfd 发表于 2017-10-24 10:05:12

分离式LAMP架构快速构建文档

【分离式LAMP架构】

分离式的LAMP架构,Apache,Mysql,PHP分别部署在独立的服务器上,静态资源放在Web服务器上,动态资源放在PHP服务器上。当客户端请求访问该站点时,web服务器根据其访问的资源类型来分别响应,如果是静态资源,则直接返回结果;如果是动态资源,则把该请求通过FastCGI交由PHP服务器去处理。PHP对动态页面的处理,在PHP对动态页面进行处理解析时,有时会访问mysql数据库,最后结果返回给web服务器,由web生成响应报文发送给客户端。



【企业案例】

某公司新增某项业务,现需架设一个动态站点,采用LAMP分离式架构。

【实验环境】

操作系统及内核版本

# cat /etc/redhat-release

CentOSLinux release 7.3.1611 (Core)

# uname -r

3.10.0-514.el7.x86_64

网络地址:

服务器
   

网络地址

Web
   

192.168.10.4/24

PHP
   

192.168.10.5/24

MySQL
   

192.168.10.6/24

软件版本:

# ls

apr-1.5.2.tar.bz2

apr-util-1.5.4.tar.bz2

httpd-2.4.27.tar.gz

mariadb-5.5.57-linux-x86_64.tar.gz

php-5.6.4.tar.xz

软件安装方式:

编译安装



【部署WEB服务器】

1 编译安装Apache

# yum-y install pcre pcre-devel opensslopenssl-devel

# tar xf apr-1.5.2.tar.bz2

# tar xf apr-util-1.5.4.tar.bz2

# tar xf httpd-2.4.27.tar.bz2

# mv apr-1.5.2 httpd-2.4.27/srclib/apr

# mv apr-util-1.5.4 httpd-2.4.27/srclib/apr-util

# cd httpd-2.4.27/

# ./configure --prefix=/usr/local/httpd \

--with-zlib \

--with-pcre \

--with-include-apr \

--with-mpm=prefork \

--enable-so \

--enable-ssl \

--enable-rewrite \

--enable-modules=most \

--enable-mpms-shared=all

# make -j 2 && make install

2 配置环境变量

# cat > /etc/profile.d/http.sh <<EOF

PATH=/usr/local/httpd/bin/:$PATH

EOF

# . /etc/profile.d/http.sh



3 配置服务器脚本

#!/bin/bash

#httpd Server

#chkconfig: 35 13 72

#description: HTTP Server



httpd_bin='/usr/local/httpd/bin/httpd'

#httpd_prefix='/usr/local/httpd/'

httpd_pid='/usr/local/httpd/logs/httpd.pid'



. /etc/rc.d/init.d/functions



httpd_is_running(){

   local httpd_pid_number=$(cat $httpd_pid)

   if [ -d /proc/$httpd_pid_number ];then

         echo 0

   else

       echo1

   fi

}



start(){

   if [ -f $httpd_pid ];then

       [$(httpd_is_running) = 0 ] && echo 'httpd is running'

       exit2

   else

       $httpd_bin-k start

   fi

}



stop (){

   if [ $(httpd_is_running) = 1 ];then

       echo'httpd is not running'

       exit2

   else

       $httpd_bin-k stop

   fi

}



restart (){

    stop

    start

}



status(){

   if [ -f $httpd_pid ];then

         [ $(httpd_is_running) = 0 ] && echo 'httpd is running'

       exit0

   fi

       echo'httpd is not running'

       exit2

}



case "$1" in

   start)

       start

       ;;

   stop)

       stop

       ;;

   restart)

       restart

       ;;

   status)

       status

       ;;

   *)

       echo"usage:$0 {start|stop|status|restart}"

       ;;

esac



4 修改配置文件

/usr/local/httpd/conf/http.conf取消两行的注释

LoadModuleproxy_module modules/mod_proxy.so

LoadModuleproxy_fcgi_module modules/mod_proxy_fcgi.so

在文档尾部添加下面4行内容:

# cat >> /usr/local/httpd/conf/httpd.conf <<EOF

AddTypeapplication/x-httpd-php .php

AddTypeapplication/x-httpd-php-source .phps

ProxyRequestsOff

ProxyPassMatch^/(.*\.php)$fcgi://192.168.10.5:9000/website/\$1

EOF



#该地址为PHP-fpm所监听的ip地址,若是PHP和web为分离则使用127.0.0.1

#以php-fpm.conf中listen选项的配置为准

5 启动服务

# service httpd start



【部署PHP服务器】

PHP服务采用FPM方式

# yum -y install libxml2-devel bzip2-devel libmcrypt-devel#该软件包在epel源中。

# tar xf php-5.6.4.tar.xz

# cd php-5.6.4/

# ./configure \

--prefix=/usr/local/php\

--with-mysql=myslnd\

--with-openssl\

--with-mysqli=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-mcrypt\

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

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

--with-bz2

# make -j 2 && make install

# cat > /etc/profile.d/php.sh <<EOF

PATH=/usr/local/php/bin:$PATH

EOF

# . /etc/profile.d/php.sh

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

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

# chkconfig --add php-fpm

# cp /usr/local/php/etc/php-fpm.conf{.default,}



修改配置文件监听端口

# sed -i's/127.0.0.1:9000/9000/g'\

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


#该项必须修改,若修改为ip:port方式,需在httpd.conf中只用配置的ip,若只使用port,http.conf文件中可使用本地的任一ip

启动服务

# chkconfig php-fpm on

# service php-fpm start

准备一个测试的动态网页文件:

# mkdir /website

# cat > /website/index.php <<EOF

<?php

\$mysqli=newmysqli("192.168.10.6","test","centos");

if(mysqli_connect_errno()){

echo"连接数据库失败!";

\$mysqli=null;

exit;

}

echo"连接数据库成功!";

\$mysqli->close();

phpinfo();

?>

EOF



【部署MySQL服务器】

# mkdir /data

# useradd -u 27 -r -m -d /data/datadb -s /sbin/nologin mysql

# tarxf mariadb-5.5.57-linux-x86_64.tar.gz-C /usr/local/ # cd /usr/local/

# ln -s /usr/local/mariadb-5.5.57-linux-x86_64/ mysql

# cd mysql/

# ./scripts/mysql_install_db --datadir=/data/datadb --user=mysql

# mkdir /etc/mysql

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

# sed –i '/mysqld]/a datadir = \/data\/datadb\ninnodb_file_per_table =on\nskip_name_resolve = on' /etc/mysql/my.cnf

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

# cat > /etc/profile.d/mysql.sh <<EOF

PATH=/usr/local/mysql/bin:$PATH

EOF

# . /etc/profile.d/mysql.sh

# mkdir /var/log/mariadb

# chown mysql.mysql /var/log/mariadb

# service mysqld start

# mysql_secure_installation#做安全初始化

添加一个测试用户:

MariaDB[(none)]> grant ALL on test.* to test@'192.168.10.5' identified by 'centos';



【测试】


看雪 发表于 2017-10-25 08:28:05

路过帮顶!!!
页: [1]
查看完整版本: 分离式LAMP架构快速构建文档