ecstore资源文件分离方案及配置

目前ECStore中存放资源文件目录
    1   每个app下的statics下的(图片,js,css)资源文件
    2   模板的资源文件(图片,js,css)  themes/
    3   public下的图片文件 (对应到 sdb_image_image 表中)

只做public中的图片分离

该方案只是将public下的图片文件进行分离,也就是说只将 sdb_image_image 表中的文件中的图片进行分离

需要注意的是:
    因为在sdb_image_image表中已经存放了对应的url地址,图片调用的是根据sdb_image_image表中的url地址进行调用
    因此,在运营中的站点进行图片分离,已存储的图片在此方案中不会进行数据迁移

方案说明

环境:
   web服务器 centos
      IP地址  :192.168.65.145
      站点目录:/data/www

   storage服务器 centos
      IP地址  :192.168.65.144

   图片调用服务器
      IP地址  :192.168.65.144

图片调用服务器和storage(图片存储服务器)为同一个服务器

说明
    使用nginx + Tokyo Tyrant进行存储的方案
    nginx的NginxHttpMemcachedModule有个很牛X的特性,可以使用url作为memcache的key进行资源访问。

    使用内存做存储有两个缺点,一是有容量限制,二是不能永久存储。因此我们使用 Tokyo Tyrant 做存储方案。
    只要在进行图片存储时,将url作为key,将图片存储到Tokyo Tyrant
    Tokyo Tyrant是Tokyo Cabinet的网络接口,可以使用memcached一样的协议。同memcache相比,Tokyo Cabinet可以将资源存放在硬盘中

storage服务器中Tokyo Tyrant安装配置

  • 安装ttserver
    wget http://fallabs.com/tokyocabinet/tokyocabinet-1.4.47.tar.gz
    wget http://fallabs.com/tokyotyrant/tokyotyrant-1.1.41.tar.gz
    
    1.安装Tokyo Cabinet
        tar zxvf tokyocabinet-1.4.47.tar.gz
        cd tokyocabinet-1.4.47
        sudo ./configure 
        sudo make
        sudo make install
    
    2.安装Tokyo Tyrant
        tar zxvf tokyotyrant-1.1.41.tar.gz
        cd tokyotyrant-1.1.41
        sudo ./configure
        sudo make
        sudo make install
    
    安装完成之后启动文件 默认在 /usr/local/sbin/ttservctl
    
    参考文档:http://www.ttlsa.com/ttserver/install-ttserver-on-linux/
    
    注意:因为tt可以无密码进行操作,并且在tt中存储了挂件的php文件,因此需要在防火墙配置,保证tt服务器的安全性
    
  • 启动ttservctl
    ttservctl start 
    

图片调用服务器nginx加入memc-nginx-module模块

  • memc-nginx-module 模块下载
    下载地址:https://github.com/agentzh/memc-nginx-module
    
    安装git:yum -y install git
    
    git clone memc-nginx-module模块:git clone https://github.com/agentzh/memc-nginx-module.git
    
  • nginx编译加载memc-nginx-module
    因为Nginx并不支持模块动态加载,所以要安装新的模块,必须重新编译Nginx
    
    wget http://nginx.org/download/nginx-1.4.7.tar.gz
    tar zxvf nginx-1.4.7.tar.gz
    cd nginx-1.4.7
    
    ./configure --prefix=/usr/local/nginx --add-module=/path_to_memc-nginx-module(memc-nginx-modul模块下载的文件地址)
    
    make && make install
    
  • nginx安装成功后,nginx.conf 配置中加入以下部分配置
    upstream tt_server1{  #配置一个tt服务器组
        server localhost:1978; #该端口为ttserver启动端口
    }
    
    server {
        listen       80;
        server_name  localhost;
    
        location / {
            set $memcached_key $uri;
            memcached_pass tt_server1;
        }
    }
    
    参考文档:http://blog.codinglabs.org/articles/nginx-memc-and-srcache.html
    

web服务器ECStore配置修改

  • php安装memcache扩展
    phpinfo检查php是否安装了memcache扩展,如果没有安装这安装扩展,则进行安装
    
    /usr/local/webserver/php/bin/pecl install memcache
    
    vim /usr/local/webserver/php/php.ini
    
    extension=memcache.so
    
  • ECstore中conf.php修改
    # file_storage
    define('FILE_STORAGER','ttsystem');//storage 使用ttserver存储
    define('STORAGE_MEMCACHED','192.168.65.144:1978');//1978端口为nginx tt_server1配置的端口
    define('STORAGE_HOST','http://192.168.65.144');//调用storage中图片的图片服务器地址
    

ECStore全站点图片,js,csss 等静态资源分离

ECStore支持版本:ECSore2.0.36及其以上版本
在ECSore2.0.36中将所有的app中的statics和lang剥离到public/app/下

方案一、ttserver+nfs

环境:
   web服务器 centos
      IP地址  :192.168.65.145
      站点目录:/data/www

   storage服务器 centos
      IP地址  :192.168.65.144

   资源调用服务器
      IP地址  :192.168.65.144

图片调用服务器和storage(图片存储服务器)为同一个服务器

