0

0

How to Cluster Magento, nginx and MySQL on Multiple Servers

php中文网

php中文网

发布时间:2016-06-07 15:35:01

|

1640人浏览过

|

来源于php中文网

原创

June 17, 2013 By Severalnines Magento is an open-source e-commerce platform built on Zend PHP and MySQL. It is widely adopted by online retailers with some 150,000 sites known to use it. Single server setups are easy to set up, but if your

june 17, 2013

By Severalnines

Magento is an open-source e-commerce platform built on Zend PHP and MySQL. It is widely adopted by online retailers with some 150,000 sites known to use it. Single server setups are easy to set up, but if your store is a huge success, then you probably need to think about clustering your environment with multiple servers. Clustering is done at the web, database and file-system level, as all web nodes need access to catalog images. 

How to Cluster Magento, nginx and MySQL on Multiple Servers

 

This post is similar to our previous posts on scaling Drupal and WordPress performance, and focuses on how to scale Magento on multiple servers. The software used is Magento version 1.7.0.2 , nginx, HAProxy, MySQL Galera Cluster and OCFS2 (Oracle Cluster File System) with a shared storage using Ubuntu 12.04.2 LTS (Precise) 64bit.

Our setup consists of 6 nodes or servers:

  • NODE1: web server + database server
  • NODE2: web server + database server
  • NODE3: web server + database server
  • LB1: load balancer (master) + keepalived
  • LB2: load balancer (backup) + keepalived
  • ST1: shared storage + ClusterControl

 

We will be using OCFS2, a shared disk file system to serve the web files across our web servers. Each of these web servers will have a nginx web server colocated with a MySQL Galera Cluster instance. We will be using 2 other nodes for load balancing.

How to Cluster Magento, nginx and MySQL on Multiple Servers

Our major steps would be:

  1. Prepare 6 instances
  2. Deploy MySQL Galera Cluster onto NODE1, NODE2 and NODE3 from ST1
  3. Configure iSCSI target on ST1
  4. Configure OCFS2 and mount the shared disk onto NODE1, NODE2 and NODE3
  5. Configure nginx on NODE1, NODE2 and NODE3
  6. Configure Keepalived and HAProxy for web and database load balancing with auto failover
  7. Install Magento and connect it to the Web/DB cluster via the load balancer

 

Prepare Hosts

 

Add following hosts definition in /etc/hosts:

192.168.197.150	mymagento.com www.mymagento.com mysql.mymagento.com #virtual IP
192.168.197.151	NODE1 web1 db1
192.168.197.152	NODE2 web2 db2
192.168.197.153	NODE3 web3 db3
192.168.197.161	LB1 
192.168.197.162	LB2
192.168.197.171	ST1 clustercontrol

 

Turn off sudo with password:

$ sudo visudo

 

And append following line:

%sudo ALL=(ALL:ALL) NOPASSWD: ALL

 

Deploy MySQL Galera Cluster

 

** The deployment of the database cluster will be done from ST1

 

1. To set up MySQL Galera Cluster, go to the Galera Configurator to generate a deployment package. In the wizard, we used the following values when configuring our database cluster:

  • Vendor: Codership (based on MySQL 5.5) 
  • Infrastructure: none/on-premises
  • Operating System: Ubuntu 12.04
  • Number of Galera Servers: 3+1
  • OS user: ubuntu
  • ClusterControl Server: 192.168.197.171
  • Database Servers: 192.168.197.151 192.168.197.152 192.168.197.153

At the end of the wizard, a deployment package will be generated and emailed to you.

 

2. Download the deployment package and run deploy.sh:

$ wget http://www.severalnines.com/galera-configurator/tmp/a3l3pnv560sforpeb29lruds94/s9s-galera-codership-2.4.0.tar.gz
$ tar xvfz s9s-galera-codership-2.4.0.tar.gz
$ cd s9s-galera-codership-2.4.0/mysql/scripts/install
$ bash ./deploy.sh 2>&1 | tee cc.log

 

