VPS – Apache & MySQL Performance Tuning

Apache

1. Open/Create /var/www/vhosts/{hostname}/conf/vhosts.conf

2. Add oradjust below code

KeepAlive On
MaxKeepAliveRequests 29
KeepAliveTimeout 15

3. Copy to make backup of apache configuration

# cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.YYYY-MM-DD.bak

4. Execute below shell script one by one

# ramCount=`awk 'match($0,/vmguar/) {print $4}' /proc/user_beancounters`
# ramBase=-16 && for ((;ramCount>1;ramBase++)); do ramCount=$((ramCount/2)); done
# perl -0 -p -i -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?StartServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MinSpareServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxSpareServers\s*?)\s\d+/\1\ '"$(($ramBase*2 + 1))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?ServerLimit\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxClients\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxRequestsPerChild\s*?)\s\d+/\1\ '"$(( 2048 + ($ramBase*256) ))"'/;' /etc/httpd/conf/httpd.conf
/etc/init.d/httpd restart

5. Create fake swap using following shell script

# free -m
# vi fakeswap.sh

6. Copy and Paste below script to fakeswap.sh

#!/bin/bash

SWAP="${1:-512}"

NEW="$[SWAP*1024]"; TEMP="${NEW//?/ }"; OLD="${TEMP:1}0"

umount /proc/meminfo 2> /dev/null
sed "/^Swap\(Total\|Free\):/s,$OLD,$NEW," /proc/meminfo > /etc/fake_meminfo
mount --bind /etc/fake_meminfo /proc/meminfo

free -m
# chmod +x fakeswap.sh
# sh fakeswap.sh 1024
# free -m

 

MySQL

1. Backup current MySQL configuration

# cp /etc/my.cnf /etc/my.cnf.YYYY-MM-DD.bak
# vi /etc/my.cnf

2. Add/Replace below configuration and adjust values (CAUTION: this is just example)

[mysqld]
key_buffer_size = 384M
table_open_cache = 5096
max_connections = 400
key_buffer = 128MB
myisam_sort_buffer_size = 64MB
join_buffer_size = 5M
read_buffer_size = 2M
sort_buffer_size = 2M
read_rnd_buffer_size = 8M
table_cache = 2048
thread_cache_size = 8
interactive_timeout = 25
wait_timeout = 1000
connect_timeout = 10
max_allowed_packet = 32M
max_connect_errors = 10
query_cache_limit = 6M
query_cache_size = 64M
query_cache_type = 1
tmp_table_size = 64M
max_heap_table_size = 64M

local-infile=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

innodb_buffer_pool_size=120M
innodb_additional_mem_pool_size=500K
innodb_log_buffer_size=500K
innodb_thread_concurrency=2
[mysqld_safe]
open_files_limit = 8192
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
innodb_buffer_pool_size=120M
innodb_additional_mem_pool_size=500K
innodb_log_buffer_size=500K
innodb_thread_concurrency=2

[mysqldump]
quick
max_allowed_packet = 16M

[myisamchk]
key_buffer = 64M
sort_buffer = 64M
read_buffer = 16M
write_buffer = 16M

local-infile=0
# /etc/rc.d/init.d/mysql restart

3. Download mysqltuner.pl

https://github.com/major/MySQLTuner-perl/blob/master/mysqltuner.pl

4. Execute mysqltuner.pl and follow suggestions

# chmod 777 mysqltuner.pl
# ./mysqltuner.pl

 

 

 

 

 

Reference:

https://kb.mediatemple.net/questions/246/Making+it+Better%3A+Basic+Apache+performance+tuning+%28httpd%29#dv

http://linux-problem-solver.blogspot.com.au/2013/08/create-fake-swap-in-openvz-vps-if-you-get-swapon-failed-operation-not-permitted-error.html

http://www.maxwhale.com/how-to-optimize-mysql-for-1gb-memory-vps/

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.