2011-12-22

다른 계정으로 명령 실행하기

다른 사용자 계정으로 명령을 실행할 수 있는 방법이 다양하게 있는데, 그 중 가장 많이 사용되는 방법을 나열하면 아래와 같다.

$ su - 아이디 -c "명령어1; 명령어2; 명령어3"
$ sudo -u 아이디 "명령어"
$ ssh 아이디@호스트 "명령어"

위와 같은 방법으로 하면 간단하게 실행할 수 있는데, 환경변수가 제대로 먹지 않아서 실행이 안되는 경우가 있다. su, ssh 의 경우, 환경변수를 .bashrc 에 넣어두면 안되고, .profile 이나 .bash_profile 넣어야 된다.

sudo 의 경우는 환경변수 보다는 보안적인 이유로 특정 디렉토리에 있는 파일만 실행되도록 설정되어 있다. /etc/sudoers 파일을 열어서 아래와 같은 부분을 찾아서, 자신이 실행하고자 하는 파일이 존재하는 디렉토리를 포함시켜주면 된다.

Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

2011-12-20

Debian/Ubuntu + NginX + trunserver + Django 1.3.1 + Python 2.7.2

Django를 Deploy하기 위한 여러가지 방법들을 소개했었는데, 이번 방법이 내가 가장 선호하는 방식이다. Instance 별로 정상동작 유무를 확인하기가 쉽고, NginX 에서는 로드밸런싱을 하고, 하나의 Instance 에 문제가 발생하면 자동으로 제외되어 무장애/무중단 서비스를 제공할 수 있다.

여기에서 더 나아가 memcached 등을 이용하여 캐싱까지 처리하면 성능에서도 상당히 만족스러운 결과를 볼 수 있을 것이다. 또한, trunserver 조작에 대해서 개선할 부분이 많이 있다. 이런 부분들은 다음에 생각해보고, NginX와 trunserver 를 이용한 방법을 살펴보자.

Install Python

$ apt-get install zlibc zlib1g-dev libxml2 libxml2-dev libbz2-dev curl build-essential python
$ curl -kL http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install | bash
$ echo 'source ~/.pythonbrew/etc/bashrc' >> ~/.bashrc
$ source ~/.bashrc
$ pythonbrew install --force --no-test 2.7.2
$ pythonbrew switch 2.7.2

Install Django, trunserver

$ pip install django psycopg2 trunserver

Create Project

$ mkdir -p /opt/project
$ cd /opt/project
$ django-admin.py startproject sample_project
$ cd sample_project
$ mkdir media

/opt/project/sample_project 에 start.sh를 만든다.

nohup python manage.py trunserver --verbosity=0 --noreload 0.0.0.0:9000 > /dev/null 2>&1 &
nohup python manage.py trunserver --verbosity=0 --noreload 0.0.0.0:9001 > /dev/null 2>&1 &
$ chmod 755 start.sh

trunuwsgi 실행

$ ./start.sh

Install NginX

$ aptitude install nginx

Configure NginX

$ cd /etc/nginx/sites-available
$ vi django 
upstream sample_project {
    ip_hash;
    server 127.0.0.1:9000;
    server 127.0.0.1:9001;
}

server {
    listen 80;
    server_name mydomain.com;
    
    location /site_media  {
        root /opt/project/sample_project/media/;
    }

    location / {
        proxy_pass sample_project;
        proxy_set_header XRealIP $remote_addr;
    }
}
$ cd /etc/nginx/sites-enabled
$ ln -s /etc/nginx/sites-available/django django

Run NginX

$ /etc/init.d/nginx restart

2011-12-17

Debian/Ubuntu + NginX + uWSGI + Django 1.3.1 + Python 2.7.2

Install Python

$ apt-get install zlibc zlib1g-dev libxml2 libxml2-dev libbz2-dev curl build-essential python
$ curl -kL http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install | bash
$ echo 'source ~/.pythonbrew/etc/bashrc' >> ~/.bashrc
$ pythonbrew install --force --no-test 2.7.2
$ pythonbrew switch 2.7.2

Install Django, uWSGI

$ pip install django psycopg2 uwsgi

Create Project

$ mkdir -p /opt/project
$ cd /opt/project
$ django-admin.py startproject sample_project
$ cd sample_project
$ mkdir media

/opt/project/sample_project 에 django0.ini를 만든다.

[uwsgi]
socket = 127.0.0.1:8000
chdir = /opt/project/sample_project
pythonpath = /opt/project
env = DJANGO_SETTINGS_MODULE=sample_project.settings
module = django.core.handlers.wsgi:WSGIHandler()
master = true
processes = 4
daemonize = /opt/project/sample_project/uwsgi0.log
pidfile = /opt/project/sample_project/uwsgi0.pid

/opt/project/sample_project 에 django1.ini를 만든다.

[uwsgi]
socket = 127.0.0.1:8001
chdir = /opt/project/sample_project
pythonpath = /opt/project
env = DJANGO_SETTINGS_MODULE=sample_project.settings
module = django.core.handlers.wsgi:WSGIHandler()
master = true
processes = 4
daemonize = /opt/project/sample_project/uwsgi1.log
pidfile = /opt/project/sample_project/uwsgi1.pid

uwsgi 실행

$ uwsgi django0.ini
$ uwsgi django1.ini

Install NginX

$ aptitude install nginx

Configure NginX

$ cd /etc/nginx/sites-available
$ vi django
upstream sample_project {
    ip_hash;
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
}

server {
    listen 80;
    server_name mydomain.com;
    
    location /site_media  {
        root /opt/project/sample_project/media/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass sample_project;
    }
}
$ cd /etc/nginx/sites-enabled
$ ln -s /etc/nginx/sites-available/django django

Run NginX

$ /etc/init.d/nginx restart