Skip to content

Instantly share code, notes, and snippets.

@pengkong
Created June 30, 2017 03:42
Show Gist options
  • Save pengkong/6b55afad061f16306de3fd5c68dbe814 to your computer and use it in GitHub Desktop.
Save pengkong/6b55afad061f16306de3fd5c68dbe814 to your computer and use it in GitHub Desktop.
Thumbor Server on AWS

Update AWS Instance

Launch and SSH to AWS Instance
Type sudo su
Type yum update -y
Type yum groupinstall "Development Tools" -y
Type pip install --upgrade pip (pip resides at /usr/local/bin/pip, pip version 8.0.2, python version 2.7.10)

Install Thumbor Dependencies

Type yum install libjpeg-turbo-devel libjpeg-turbo-utils libtiff-devel libpng-devel pngcrush jasper-devel libwebp-devel libcurl-devel cmake -y
Type /usr/local/bin/pip uninstall pycurl
Type export PYCURL_SSL_LIBRARY=nss
Type /usr/local/bin/pip install pycurl
Type /usr/local/bin/pip install --upgrade setuptools
Type /usr/local/bin/pip install numpy

Install Gifsicle

git clone https://github.com/kohler/gifsicle.git
cd gifsicle
./bootstrap.sh
./configure
make
make install
cd ../
rm -rf gifsicle/

Gifscile is installed at /usr/local/bin/gifscile

Install Thumbor

Type /usr/local/bin/pip install thumbor
Type /usr/local/bin/thumbor-config > /etc/thumbor.conf
Type vim /etc/thumbor.conf

RESPECT_ORIENTATION = True
...
SECURITY_KEY = 'xehbyQKFPsp6gh64yDEnCUEGEecC
...
DETECTORS = [
    'thumbor.detectors.face_detector',
    'thumbor.detectors.feature_detector'
]
...
OPTIMIZERS = [
    'thumbor.optimizers.jpegtran',
    'thumbor.optimizers.gifsicle',
    'thumbor.optimizers.pngcrush'
]
JPEGTRAN_PATH = '/usr/bin/jpegtran'
GIFSICLE_PATH = '/usr/local/bin/gifsicle'
PNGCRUSH_PATH = '/usr/bin/pngcrush'

Also enable FILTERS[] needed.

vim /usr/local/lib64/python2.7/site-packages/thumbor/optimizers/pngcrush.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

# thumbor imaging service
# https://github.com/thumbor/thumbor/wiki

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license


import os  
import subprocess

from thumbor.optimizers import BaseOptimizer  
from thumbor.utils import logger


class Optimizer(BaseOptimizer):  
    def __init__(self, context):
        super(Optimizer, self).__init__(context)

        self.runnable = True
        self.pngcrush_path = self.context.config.PNGCRUSH_PATH
        if not (os.path.isfile(self.pngcrush_path) and os.access(self.pngcrush_path, os.X_OK)):
            logger.error("ERROR pngcrush path '{0}' is not accessible".format(self.pngcrush_path))
            self.runnable = False

    def should_run(self, image_extension, buffer):
        return 'png' in image_extension and self.runnable

    def optimize(self, buffer, input_file, output_file):
        command = '%s -reduce -q %s %s ' % (
            self.pngcrush_path,
            input_file,
            output_file,
        )
        with open(os.devnull) as null:
            subprocess.call(command, shell=True, stdin=null)

vim /usr/local/lib64/python2.7/site-packages/thumbor/optimizers/gifsicle.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

# thumbor imaging service
# https://github.com/globocom/thumbor/wiki

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 globo.com timehome@corp.globo.com

import os

from thumbor.optimizers import BaseOptimizer

class Optimizer(BaseOptimizer):

    def should_run(self, image_extension, buffer):
        return 'gif' in image_extension

    def optimize(self, buffer, input_file, output_file):
        gifsicle_path = self.context.config.GIFSICLE_PATH
        command = '%s --optimize --output %s %s ' % (
            gifsicle_path,
            output_file,
            input_file,
        )
        os.system(command)

Install Supervisord

Type /usr/local/bin/pip install supervisor
Type echo_supervisord_conf > /etc/supervisord.conf
Type vim /etc/supervisord.conf

[program:thumbor]
command=/usr/local/bin/thumbor --ip=127.0.0.1 --port=900%(process_num)s --conf=/etc/thumbor.conf
process_name=thumbor900%(process_num)s
numprocs=4
stdout_logfile=/var/log/thumbor900%(process_num)s.stdout.log
stderr_logfile=/var/log/thumbor900%(process_num)s.stderr.log

Supervisor socket is at /tmp/supervisor.sock
Supervisor log file is at /tmp/supervisord.log
Supervisor pid file is at /tmp/supervisord.pid

Type sudo vim /etc/init.d/supervisord

#! /bin/sh
### BEGIN INIT INFO
# Provides:          supervisord
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Supervisor init script
# Description:       Supervisor init script    
### END INIT INFO

# Supervisord auto-start
#
# description: Auto-starts supervisord
# processname: supervisord
# pidfile: /tmp/supervisord.pid

SUPERVISORD=/usr/local/bin/supervisord
SUPERVISORCTL=/usr/local/bin/supervisorctl
ARGS="-c /etc/supervisord.conf"

case $1 in
start)
        echo -n "Starting supervisord: "
        $SUPERVISORD $ARGS
        echo
        ;;
stop)
        echo -n "Stopping supervisord: "
        $SUPERVISORCTL shutdown
        ;;
restart)
        echo -n "Stopping supervisord: "
        $SUPERVISORCTL shutdown
        echo -n "Starting supervisord: "
        $SUPERVISORD $ARGS
        echo
        ;;
esac

Type chmod +x supervisord
Type chkconfig --add supervisord
Type chkconfig supervisord on

Start, Stop, Restart Supervisord

/etc/init.d/supervisord start
/etc/init.d/supervisord stop
/etc/init.d/supervisord restart

Install Nginx

Type sudo yum install nginx -y
Type chkconfig nginx on
Type vim /etc/nginx/conf.d/thumbor.conf

upstream thumbor  {  
    server 127.0.0.1:9000;
    server 127.0.0.1:9001;
    server 127.0.0.1:9002;
    server 127.0.0.1:9003;
}

server {  
    listen       80;
    server_name  thumbor.vanitee.com;
    client_max_body_size 10M;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://thumbor;
        proxy_redirect off;
    }
}

Type vim /etc/nginx/nginx.conf

Use pound sign (#) to comment out all server {} so that port 80 is freed up for thumbor

Type service nginx restart

Install OpenCV

wget https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip 3.1.0.zip
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
make
make install

Reference: http://www.dadoune.com/blog/best-thumbnailing-solution-set-up-thumbor-on-aws/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment