1. 程式人生 > >HAProxy+LAMP動靜分離

HAProxy+LAMP動靜分離

haproxy

實驗目的:為了減輕後端服務器的承載量,使用HAProxy做動靜分離,將動態資源放在動態web組中,靜態資源放在static組中,圖片放在image server中,數據庫單獨用一臺服務器。

網絡拓撲圖

技術分享圖片

配置HAProxy服務器

~]# yum -y install haproxy
~]# vim /etc/haproxy/haproxy.cfg
frontend  main *:80
    acl url_web          path_end       -i .html .css .js .htm
    use_backend websers         if url_web

    acl url_static       path_end       -i .jpg .gif .png
    use_backend static          if url_static
    default_backend             app

    rspadd X-Via:\ HAProxy
    rspidel ^Server.*

backend websers
    balance     roundrobin
    rspadd X-Service:\ WEBServer
    server ws1 172.16.67.1:80 check rise 3 fall 5 weight 2 maxconn 10 cookie ws1
    server ws2 172.16.67.2:80 check rise 3 fall 5 weight 3 maxconn 10 cookie ws1
    
backend static
    balance     roundrobin
    rspadd X-Service:\ ImageServer
    server      static 172.16.69.1:80 check
    
backend app
    balance roundrobin
    rspidel ^Server.*
    rspadd X-Service:\ AppServer
    server  app1 172.16.67.1:80 check rise 3 fall 5 weight 2 maxconn 10 cookie as1
    server  app2 172.16.67.2:80 check rise 3 fall 5 weight 3 maxconn 10 cookie as1
~]# service haproxy start

配置後端靜態web組和images server

~]# yum install -y httpd
~]# service httpd start
~]#  tar xf wordpress-4.8.1-zh_CN.tar.gz -C /var/www/html/

配置後端動態web組

~]# yum install -y httpd php php-mysql
~]# service httpd start
~]# tar xf wordpress-4.8.1-zh_CN.tar.gz -C /var/www/html/

~]# cd /var/www/html/wordpress
wordpress]# cp wp-config-sample.php wp-config.php
wordpress]# vim wp-config.php
/** WordPress數據庫的名稱 */
define('DB_NAME', 'wpdb');

/** MySQL數據庫用戶名 */
define('DB_USER', 'wpadmin');

/** MySQL數據庫密碼 */
define('DB_PASSWORD', '123456');

/** MySQL主機 */
define('DB_HOST', '172.16.69.2');

/** 創建數據表時默認的文字編碼 */
define('DB_CHARSET', 'utf8');

/** 數據庫整理類型。如不確定請勿更改 */
define('DB_COLLATE', '');

配置MySQL數據庫服務器

~]# yum install -y mysql mysql-server
~]# service mysqld start
~]# mysql
mysql> create database wpdb;
mysql> grant all on wpdb.* to 'wpadmin'@'172.16.%.%' identified by '123456';
mysql> flush privileges;

訪問測試

http://172.16.72.1/wordpress/

技術分享圖片到此,試驗結束。


HAProxy+LAMP動靜分離