I have a small Ubuntu server with 512MB of ram and several php/mysql websites (~20) running on it.It's been running for 2 years now without a single issue. But I think the moment for some optimization has just came.

The website that are hosted on this server are very low traffic, but I see some CPU issues now that they started to get more visits.

First thing I'm trying to do is identify the issues, so I've been playing around with several command line tools, like top htop apachetop, etc, and workbench for monitoring MySQl.With these tools is kind of difficult to know what i's going on exactly. For example, I created a PHP daemons that is running in the background, and every 10 seconds stores in the database the CPU usage.If the CPU is bigger than 60%, it stores the output of apachetop. This hasn't been specially useful since many of the records are OPTIONS * HTTP/1.0. Also, it looks like it's a list of the last requests, not the current request.

Seconds, when I try to monitor what's going on using htop, I always see ~15 records like:

6905 mysql     20   0  462M  110M  5220 S  0.0 21.6  5:29.79 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock

and another ~15 like:

23382 www-data  20   0  259M 27384  4820 S  1.3  5.2  0:01.00 /usr/sbin/apache2 -k start

here's an screenshot:

I'm not sure if that amount of processes should be open all the time or this is something I can optimize. I can upgrade this server now, I just want to optimize it as much as possible before doing it.

Also, I turned on the log for slow queries, the only queries there are all queries that are executed when the server's CPU is over 90%. This just confirms that php/mysql I'm using (custom) is not very heavy. I can say that the website with more traffic has 300 unique visitors and 1200 page views a day. All other websites have much lower traffic.

1) I'm looking for suggestions of how to monitor this server efficiently.

2) I need suggestions for MySQl and PHP. For example, how much memory should I assigned to them or which options should I turn on/off.


here's a link to a screenshot of htop: http://www.flickr.com/photos/58586868@N02/5375745794/

migrated from stackoverflow.com Jan 21 '11 at 16:27

This question came from our site for professional and enthusiast programmers.


As one might guess from some of my previous posts I run collectl everywhere. Download/install the rpm from sourcefore and '/etc/init.d/collectl start' and you're done. It will collectl over a couple of hundred performance metrics every 10 seconds at <0.1% of the cpu. It will also take a snapshot of process data every minute since that's a heavier weight operation. You can then play back the collected data in a variety of formats or even plot it with a web-based tool called colplot, which is part of the collectl-utils package.

Looking at brief and/or incomplete pictures of what the system is doing ain't going to get you there. You need a longer-term picture of fine-grained data that you can drill down into to see what's really happening.



How to tune a Linux server, running Apache, PHP and MySQL would fill at least 4 large books - and there is no magic solution.

However in my experience, most of the performance problems on LAMP stacks are poorly written database queries. You have discarded this as a possible contribution to what you perceive as a performance issue - I'd recommend you go back and have a very long look at this.

Once you've sorted out the database, the other things to check are that you are compressing all html, javascript and css (mod_gzip) and PHP generated HTML (output buffering). Do make sure you've got an opcode cache running (like APC). And do make sure you're serving all your static content with sensible caching info.

System metrics are good for telling you how the performance of a system is constrained - NOT what the performance actually is. Start recording %D in your apache logs, and analyse the results to find out how your system really is performing. Prioritize the URLs with the highest totals for optimization.

The details you've provided from your system are very contextual - assuming that you're webserver is setup correctly, they suggest that the CPU usage is mostly down to mysql - but if the webserver config is wrong then the opposite may be the case.

It would have been helpful if you'd provided the apache config (less the comments).

I turned on the log for slow queries, the only queries there are all queries that are executed when the server's CPU is over 90%

Sounds like you set too high a threshold. Try setting the threshold to 0 and write a parser to strip the parameters out of the queries to identify which ones really are slow. Or just find one already written.


Propably your table are growing with time and you're losing on the mysql buffers. Try to find out if your tables are of type MyISAM or InnoDB and tweak buffer settings accordingly.

If you don't use InnoDB turn it off. InnoDB has buffer set up with innodb_buffer_pool_size and MyISAM uses key_buffer. You can find both values at mysqladmin variables.

As the tables grow the same queries that were previously fast work slower and slower.

Regarding knowing what the computer does? It's difficult. Try to find the exact times when the system was busy and check out both system and user crontabs and webserver access logs.


You need more information. Is the server always running slow? Or only during certain time periods? What time periods? What does the box look like during those time periods?

I would look into something like Munin. It's a very simple system monitoring app that collects data and presents it as graphs. They have plugins for Apache and MySQL to track accesses, processes, and volume.

The best thing about Munin is that you get a lot of information for almost no work. Installation and setup is a 5 minute job if you do it manually, and a 30 second job if you are using Debian/Ubuntu. The only thing you have to do after dropping it in is add an entry for the host to the config and make sure the plugins you want are enabled.

Munin, or something similar, will help give you some current baselines for what your system looks like. This will be very useful as you start making changes, tweaks, and tuning the applications, as you'll have better feedback on what's working. Then, you can start digging into the more specific places, and improve the MySQL performance, and the Apache performance, and anything else running on the box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy