2016. 4. 5. 14:04

PHP 운영구성 관련 정리 #2

PHP 속도를 위해

OPcache + APCu 구성에서 고성능 PHP 환경을 구축합니다. OPcache은 기본 OP 코드의 캐시 기능을 지원하고 APCu는 사용자 데이터의 캐시 기능을 지원한다. 이전 APC는 두 기능이 하나가되어있었습니다 만, PHP5.5에서 네이티브 코드 캐시 Z​​end Opcache가 지원되게되었습니다.

PHP 빌드 설정

apache2 용 모듈 (prefork 용)도 함께 생성합니다. 이번에 구축하는 서버는 FastCGI에서 운영이기 때문에 C shared 옵션으로 컴파일 된 바이너리를 설치합니다. 스레드 안전 (Threads Safe)의 PHP는 아직 지원하지 않는 모듈 (PHP 확장)이 많이 있고, "Non Threads model"로 컴파일합니다. 스레드로부터 안전하게 보호하려면 "-enable-maintainer-zts"를 추가합니다. 스레드 안전에 컴파일하고 빌드하면 스레드 안전을 지원하지 않는 확장 (geos.c 등)을 나중에 추가하고 싶어도 컴파일 할 수 없기 때문에 매우 불편합니다. 불행히도 고기능 모듈을 컴파일 할 수없는 경우가 많아 곤란 때가 종종 있습니다.

미들웨어 빌드 정책에 대해

CentOS 에서는 개발 라이브러리 (xxx-devel.x86_64)는 yum으로 설치하도록합니다. 특히 "openssl"컴파일하고 빌드하면 최근 HeartBleed이라고 보안 취약점이 발견 된 경우에는 라이브러리를 포함하여 모든 다시 컴파일해야합니다. 버전이 맞지 않아 어쩔 수없이 라이브러리를 구축해야하는 케이스에는 예외로합니다. apache의 경우도 특별한 이유가 없으면 yum으로 설치하고 설정에서 보안 조치를 할 것을 권장합니다. 

PHP 빌드의 사전 준비

php 다운로드

빌드는 /opt/src 디렉토리에서 수행한다고 가정합니다. 
다음 작업은 모두 root로합니다.

su - cd /opt/src wget -q http://kr1.php.net/get/php-5.5.16.tar.gz/from/this/mirror -O php-5.5.16.tar.gz tar zxvf php-5.5.16.tar.gz

Shared Memory Allocation 컴파일 설치

세션을 공유 메모리에서 처리하면 성능이 향상됩니다. 이 라이브러리는 Threads Safe에 대응하지 않는 모듈입니다. Non Threads model에서 구축의 경우, 혹은 세션 처리가 전문 용도의 서버로 빌드 할 경우 고려할 가치가 있을지도 모릅니다. 이 확장의 설정은 여기서하지 않겠습니다.

wget ftp://ftp.ossp.org/pkg/lib/mm/mm-1.4.2.tar.gz
tar zxvf mm-1.4.2.tar.gz
cd mm-1.4.2
./configure --prefix=/usr --libdir='${prefix}/lib64'
make
make install
cd ../

세션은 아래와 같이 RAM 디스크에 마운트하고 설정하는 방법도 있습니다.

mkdir /tmp/ramdisk
chmod 777 /tmp/ramdisk
mount -t tmpfs -o size=512M tmpfs /tmp/ramdisk/
vi php.ini
session.save_path="/tmp/ramdisk"

Igbinary 모듈 설치

cd /opt/src/php-5.5.16/ext
wget -q https://github.com/phadej/igbinary/archive/master.zip -O igbinary.zip
unzip igbinary.zip
mv igbinary-master igbinary

Redis (remote dictionary server) 모듈 설치

Redis KVS 클라이언트 모듈

cd /opt/src/php-5.5.16/ext
wget -q https://github.com/nicolasff/phpredis/archive/master.zip -O phpredis.zip
unzip phpredis.zip
mv phpredis-master redis

APCu (APC User Cache) 모듈

cd /opt/src/php-5.5.16/ext
wget -q https://github.com/krakjoe/apcu/archive/master.zip -O apcu.zip
unzip apcu.zip
mv apcu-master apcu

PHP 컴파일 옵션 추가 설정

설치 한 모듈 (Extension)을 configure에 인식시킵니다.

cd /opt/src/php-5.5.16
./buildconf --force

옵션에 추가되어 있는지 확인합니다.

./configure --help | grep "redis\|igbinary\|apcu"
  --enable-igbinary          Enable igbinary support
  --enable-redis           Enable redis support
  --disable-redis-session      Disable session support
  --enable-redis-igbinary      Enable igbinary serializer support

igbinay/redis/apcu 소스 파일을 다운로드하여 phpize; ./configure;make; make install 명령으로도 설치가 가능합니다.

