≡ Menu

Apache, PHP and MySQL Optimization

For Dedicated Server or VPS users, Apache, PHP and MySQL Optimization is very important, because with a little optimization will reduce the burden on our resources of dedicated or VPS put through the process, especially if our server has a limited hardware resources.

Here I will attach some optimizations for Apache web server is one that is fast, reliable and flexible. But it is by default, Apache can take a lot of resources. In it I also convey to the optimization of PHP and MySQL so our servers will be faster and save server resources used.

Turn off Apache and MySQL service:

server# /usr/local/etc/rc.d/mysql-server stop
server# /usr/local/etc/rc.d/apache22 stop

PHP Optimization

Remove remark or comment in the file /usr/local/etc/php.ini, so that the contents of a file like the following:

[PHP]
engine = On
zend.ze1_compatibility_mode = Off
short_open_tag = On
asp_tags = Off
precision    =  12
y2k_compliance = On
output_buffering = Off
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func=
serialize_precision = 100
allow_call_time_pass_reference = On
safe_mode = Off
safe_mode_gid = Off
safe_mode_include_dir =
safe_mode_exec_dir =
safe_mode_allowed_env_vars = PHP_
safe_mode_protected_env_vars = LD_LIBRARY_PATH
disable_functions =
disable_classes =
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting  =  E_ALL & ~E_NOTICE
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
error_log = "/var/log/php_error.log"
variables_order = "EGPCS"
register_globals = On
register_long_arrays = On
register_argc_argv = On
auto_globals_jit = On
post_max_size = 8M
magic_quotes_gpc = On
magic_quotes_runtime = Off
magic_quotes_sybase = Off
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
enable_dl = On
file_uploads = On
upload_max_filesize = 8M
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[Date]
[filter]
[iconv]
[sqlite]
[xmlrpc]
[Pcre]
[Syslog]
define_syslog_variables  = Off
[mail function]
SMTP = localhost
smtp_port = 25
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[MySQL]
mysql.allow_persistent = On
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mSQL]
msql.allow_persistent = On
msql.max_persistent = -1
msql.max_links = -1
[OCI8]
[PostgresSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase]
sybase.allow_persistent = On
sybase.max_persistent = -1
sybase.max_links = -1
sybase.min_error_severity = 10
sybase.min_message_severity = 10
sybase.compatability_mode = Off
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Informix]
ifx.default_host =
ifx.default_user =
ifx.default_password =
ifx.allow_persistent = On
ifx.max_persistent = -1
ifx.max_links = -1
ifx.textasvarchar = 0
ifx.byteasvarchar = 0
ifx.charasvarchar = 0
ifx.blobinfile = 0
ifx.nullformat = 0
[Session]
session.save_handler = files
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor     = 100
session.gc_maxlifetime = 1440
session.bug_compat_42 = 1
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 4
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset="
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
mssql.secure_connection = Off
[Assertion]
[COM]
[mbstring]
[FrontBase]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400

Prepare the PHP error log file:

server# touch /var/log/php_error.log
server# chmod 666 /var/log/php_error.log

Apache Optimization

Remove modules that are not needed and the remark or comment in the file /usr/local/etc/apache22/httpd.conf. Thus becomes:

ServerRoot "/usr/local"
Listen 80
LoadModule authn_file_module libexec/apache22/mod_authn_file.so
LoadModule authz_host_module libexec/apache22/mod_authz_host.so
LoadModule auth_basic_module libexec/apache22/mod_auth_basic.so
LoadModule authz_user_module libexec/apache22/mod_authz_user.so
LoadModule deflate_module libexec/apache22/mod_deflate.so
LoadModule log_config_module libexec/apache22/mod_log_config.so
LoadModule mime_magic_module libexec/apache22/mod_mime_magic.so
LoadModule unique_id_module libexec/apache22/mod_unique_id.so
LoadModule setenvif_module libexec/apache22/mod_setenvif.so
LoadModule mime_module libexec/apache22/mod_mime.so
LoadModule cgi_module libexec/apache22/mod_cgi.so
LoadModule vhost_alias_module libexec/apache22/mod_vhost_alias.so
LoadModule dir_module libexec/apache22/mod_dir.so
LoadModule alias_module libexec/apache22/mod_alias.so
LoadModule rewrite_module libexec/apache22/mod_rewrite.so
LoadModule status_module libexec/apache22/mod_status.so
LoadModule php5_module libexec/apache22/libphp5.so
LoadModule expires_module libexec/apache22/mod_expires.so
LoadModule headers_module libexec/apache22/mod_headers.so
User nobody
Group nobody
ServerAdmin [email protected]
ServerName 192.168.0.100
DocumentRoot "/usr/local/www/apache22/data"

  AllowOverride None
  Order deny,allow
  Deny from all


  Options FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all

DirectoryIndex index.php index.html

  Order allow,deny
  Deny from all
  Satisfy All

ErrorLog "/var/log/httpd-error.log"
LogLevel warn
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common
SetEnvIf Remote_Addr "127.0.0.1" dontlog
CustomLog "/var/log/httpd-access.log" combined env=!dontlog
ScriptAlias /cgi-bin/ "/usr/local/www/apache22/cgi-bin/"

  AllowOverride None
  Options None
  Order allow,deny
  Allow from all

DefaultType text/plain
TypesConfig etc/apache22/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Header unset ETag
FileETag None

  Header unset Last-Modified
  Header set Expires "Fri, 08 Jun 2020 00:00:00 GMT"
  Header set Cache-Control "public, no-transform"
  SetOutputFilter DEFLATE

Include etc/apache22/extra/httpd-mpm.conf
Include etc/apache22/extra/httpd-vhosts.conf
Include etc/apache22/extra/httpd-default.conf
Include etc/apache22/Includes/*.conf

Edit MPM worker file ini /usr/local/etc/apache22/extra/httpd-mpm.conf :

PidFile "/var/run/httpd.pid"
LockFile "/var/log/accept.lock"
StartServers             150
MinSpareServers         25
MaxSpareServers        35
ServerLimit               435
MaxClients                435
MaxRequestsPerChild 1000

Edit Virtual Host at file /usr/local/etc/apache22/extra/httpd-vhost.conf :

NameVirtualHost 192.168.0.100


Edit the default configuration file /usr/local/etc/apache22/extra/httpd-default.conf :

Timeout 60
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15
UseCanonicalName Off
AccessFileName .htaccess
ServerTokens productonly
ServerSignature Off
HostnameLookups Off

Enable mod_deflate by creating a file /usr/local/etc/apache22/Includes/mod_deflate.conf contains:

AddOutputFilterByType DEFLATE text/html text/plain
#Highest 9 - Lowest 1
DeflateCompressionLevel 9

#Optional
#Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

#Optional
#Logging
DeflateFilterNote ratio
LogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' deflate
CustomLog /var/log/deflate_log deflate

Check the Apache configuration:

server# apachectl configtest

Syntax OK

MySQL Optimazitaion

server# ee /var/db/mysql/my.cnf

The contents of the file /var/db/mysql/my.cnf:

[client]
port            = 3306
socket          = /tmp/mysql.sock

[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

Run all the services by typing:

server# /usr/local/etc/rc.d/mysql-server start
server# /usr/local/etc/rc.d/apache22 start

Hopefully the discussion Apache, php and mysql optimization can be a reference for all of us in the optimization of our server

{ 0 comments… add one }

Leave a Comment

CAPTCHA
*