Nginx , Apache , Mysql , Memcache with server 4G ram. How optimize to enought memory?

i have 1 dedicated server with Nginx proxy for Apache.
Memcache, mysql, 4G Ram.

These day, my visitor on my site wasn’t increased, but my server get overload always in some specified time. (9AM – 15PM)

Ram in use is increased second by second to full.
that’s moment, my server will get overload.

i have to kill all apache , mysql service and reboot it to get free memory.
that’s the circle.

here is my ram in use at the moment
160(nginx) 220(apache) 512(memcache) 924(mysql)
here’s process number
4(nginx) 14(apache) 5(memcache) 20(mysql)

and here’s my my.cnf config.
someone can help me to optimize it?

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
skip-locking 
skip-networking 
skip-name-resolve

# enable log-slow-queries
log-slow-queries = /var/log/mysql-slow-queries.log
long_query_time=3

max_connections=200
wait_timeout=64
connect_timeout = 10
interactive_timeout = 25
thread_stack = 512K
max_allowed_packet=16M 
table_cache=1500 
read_buffer_size=4M 
join_buffer_size=4M 
sort_buffer_size=4M
read_rnd_buffer_size = 4M 
max_heap_table_size=256M 
tmp_table_size=256M 
thread_cache=256 
query_cache_type=1 
query_cache_limit=4M 
query_cache_size=16M 
thread_concurrency=8
myisam_sort_buffer_size=128M 
# Disabling symbolic-links is recommended to prevent assorted security risks 
symbolic-links=0 
[mysqldump] 
quick 
max_allowed_packet=16M 

[mysql] 
no-auto-rehash 

[isamchk] 
key_buffer=256M 
sort_buffer=256M 
read_buffer=64M 
write_buffer=64M 

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

[mysqlhotcopy] 
interactive-timeout 

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Answer

I’m not sure how many request do you get, but I would say first you need to find out where is the bottleneck. In term of number of applications you are using, it is quite overloaded. Here are some suggestions,

1. The most important thing, make sure your codes are optimized. Try to find if any of the codes, or queries to database is the bottleneck. Check out all the MySql do’s and don’t. Cache the queries results which is slowing things down.

2. Make sure you are using PHP-FPM.

3. Use APC for op-code caching.

4. If possible, host MySql on it’s own server(or virtual server).

5. If you are using Nginx to only serve static content and Apache to serve dynamic content, I would like to suggest to remove the Apache and use Nginx for both static and dynamic.

Hope this could improve your server performance.

On your “my.cnf config”, I’m not an expert in it so I won’t able to help you on it.

Attribution
Source : Link , Question Author : TomSawyer , Answer Author : Arrivu

Leave a Comment