웹마스터 팁

APM이라는 말이 널리 이용될정도로 Apache + PHP + MySQL을 이용한 웹서비스들이 많이 생겼습니다.

Apache라는 웹서버는 초기 강력한 기능등으로 웹서버의 카테고리 킬러로 통할정도로 높은 점유율을 보유하고 있었습니다.

그런데 이 Apache가 범용적인 기능을 많이 보유하고 있고 또 메모리나 자원 점유율이 높은 편입니다.

아파치 1.x는 process단위이고 2.x는 thread를 이용하여 조금더 나아졌지만 자원 소모량등이 많은 것은 여전합니다.

이에 반해 lighttpd 는 non-blocking I/O로 단일 프로세스에서 실행되어 보다 가볍고 빠르다는 장점이 있습니다.

그리고 PHP를 다룰때 fastcgi기반으로 사용하기에 아파치의 mod_php와 비슷하거나 훨씬 빨라졌습니다.

lighttp는 apache의 모듈을 거의 다 쓸수 있다고 하네요. (실제 제게 필요한 모듈들은 다 있습니다)

lighttp에 php (enable fastcgi)를 연동하고 최근 각광받는 xcache를 이용하여 기존의 아파치+PHP보다 가볍고 빠른 웹서버 구축에 대해서 간단히 글을 쓰겠습니다.


  1. php 설치
    여기서는 php 5.2.0 을 기준으로 설치하도록 하겠습니다.
    ./configure의 옵션은 적당히 알아서 해주시면 됩니다. ( 역슬래시는 줄나눔의 의미이니 주의하세요)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    tar xvfz php-5.2.0.tar.gz
    cd php-5.2.0
    ./configure
    --prefix=/usr/local/php 
    --with-exec-dir=/usr/bin 
    --with-mysql 
    --with-sqlite 
    --with-config-file-path=/etc 
    --disable-debug 
    --enable-sockets 
    --with-png-dir=/usr/lib 
    --with-freetype-dir=/usr/include/freetype2 
    --enable-mod-charset 
    --enable-calendar 
    --enable-sysvsem=yes 
    --enable-sysvshm=yes 
    --enable-ftp 
    --enable-magic-quotes 
    --enable-gd-native-ttf 
    --enable-inline-optimization 
    --enable-bcmath 
    --with-zlib 
    --with-jpeg-dir=/usr/src 
    --with-gd 
    --with-ttf 
    --with-gettext 
    --enable-sigchild 
    --with-libxml-dir=/usr/local/src/libxml2-2.6.11 
    --with-zlib-dir=/usr/local/src/zlib-1.2.1 
    --with-iconv 
    --enable-fastcgi
    --enable-force-cgi-redirect
    make
    make install


    이렇게 하면 php의 설치가 완료됩니다.
    --enable-fastcgi, --enable-force-cgi-redirect는 필수입니다.

    php.ini 파일은 /etc 디렉토리에 있는 것으로 설정하였습니다.
    Zend Optimizer, eAccelerator등의 설치에 대해서는 소개하지 않습니다. :)
  2. xcache 설치
    xcache는 제로보드XE와 같은 class/object기반의 프로그램들을 보다 빠르고 가볍게 해주는 캐쉬 프로그램입니다.
    APC나 eaccelerator 속도도 더 빠르다고 알려져 있구요.
    php5.2.0에 돌아가는 xcache 1.2.1 을 기준으로 설치법을 알려드립니다.

    wget http://xcache.lighttpd.net/pub/Releases/1.2.1/xcache-1.2.1.tar.gz
    tar xvfz xcache-1.2.1.tar.gz
    cd xcache-1.2.1
    phpize
    ./configure --enable-xcache --enable-xcache-coverager
    make
    make install
    cat xcache.ini >> /etc/php.ini

    위에서 phpize나 configure시에 php 실행파일들의 경로 문제가 생길 수 있습니다.
    1번처럼 php를 /usr/local/php/bin 에 실행파일이 생기게 했다면 아래와 같이 미리 PATH를 지정해주세요.

    export PATH="$PATH:/usr/local/php/bin"

    그 다음 /etc/php.ini 파일을 여세요.
    만약 zend optimizer등을 설치하였다면 xcache 설정중 zend_extension이 zend optimizer보다 위에 있어야 합니다.

    저는 아래와 같이 설정하였습니다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    [xcache-common]
    zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so
    [xcache.admin]
    xcache.admin.auth = On
    xcache.admin.user = "관리자 아이디"
    xcache.admin.pass = "md5 hash 비밀번호"
    [xcache]
    xcache.shm_scheme =        "mmap"
    xcache.size  =                64M
    xcache.count =                 4
    xcache.slots =                8K
    xcache.ttl   =                 0
    xcache.gc_interval =           0
    xcache.var_size  =            64M
    xcache.var_count =             4
    xcache.var_slots =            8K
    xcache.var_ttl   =             0
    xcache.var_maxttl   =          0
    xcache.var_gc_interval =     300
    xcache.test =                Off
    xcache.readonly_protection = On
    ;xcache.mmap_path =    "/tmp/xcache"
    xcache.mmap_path =    "/dev/zero"
    xcache.coredump_directory =   ""
    xcache.cacher =               On
    xcache.stat   =               On
    xcache.optimizer =           On
    [xcache.coverager]
    xcache.coverager =          On
    xcache.coveragedump_directory = ""



    위의 설정중 xcache.admin.user와 xcache.admin.pass는 xcache 관리자 페이지를 사용할 경우 입력해주면 됩니다.
    참고로 관리자 페이지는 xcache 소스중 admin 이라는 디렉토리에 있고 이 admin 디렉토리를 웹에서 접근할 수 있는 곳에 복사해주시면 됩니다.

    xcache.size나 xcache.var_size는 적절히 해주시면 됩니다.
    xcache.count와 xcache.var_count는 cpu process의 수를 적어주시면 됩니다.

    cat /proc/cpuinfo |grep -c processor

    위와 같이 명령어를 입력하면 프로세스의 수가 나옵니다.
  3. lighttp 설치
    • 공식사이트 : http://www.lighttpd.net
    • 설치방법 ( 2007년 12월 24일 현재 최신 버전 기준)
      1
      2
      3
      4
      5
      6
      7
      tar xvfz lighttpd-1.4.18.tar.gz
      cd lighttpd-1.4.13
      ./configure --with-pcre
      make
      make install
      cp doc/lighttpd.conf /etc

      위와 같이 하면 설치가 완료됩니다.
    • 설정 파일
      위 설치방법에서 lighttpd.conf 파일을 /etc 로 이동시켰습니다.
      vi /etc/lighttpd.conf 해서 수정 작업 들어갑니다.

      lighttpd의 경우 기본적으로 아파치에서 유용하게 사용되는 모듈들을 포함하고 있고 이를 사용하는 것은 lighttpd.conf 파일의 주석을 제거함으로 바로 사용가능합니다.

      일단 lighttpd.conf 파일의 제일 위 부분을 보면 아래와 같습니다.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      server.modules              = (
                                     "mod_rewrite",
      #                               "mod_redirect",
      #                               "mod_alias",
                                      "mod_access",
      #                               "mod_cml",
      #                               "mod_trigger_b4_dl",
                                     "mod_auth",
      #                               "mod_status",
      #                               "mod_setenv",
                                     "mod_fastcgi",
      #                               "mod_proxy",
                                     "mod_simple_vhost",
      #                               "mod_evhost",
      #                               "mod_userdir",
      #                               "mod_cgi",
                                     "mod_compress",
      #                               "mod_ssi",
      #                               "mod_usertrack",
      #                               "mod_expire",
      #                               "mod_secdownload",
      #                               "mod_rrdtool",
                                      "mod_accesslog"
                                    )

      # 은 주석을 의미합니다.
      즉 필요치 않은 모듈은#을 줄앞에 붙여줌으로서 사용하지 않도록 하면 됩니다.

      그외 설정은 아래를 따르면 됩니다.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      server.document-root = "/home/..../public_html"; # document root 지정
      server.error_log = "/var/log/lighttpd/error.log"; # 에러 로그 저장. 디렉토리 생성해주세요.
      index-file.name = ( "index.php", "index.html", "index.htm", "default.htm") # index file 지정
      mimetype.assign = ... # 그냥 두시면 됩니다.
      accesslog.filename = "/var/log/lighttpd/access.log"; # access log 저장
      url.access-deny = ( "~", ".inc") # 특정 파일 형식에 대해 접근 금지 시킬 수 있습니다.
      static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" ) # mod_fastcgi나 mod_cgi 사용시 지정될 파일입니다.
       
      # 아파치의 vhost와 같은 정의를 아래와 같이 쉽게 할 수 있습니다. 도메인에 대한 document-root 지정
      # 1개 이상의 vhost일 경우 아래 3라인을 복사해서 주죽 써나가시면 됩니다.
      # 좀 더 쉽게 하려면 가상호스트 모듈 mod_evhost를 이용하면 됩니다.
      $HTTP["host"] == "www2.zerophp.com" {
              server.document-root = "/home/DOMAINS/WWW"
      }
       
      # mod_evhost 사용시 vhost 지정
      # %3 는 subdomain 1의 이름입니다. 아래와 같은 패턴이 정의되어 있습니다.
      # %% => % sign
      # %0 => domain name + tld
      # %1 => tld
      # %2 => domain name without tld
      # %3 => subdomain 1 name
      # %4 => subdomain 2 name
      evhost.path-pattern = "/var/www/hosts/%3/"
       
      server.username = "nobody" # 웹서버가 사용한 uid
      server.groupname = "nobody" # 웹서버가 사용할 gif
       
      # 아래가 php의 fastcgi 연결 부분입니다.
      # 다른 부분은 그대로 두고 --enable-fastcgi, --enable-force-cgi-redirect 옵션으로 컴파일한 php 실행 파일 경로만 신경쓰시면 됩니다.
      fastcgi.server = ( ".php" => ( "localhost" => ( "socket" => "/var/run/lighttpd/php-fastcgi.socket", "bin-path" => "/usr/local/php/bin/php" )))


      거의 대부분 기본 설정을 쓰시면 됩니다.
      다만 사용하려는 모듈의 지정과 fastcgi.server, vhost 등 실제 값이 필요한 부분만 설정해주시면 됩니다.

      실행은 다음과 같이 하면 됩니다.

      lighttpd -f /etc/lighttpd.conf