方案说明
    在ECSore2.0.34版本中,将每个app下的lang,statics,widgets文件夹移动到public/app/对应app下
    因此在初始化的时候可以直接将public中的文件存储到ttserver中,进行调用
    之后对public文件夹使用inotify进行监控,如果文件有改变则更新或删除ttserver中的数据
    保证tterver中数据和本地文件夹的一致性

    ECstore模板中的资源文件,调用判断分离不完善,暂时不能进行storage方式进行存储调用
    因此模板文件使用nfs进行同步

配置

说明
    1 storage服务器中安装 ttserver
    2 web服务器中安装inotify对资源文件进行监控
    3 资源调用服务器中安装nfs,web服务器中将网站的模板目录挂载到资源调用服务器

Tokyo Tyrant安装配置

参照public中的图片分离中的Tokyo Tyrant安装配置

web服务器安装inotify

inotify-tools项目地址:https://github.com/rvoicilas/inotify-tools

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install

其他Linux发行版安装方法可以参见:https://github.com/rvoicilas/inotify-tools/wiki#wiki-getting

NFS安装&配置

  • 资源调用服务器和web服务器都安装NFS
    安装:nfs-utils
        yum install nfs-utils
    
    安装:rpcbind, 注意(portmap,发现CentOS 6上不叫portmap,而是改为rpcbind)
          yum install portmap  (适用centos 5)
          yum install rpcbind  (适用centos 6)
    
  • 启动NFS服务
    /etc/init.d/rpcbind start
    /etc/init.d/nfs start
    
  • NFS配置
    1 进入资源调用服务器配置的站点目录/data/www,创建NFS挂载目录
      mkdir nfs
    
    2 在web服务器中,将web服务器中的themes文件复制到资源调用服务器
      scp -r /data/www/themes root@192.168.65.144:/data/www/nfs/
      scp -r /data/www/wap_themes root@192.168.65.144:/data/www/nfs/
    
    3 资源服务器:vim /etc/exports
      /data/www/nfs/themes  192.168.65.145/24(rw,sync,all_squash,anonuid=502,anongid=502)
      /data/www/nfs/wap_themes  192.168.65.145/24(rw,sync,all_squash,anonuid=502,anongid=502)
    
      修改好执行命令:
      exportfs -rv
    
    4 在web服务器中查看
      showmount -e 192.168.65.144
    
    5 web中将模板目录挂载到资源调用服务器
      mount 192.168.65.144:/data/www/nfs/themes /data/www/themes
      mount 192.168.65.144:/data/www/nfs/wap_themes /data/www/wap_themes
    
    6 修改图片资源调用服务器的nginx.conf ,使得nginx可以调用themes文件
        location /themes/{
            root /data/www/nfs/;
            index  index.html index.htm;
        }
        location /wap_themes/{
            root /data/www/nfs/;
            index  index.html index.htm;
        }
    
    NFS配置:参考文档 http://man.ddvip.com/linux/debian/nfs/nfs-conf-4.html
    

图片调用服务器nginx加入memc-nginx-module模块

参照public中的图片分离中的,图片调用服务器nginx加入memc-nginx-module模块

解决跨域加载字体问题:firefox和IE9不支持对icon font字体的跨域访问

  • 资源调用服务器修改nginx配置文件 nginx.conf
    在nginx.conf文件中加入
    
    server {
        ...
        #在server中新增如果下配置
        location ~* \.(eot|ttf|woff)$ {
            add_header Access-Control-Allow-Origin *;
            set $memcached_key $uri;
            memcached_pass tt_server1;
        }
        ...
    }
    
    新增 AddType vim /usr/local/webserver/nginx/conf/mime.types
    在mime.types 中新增如果代码
    
    application/vnd.ms-fontobject         eot;
    font/ttf                              ttf;
    font/otf                              otf;
    application/x-font-woff               woff;
    
  • apache
    参考地址:http://w.67ge.com/p/fixing-firefox-font-face-cross-domain.html
    

解决图片上传flash的跨域问题

  • 在域名根目录中添加crossdomain.xml
    <?xml version="1.0"?>
    <cross-domain-policy>
    <allow-access-from domain="*" />
    </cross-domain-policy>
    

web服务器中安装ecstore

  • 安装ecstore
  • 执行初始化脚本
    1 修改 script/storage/ttserver.sh中storage存储地址和端口
      #storage存储服务器地址
      host=192.168.65.144
      #ttserver端口
      port=1978
    
    2 执行 ecstore站点目录:script/storage/init.sh
    
  • 执行inotify监控脚本
    1 修改 在script/storage/inotify.sh中配置正确的inotifywait命令路径
      inotifywait="/usr/local/bin/inotifywait"
    
    2 执行 ecstore站点目录:script/storage/inotify.sh start
    
  • web服务器中修改config/config.php
     define('FILE_STORAGER', 'ttprosystem');
     define('STORAGE_MEMCACHED', '192.168.65.144:1978');
     define('HOST_MIRRORS', 'http://192.168.65.144');
    

全nsf方案

public , themes , wap_themes 三个文件夹挂载到nfs服务器共享目录
方式参照ttserver+nfs方案,将public中的文件不进行storage存储,进行nfs挂载
  • web服务器中修改config/config.php
     define('FILE_STORAGER', 'filesystem');
     define('HOST_MIRRORS', 'http://192.168.65.144');
    
  • nginx.conf配置
    location /{
        root /data/www/nfs/;
        index  index.html index.htm;
    }
    
  • 解决跨域加载字体问题:firefox和IE9不支持对icon font字体的跨域访问
  • 解决图片上传flash的跨域问题