systemadmin.es > LAMP y web > Instalación de un servidor LAMP (apache con PHP y XCache)

Instalación de un servidor LAMP (apache con PHP y XCache)

Para instalar PHP con Apache en un entorno de producción se suele acompañar de un opcode cache para evitar el overhead de parsear y compilar el código PHP a cada petición. Otras veces hemos visto cómo instalar eAccelerator pero dicho proyecto se ha quedado parado por lo que hoy lo veremos con XCache sobre CentOS 6:

Primero deberemos instalar Apache, lo descargamos y descomprimimos:

cd /usr/local/src
yum install zlib-devel -y
wget http://apache.rediris.es//httpd/httpd-2.2.21.tar.gz
tar xzf httpd-2.2.*.tar.gz
cd httpd-2.2.*

A continuación lo compilamos con las opciones más comunes y deshabilitando algunos módulos como autoindex:

./configure --prefix=/usr/local/apache22 --exec-prefix=/usr/local/apache22 --enable-so --disable-autoindex --enable-rewrite --enable-deflate --enable-expires --disable-asis --disable-userdir
make && make install
useradd apache
sed -i 's%^#Include conf/extra/httpd-default.conf%Include conf/extra/httpd-default.conf%' /usr/local/apache22/conf/httpd.conf
sed -i 's%^#Include conf/extra/httpd-vhosts.conf%Include conf/extra/httpd-vhosts.conf%' /usr/local/apache22/conf/httpd.conf
sed -i 's/^User daemon/User apache/' /usr/local/apache22/conf/httpd.conf
sed -i 's/^Group daemon/Group apache/' /usr/local/apache22/conf/httpd.conf
sed -i 's%^#Include conf/extra/httpd-mpm.conf%Include conf/extra/httpd-mpm.conf%' /usr/local/apache22/conf/httpd.conf
sed -i 's%^#Include conf/extra/httpd-info.conf%Include conf/extra/httpd-info.conf%' /usr/local/apache22/conf/httpd.conf
sed -i 's/^ServerSignature On/ServerSignature Off/' /usr/local/apache22/conf/extra/httpd-default.conf
sed -i 's/^ServerTokens Full/ServerTokens Prod/' /usr/local/apache22/conf/extra/httpd-default.conf
sed -i 's/^KeepAliveTimeout 5/KeepAliveTimeout 2/' /usr/local/apache22/conf/extra/httpd-default.conf
sed -i 's/^MaxKeepAliveRequests 100/MaxKeepAliveRequests 512/' /usr/local/apache22/conf/extra/httpd-default.conf
echo "TraceEnable off" >> /usr/local/apache22/conf/httpd.conf
echo "AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/x-javascript text/xml" >> /usr/local/apache22/conf/httpd.conf
cat > /usr/local/apache22/conf/extra/httpd-vhosts.conf <<EOF
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

# HERE BE DRAGONS
<VirtualHost *:80>

 DocumentRoot /var/www/void

 <Directory /var/www/void>
  Options FollowSymLinks
  AllowOverride None
  Order deny,allow
  Allow from all
 </Directory>

 Include conf/extra/httpd-info.conf

</VirtualHost>

Include conf/extra/vhosts/*.conf
EOF
cat > /usr/local/apache22/conf/extra/httpd-info.conf <<EOF

<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>

EOF
echo "ExtendedStatus On" >> /usr/local/apache22/conf/httpd.conf
mkdir -p /usr/local/apache22/conf/extra/vhosts
mkdir -p /var/www/void 

Para la gestión de los logs podemos usar cronolog para que se roten automáticamente:

cd /usr/local/src
wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
tar xzf cronolog-1.6.2.tar.gz
svn export http://nagios-systemadmin.googlecode.com/svn/trunk/patchs/cronolog-syslog-onrotate.patch /usr/local/src/cronolog-onrotate-syslog.patch
cd cronolog-1.6.2/
patch -p1 < ../cronolog-onrotate-syslog.patch
./configure && make && make install

A continuación instalamos PHP 5.3, actualmente PHP 5.2 ya se encuentra sin soporte:

yum install libxml2-devel -y
yum install openssl-devel -y
yum install libxslt-devel -y
yum install curl-devel -y
yum install libjpeg-devel -y
yum install libmcrypt-devel -y
yum install libjpeg-devel -y
yum install libpng-devel -y
yum install libtool-ltdl-devel -y
yum install libc-client-devel -y
yum install gmp-devel -y
yum install expat-devel -y
cd /usr/local/src
wget http://fr.php.net/get/php-5.3.8.tar.gz/from/this/mirror
tar xzf php-5.3.8.tar.gz 
cd php-5.3.8
./configure --with-apxs2=/usr/local/apache22/bin/apxs --with-mysql=/usr/local/mysql/ \
            --with-config-file-path=/usr/local/apache22/conf/ --with-zlib --with-gd \
            --with-jpeg-dir=/usr/ --with-gettext --with-mcrypt --with-iconv \
            --enable-mbstring=all --enable-mbregex --with-png-dir=/usr \
            --with-libdir=lib64
make && make install
cp php.ini-production /usr/local/apache22/conf/php.ini
mkdir /var/www/php/sessions -p
mkdir /var/www/php/tmp -p
chown -R apache. /var/www/php
sed -i 's%^;session.save_path = \"/tmp\"%session.save_path = \"/var/www/php/sessions\"%' /usr/local/apache22/conf/php.ini
sed -i 's%^;upload_tmp_dir =%upload_tmp_dir = /var/www/php/tmp%' /usr/local/apache22/conf/php.ini
sed -i 's#libphp5.so$#libphp5.so\n<FilesMatch \\.php\$>\nSetHandler application/x-httpd-php\n</FilesMatch>\n#' /usr/local/apache22/conf/httpd.conf
sed 's#expose_php = On#expose_php = Off#g' -i /usr/local/apache22/conf/php.ini
sed 's/;\(date.timezone =\)/\1"Europe\/Andorra"/g' -i /usr/local/apache22/conf/php.ini

Finalmente instalamos XCache de una forma muy similar a la que se hacía con eAccelerator:

cd /usr/local/src/
wget http://xcache.lighttpd.net/pub/Releases/1.3.2/xcache-1.3.2.tar.gz
tar xzf xcache-1.3.2.tar.gz
cd xcache-1.3.2
phpize
./configure --enable-xcache
make && make install

Para terminar la instalación de XCache definimos algunas variables

export XCACHETMP=$(mktemp /tmp/xcache.XXXXXXXXXXXXXXXXXXX)
pwgen 20 > $XCACHETMP
cat <<EOF >> /usr/local/apache22/conf/php.ini
[xcache-common]
;; install as zend extension (recommended), normally "$extension_dir/xcache.so"
zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20090626/xcache.so

[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "xcacheadmin"
; xcache.admin.pass = md5($your_password)
xcache.admin.pass = "$(echo -n "$(cat $XCACHETMP)" | md5sum | awk '{ print $1 }')" ;password: $(echo -n "$(cat $XCACHETMP)")

[xcache]
; ini only settings, all the values here is default unless explained

; select low level shm/allocator scheme implemenation
xcache.shm_scheme =        "mmap"
; to disable: xcache.size=0
; to enable : xcache.size=64M etc (any size > 0) and your system mmap allows
xcache.size  =               128M
; set to cpu count (cat /proc/cpuinfo |grep -c processor)
xcache.count =                 $(cat /proc/cpuinfo |grep -c processor)
; just a hash hints, you can always store count(items) > slots
xcache.slots =                16K
; ttl of the cache item, 0=forever
xcache.ttl   =                 0
; interval of gc scanning expired items, 0=no scan, other values is in seconds
xcache.gc_interval =           0

; same as aboves but for variable cache
xcache.var_size  =            4M
xcache.var_count =             $(cat /proc/cpuinfo |grep -c processor)
xcache.var_slots =            8K
; default ttl
xcache.var_ttl   =             0
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300

xcache.test =                Off
; N/A for /dev/zero
xcache.readonly_protection = Off
; for *nix, xcache.mmap_path is a file path, not directory.
; Use something like "/tmp/xcache" if you want to turn on ReadonlyProtection
; 2 group of php won't share the same /tmp/xcache
; for win32, xcache.mmap_path=anonymous map name, not file path
xcache.mmap_path =    "/dev/zero"


; leave it blank(disabled) or "/tmp/phpcore/"
; make sure it's writable by php (without checking open_basedir)
xcache.coredump_directory =   ""

; per request settings
xcache.cacher =               On
xcache.stat   =               On
xcache.optimizer =           Off

[xcache.coverager]
; per request settings
; enable coverage data collecting for xcache.coveragedump_directory and xcache_coverager_start/stop/get/clean() functions (will hurt executing performance)
xcache.coverager =          Off

; ini only settings
; make sure it's readable (care open_basedir) by coverage viewer script
; requires xcache.coverager=On
xcache.coveragedump_directory = ""
EOF
rm -f $XCACHETMP
unset XCACHETMP

A continuación podemos arrancar el apache con upstart:

cat <<EOF > /etc/init/apache22.conf
author      "systemadmin.es"
start on runlevel 3

respawn

exec /usr/local/apache22/bin/httpd -DNO_DETACH -f /usr/local/apache22/conf/httpd.conf
EOF

Y lo arrancamos con initctl:

initctl start apache22

Podemos comprobar que se ha instalado el XCache con php -v:

# php -v
PHP 5.3.9 (cli) (built: Jan 22 2012 12:30:35)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with XCache v1.3.2, Copyright (c) 2005-2011, by mOo

Deja un comentario:

XHTML - Tags permitidos:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>