Ansible + shell 实现部署fastdfs+nginx 实现图片服务器并提供动态缩放功能;

Posted 开胃咸菜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible + shell 实现部署fastdfs+nginx 实现图片服务器并提供动态缩放功能;相关的知识,希望对你有一定的参考价值。

   因为公司阿里服务器变动几次,手动部署了好几次fastdfs+nginx,于是就想到了自动化部署,以下为脚本内容,由于只是想把着功能实现,并未有完完整的判断逻辑;

   以下为ansible-playbook及shell及模板nginx-fastdfs及动态缩略图配置; 

   如有错误,请不吝赐教;

---
  - hosts: apollo
  #  vars:
  #    filedest: /usr/local/
    tasks:
      - name: prepare fastdfs-nginx-module
        copy: src=/etc/ansible/fastdfs-nginx-module_v1.16.tar.gz dest=/usr/local/
      - name: prepare nginx-1.12.0.tar.gz
        copy: src=/etc/ansible/nginx-1.12.0.tar.gz dest=/usr/local/
      - name: prepare V5.05.tar.gz
        copy: src=/etc/ansible/V5.05.tar.gz dest=/usr/local/ 
      - name: gcc,git,openssl-devel,pcre-devel,gd-devel 
        yum: name=gcc,git,openssl-devel,pcre-devel,gd-devel state=present
      - name: push fastdfs-nginx-module-config
        copy: src=/etc/ansible/fastdfs_nginx.conf dest=/usr/local/
      - name: create user www
        user: name=www shell=/sbin/nologin

      - name: unzip packages and make install....
        script: /etc/ansible/install_fastdfs.sh

      - name: delete fastdfs-nginx-module
        file: name=/usr/local/fastdfs-nginx-module_v1.16.tar.gz state=absent
      - name: delete nginx-1.12.0.tar.gz
        file: name=/usr/local/nginx-1.12.0.tar.gz state=absent
      - name: delete V5.05.tar.gz 
        file: name=/usr/local/V5.05.tar.gz state=absent

  以下为shell 部分

#!/usr/bin/env bash
set -x 
set -e 
IFS=$‘\n‘
dest=/usr/local/
fastdfsgz="V5.05.tar.gz"
fastdfsmod="fastdfs-nginx-module_v1.16.tar.gz"
nginxgz="nginx-1.12.0.tar.gz"
IP=$(ip a s eth0 | grep  "inet " | awk ‘{print $2}‘ | sed -r "s/\/24//g")
for gz in ${dest}${fastdfsgz} ${dest}${fastdfsmod} ${dest}${nginxgz}
do
	tar xf $gz -C ${dest}
done
########安装libfast
cd ${dest}
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon
./make.sh && ./make.sh install 
############更改fastdfs-nginx-module/src/config
sed -ri "4s/local\///g" ${dest}fastdfs-nginx-module/src/config
##################安装fastdfs
cd ${dest}fastdfs-5.05/
./make.sh && ./make.sh install & 
wait 
cp conf/http.conf /etc/fdfs/
cp conf/mime.types /etc/fdfs/
cd /etc/fdfs/
for filename in client.conf.sample storage.conf.sample tracker.conf.sample
do
	cp ${filename} ${filename/.sample/}
	sed -ri "s/base_path=/#base_path=/g" ${filename/.sample}
done
mkdir -p /home/fast/client /home/fast/tracker /home/fast/storage/data
cp ${dest}fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
sed -ri "/^connect_timeout/cconnect_timeout = 10" /etc/fdfs/mod_fastdfs.conf
sed -ri "/^tracker_server/ctracker_server=${IP}:22122" /etc/fdfs/mod_fastdfs.conf
sed -ri "/^url_have_group_name/curl_have_group_name=true" /etc/fdfs/mod_fastdfs.conf
sed -ri "/^store_path0=/cstore_path0=\/home\/fast\/storage\/data" /etc/fdfs/mod_fastdfs.conf

###tracker.conf
sed -ri "/#base_path=/abase_path=\/home\/fast\/tracker" tracker.conf
####client.conf
sed -ri "/#base_path=/abase_path=\/home\/fast\/client" client.conf
sed -ri "s/tracker_server=/#tracker_server=/g" client.conf
sed -ri "/#tracker_server=/atracker_server=${IP}:22122" client.conf
####storage.conf
sed -ri "/#base_path=/abase_path=\/home\/fast\/storage" storage.conf
sed -ri "s/tracker_server=/#tracker_server=/g" storage.conf
sed -ri "/#tracker_server=/atracker_server=${IP}:22122" storage.conf
sed -ri "/store_path0=/cstore_path0=\/home\/fast\/storage\/data" storage.conf
############编译nginx 
cd ${dest}nginx-1.12.0 
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-pcre --add-module=/usr/local/fastdfs-nginx-module/src/ --with-http_image_filter_module  & 
#--add-module=/usr/local/nginx-rtmp-module & 
wait
make && make install & 
wait
if [ -d ${dest}/nginx/conf.d ];then
	mv ${dest}fastdfs_nginx.conf ${dest}/nginx/conf.d/
else
	mkdir ${dest}/nginx/conf.d
	mv ${dest}fastdfs_nginx.conf ${dest}/nginx/conf.d/
fi
cd /usr/local/nginx/conf.d/
for rows in $(tac ./fastdfs_nginx.conf)
do
sed -ri "40a$rows" /usr/local/nginx/conf/nginx.conf
done 
############
/usr/local/nginx/sbin/nginx  -t 

  以下为nginx动态缩放配置及网页

location ~/group[0-9]/M00/(.+)_([0-9]+)x([0-9]+)\.(jpg|gif|png) {
                ngx_fastdfs_module;
                index index.html index.htm;
                set $w $2;
                set $h $3;
                if ($w != "0" ) {
                        rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ group1/M00$1.$4 break;
}
                if ($h != "0" ) {
                        rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ group1/M00$1.$4 break;
}
                image_filter resize $w $h ;

                image_filter_buffer 5M;
}
        location ~ /group[0-9] {
                ngx_fastdfs_module;
                index index.html index.htm;
}
		location = /favicon.ico {
                log_not_found off;
                access_log off;
}

  

最后安装完成以后,并未启动fastdfs ,请先运行命令启动fastdfs: fdfs_trackerd /etc/fdfs/tracker.conf && fdfs_storaged /etc/fdfs/storage.conf 

       启动完成以后,如果不能正常访问返回415,请复制粘贴模板内容到nginx的配置文件里;

以上是关于Ansible + shell 实现部署fastdfs+nginx 实现图片服务器并提供动态缩放功能;的主要内容,如果未能解决你的问题,请参考以下文章

自动化运维Ansible批量部署服务+shell脚本批量推送公钥

自动化运维Ansible批量部署服务+shell脚本批量推送公钥

ansible批量部署

ansible 中shell 模块和command 模块的区别

shell脚本ansible执行不成功

Ansible 自动化运维工具