分離式LAMP架構快速構建文檔

【分離式LAMP架構】

分離式的LAMP架構,ApacheMysqlPHP分別部署在獨立的服務器上,靜態資源放在Web服務器上,動態資源放在PHP服務器上。當客戶端請求訪問該站點時,web服務器根據其訪問的資源類型來分別響應,如果是靜態資源,則直接返回結果;如果是動態資源,則把該請求通過FastCGI交由PHP服務器去處理。PHP對動態頁面的處理,在PHP對動態頁面進行處理解析時,有時會訪問mysql數據庫,最後結果返回給web服務器,由web生成響應報文發送給客戶端。

 

【企業案例】

某公司新增某項業務,現需架設一個動態站點,採用LAMP分離式架構。

【實驗環境】

操作系統及內核版本

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

CentOS  Linux release 7.3.1611 (Core)

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

軟件版本:

[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';

 

【測試】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章