93
Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP [email protected]

Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP [email protected]

  • Upload
    others

  • View
    80

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Tuning Apache/MySQL/PHPpara Desenvolvedores

By Douglas V. PasquaZend Certified Engineer / LPI / SCJP

[email protected]

Page 2: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Objetivos

● Dicas de Tuning para Apache/MySQL e PHP. ● Parâmetros de configuração para tuning de Apache e MySQL ● Instalação e configuração de ferramentas de tuning. ● Análise de códigos PHP.● Identifcar Gargalos.● Voltado para desenvolvedores.

Page 3: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Nota do Google sobre Tuning

"Speeding up websites is important - not just to site owners, but to all Internet users. Faster sites create happy users and we´ve seen in our internal studies the when a site responds slowly, visitors spend less time there. But faster sites don´t just improve user experience; recent data shows that improving site speed also reduces operating costs. Like us, our users place a lot of value in speed - that´s why we´ve decided to take site speed int account in our search rankings. We use a variety of sources to determine the speed of a site relative to other sites". - Google9 Abril 2010

Page 4: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Tópicos Apache

● AllowOverride● ExtendedStatus ● SymLinks● KeepAlive● MaxClients

Page 5: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Tópicos MySQL

● Monitorando consultas lentas● max_connections● table_cache● query_cache_size● thread_cache_size

Page 6: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Tópicos PHP

● Identificando gargalos em aplicação PHP● memcached● Opções de aceleradores de código php● Usando APC (Alternative PHP Cache)● Caching com Zend_Cache● PHP e Computação em Nuvem

Page 7: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache

● AllowOverride● ExtendedStatus ● SymLinks● KeepAlive● MaxClients

Page 8: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, AllowOverride

● Permite sobrescrever configurações do Apache através dos arquivos .htaccess:

AuthUserFile /home/pathto/.htpasswd AuthType Basic AuthName "Secret Place"

<LIMIT GET POST> require valid-user </LIMIT>

Page 9: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, AllowOverride

● Caso habilitado, Apache terá que checar por arquivos .htaccess em todas requisições.

● Sempre que possível, manter desabilitado.

DocumentRoot /var/www <Directory /> AllowOverride None </Directory>

Page 10: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, AllowOverride

● Transportar as configurações de .htaccess para o arquivo de configuração principal

<Directory /var/www/protegido> AuthUserFile /home/pathto/.htpasswd AuthType Basic AuthName "Secret Place"

<LIMIT GET POST> require valid-user </LIMIT> </Directory>

Page 11: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache

● AllowOverride● ExtendedStatus ● SymLinks● KeepAlive● MaxClients

Page 12: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, ExtendedStatus

● Conhece o mod_status do Apache ?

Page 13: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, ExtendedStatus

● Manter o parâmetro ExtendedStatus -> Off

Page 14: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, ExtendedStatus

● status.conf: <IfModule mod_status.c> <Location /server-status> SetHandler server-status Allow from all </Location> ExtendedStatus Off <IfModule mod_proxy.c> ProxyStatus On </IfModule> </IfModule>

Page 15: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache

● AllowOverride● ExtendedStatus ● SymLinks● KeepAlive● MaxClients

Page 16: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, SymLinks

● Diz ao Apache para seguir/não seguir links simbólicos.

● Manter desabilitado faz com que o Apache realize checagens extras. (verificando se o arquivo acessado é um link ou não)

DocumentRoot /www/htdocs <Directory /> Options FollowSymLinks </Directory>

Page 17: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache

● AllowOverride● ExtendedStatus ● SymLinks● KeepAlive● MaxClients

Page 18: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, KeepAlive

● Habilita conexões persistentes no Apache. Manter habilitado para uma melhor perfomance.

KeepAlive On

Page 19: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, KeepAlive

● Habilita conexões persistentes no Apache. Manter habilitado para uma melhor perfomance.

KeepAlive On

● MaxKeepAliveRequest, quantidade de conexões efetuadas por conexão. Manter um valor alto para melhor perfomance.

MaxKeepAliveRequests 10000

Page 20: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, KeepAlive

● Habilita conexões persistentes no Apache. Manter habilitado para uma melhor perfomance.

KeepAlive On

● MaxKeepAliveRequest, quantidade de conexões efetuadas por conexão. Manter um valor alto para melhor perfomance.

MaxKeepAliveRequests 10000

● KeepAliveTimeout, número de segundos para aguardar nova conexão KeepAliveTimeout 3

Page 21: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache

● AllowOverride● ExtendedStatus ● SymLinks● KeepAlive● MaxClients