일단 php with fastcgi, xcache, lighttpd 설치와 설정에 대해서 간단히 적었습니다.

더 상세히 적고 싶지만 저 역시 아직 공부하는 중이라 필요한 부분들만 적었습니다.

서버 관리를 하신다면 그렇게 어렵지 않을 거라 생각합니다.

참고로.. 제로보드XE에서 주소를 이쁘게 하는 mod_rewrite를 lighttpd에서도 쓸수 있는데 이걸 각 서브도메인별로 지정하는걸 모르겠네요.

일단 저는 lighttpd.conf에 지정해 놓았습니다.

먼저 server.module에서 mod_rewrite를 사용하게 해 놓으시구요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
url.rewrite = (
                "^/([a-zA-Z0-9_]+)/files/attach/images/(.*)" => "./files/attach/images/$2",
                "^/([a-zA-Z0-9_]+)/modules/(.*)" => "./modules/$2",
                "^/([a-zA-Z0-9_]+)/common/(.*)" => "./common/$2",
                "^/([a-zA-Z0-9_]+)/([[:digit:]]+)page$" => "./index.php?mid=$1&page=$2",
                "^/rss/([[:digit:]]+){0,14}/([[:digit:]]+){0,14}/([[:digit:]]+)$" => "./index.php?module=rss&act=rss&start_date=$1&end_date=$2&page=$3",
                "^/rss/([[:digit:]]+)$" => "./index.php?module=rss&act=rss&page=$1",
                "^/rss$" => "./index.php?module=rss&act=rss",
                "^/admin$" => "./index.php?module=admin",
                "^/([a-zA-Z0-9_]+)/api$" => "./index.php?mid=$1&act=api",
                "^/([[:digit:]]+)$" => "./index.php?document_srl=$1",
                "^/([[:digit:]]+)/([a-zA-Z0-9_]+)$" => "./index.php?document_srl=$1&act=$2",
                "^/([[:digit:]]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$" => "./index.php?document_srl=$1&act=$3&key=$2",
                "^/([a-zA-Z0-9_]+)/([[:digit:]]+)$" => "./index.php?mid=$1&document_srl=$2",
                "^/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$" => "./index.php?mid=$1&act=$2",
                "^/([a-zA-Z0-9_]+)/page/([[:digit:]]+)$" => "./index.php?mid=$1&page=$2",
                "^/([a-zA-Z0-9_]+)/category/([[:digit:]]+)$" => "./index.php?mid=$1&category=$2",
                "^/([a-zA-Z0-9_]+)/category/([[:digit:]]+)/page/([[:digit:]]+)$" => "./index.php?mid=$1&category=$2&page=$3",
                "^/([a-zA-Z0-9_]+)/([[:digit:]]+)/([[:digit:]]+)$" => "./index.php?mid=$1&search_target=regdate&search_keyword=$2$3",
                "^/([a-zA-Z0-9_]+)/([[:digit:]]+)/([[:digit:]]+)/([[:digit:]]+)$" => "./index.php?mid=$1&search_target=regdate&search_keyword=$2$3$4",
                "^/([a-zA-Z0-9_]+)/tag/(.*)$" => "./index.php?mid=$1&search_target=tag&search_keyword=$2",
                "^/([a-zA-Z0-9_]+)/writer/(.*)$" => "./index.php?mid=$1&search_target=nick_name&search_keyword=$2",
                "^/([a-zA-Z0-9_]+)(/){0,1}$" => "./index.php?mid=$1" )