빌드 환경 구축

이미 설치되어 있으면 건너 뜁니다.

yum -y install autoconf
yum -y install automake
yum -y install libtool.x86_64
yum -y install flex.x86_64
yum -y install bison.x86_64
yum -y install gcc.x86_64
yum -y install gcc-c++.x86_64
yum -y install make.x86_64
yum -y install kernel-headers.x86_64
yum -y install kernel-devel.x86_64


PHP 빌드 환경 구축

이미 설치되어 있으면 건너 뜁니다. 
httpd-devel은 HTTPD 용 PHP 바이너리도 함께 생성 할 수 있습니다. 
라이브러리마다 일행하고있는 것은 쉽게 볼 수 있도록합니다.

yum install -y httpd-devel.x86_64
yum install -y libxml2-devel.x86_64
yum install -y bzip2-devel.x86_64
yum install -y libcurl-devel.x86_64
yum install -y libpng-devel.x86_64
yum install -y openjpeg-devel.x86_64
yum install -y freetype-devel.x86_64
yum install -y t1lib-devel.x86_64
yum install -y libXpm-devel.x86_64
yum install -y gd-devel.x86_64
yum install -y gmp-devel.x86_64
yum install -y libc-client-devel.x86_64
yum install -y libicu-devel.x86_64
yum install -y openldap-devel.x86_64
yum install -y libmcrypt-devel.x86_64
yum install -y mysql-devel.x86_64
yum install -y readline-devel.x86_64
yum install -y net-snmp-devel.x86_64
yum install -y libtidy-devel.x86_64
yum install -y libxslt-devel.x86_64
yum install -y libmcrypt-devel.x86_64
yum install -y libevent.x86_64
yum install -y libevent-devel.x86_64
yum install -y aspell-devel.x86_64
yum install -y enchant-devel.x86_64
yum install -y oniguruma-devel.x86_64
yum install -y unixODBC-devel.x86_64
yum install -y sqlite-devel.x86_64
yum install -y krb5-devel.x86_64

MySQL 서버 설치

yum install -y mysql-server.x86_64
서비스 등록
chkconfig mysqld on
서비스에서 삭제

사용하지 않는 경우는 서비스에서 제거해야합니다

chkconfig mysqld off
확인
chkconfig --list mysqld


MySQL 설정

cp /etc/my.cnf /etc/my.cnf.org

symbolic-links = 0 과 [mysqld_safe] 사이에 다음을 추가합니다.

vi /etc/my.cnf
character_set_server=utf8
default-storage-engine=InnoDB
innodb_file_per_table
[mysql]
default-character-set=utf8
[mysqldump]
default-character-set=utf8

서비스 시작 (root)

service mysqld start

명령도 실행 및 설정 단계 (초기 설정)

이 설정은 보안을 위해 반드시 실시합니다.

mysql_secure_installation

root 패스워드 → 그대로 Enter 
root 패스워드 설정 → Y Enter 
root 패스워드 입력 (2 회) → ******* Enter 
anonymous 사용자 삭제 → Y Enter 
원격 root 로그인 금지 → Y Enter 
test 데이터베이스 삭제 → Y Enter 
설정 반영 → Y Enter

로그인 확인

mysql -u root -p

암호 입력 Enter

mysql>\s

상기 설정 한 비밀번호로 로그인 할 수 있는지 확인 후 종료

mysql> exit

서비스를 중지합니다.

service mysqld stop

평소 사용하지 않는 때에는 자동시작 되지 않도록 서비스에서 off해둡니다.

chkconfig mysqld off

Redis 클라이언트와 서버 설치

현재에는 Memcached보다는 Redis 를 더 많이 사용하고 있습니다.

cd /opt/src

다운로드 및 설치

wget -q http://redis.googlecode.com/files/redis-2.6.14.tar.gz
tar xzf redis-2.6.14.tar.gz
cd redis-2.6.14
make -j10
make install

Redis 설정

rm -rf /etc/redis /var/lib/redis
mkdir /etc/redis /var/lib/redis
\cp src/redis-server src/redis-cli /usr/bin
\cp redis.conf /etc/redis
sed -e "s/^daemonize no$/daemonize yes/" -e "s/^# bind 127.0.0.1$/bind 127.0.0.1/" -e "s/^dir \.\//dir \/var\/lib\/redis\//" -e "s/^loglevel verbose$/loglevel notice/" -e "s/^logfile stdout$/logfile \/var\/log\/redis.log/" redis.conf > /etc/redis/redis.conf

Redis 서비스 스크립트 작성 및 설정

cat > "/etc/init.d/redis-server" <<'EOF'
#!/bin/sh
#
# redis-server        Startup script for the Redis Server
# 
# chkconfig:   - 85 15 
# description:  Redis is a persistent key-value database
# processname: redis-server
# config:      /etc/redis/redis.conf
# config:      /etc/sysconfig/redis
# pidfile:     /var/run/redis.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

redis="/usr/bin/redis-server"
prog=$(basename $redis)

REDIS_CONF_FILE="/etc/redis/redis.conf"

[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/var/lock/subsys/redis

start() {
    [ -x $redis ] || exit 5
    [ -f $REDIS_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $redis $REDIS_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    killproc $redis -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
EOF
퍼미션 변경 
chmod 755 /etc/init.d/redis-server
서비스 등록
chkconfig --add redis-server
chkconfig --level 345 redis-server on

또는

chkconfig redis-server on

런레벨에 대해

0 : 종료 (시스템 종료)
1 : 단일 사용자 모드 (root 만)
2 : 네트워크없이 다중 사용자 모드
3 : 일반 다중 사용자 모드 (텍스트 로그인)
4 : 사용하지 않는
5 : 그래픽 로그인 다중 사용자 모드
6 : 시스템 재부팅

redis 서비스 시작

service redis-server start
cd /opt/src

PostgreSQL 설치

PostgreSQL-9.3.4 yum을 기본 저장소에 없기 때문에 컴파일 설치합니다.PostgreSQL는 root 권한으로 실행할 수 없기 때문에 다음 사용자를 추가해야합니다. make시 옵션 "-j10"옵션을 추가하고 시간 절약을 위해 병렬로 컴파일합니다.

중요 : PostgreSQL 백엔드와 기능의 대부분이 C ++ 언어로 작성되어 있기 때문에 안정적인 운용을 위해서는 std c++ 라이브러리를 사용해야 합니다. 지금까지 PostgreSQL은 한번도 다운 된 경험이 없습니다. 매우 신뢰할 수있는 데이터베이스입니다.

GIS 서버 포트 빌드 등의 자세한 내용은 다음 URL을 참조

http://nextdeveloper.hatenablog.com/entry/2014/05/20/164605

사용자 확인 및 추가

yum install -y finger
id postgres
finger postgres

이미 추가되어있는 경우는 생략합니다.

useradd postgres

다운로드 및 기본 DB의 설치 (PHP 용 라이브러리 구축)

http://nextdeveloper.hatenablog.com/entry/2014/05/20/164605

cd /opt/src
wget -q http://ftp.postgresql.org/pub/source/v9.3.4/postgresql-9.3.4.tar.gz
tar zxvf postgresql-9.3.4.tar.gz
cd postgresql-9.3.4
LDFLAGS=-lstdc++ ./configure --prefix=/usr --libdir='${prefix}/lib64'
make -j10 world
make install-world
cd ..

컴파일 전에 확인

일단 구성을하고 make 명령을 실행했을 때 반드시 make clean; ./buildconf --force 를 실행 한 후 ./configure -> make 단계에서 컴파일합니다. 그래도 구성에서 컴파일 옵션이 인식되지 않는 경우는 소스를 삭제하고 처음부터 다시 시작하십시오.

PHP 컴파일 구축 1 (C 공유 빌드 / FastCGI 용 / Apache 모듈)

옵션 설정에서 -dir 옵션은 /usr/local/mysql처럼 설치된 디렉토리 (prefix 디렉토리)를 지정합니다. . --with-xxx 옵션이 실패한 경우, 라이브러리의 디렉토리도 함께 설정합니다. 여기에서는, PHP 바이너리는 C shared 컴파일 바이너리를 기본으로합니다. 설치되는 모듈은 /etc/php.d/xxxx.ini 라는 파일명으로 모듈 파일 (.so)를 개별적으로 설정할 수 있습니다. 여기에서 PHP Apache 모듈 바이너리를 생성합니다. 필요없는 경우, 아래의 컴파일 옵션 --with-apxs2 \ 를 제거합니다.

PHP 런타임 모듈 중에서도 세션이 먼저 실행되고 있기 때문에 "session.so"이로드되지 않으면이 모듈에 의존하는 Extenshon에서 "Undefined symbol"오류가 발생합니다. 따라서 session 모듈은 PHP 본체에 통합 정적으로 컴파일하여이 문제를 해결합니다.

구성을 실행하기까지, phpredis/igbinary/apcu 이 ./configure 에서 아래와 같이 인식되고 있는지를 확인합니다.

./buildconf --force
./configure --help | grep "redis\|igbinary\|apcu"
  --enable-apcu           Enable APCu support
  --disable-apcu-rwlocks  Disable rwlocks in APCu
  --enable-apcu-debug     Enable APCu debugging
  --enable-apcu-clear-signal  Enable SIGUSR1 clearing handler
  --disable-apcu-mmap     Disable mmap, falls back on shm
  --enable-apcu-spinlocks        Use spinlocks before flocks
  --enable-igbinary          Enable igbinary support
  --enable-redis           Enable redis support
  --disable-redis-session      Disable session support
  --enable-redis-igbinary      Enable igbinary serializer support

--enable-cgi 는 FastCGI 옵션으로 컴파일됩니다. 지정하지 않아도 기본적으로 설정됩니다. CGI / FastCGI의 두 인터페이스를 지원합니다.

./configure \
    --prefix=/usr \
    --exec-prefix=/usr \
    --bindir=/usr/bin \
    --sbindir=/usr/sbin \
    --sysconfdir=/etc \
    --datadir=/usr/share \
    --includedir=/usr/include \
    --libdir=/usr/lib64 \
    --libexecdir=/usr/libexec \
    --localstatedir=/var \
    --sharedstatedir=/usr/com \
    --mandir=/usr/share/man \
    --infodir=/usr/share/info \
    --with-apxs2 \
    --with-config-file-path=/etc \
    --with-config-file-scan-dir=/etc/php.d \
    --with-libdir=lib64 \
    --with-layout=GNU \
    --with-fpm-user=www-data \
    --with-fpm-group=www-data \
    --enable-apcu=shared \
    --enable-bcmath=shared \
    --enable-calendar=shared \
    --enable-cgi \
    --enable-cli \
    --enable-ctype=shared \
    --enable-dba=shared \
    --enable-dom=shared \
    --enable-exif=shared \
    --enable-fileinfo=shared \
    --enable-filter=shared \
    --enable-fpm \
    --enable-ftp=shared \
    --enable-gd-jis-conv \
    --enable-gd-native-ttf \
    --enable-igbinary=shared \
    --enable-inline-optimization \
    --enable-intl=shared \
    --enable-json=shared \
    --enable-mbstring=shared \
    --enable-opcache=shared \
    --enable-pdo=shared \
    --enable-phar=shared \
    --enable-pcntl=shared \
    --enable-posix=shared \
    --enable-re2c-cgoto \
    --enable-redis=shared \
    --enable-redis-session \
    --enable-redis-igbinary \
    --enable-session=static \
    --enable-shmop=shared \
    --enable-sigchild=shared \
    --enable-simplexml=shared \
    --enable-soap=shared \
    --enable-sockets=shared \
    --enable-sysvsem=shared \
    --enable-sysvshm=shared \
    --enable-sysvmsg=shared \
    --enable-tokenizer=shared \
    --enable-xml=shared \
    --enable-xmlreader=shared \
    --enable-xmlwriter=shared \
    --enable-zend-signals \
    --enable-zip=shared \
    --enable-wddx=shared \
    --disable-rpath \
    --disable-ipv6 \
    --disable-debug \
    --with-freetype-dir=/usr \
    --with-icu-dir=/usr \
    --with-jpeg-dir=/usr \
    --with-libxml-dir=/usr \
    --with-png-dir=/usr \
    --with-openssl-dir=shared,/usr \
    --with-xpm-dir=/usr \
    --with-zlib-dir=shared,/usr \
    --with-bz2=shared \
    --with-curl=shared,/usr \
    --with-db4=shared \
    --with-enchant=shared \
    --with-gd=shared \
    --without-gdbm \
    --with-gettext=shared \
    --with-gmp=shared \
    --with-iconv=shared \
    --with-imap=shared \
    --with-imap-ssl \
    --with-kerberos \
    --with-ldap=shared \
    --with-ldap-sasl \
    --with-mcrypt=shared \
    --with-mhash=shared \
    --with-mm=/usr \
    --with-mysql=shared,/usr \
    --with-mysqli=shared,/usr/bin/mysql_config \
    --with-pdo-mysql=shared,/usr/bin/mysql_config \
    --with-openssl=shared \
    --with-pear \
    --with-pic=shared \
    --with-pgsql=shared,/usr \
    --with-pdo-pgsql=shared,/usr \
    --with-pdo-sqlite=shared,/usr \
    --with-pdo-odbc=shared,unixODBC,/usr \
    --with-pcre-regex \
    --with-pspell=shared \
    --with-readline=shared \
    --with-snmp=shared \
    --with-t1lib=shared \
    --with-tidy=shared,/usr \
    --with-unixODBC=shared,/usr \
    --with-xmlrpc=shared \
    --with-xsl=shared,/usr \
    --with-zlib=shared

컴파일

make -j10

php-fpm 사용자 추가

useradd www-data

설치

make install &&
install -v -m644 php.ini-production /etc/php.ini &&
install -v -m644 sapi/fpm/php-fpm.conf /etc/php-fpm.conf &&
install -v -m755 sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

php.ini 설정 변경

sed -i 's/expose_php = On/expose_php = Off/g;' /etc/php.ini sed -i 's/\;date.timezone =/date\.timezone = Asia\/Seoul/g;' /etc/php.ini


C 공유 PHP 설치 로그

make install
Installing PHP SAPI module:       apache2handler
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' libphp5.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp libphp5.la /usr/lib64/httpd/modules/
libtool: install: cp .libs/libphp5.so /usr/lib64/httpd/modules/libphp5.so
libtool: install: cp .libs/libphp5.lai /usr/lib64/httpd/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /opt/src/php-5.5.16/libs'
chmod 755 /usr/lib64/httpd/modules/libphp5.so
[activating module `php5' in /etc/httpd/conf/httpd.conf]
Installing shared extensions:     /usr/lib64/20121212/
Installing PHP CLI binary:        /usr/bin/
Installing PHP CLI man page:      /usr/share/man/man1/
Installing PHP FPM binary:        /usr/sbin/
Installing PHP FPM config:        /etc/
Installing PHP FPM man page:      /usr/share/man/man8/
Installing PHP FPM status page:      /usr/share/fpm/
Installing PHP CGI binary:        /usr/bin/
Installing PHP CGI man page:      /usr/share/man/man1/
Installing build environment:     /usr/lib64/build/
Installing header files:          /usr/include/php/
Installing helper programs:       /usr/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/share/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/share/pear/
[PEAR] Archive_Tar    - already installed: 1.3.11
[PEAR] Console_Getopt - already installed: 1.3.1
[PEAR] PEAR           - already installed: 1.9.4
Wrote PEAR system config file at: /etc/pear.conf
You may want to add: /usr/share/pear to your php.ini include_path
[PEAR] Structures_Graph- already installed: 1.0.4
[PEAR] XML_Util       - already installed: 1.2.1
/opt/src/php-5.5.16/build/shtool install -c ext/phar/phar.phar /usr/bin
ln -s -f /usr/bin/phar.phar /usr/bin/phar
Installing PDO headers:          /usr/include/php/ext/pdo/

C 공유 PHP의 Extension 설정

쉘 스크립트에서 백지는 개별적으로 확인하기 쉽게하기 위해서입니다. 
내용을 확인 한 후 환경에 맞게 수정하십시오.

mkdir /etc/php.d
cat > "/etc/php.d/apcu.ini" <<EOF
extension=apcu.so
apc.enabled=1
apc.shm_size=128M
apc.enable_cli=1
apc.write_lock=1
apc.slam_defense=0
apc.gc_ttl=3600
apc.ttl=7200
apc.entries_hint=4096
apc.slam_defense=1
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.serializer=igbinary
EOF
echo -e extension=bcmath.so >/etc/php.d/bcmath.ini
echo -e extension=bz2.so >/etc/php.d/bz2.ini
echo -e extension=calendar.so >/etc/php.d/calendar.ini]
echo -e extension=ctype.so >/etc/php.d/ctype.ini
echo -e extension=curl.so >/etc/php.d/curl.ini
echo -e extension=dba.so >/etc/php.d/dba.ini
echo -e extension=dom.so >/etc/php.d/dom.ini
echo -e extension=enchant.so >/etc/php.d/enchant.ini
echo -e extension=exif.so >/etc/php.d/exif.ini
echo -e extension=fileinfo.so >/etc/php.d/fileinfo.ini
echo -e extension=ftp.so >/etc/php.d/ftp.ini
echo -e extension=gd.so >/etc/php.d/gd.ini
echo -e extension=gettext.so >/etc/php.d/gettext.ini
echo -e extension=gmp.so >/etc/php.d/gmp.ini
echo -e extension=iconv.so >/etc/php.d/iconv.ini
echo -e extension=igbinary.so >/etc/php.d/igbinary.ini
echo -e extension=imap.so >/etc/php.d/imap.ini
echo -e extension=intl.so >/etc/php.d/intl.ini
echo -e extension=json.so >/etc/php.d/json.ini
echo -e extension=ldap.so >/etc/php.d/ldap.ini
cat > "/etc/php.d/mbstring.ini" <<EOF
extension=mbstring.so
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.http_output = pass
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII
EOF
echo -e extension=mcrypt.so >/etc/php.d/mcrypt.ini
echo -e extension=mysql.so >/etc/php.d/mysql.ini
echo -e extension=mysqli.so >/etc/php.d/mysqli.ini
echo -e extension=odbc.so >/etc/php.d/odbc.ini

OPCache 프로덕션 설정

메모리를 어느 정도 포함하고있는 머신에서는 "opcache.memory_consumption"숫자를 늘립니다.

mkdir /var/lib/php
cat > "/etc/php.d/opcache.ini" <<EOF
zend_extension=opcache.so
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.max_file_size=5M
opcache.blacklist_filename=/var/lib/php/opcache*.blacklist
opcache.revalidate_freq=0
opcache.revalidate_path=0
opcache.save_comments=0
opcache.load_comments=0
opcache.consistency_checks=1
opcache.fast_shutdown=1
EOF
echo -e extension=openssl.so >/etc/php.d/openssl.ini
echo -e extension=pcntl.so >/etc/php.d/pcntl.ini
echo -e extension=pdo.so >/etc/php.d/pdo.ini
echo -e extension=pdo_mysql.so >/etc/php.d/pdo_mysql.ini
echo -e extension=pdo_odbc.so >/etc/php.d/pdo_odbc.ini
echo -e extension=pdo_pgsql.so >/etc/php.d/pdo_pgsql.ini
echo -e extension=pdo_sqlite.so >/etc/php.d/pdo_sqlite.ini
echo -e extension=pgsql.so >/etc/php.d/pgsql.ini
echo -e extension=phar.so >/etc/php.d/phar.ini
echo -e extension=posix.so >/etc/php.d/posix.ini
echo -e extension=pspell.so >/etc/php.d/pspell.ini
echo -e extension=readline.so >/etc/php.d/readline.ini
echo -e "extension=redis.so\nredis.serializer=igbinary" >/etc/php.d/redis.ini
echo -e extension=shmop.so >/etc/php.d/shmop.ini
echo -e extension=simplexml.so >/etc/php.d/simplexml.ini
echo -e extension=snmp.so >/etc/php.d/snmp.ini
echo -e extension=soap.so >/etc/php.d/soap.ini
echo -e extension=sockets.so >/etc/php.d/sockets.ini
echo -e extension=sysvmsg.so >/etc/php.d/sysvmsg.ini
echo -e extension=sysvmsg.so >/etc/php.d/sysvmsg.ini
echo -e extension=sysvshm.so >/etc/php.d/sysvshm.ini
echo -e extension=tidy.so >/etc/php.d/tidy.ini
echo -e extension=tokenizer.so >/etc/php.d/tokenizer.ini
echo -e extension=wddx.so >/etc/php.d/wddx.ini
echo -e extension=xml.so >/etc/php.d/xml.ini
echo -e extension=xmlreader.so >/etc/php.d/xmlreader.ini
echo -e extension=xmlrpc.so >/etc/php.d/xmlrpc.ini
echo -e extension=xmlwriter.so >/etc/php.d/xmlwriter.ini
echo -e extension=xsl.so >/etc/php.d/xsl.ini
echo -e extension=zip.so >/etc/php.d/zip.ini
echo -e extension=zlib.so >/etc/php.d/zlib.ini

Pecl 명령에서 모듈 추가 설치 예

다음은 필요없는 경우는 생략합니다.

pecl install -f oauth-beta

PHP 모듈의 컴파일 설치

다음은 필요없는 경우는 생략합니다. geos 라이브러리는 컴파일시에주의가 필요합니다. GIS 기반의 라이브러리는 버전에 따라 의존성이 다르고 같은 라이브러리가 이미 설치되어있는 경우에는 라이브러리의 디렉토리를 변경하여 부딪치지 않도록해야합니다. 컴파일 할 때 가장 안정되어있는 3.3.9 버전을 선택합니다.

geos 확장을 설치하면 geoPHP이라는 편리한 클래스를 사용할 수 있습니다. 이 라이브러리를 설치하여 PHP에서 GIS의 다양한 데이터 포맷을 취급 할 수있게됩니다.지도보기를 더 고기능 할 때 매우 편리합니다.

geoPHP 모듈

https://github.com/phayes/geoPHP/

Goes 확장 설치

wget http://download.osgeo.org/geos/geos-3.3.9.tar.bz2
tar -xvjf geos-3.3.9.tar.bz2
cd geos-3.3.9
./autogen.sh
./configure --enable-php
make clean
make -j20
make install

설치된 확장의 대상이 소스 디렉토리되어 있기 때문에 수정해야합니다. 다음과 같이 기존 링크를 제거하고 ldconfig 에 설치된 먼저로드되도록합니다.

mv src/.libs/libgeos-3.3.9.so src/.libs/libgeos-3.3.9.so_orig
mv capi/.libs/libgeos_c.so.1 capi/.libs/libgeos_c.so.1_orig

sudo echo -e /usr/local/lib >/etc/ld.so.conf.d/geos-x86_64.conf
sudo echo -e extension=geos.so >/etc/php.d/geos.ini
/sbin/ldconfig

[root@dev geos-3.3.9]# ldd /usr/lib64/20121212/geos.so
        linux-vdso.so.1 =>  (0x00007fff549ff000)
        libgeos_c.so.1 => /usr/local/lib/libgeos_c.so.1 (0x00007f2609235000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f2608e8f000)
        libgeos-3.3.9.so => /usr/local/lib/libgeos-3.3.9.so (0x00007f2608afb000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f26087f5000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f2608570000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f260835a000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003424400000)

※주의 ※

기존 OS는 yum으로 설치되어있는 "geos-devel" 패키지 3.3.2이기 때문에 부딪치지 않도록주의합니다. 컴파일 설치 한 라이브러리의 디렉토리가 ldd명령으로 /usr /local/lib/ 되어 있는지 확인합니다.

이상 여기까지가 PHP를 C shared 빌드 방법입니다. 위에서 컴파일은 대부분의 확장이 추가 된 PHP 패키지입니다. 확장에 필요없는 경우는 코멘트에로드하지 않도록하고 구축합니다.

PHP 컴파일 구축 2 (정적 빌드 / Apache2의 PHP 모듈 용)

mod_php 용으로 빌드 할 때 static으로 컴파일하는 것이 다소 빠르기 때문에 정적 빌드 방법도 아래에 추가합니다.  staitc php-fpm 바이너리가 생성됩니다. 옵션 설정에서 -dir 옵션은 /usr/local/mysql 처럼 설치된 디렉토리 (prefix 디렉토리)를 지정합니다. . --with-xxx 옵션이 실패한 경우, 라이브러리의 디렉토리도 함께 설정합니다. 모듈 (extension)를 추가하려면 동적 라이브러리 (.so)에 추가하고 ". ini"파일에서 설정합니다. --with-apxs2 옵션도 추가하고 Apache의 PHP 모듈 (libphp5.so) 바이너리를 생성합니다. Opcache는 정적으로 컴파일해서 동적 라이브러리로 설치되므로주의합니다. C 공유 빌드 한 경우 또는 필요없는 경우는 생략합니다.

cd /opt/src/php-5.5.16

phpredis / igbinary / apcu가 configure에서 아래와 같이 인식되고 있는지를 확인합니다.

./buildconf --force
./configure --help | grep "redis\|igbinary\|apcu"
  --enable-apcu           Enable APCu support
  --disable-apcu-rwlocks  Disable rwlocks in APCu
  --enable-apcu-debug     Enable APCu debugging
  --enable-apcu-clear-signal  Enable SIGUSR1 clearing handler
  --disable-apcu-mmap     Disable mmap, falls back on shm
  --enable-apcu-spinlocks        Use spinlocks before flocks
  --enable-igbinary          Enable igbinary support
  --enable-redis           Enable redis support
  --disable-redis-session      Disable session support
  --enable-redis-igbinary      Enable igbinary serializer support


./configure \
    --prefix=/usr \
    --exec-prefix=/usr \
    --bindir=/usr/bin \
    --sbindir=/usr/sbin \
    --sysconfdir=/etc \
    --datadir=/usr/share \
    --includedir=/usr/include \
    --libdir=/usr/lib64 \
    --libexecdir=/usr/libexec \
    --localstatedir=/var \
    --sharedstatedir=/usr/com \
    --mandir=/usr/share/man \
    --infodir=/usr/share/info \
    --with-apxs2 \
    --with-config-file-path=/etc \
    --with-config-file-scan-dir=/etc/php.d \
    --with-libdir=lib64 \
    --with-layout=GNU \
    --with-fpm-user=www-data \
    --with-fpm-group=www-data \
    --enable-fpm \
    --enable-inline-optimization \
    --enable-apcu \
    --enable-bcmath \
    --enable-calendar \
    --enable-cgi \
    --enable-cli \
    --enable-ctype \
    --enable-dba \
    --enable-dom \
    --enable-exif \
    --enable-fileinfo \
    --enable-filter \
    --enable-ftp \
    --enable-gd-jis-conv \
    --enable-gd-native-ttf \
    --enable-intl \
    --enable-igbinary \
    --enable-json \
    --enable-mbstring \
    --enable-opcache \
    --enable-pdo \
    --enable-phar \
    --enable-pcntl \
    --enable-posix \
    --enable-re2c-cgoto \
    --enable-redis=static \
    --enable-redis-session \
    --enable-redis-igbinary \
    --enable-session \
    --enable-shmop \
    --enable-sigchild \
    --enable-simplexml \
    --enable-soap \
    --enable-sockets \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-sysvmsg \
    --enable-tokenizer \
    --enable-xml \
    --enable-xmlreader \
    --enable-xmlwriter \
    --enable-zip \
    --enable-wddx \
    --enable-zend-signals \
    --disable-rpath \
    --disable-debug \
    --with-freetype-dir=/usr \
    --with-icu-dir=/usr \
    --with-jpeg-dir=/usr \
    --with-libxml-dir=/usr \
    --with-png-dir=/usr \
    --with-openssl-dir=/usr \
    --with-xpm-dir=/usr \
    --with-zlib-dir=/usr \
    --with-bz2 \
    --with-curl=/usr \
    --with-db4 \
    --with-enchant \
    --with-gd \
    --without-gdbm \
    --with-gettext \
    --with-gmp \
    --with-iconv \
    --with-imap \
    --with-imap-ssl \
    --with-kerberos \
    --with-ldap \
    --with-ldap-sasl \
    --with-mcrypt \
    --with-mhash \
    --with-mm=/usr \
    --with-mysql=/usr \
    --with-mysqli=/usr/bin/mysql_config \
    --with-pdo-mysql=/usr/bin/mysql_config \
    --with-onig \
    --with-openssl \
    --with-pear \
    --with-pic \
    --with-pgsql=/usr \
    --with-pdo-pgsql=/usr \
    --with-pdo-sqlite=/usr \
    --with-pdo-odbc=unixODBC,/usr \
    --with-pcre-regex \
    --with-pspell \
    --with-readline \
    --with-snmp \
    --with-t1lib \
    --with-tidy \
    --with-unixODBC=/usr \
    --with-xmlrpc \
    --with-xsl=/usr \
    --with-zlib

정적 PHP를 컴파일 설치

컴파일
make -j10
php-fpm 사용자 추가
useradd www-data
설치
make install &&
install -v -m644 php.ini-production /etc/php.ini &&
install -v -m644 sapi/fpm/php-fpm.conf /etc/php-fpm.conf &&
install -v -m755 sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
php.ini 설정 변경
sed -i 's/expose_php = On/expose_php = Off/g;' /etc/php.ini
sed -i 's/\;date.timezone =/date\.timezone = Asia\/Seoul/g;' /etc/php.ini

정적 PHP 설치 로그

make install all
Installing PHP SAPI module:       apache2handler
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' libphp5.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp libphp5.la /usr/lib64/httpd/modules/
libtool: install: cp .libs/libphp5.so /usr/lib64/httpd/modules/libphp5.so
libtool: install: cp .libs/libphp5.lai /usr/lib64/httpd/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /opt/src/php-5.5.16/libs'
chmod 755 /usr/lib64/httpd/modules/libphp5.so
[activating module `php5' in /etc/httpd/conf/httpd.conf]
Installing shared extensions:     /usr/lib64/20121212/
Installing PHP CLI binary:        /usr/bin/
Installing PHP CLI man page:      /usr/share/man/man1/
Installing PHP FPM binary:        /usr/sbin/
Installing PHP FPM config:        /etc/
Installing PHP FPM man page:      /usr/share/man/man8/
Installing PHP FPM status page:      /usr/share/fpm/
Installing PHP CGI binary:        /usr/bin/
Installing PHP CGI man page:      /usr/share/man/man1/
Installing build environment:     /usr/lib64/build/
Installing header files:          /usr/include/php/
Installing helper programs:       /usr/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/share/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/share/pear/
[PEAR] Archive_Tar    - already installed: 1.3.11
[PEAR] Console_Getopt - already installed: 1.3.1
[PEAR] PEAR           - already installed: 1.9.4
Wrote PEAR system config file at: /etc/pear.conf
You may want to add: /usr/share/pear to your php.ini include_path
[PEAR] Structures_Graph- already installed: 1.0.4
[PEAR] XML_Util       - already installed: 1.2.1
/opt/src/php-5.5.16/build/shtool install -c ext/phar/phar.phar /usr/bin
ln -s -f /usr/bin/phar.phar /usr/bin/phar
Installing PDO headers:          /usr/include/php/ext/pdo/

정적 PHP 설정 (Extension 설정)

정적 빌드에서도 opcache는 기본적으로 C 공유로 컴파일되므로 별도 설정해야합니다. 
extensions.ini 파일에 추가되는 모듈을 정리하고 설정하도록합니다

mkdir /var/lib/php
cat > "/etc/php.d/extensions.ini" <<'EOF'
;Opcache
zend_extension=opcache.so
[opcache]
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.max_file_size=5M
opcache.blacklist_filename=/var/lib/php/opcache*.blacklist
opcache.revalidate_freq=0
opcache.revalidate_path=0
opcache.save_comments=0
opcache.load_comments=0
opcache.consistency_checks=1
opcache.fast_shutdown=1

;apcu
apc.enabled=1
apc.shm_size=128M
apc.enable_cli=1
apc.write_lock=1
apc.slam_defense=0
apc.gc_ttl=3600
apc.ttl=7200
apc.entries_hint=4096
apc.slam_defense=1
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.serializer=igbinary

;mbstring
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.http_output = pass
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII

EOF