Python 으로 가장 많이 이용되는 웹프레임워크는 Django 이다. 최근 가벼움과 성능으로 인정을 받고 있는 NginX 와 FastCGI 로 연동하여 설치하는 방법을 간단하게 정리해보았다. CentOS 6.0 이 나오긴 했지만, 아직 5.x 환경도 많이 이용되고 있으리라 생각하여 CentOS 5.6 에서 테스트하였으며, 아마 6.0 에서도 무리없이 설치될 것으로 믿는다.

  1. Install Python
    # echo 'export PYTHONBREW_ROOT=/opt/pythonbrew' >> /etc/profile; source /etc/profile
    # curl -kLO http://xrl.us/pythonbrewinstall; chmod +x pythonbrewinstall; ./pythonbrewinstall
    # echo 'source /opt/pythonbrew/etc/bashrc' >> /etc/profile; source /etc/profile
    # pythonbrew install --force --no-test 2.7.2
    # pythonbrew switch 2.7.2
    
  2. Install Django
    # pip install django flup
    
  3. Create Project
    # mkdir -p /opt/project
    # cd /opt/project
    # django-admin.py startproject sample_project
    # cd sample_project
    # mkdir media
    
  4. Run Project
    # cd /opt/project/sample_project
    # python manage.py runfcgi method=prefork pidfile=/tmp/django_sample.pid host=127.0.0.1 port=8000
    
  5. Install NginX
    # yum install nginx
    
  6. Configure NginX
    # cd /etc/nginx
    # vi nginx.conf
    ...
        server {
            server_name 도메인주소;
            location /site_media  {
                root /opt/project/sample_project/media/;
            }
            location / {
                # host and port to fastcgi server
                fastcgi_pass 127.0.0.1:8000;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param QUERY_STRING $query_string;
                fastcgi_param CONTENT_TYPE $content_type;
                fastcgi_param CONTENT_LENGTH $content_length;
                fastcgi_pass_header Authorization;
                fastcgi_intercept_errors off;
            }
        }
    ...
    
  7. Run NginX
    # /etc/init.d/nginx restart
    

예전에 Debian+Apache+PHP+CodeIgniter+MySQL 를 올린 적이 있다. 이번엔 CentOS에서 설치하는 법도 한 번 알아보도록 하자. DBMS 도 MySQL 이 아닌 PostgreSQL을 설치한다.

아래의 설치방법은 CentOS 5.6 과 Scientific Linux 5.5 에서 적용 가능하다.

기본 프로그램 설치
# yum groupinstall "Development Tools"
# yum install git htop ntp
Apache + mod_php 설치
# yum install php
PostgreSQL 8.4.7 설치
  • 설치
    # yum install postgresql84 postgresql84-devel postgresql84-libs postgresql84-contrib postgresql84-server
    
  • DB 초기화
    # service postgresql initdb
    
    /var/lib/pgsql/data 에 초기화된 파일들이 생성된다.
  • 설정파일 위치 : /var/lib/pgsql/data
  • 시스템 부팅시 자동으로 실행되게 하려면, ntsysv 에서 postgresql 을 체크해주어야 한다.
사용자, DB 생성
  • 사용자, DB 생성
    # su - postgres
    # psql
    
    postgres=# CREATE USER 아이디 WITH PASSWORD '비밀번호';
    postgres=# CREATE DATABASE 디비이름 WITH ENCODING='utf-8' OWNER 아이디;
    
  • 계정으로 로그인할 수 있도록 연결설정
    # su -
    # cd /var/lib/pgsql/data/
    # vi pg_hba.conf
    모두 주석으로 막고 아래만 남김
    host    all         all         0.0.0.0           0.0.0.0           password
    local   all         postgres                                        trust
    local   all         all                                             password
    
    # vi postgresql.conf
    listen_addresses = '*'
    
    # /etc/init.d/postgresql restart
    # psql -d 디비이름 -U 아이디 -W
    
프로젝트 생성
$ mkdir -p /opt/project/프로젝트명/web
$ cd /opt/project/프로젝트명/web
Apache 설정
# cd /etc/httpd/conf
# vi httpd.conf
NameVirtualHost *:80

<VirtualHost *:80>
    ServerName 자신의도메인URL
    DocumentRoot /opt/project/프로젝트명/web
    <Directory />
        Options Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

