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

【分离式LAMP架构】php

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

 

【企业案例】linux

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

【实验环境】sql

操做系统及内核版本数据库

[root@pxe  ~]# cat /etc/redhat-release centos

CentOS  Linux release 7.3.1611 (Core) api

[root@pxe  ~]# uname -r安全

3.10.0-514.el7.x86_64bash

网络地址:

服务器

网络地址

Web

192.168.10.4/24

PHP

192.168.10.5/24

MySQL

192.168.10.6/24

软件版本:

[root@pxe  sources]# 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

[root@web  src]# yum  -y install pcre pcre-devel openssl  openssl-devel

[root@web  src]# tar xf apr-1.5.2.tar.bz2

[root@web  src]# tar xf apr-util-1.5.4.tar.bz2

[root@web  src]# tar xf httpd-2.4.27.tar.bz2

[root@web  src]# mv apr-1.5.2 httpd-2.4.27/srclib/apr

[root@web  src]# mv apr-util-1.5.4 httpd-2.4.27/srclib/apr-util

[root@web  src]# cd httpd-2.4.27/

[root@web  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

[root@web  httpd-2.4.27]# make -j 2 && make install

2 配置环境变量

[root@web  httpd-2.2.32]# cat > /etc/profile.d/http.sh <<EOF

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

EOF

[root@web  httpd-2.2.32]# . /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

       echo  1

     fi

}

 

start(){

     if [ -f $httpd_pid ];then

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

       exit  2

     else

       $httpd_bin  -k start

     fi

}

 

stop (){

     if [ $(httpd_is_running) = 1 ];then

       echo  'httpd is not running'

       exit  2

     else

       $httpd_bin  -k stop

     fi

}

 

restart (){

    stop

    start

}

 

status(){

     if [ -f $httpd_pid ];then

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

       exit  0

     fi

       echo  'httpd is not running'

       exit  2

}

 

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取消两行的注释

LoadModule  proxy_module modules/mod_proxy.so

LoadModule  proxy_fcgi_module modules/mod_proxy_fcgi.so

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

[root@web  ~]# cat >> /usr/local/httpd/conf/httpd.conf <<EOF

AddType  application/x-httpd-php .php

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

ProxyRequests  Off

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 启动服务

[root@web  ~]# service httpd start

 

【部署PHP服务器】

PHP服务采用FPM方式

[root@php  src]# yum -y install libxml2-devel bzip2-devel libmcrypt-devel  #该软件包在epel源中。

[root@php  src]# tar xf php-5.6.4.tar.xz

[root@php  src]# cd php-5.6.4/

[root@php  src]# ./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

[root@php  php-5.6.4]# make -j 2 && make install

[root@php  php-5.6.4]# cat > /etc/profile.d/php.sh <<EOF

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

 EOF

[root@php  php-5.6.4]# . /etc/profile.d/php.sh

[root@php  php-5.6.4]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@php  php-5.6.4]# chmod +x /etc/init.d/php-fpm

[root@php  php-5.6.4]# chkconfig --add php-fpm

[root@php  php-5.6.4]# cp /usr/local/php/etc/php-fpm.conf{.default,}

 

修改配置文件监听端口

[root@php etc]# 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

启动服务

[root@php  website]# chkconfig php-fpm on

[root@php  php-5.6.4]# service php-fpm start

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

[root@php  website]# mkdir /website

[root@php  website]# cat > /website/index.php <<EOF

<?php

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

if(mysqli_connect_errno()){

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

\$mysqli=null;

exit;

}

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

\$mysqli->close();

phpinfo();

?>

EOF

 

【部署MySQL服务器】

[root@storage  src]# mkdir /data

[root@storage  src]# useradd -u 27 -r -m -d /data/datadb -s /sbin/nologin mysql

[root@storage  src]# tar  xf mariadb-5.5.57-linux-x86_64.tar.gz  -C /usr/local/ [root@storage src]# cd /usr/local/

[root@storage  local]# ln -s /usr/local/mariadb-5.5.57-linux-x86_64/ mysql

[root@storage  local]# cd mysql/

[root@storage  mysql]# ./scripts/mysql_install_db --datadir=/data/datadb --user=mysql

[root@storage  mysql]# mkdir /etc/mysql

[root@storage  mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf

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

[root@storage  mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@storage  mysql]# cat > /etc/profile.d/mysql.sh <<EOF

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

 EOF

[root@storage  mysql]# . /etc/profile.d/mysql.sh

[root@storage  mysql]# mkdir /var/log/mariadb

[root@storage  mysql]# chown mysql.mysql /var/log/mariadb

[root@storage  mysql]# service mysqld start

[root@storage  mysql]# mysql_secure_installation  #作安全初始化

添加一个测试用户:

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

 

【测试】

相关文章
相关标签/搜索