|
【分离式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';
【测试】
略
|
|
|
|
|
|
|