设为首页 收藏本站
查看: 1794|回复: 1

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

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-10-24 10:05:12 | 显示全部楼层 |阅读模式
【分离式LAMP架构】

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



【企业案例】

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

【实验环境】

操作系统及内核版本

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

CentOS  Linux release 7.3.1611 (Core)

[iyunv@pxe  ~]# 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

软件版本:

[iyunv@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

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

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

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

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

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

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

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

[iyunv@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

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

2 配置环境变量

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

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

EOF

[iyunv@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行内容:

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

[iyunv@web  ~]# service httpd start



【部署PHP服务器】

PHP服务采用FPM方式

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

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

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

[iyunv@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

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

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

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

EOF

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

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

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

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

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



修改配置文件监听端口

[iyunv@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

启动服务

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

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

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

[iyunv@php  website]# mkdir /website

[iyunv@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服务器】

[iyunv@storage  src]# mkdir /data

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

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

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

[iyunv@storage  local]# cd mysql/

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

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

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

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

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

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

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

EOF

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

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

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

[iyunv@storage  mysql]# service mysqld start

[iyunv@storage  mysql]# mysql_secure_installation  #做安全初始化

添加一个测试用户:

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



【测试】



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-405827-1-1.html 上篇帖子: LAMP环境搭建WordPress博客 下篇帖子: CentOS7编译LAMP应用wordpress
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表