로 입력해 놓았습니다.

일단 제로보드XE가 각 서브도메인의 document root에 위치한다고 가정하고 적은것이고 잘 동작하더군요.

저도 어제부터 알아보기 시작한거라 더 유용하거나 상세한 내용 찾게 되면 추가하도록 하겠습니다.

태그 연관 글
  1. [2018/08/29] 묻고답하기 로그인해야만 사이트가 보여요 by sselang *2
  2. [2018/02/01] 묻고답하기 php 구문 오류라고 나오는데 어디가 잘못된건지 ㅠㅠ by babi****
  3. [2016/12/07] 묻고답하기 유튜브(YouTube) 모듈 반응형으로!!! by 힐디
  4. [2016/05/21] 묻고답하기 제로보드 코딩 관련 질문입니다ㅠㅠ 메뉴가 안 눌려요... by Ashleya9757
  5. [2016/03/24] 묻고답하기 폴더안 txt 문서를 php explode 하여 mysql 에 넣는 과정이 안됩니다ㅠㅠ by theaud****
제목 글쓴이 날짜
PDF 뷰어 팁 [4] 돼지코구뇽 2014.12.06
kakao Javascript SDK logout 컴박살 2015.02.25
특정 게시판에서는 스팸 ip 동작 안하게 하기 [4] Kxo 2015.02.22
htaccess 보안 팁 포럼위트 2015.02.24
300기가 Zboard4->XE 이전기 [14] file forest535 2015.02.13
스케치북 최신버전에서 미리 덧글 입력해두기 [5] file LI-NA 2015.02.18
추천 취소 기능 만들기 [5] file Summer 2013.09.11
xe core 설치 화면 오류 있습니다. 이렇게 바꿔주세요. [2] 한꼬마 2015.02.18
데이타 이전 시 xml 파일 임포트 속도 높이기 ^^ forest535 2015.02.17
jquery 외부로드하기 [22] DynamicLaser 2014.04.07
XE와 별도 프로그램 연동시 세션공유 [15] 똑디 2008.12.12
snoop가 안될때 curl 로 가져오기 [3] 한꼬마 2015.02.13
템플릿등에서 PHP 제어 구조(if, for, foreach)의 대체 문법 적용 [3] 총모아 2015.01.30
1.4.2.3 에서 1.4.3 이상 버전으로 업그레이드 시 백지화면 해결팁 [4] 2년후 2010.12.15
회원가입후 24시간 이후 글작성 가능하기 [2] 샵사이드 2015.02.09
인증메일을 gmail의 smtp로 보내기 [29] showjean 2012.07.17
XE 비회원이 귀찮게 이메일 홈페이지 입력 안하도록 하기 [7] jhrun 2012.12.16
제로보드4에서 xe로 이전시 갤러리 게시판 이미지 깨어지는 현상 해결방법 [2] 최르토스 2012.04.22
스케치북5 1.7.0 버전 모바일 게시판 스킨 댓글수 안나올때 [2] 키스투엑스이 2014.10.22
제가 쓰는 자동링크 (스킨수정) [4] okiz 2014.04.29