Page 22: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, MaxClients

● Determina o limite máximo de conexões simultâneas no Apache.

● Determine o valor de acordo com os recursos do servidor. MaxClients ≈ (RAM - memória outros processos) / (média do processo apache)

Page 23: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Apache, MaxClients

MaxClients ≈ (RAM - memória outros processos) / (média do processo apache)

● RAM # free -m (6109184) ● Média de Memória do Apache

# ps -ylC apache2 --sort:rss (12000) ● Reservar memória para outros processos

819200

MaxClients = (6109184 - 819200) / 12000 MaxClients = 441

Page 24: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL

● Monitorando consultas lentas● max_connections● table_cache● query_cache_size● thread_cache_size

Page 25: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, Consultas Lentas

● Habilitar o log de consultas lentas permite identificar gargalos de maneira simples.

● Para Habiltiar, editar o my.cnf e acrescentar:

[mysqld] ... log-slow-queries long_query_time = 1 log-slow-queries = /var/log/mysql-slow.log

Page 26: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, Consultas Lentas

● Exemplo de Log /var/log/mysql-slow.log: # Time: 110406 17:00:00 # Query_time: 9 Lock_time: 0 Rows_sent: 1 Rows_examined: 5643851 select count(*) from backuplog;

Page 27: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL

● Monitorando consultas lentas● max_connections● table_cache● query_cache_size● thread_cache_size

Page 28: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, max_connections

● Determina o número máximo de conexões simultâneas no MySQL.

● Aumentar de acordo com a necessidade.

● Receber “Too many connections” significa que excedeu esse

limite.

● Aumentar esse parâmetro influência:○ Quantidade de Memória disponível.○ Quantidade de memória usada por cada conexão. ○ Número de file descriptors abertos.

Page 29: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, max_connections

● Para aumentar o número de conexões simultâneas: [mysqld] ... max_connections = 200

● Para diagnósticos das conexões em processo no momento: mysql> show processlist\G

Page 30: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL

● Monitorando consultas lentas● max_connections● table_cache● query_cache_size● thread_cache_size

Page 31: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, table_cache

● Toda vez que o MySQL abre uma tabela, coloca ela em cache.

● table_cache define o número de tabelas abertas em cache.

● Está diretamente relacionado com o parâmetro max_connections: table_cache = max_connections * n

● n é o número máximo de tabelas usadas em relação à todas queries do seu sistema.

Page 32: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, table_cache

● table_cache é limitado pelo número de file descriptors disponiveis pelo S.O.

● Verificar número de file descriptors do S.O. (Linux):

# cat /proc/sys/fs/file-max

Page 33: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL

● Monitorando consultas lentas● max_connections● table_cache● query_cache_size● thread_cache_size

Page 34: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, query_cache_size

● Quantidade de memória alocada para guardar resultado das queries.

● Por padrão o valor é 0 (Desabilitado).

● Os valores para esse parâmetro devem ser múltiplos de 1024.

● Cuidado para não setar para um valor muito alto. As threads

precisam fazer lock no cache durante updates.

● O valor mínimo é 40Kb, para alocação das estruturas.

Page 35: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, query_cache_size

[mysqld] ... query_cache_size = 128M

Page 36: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL

● Monitorando consultas lentas● max_connections● table_cache● query_cache_size● thread_cache_size

Page 37: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, thread_cache_size

● Quantidade de threads em cache. (Conexões persistentes).

● Diminuir o máximo possível a criação de novas threads.

● Equação para testar a eficiência do cache: 100 - ((Threads_created / Connections) * 100) = 99%

Page 38: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, thread_cache_size

● Equação para testar a eficiência do cache: 100 - ((Threads_created / Connections) * 100) Threads_created Número de Threads criadas desde que o MySQL foi iniciado.

Connections Número total de conexões desde que o MySQL foi iniciado.

Page 39: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, thread_cache_size

Threads_created mysql> SHOW STATUS LIKE '%threads_created%';+------------------------+-------+| Variable_name | Value |+------------------------+-------+| Threads_created | 636 |+------------------------+-------+1 row in set (0.00 sec) Threads_created: 636

Page 40: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, thread_cache_size

Connections: mysql> SHOW STATUS LIKE '%connections%';+----------------------------+------------+| Variable_name | Value |+----------------------------+------------+| Connections | 24340093 || Max_used_connections | 636 |+----------------------------+------------+2 rows in set (0.00 sec) Connections: 24340093

Page 41: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

MySQL, thread_cache_size

Threads_created: 636 Connections: 24340093 100 - ((636 / 24340093) * 100) = 99,99%

Page 42: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP

● Identificando gargalos em aplicação PHP● memcached● Opções de aceleradores de código php● Usando APC (Alternative PHP Cache)● Caching com Zend_Cache● PHP e Computação em Nuvem

Page 43: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

● O Profiler do xdebug permite identificar gargalhos e/ou partes mais lentas de sua aplicação.

● Ideal para saber em quais pontos você pode melhor o

desempenho.

Page 44: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

● Habilitando o Profiler do xdebug no PHP: $ sudo apt-get install php5-xdebug

Page 45: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

● Habilitando o Profiler do xdebug no PHP: $ sudo apt-get install php5-xdebug

● Editando o arquivo /etc/php5/apache2/conf.d/xdebug.ini: zend_extension=/usr/lib/php5/20090626/xdebug.so xdebug.profiler_enable = 1 xdebug.profiler_output_dir = /log/xdebug

Page 46: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

● Habilitando o Profiler do xdebug no PHP: $ sudo apt-get install php5-xdebug

● Editando o arquivo /etc/php5/apache2/conf.d/xdebug.ini: zend_extension=/usr/lib/php5/20090626/xdebug.so xdebug.profiler_enable = 1 xdebug.profiler_output_dir = /log/xdebug

● Finalizando: $ sudo chown www-data /log/xdebug $ sudo service apache2 restart

Page 47: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

Page 48: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

● Precisamos de um software para ajudar na análise dos logs do xdebug, webgrind

● Instalando WebGrind

$ cd /var/www $ sudo wget http://webgrind.googlecode.com/files/webgrind-release-1.0.zip $ unzip webgrind-release-1.0.zip

Page 49: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

●Configurando webgrind ● config.php:

○ $storageDir - Onde será armazenado os arquivos já processados. ○ $profileDir - Arquivos de log gerados pelo xdebug○ $defaultTimezone - Configuração do fuso horário,

America/Sao_Paulo

Page 50: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Profiler

Page 51: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP

● Identificando gargalos em aplicação PHP● memcached● Opções de aceleradores de código php● Usando APC (Alternative PHP Cache)● Caching com Zend_Cache● PHP e Computação em Nuvem

Page 52: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, memcached

● Sistema de cache de alta perfomance com dados armazenados em memória.

Page 53: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, memcached

● Sistema de cache de alta perfomance com dados armazenados em memória.

● Ideal para:

○ Fazer cache de dados de queries lentas do banco de dados.

Page 54: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, memcached

● Sistema de cache de alta perfomance com dados armazenados em memória.

● Ideal para:

○ Fazer cache de dados de queries lentas do banco de dados.

● Instalando:

$ sudo apt-get install memcached $ sudo apt-get install php5-memcached

Page 55: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, memcached

● Inserindo e obtendo dados do cache:

Page 56: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, memcached

● Limpando dados do cache:

Page 57: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, memcached

● Usando memcached para armazenar dados de sessão, php.ini: session.save_handler = memcached session.save_path = "127.0.0.1:11211"

Page 58: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP

● Identificando gargalos em aplicação PHP● memcached● Opções de aceleradores de código php● Usando APC (Alternative PHP Cache)● Caching com Zend_Cache● PHP e Computação em Nuvem

Page 59: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, aceleradores de código

● O que é um acelerador de código ou mais especificamente um cache de Opcode ?

Page 60: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, aceleradores de código

●APC - Alternative PHP Cache.○ Software Livre ○ Mantido pelos desenvolvedores do PHP.○ Distribuído através de um pacote PECL.○ Fácil de instalar em Linux baseados em Debian.○ Previsão para estar integrado junto com o PHP versão 5.4 ○ Interface de administração fácil de usar ○ Compatibilidade com Zend Optimizer ○ Fácil administração○ Suporte para Windows ○ Suporte para PHP 5.3

Page 61: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, aceleradores de código

● eAccelerator○ Software Livre ○ Apresenta melhor desempenho que outros aceleradores de

código PHP e normalmente utiliza menos memória.○ Requer mais tempo de administração e mais ajustes finos

comparado com APC.○ Compátivel com Zend Optimizer ○ Suporte para PHP 5.3

Page 62: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, aceleradores de código

●XCache○ Software Livre ○ Apresenta desempenho próximo do eAccelerator○ Mantido pelo mesmo desenvolvedor do lighttpd○ Suporte para Windows ○ Suporte para PHP 5.3

Page 63: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, aceleradores de código

●Zend Server○ Opção comercial

○ Diversas outras opções para tuning:

■ Cache de dados■ Cache de conteúdo (html output)■ Otimização de download■ Monitoração■ Session HA (failover)

○ Várias outras fucionalidades

Page 64: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP

● Identificando gargalos em aplicação PHP● memcached● Opções de aceleradores de código php● Usando APC (Alternative PHP Cache)● Caching com Zend_Cache● PHP e Computação em Nuvem

Page 65: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, APC

● Instalando: $ sudo apt-get install php-apc

Page 66: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, APC

Page 67: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, APC

● Instalando o administrador do Cache: $ cp /usr/share/doc/php-apc/apc.php.gz /var/www $ cd /var/www $ gunzip apc.php.gz

● Definindo senha de administração (apc.php):

Page 68: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, APC

● Interface Administrativa:

Page 69: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, APC

● Usando APC como cache de dados:

Page 70: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, APC

● Mais algumas dicas de cache com APC:

Page 71: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP

● Identificando gargalos em aplicação PHP● memcached● Opções de aceleradores de código php● Usando APC (Alternative PHP Cache)● Caching com Zend_Cache● PHP e Computação em Nuvem

Page 72: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

● Distribuído junto com o Zend Framework.

● Pode ser utilizado como Stand Alone ou junto com o MVC.

● Necessário especificar frontend e backend.

○ frontend, para acessar dados○ backend, para gerenciar os dados

Page 73: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

● Frontend○Core ○Class ○ File ○ Function ○Output ○ Page○Capture

Page 74: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

●Backend○ File ○ APC○ Zend Server○ XCache○ Sqllite○memcached ○ TowLevels○ Static

Page 75: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

● Configurando frontend Core e backend File

Page 76: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

● Obtendo e armazenando dados no Cache:

Page 77: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

● Configurando frontend Class e backend File

Page 78: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

● Utilizando objetos à partir do cache:

Page 79: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Zend_Cache

● Limpando dados do Cache:

Page 80: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP

● Identificando gargalos em aplicação PHP● memcached● Opções de aceleradores de código php● Usando APC (Alternative PHP Cache)● Caching com Zend_Cache● PHP e Computação em Nuvem

Page 81: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● IaaS (Infrastructure-as-a-Service)○ Você é responsável pela infraestrutura○ Configuração de webserver, banco de dados, etc. ○ Usuário root

Page 82: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● IaaS (Infrastructure-as-a-Service)○ Você é responsável pela infraestrutura○ Configuração de webserver, banco de dados, etc. ○ Usuário root

● PaaS (Plataforme-as-a-Service)

○ Foco no desenvolvimento da aplicação ○ Interação com a plataforma através de APIs, Webservice,

REST

Page 83: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

●Amazon○ Amazon S3 (Simple Storage Service)○ Simple Queue Service○ SimpleDB○ EC2 (Elastic Compute Cloud) ○ Auto Scaling

http://aws.amazon.com/sdkforphp/

Page 84: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

●Windows Azure○ Blob Storage○ Table Storage○ Queue Storage

http://phpazure.codeplex.com/

Page 85: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

●Outros○ Nirvanix○ Rackspace○ IBM SmartCloud

http://www.phpcloud.com

Page 86: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

●Zend_Cloud○ Componente do Zend Framework○ "SimpleCloud" ○ Portabilidade

■ Amazon■ Windows Azure■ Nirvanix

http://www.simplecloud.org http://framework.zend.com/manual/en/zend.cloud.html

Page 87: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● Exemplo Serviço de Storage no Cloud com Zend_Cloud ● Realizar cadastro no link abaixo:

http://aws.amazon.com/s3/

Page 88: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● Criando um bucket no console de WebService da Amazom

● Bucket seria equivalente ao drive ou partição do sistema operacional.

https://console.aws.amazon.com/s3/

Page 89: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● Obter seu accesskey e secretkey no site da Amazom

● cloud.ini:

Page 90: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● Armazenando arquivos no storage

Page 91: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● Acessando os objetos no storage através de URL.

● Acesso somente arquivos com permissão pública para leitura: http://<bucket-name>.s3.amazonaws.com/imagens/zend-logo.png

ou http://s3.amazonaws.com/<bucket-name>/imagens/zend-logo.png

Page 92: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

PHP, Cloud

● Acessando os objetos no storage

Page 93: Tuning Apache/MySQL/PHP para Desenvolvedores · Tuning Apache/MySQL/PHP para Desenvolvedores By Douglas V. Pasqua Zend Certified Engineer / LPI / SCJP douglas.pasqua@gmail.com

Obrigado por assistir!

Para saber mais:http://dpasqua.wordpress.com

[email protected]