3. The deployment takes about 15 minutes, and once it is completed, note your API key. Use it to register the cluster with the ClusterControl UI by going to http://192.168.197.171/cmonapi . You will now see your MySQL Galera Cluster in the UI. 

How to Cluster Magento, nginx and MySQL on Multiple Servers

 

Configure iSCSI

 

1. The storage server (ST1) needs to export a disk through iSCSI so it can be mounted on all three web servers (NODE1, NODE2 and NODE3). iSCSI basically tells your kernel you have a SCSI disk, and it transports that access over IP. The “server” is called the “target” and the “client” that uses that iSCSI device is the “initiator”.

Install iSCSI target in ST1:

$ sudo apt-get install -y iscsitarget iscsitarget-dkms

 

2. Enable iscsitarget:

$ sudo sed -i "s|false|true|g" /etc/default/iscsitarget

 

3. It is preferred to have separate disk for this file system clustering purpose. So we are going to use another disk mounted in ST1 (/dev/sdb) to be shared among web server nodes. Define this in iSCSI target configuration file:

$ vim /etc/iet/ietd.conf

 

And add following lines:

Target iqn.2013-06.ST1:ocfs2
        Lun 0 Path=/dev/sdb,Type=fileio
        Alias iscsi_ocfs2

 

4. Add NODE1, NODE2 and NODE3 by specifying the network into iSCSI allow list:

$ vim /etc/iet/initiators.allow

 

And append following line:

ALL 192.168.197.0/24

 

5. Start iSCSI target service:

$ sudo service iscsitarget start

 

** The following steps should be performed on NODE1, NODE2 and NODE3

 

6. Install iSCSI initiator on respective hosts:

$ sudo apt-get install -y open-iscsi

 

7. Set the iSCSI initiator to automatically start and restart the iSCSI initiator service to apply changes:

$ sudo sed -i "s|^node.startup.*|node.startup = automatic|g" /etc/iscsi/iscsid.conf
$ sudo service open-iscsi restart

 

8. Discover iSCSI targets that we have setup earlier:

$ sudo iscsiadm -m discovery -t sendtargets -p ST1
192.168.197.171:3260,1 iqn.2013-06.ST1:ocfs2

 

9. If you see some result as above, means we can see and able to connect to the iSCSI target. We just need to do another restart to access the iSCSI target:

$ sudo service open-iscsi restart

 

10. Make sure you can see the new hard disk (/dev/sdb) listed under /dev directory:

$ ls -1 /dev/sd*

 

Configure OCFS2

 

** The following steps should be performed on NODE1 unless specified.

 

1. OCFS2 allows for file system to be mounted more than one place. Install OCFS2 tools in NODE1, NODE2 and NODE3:

$ sudo apt-get install -y ocfs2-tools

 

2. Create disk partition table for hard disk drive /dev/sdb:

$ sudo cfdisk /dev/sdb

 

Create a partition by using following sequences in the wizard: New > Primary > accept Size > Write > yes

 

3. Creates an OCFS2 file system on /dev/sdb1:

$ sudo mkfs.ocfs2 -b 4K -C 128K -L "Magento_Cluster" /dev/sdb1

 

4. Create cluster configuration file and define the node and cluster directives:

# /etc/ocfs2/cluster.conf
cluster:
        node_count = 3
        name = ocfs2
node:
        ip_port = 7777
        ip_address = 192.168.197.151
        number = 1
        name = NODE1
        cluster = ocfs2
node:
        ip_port = 7777
        ip_address = 192.168.197.152
        number = 2
        name = NODE2
        cluster = ocfs2
node:
        ip_port = 7777
        ip_address = 192.168.197.153
        number = 3
        name = NODE3
        cluster = ocfs2

*Notes: The attributes under the node or cluster clause need to be after a tab.

 

** The following steps should be performed on NODE1, NODE2 and NODE3 unless specified.

 

5. Create the same configuration file (/etc/ocfs2/cluster.conf) in NODE2 and NODE3. This file should be the same on all nodes in the cluster, and changes to this file must be propagated to the other nodes in the cluster.

 

6. Enable o2cb driver to load the driver on boot on all nodes:

$ sudo sed -i "s|false|true|g" /etc/default/o2cb

 

7. Restart iSCSI initiator to update the newly created disk partition:

轻幕
轻幕

轻幕是一个综合性短视频制作平台,诗词、故事、小说等一键成片转视频,让内容传播更生动!

下载

$ sudo service open-iscsi restart

 

8. Restart o2cb service to apply the changes in /etc/ocfs2/cluster.conf:

$ sudo service o2cb restart

 

9. Create the web files directory under /var/www:

$ sudo mkdir -p /var/www/magento

 

10. Get the block ID for the /dev/sdb1. UUID is recommended in fstab if you use iSCSI device:

$ sudo blkid /dev/sdb1 | awk {'print $3'}
UUID="82b1d98c-30e7-4ade-ab9b-590f857797fd"

 

11. Add following line into /etc/fstab:

UUID=82b1d98c-30e7-4ade-ab9b-590f857797fd		/var/www/magento     ocfs2   defaults,_netdev        0 0

 

12. Mount the filesystem:

$ sudo mount -a

 

13. In NODE1, uncompress and copy Magento web files into /var/www/magento and setup directory permission:

$ tar -xzf magento-1.7.0.2.tar.gz
$ sudo cp -Rf magento/* /var/www/magento
$ sudo chown -R www-data.www-data /var/www/magento
$ sudo chmod 777 /var/www/magento/app/etc
$ sudo chmod 777 -Rf /var/www/magento/var
$ sudo chmod 777 -Rf /var/www/magento/media

 

Configure nginx and PHP-FPM

 

** The following steps should be performed on NODE1, NODE2 and NODE3.

 

1. We will use nginx as the web server for Magento. Install nginx and all required PHP modules:

$ sudo apt-get install nginx php5-common php5-cli php5-fpm php5-mysql php5-mcrypt php5-gd php5-curl php-soap

 

2. Open nginx virtual host configuration file at /etc/nginx/sites-available/default and add following lines:

# /etc/nginx/sites-available/magento
server {
    server_name mymagento.com www.mymagento.com;
    root /var/www/magento;
 
    location / {
        index index.html index.php;
        try_files $uri $uri/ @handler;
        expires 30d;
    }
 
    location /app/                { deny all; }
    location /includes/           { deny all; }
    location /lib/                { deny all; }
    location /media/downloadable/ { deny all; }
    location /pkginfo/            { deny all; }
    location /report/config.xml   { deny all; }
    location /var/                { deny all; }
 
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
 
    location @handler {
        rewrite / /index.php;
    }
 
    location ~ .php/ {
        rewrite ^(.*.php)/ $1 last;
    }
 
    location ~ .php$ {
        if (!-e $request_filename) { rewrite / /index.php last; }
        expires        off;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE default;
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params;
    }
}

 

3. Create a symbolic link from sites-available directory to enable the magento virtual host:

$ cd /etc/nginx/sites-enabled
$ sudo ln -s /etc/nginx/sites-available/magento magento

 

4. Restart nginx and PHP:

$ sudo service php5-fpm restart
$ sudo service nginx restart

 

Load Balancer and Failover

 

Instead of using HAProxy for doing SQL load balancing, we will be using some of the suggestions based on this article and just have the Magento instances connect to their local MySQL Server using localhost, with following criteria:

  • Magento in each node will connect to MySQL database using localhost and bypassing HAProxy.
  • Load balancing on database layer is only for mysql client/console. HAProxy will be used to balance HTTP.
  • Keepalived will be used to hold the virtual IP: 192.168.197.150 on load balancers LB1 and LB2

      In case you plan to place the MySQL Servers on separate servers, then the Magento instances should connect to the database cluster via the HAProxy.

       

      ** The following steps should be performed on ST1

       

      1. We have created scripts to install HAProxy and Keepalived, these can be obtained from our Git repository.

      Install git and clone the repo:

      $ apt-get install -y git
      $ git clone https://github.com/severalnines/s9s-admin.git

       

      2. Make sure LB1 and LB2 are accessible using passwordless SSH. Copy the SSH keys to LB1 and LB2:

      $ ssh-copy-id -i ~/.ssh/id_rsa 192.168.197.161
      $ ssh-copy-id -i ~/.ssh/id_rsa 192.168.197.162

       

      3. Install HAProxy on both nodes:

      $ cd s9s-admin/cluster/
      $ sudo ./s9s_haproxy --install -i 1 -h 192.168.197.161
      $ sudo ./s9s_haproxy --install -i 1 -h 192.168.197.162

       

      4. Install Keepalived on LB1 (master) and LB2 (backup) with 192.168.197.150 as virtual IP:

      $ sudo ./s9s_haproxy --install-keepalived -i 1 -x 192.168.197.161 -y 192.168.197.162 -v 192.168.197.150

       

      ** The following steps should be performed on LB1 and LB2

       

      5. By default, the script will configure the MySQL reverse proxy service to listen on port 33306. We will need to add a few more lines to tell HAproxy to load balance our web server farm as well. Add following line in /etc/haproxy/haproxy.cfg:

      frontend http-in
          bind *:80
          default_backend web_farm
       
      backend web_farm
          server NODE1 192.168.197.151:80 maxconn 32
          server NODE2 192.168.197.152:80 maxconn 32
          server NODE3 192.168.197.153:80 maxconn 32

       

      6. Restart HAProxy service:

      $ sudo killall haproxy
      $ sudo /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -st `cat /var/run/haproxy.pid`

       

      Install Magento

       

      1. Now that we have a load-balanced setup that is ready to support Magento, we will now create our Magento database. From the ClusterControl UI, go to Manage > Schema and Users > Create Database to create the database:

      How to Cluster Magento, nginx and MySQL on Multiple Servers

       

      2. Create the database user under Privileges tab:

      How to Cluster Magento, nginx and MySQL on Multiple Servers

       

      3. Assign the correct privileges for magento_user on database magento_site:

      How to Cluster Magento, nginx and MySQL on Multiple Servers

      At the moment, we assume you have pointed mymagento.com and www.mymagento.com to the virtual IP, 192.168.197.150. 

       

      4. Open web browser and go to mymagento.com. You should see an installation page similar to screenshot below:

      How to Cluster Magento, nginx and MySQL on Multiple Servers

       

      * Take note that we are using localhost in the host value, session data will be saved in database. It will allow users to use the same session regardless of which web server they are connected to.

       

      Notes

       

      ** Updated on 9th Dec 2013 **

      By default Magento will setup a MyISAM table specifically for FULLTEXT indexing called catalogsearch_fulltext. MyISAM tables are supported within MySQL Galera Cluster, however, MyISAM has only basic support, primarily because the storage engine is non-transactional and so Galera cannot guarantee the data will remain consistent within the cluster.

      Codership has released MySQL-wsrep 5.6 supports with Galera 3.0 which currently in beta release at the time of this update. You could either use the MySQL-wsrep 5.6 which supports InnoDB FTS or convert all non-Galera friendly tables to use InnoDB with primary keys. Alternatively, you can use external search engine (such as Solr or Sphinx) for FTS capabilities.

      If you choose the latter option, you need to convert some of the tables to work well with Galera by executing following queries on one of the DB node:

      mysql> ALTER TABLE magento.api2_acl_user ADD id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST;
      mysql> ALTER TABLE magento.api_session ADD id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST;
      mysql> ALTER TABLE magento.weee_discount ADD id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST;
      mysql> ALTER TABLE magento.widget_instance_page_layout ADD id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST;
      mysql> ALTER TABLE magento.xmlconnect_config_data ADD id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST;
      mysql> ALTER TABLE magento.oauth_nonce ADD id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST, ENGINE='InnoDB';
      mysql> ALTER TABLE magento.catalogsearch_fulltext DROP INDEX FTI_CATALOGSEARCH_FULLTEXT_DATA_INDEX;
      mysql> ALTER TABLE magento.catalogsearch_fulltext ENGINE='InnoDB';

       

      Verify The Architecture

       

      1. Check the HAproxy statistics by logging into the HAProxy admin page at LB1 host port 9600. The default username/password is admin/admin. You should see some bytes in and out on the web_farm ands9s_33306_production divs:

      How to Cluster Magento, nginx and MySQL on Multiple Servers

       

      2. Check and observe the traffic on your database cluster from the ClusterControl overview page athttps://192.168.197.171/clustercontrol :

      How to Cluster Magento, nginx and MySQL on Multiple Servers

       

      There are many improvements that could be made to this setup. For example, you could provide redundancy to the shared storage server by installing DRBD. You can also add a Varnish Cache in the load balancing servers to provide better caching on your static contents and reduce the load on the web servers/database servers.

相关文章

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法

本专题汇总了包子漫画官网和网页版入口,提供最新章节抢先看方法、正版免费阅读指南,以及稳定访问方式,帮助用户快速直达包子漫画页面,无广告畅享全集漫画内容。

36

2026.02.10

MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法
MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法

本专题汇总了MC.JS官网入口和网页版快速畅玩方法,提供免安装访问、不同版本(1.8.8、1.12.8)在线体验指南,以及正版网页端操作说明,帮助玩家轻松进入MC.JS世界,实现即时畅玩与高效体验。

20

2026.02.10

谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程
谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程

本专题汇总了谷歌邮箱网页版的最新登录入口和注册方法,详细提供官方账号快速访问方式、网页版操作教程及安全登录技巧,帮助用户轻松管理Gmail邮箱账户,实现高效、安全的邮箱使用体验。

18

2026.02.10

铁路12306订票与退改全攻略_高效购票与座位选取技巧
铁路12306订票与退改全攻略_高效购票与座位选取技巧

本专题全面汇总铁路12306订票、退票、改签及候补订单操作技巧,提供车厢座位分布参考、抢票攻略和高铁安检注意事项,帮助新手用户快速掌握高效购票与退改流程,提高出行效率和体验。

11

2026.02.10

TensorFlow2深度学习模型实战与优化
TensorFlow2深度学习模型实战与优化

本专题面向 AI 与数据科学开发者,系统讲解 TensorFlow 2 框架下深度学习模型的构建、训练、调优与部署。内容包括神经网络基础、卷积神经网络、循环神经网络、优化算法及模型性能提升技巧。通过实战项目演示,帮助开发者掌握从模型设计到上线的完整流程。

0

2026.02.10

Vue3组合式API与组件开发实战
Vue3组合式API与组件开发实战

本专题讲解 Vue 3 组合式 API 的核心概念与应用技巧,深入分析响应式系统、生命周期管理、组件设计与复用策略。通过完整项目案例,指导前端开发者实现高性能、结构清晰的 Vue 应用,提升开发效率与代码可维护性。

4

2026.02.10

Go语言微服务架构与gRPC实战
Go语言微服务架构与gRPC实战

本专题面向有 Go 基础的开发者,系统讲解微服务架构设计与 gRPC 的高效应用。内容涵盖服务拆分、RPC 通信、负载均衡、错误处理、服务注册与发现等关键技术。通过实战案例,帮助开发者搭建高性能、可扩展的 Go 微服务系统。

1

2026.02.10

React 18状态管理与Hooks高级实践
React 18状态管理与Hooks高级实践

本专题专注于 React 18 的高级开发技术,详细讲解 useState、useEffect、useReducer、useContext 等 Hooks 的使用技巧,以及 Redux、Zustand 等状态管理工具的集成与优化方法。通过真实案例,帮助前端开发者构建可维护、性能优良的现代 React 应用。

4

2026.02.10

Node.js后端开发与Express框架实践
Node.js后端开发与Express框架实践

本专题针对初中级 Node.js 开发者,系统讲解如何使用 Express 框架搭建高性能后端服务。内容包括路由设计、中间件开发、数据库集成、API 安全与异常处理,以及 RESTful API 的设计与优化。通过实际项目演示,帮助开发者快速掌握 Node.js 后端开发流程。

2

2026.02.10

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.4万人学习

nginx浅谈
nginx浅谈

共15课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号