How to optimize Apache and MySQL?

Hello, my linode is Linode 540. Everytime I put one of my wordpress powered sites on it, which has a most user online of 240 and about 9K daily traffic, all things stopped within 5 minutes. The pages wouldn't load but a HTTP 500. Seemed apache shut down. I had to reboot my linode to login via SSH. :cry:

All errors were almost the same:

WordPress database error MySQL server has gone away for query...

I submitted a ticket. They told me to "tune the Apache MaxClients directive to limit the number of connections". Honestly I do not know how to set that numbers.

In my apache2.conf, it's:

 <ifmodule mpm_prefork_module="">StartServers          16
    MinSpareServers       8
    MaxSpareServers      24
    ServerLimit         400
    MaxClients          200           
    MaxRequestsPerChild   500</ifmodule> 

I think it's using prefork mpm (dont know how to check that out).

My control panel is ISPConfig.

I have been too afraid to put that site on my linode. Several months ago I've tried several times then finally I gave up. Now I have to face it. The shared hosting wont allow the site on it any more.

If any body can help me out, it would be so nice and I'm so happy.

Thank you so much!

19 Replies

Try to raise mysql wait_timeout in /etc/mysql/my.cnf

Mine is set to 300.

@nfn:

Try to raise mysql wait_timeout in /etc/mysql/my.cnf

Mine is set to 300.
Hi, my MySQL database is not in localhost, it's in another shared hosting. I connected to that database from my Linode.

What is happening is that the MySQL server closes the connection - and when PHP tries to access it again, it fails.

Moving MySQL to localhost will probably solve this. Or, you may want to use this workaround: http://robsnotebook.com/wordpress-mysql-gone-away

@saman007uk:

What is happening is that the MySQL server closes the connection - and when PHP tries to access it again, it fails.

Moving MySQL to localhost will probably solve this.
But at the same time, my other site which uses localhost MySQL was also down. I think there must be something wrong with apache.

@whatness:

I think it's using prefork mpm (dont know how to check that out).
You can do this with the -l option to the Apache server (depending on your system and version, probably either apache2 or httpd).

# apache2 -l
Compiled in modules:
  core.c
  mod_log_config.c
  mod_logio.c
  prefork.c
  http_core.c
  mod_so.c

On Linux, you'll probably see either prefork.c (the default) or worker.c. I think any others are experimental.

@Vance:

You can do this with the -l option to the Apache server (depending on your system and version, probably either apache2 or httpd).
Hi, thanks! It's prefork.c.

For word press, there is a work around: http://robsnotebook.com/wordpress-mysql-gone-away

It basically checks that the persistent connection is working before using it.

Also, this does not have anything to do with Apache directly - it is PHP which connects to the MySQl server through the php5-mysql library, not the Apache server itself.

I use WP Super Cache on my WordPress sites to increase performance, and it has worked really well for me. See the plugin page: http://wordpress.org/extend/plugins/wp-super-cache/

I hope this helps somewhat; the plugin is intended to make sites "Digg-proof." I've withstood a Slashdotting, if that's any indication of its worth.

Try reducing MaxClients to 35. You should see your performance improve dramatically.

Thank you guys. I bought more RAM, and now things get better. Site down again hours ago when the CPU usage jump to about 400%, and I really don't know what was going on at that moment :shock:

> Site down again hours ago when the CPU usage jump to about 400%
If you haven't already, install munin (as is posted ad nausium elsewhere ;)).

Or for a quick (?) check, run a cron job every few minutes to append useful stats to a file.

you may want to create a cron script to troublshoot memory/cpu usage. This could be apache/mysql/spamassassin or any other memory demanding application.

Read this first

http://rimuhosting.com/howto/memory.jsp

@refactorial:

Try reducing MaxClients to 35. You should see your performance improve dramatically.
Yay! that works! The site is up for one whole day now.

Oops, now it's down again. Service httpd not running … :cry:

Here's my memory usage. Any ideas? It seems i have to buy more RAM again?

Not to be flip, but I switched to Lighttpd and SQLite for all my web apps. For wordpress, you can hack in SQLite support with http://rathercurious.net/pdo-for-wordpress - I had to manually create tables for a couple WP plugins, but it was worth the effort because I'm seeing a lot better response from my lil Linode 360.

hehe. not to be even more flip, but i dropped wordpress all together and wrote my own lite blogging engine! :) (if you're curious: http://dertyn.com )

you seem to be using way too much memory for what you're doing

did you look "ps aux" of what's going on on your box and what programs exactly are using the memory ?

also, if you don't want to completely replace apache you can consider using nginx as a reverse proxy and have it serve the static pages while you'd keep apache for just the PHP pages.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct