EC2上でPython3によるDjangoを動かす

Amazon Web Services(AWS);Amazon Linux AMI 2013.03.1 でPython 3+Green Unicorn+Django 1.5を動かすメモです.
まっさらなInstanceからのインストールです.

Python install

基本的にはhttp://librabuch.jp/2013/01/python33-install/が参考になります.

Python 3コンパイル

ここは普通.
yum install gcc make openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
wget http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz
tar -Jxf Python-3.3.2.tar.xz
cd Python-3.3.2
./configure --prefix=/usr/local/python --enable-shared
make
sudo make install
sudo sh -c "echo '/usr/local/python/lib' >> /etc/ld.so.conf"
sudo ldconfig
sudo ln -s /usr/local/python/bin/python3 /usr/local/bin/python3

Distributeインストール

パスに注意しながら.
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.45.tar.gz
tar xzf distribute-0.6.45.tar.gz
sudo /usr/local/bin/python3 distribute-0.6.45/setup.py install
sudo /usr/local/python/bin/easy_install pip

Django, Green Unicorn, nginx周り

こっからはhttp://adrian.org.ar/django-nginx-green-unicorn-in-an-ubuntu-11-10-ec2-instance/
http://dat.plastica-romantica.com/log699.htmlを参考に.

Virtualenv, Djangoインストール

sudo /usr/local/python/bin/pip install virtualenv
/usr/local/python/bin/virtualenv --no-site-packages django_app
cd django_app
source bin/activate


/usr/local/python/bin/django-admin.py startproject app
cd app
/usr/local/python/bin/gunicorn_django -b 0.0.0.0:8000

ホストのtcp/8000にブラウザでアクセスすると見えます.(Security Groupでtcp/8000を一時的に開ける必要あり)

sudo yum install nginx

sudo mkdir -p /opt/django/logs/nginx/
mkdir $HOME/django_app/static
mkdir $HOME/django_app/templates
sudo ln -s $HOME/django_app/static /opt/django

nginx設定:リバースProxyでgunicornにつなぐ

sudo vim /etc/nginx/conf.d/gunicorn.conf
こちらをもとにしています
https://bitbucket.org/deccico/django_gunicorn/raw/tip/server/etc/nginx/sites-available/default

server {
    listen   80;
    server_name localhost;

    access_log /opt/django/logs/nginx/vc_access.log;
    error_log  /opt/django/logs/nginx/vc_error.log;

    # no security problem here, since / is alway passed to upstream
    root /opt/django/;
    # serve directly - analogous for static/staticfiles
    location /media/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location /admin/media/ {
        # this changes depending on your python version
        root /usr/local/python/lib/python3.3/site-packages/django/contrib;
    }
    location /static/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}

sudo /etc/init.d/nginx start

settings.pyのTEMPLATE_DIRS = に’/home/ec2-user/django_app/templates’を追加.

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>