# /etc/init.d/httpd restart
CodeIgniter 설치
  1. http://codeigniter.com/download.php 에서 CodeIgniter_2.0.2.zip 다운로드한다.
  2. 압축을 풀고 /opt/project/프로젝트명/web 에 업로드 한다.
  3. /opt/project/프로젝트명/web 에.htaccess를 작성한다.
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
  4. system/application/config/config.php 파일을 열어 기반url(base URL)을 세팅한다.
    $config['base_url'] = "자신의도메인URL";
    
  5. 데이터베이스를 사용한다면 application/config/database.php 파일을 열어서 데이터베이스 정보를 세팅한다.
    $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "아이디";
    $db['default']['password'] = "비밀번호";
    $db['default']['database'] = "디비이름";
    $db['default']['dbdriver'] = "postgre";
    
  6. 마지막으로 보안을 위해서 system 디렉토리를 sys로 변경하고, index.php 의 $system_folder 변수를 변경해준다.
    $system_path = "sys";
    
    sys 대신 자신이 원하는 다른 것으로 변경하길 바란다.
  7. 웹브라우저에서 자신의도메인URL 로 접속해본다.
  • 참조
    - http://rvm.beginrescueend.com/ : Ruby Version Manager (RVM)
    - Unix 계열의 OS에서는 RVM 으로 다양한 버전의 Ruby 를 설치/관리할 수 있다.
    - 소스 컴파일을 하기 때문에 설치가 느리긴 하지만 다양한 OS에서 일관된 설치/관리를 제공하므로 효과적이다.
    - http://railstutorial.org/ruby-on-rails-tutorial-book : Ruby on Rails Tutorial
  • 필요한 패키지 설치
    # yum groupinstall "Development Tools"
    # yum install autoconf bison curl git zlib zlib-devel libxslt-devel libxml2-devel
    
  • rvm 설치
    # bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
    
  • /etc/profile 에 다음 내용 추가
    [[ -s "/usr/local/rvm/bin/rvm" ]] && source "/usr/local/rvm/bin/rvm"
    
  • rvm 정상설치 확인
    # source "/usr/local/rvm/bin/rvm"
    # type rvm | head -1
    rvm is a function
    
    위에서 rvm is a function 이 나오면 정상 설치된 것이다.
  • 설치할 수 있는 ruby 버전들 확인
    # rvm list known
    
  • 원하는 버전의 ruby 설치
    # rvm install 1.9.2
    # rvm --default use 1.9.2
    
  • ruby 버전 확인
    # ruby -v
    ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
    

CentOS 5.x 버전이 많은 곳에서 사용되고 있지만, 패키지들이 너무 오래전 것들이라, 실전에 적용하기가 그리 만만치 않다. 이번에는 최신 웹프레임워크로 인기(?)를 끌고 있는 Play Framework를 사용할 수 있는 방법을 기술해보았다. 다른 것은 특별히 문제가 없는데, python 버전 한가지 말썽이다. ^^ 해결책은 간단하니, 한번 보기 바란다. 빨리, CentOS 6.0 이 출시되기만을 바랄뿐이다.

  • 먼저 Play Framework는 Java기반 프레임워크이므로 OpenJDK를 설치한다.
    # yum install java-1.6.0-openjdk java-1.6.0-openjdk-devel
    
  • http://playframework.org에서 play-1.1.1.zip 을 다운로드 받고, 적당한 곳(/opt/play)에 압축을 푼다. 그리고, PATH 환경 변수에 등록한다.
  • Play 의 관리툴은 Python 으로 되어 있는데, 버전 2.5 이상이어야만 한다. 그런데, CentOS 에 설치되어 있는 Python 은 2.4 이다.
  • 두가지 버전의 Python을 사용하기 위해서 이렇게 하자.
    - 기존에 설치된 Python 2.4 는 root 에서, CentOS 관리툴을 위한 것으로 사용하며,
    - 새로 설치할 Python 은 새로운 계정을 만들어서 사용하며, 새로운 계정으로 play 프레임워크 프로젝트를 만들고, 실행하는 데 사용한다.
  • 새로운 계정 play를 만들고, 이 계정으로 로그인 한다. 당연히 /opt/play 디렉토리가 PATH에 등록이 되어 있어야 한다.
  • 새로운 계정 play로 로그인 한 후에, http://python.org 에서 2.5 이상의 원하는 python 소스 파일을 다운로드 하여 적당한 위치에 풀어놓는다.
  • 풀려진 디렉토리에 들어가서 설치를 진행하자. 소스 설치를 진행하려면, 당연히 gcc, make 등은 미리 설치되어 있어야 하겠지?
    $ configure --prefix=/opt/python
    $ make; make install
    
  • 마지막으로 /opt/python 을 PATH의 맨 앞쪽으로 추가시킨다.
  • 이제 명령창에서 play 라고 입력해보자. 다음과 같이 나오면 성공이다. ^^
    $ play
    ~        _            _
    ~  _ __ | | __ _ _  _| |
    ~ | '_ \| |/ _' | || |_|
    ~ |  __/|_|\____|\__ (_)
    ~ |_|            |__/
    ~
    ~ play! 1.1.1, http://www.playframework.org
    ~
    ~ Usage: play cmd [app_path] [--options]
    ~
    ~ with,  new      Create a new application
    ~        run      Run the application in the current shell
    ~        help     Show play help
    ~
    

+ Recent posts