Ubuntu 10 Linux / Apache2 / MySQL / PHP Guide is all about optimizing Apache, MYSQL and PHP to work faster together to serve up webpages created by the WordPress content management system.
The setting you use for any server depend heavily upon the amount of available RAM / CPU etc as well as the consumption of these resources by other application and or services running in your environment.
In general you want to modify several configuration files to obtain optimal performance. The files you need to modify are:
- /etc/apache2/apache2.conf
- /etc/mysql/my.cnf
- /etc/php5/apache2/php.ini
Let’s start with apache2.conf
Apache2.conf is the main Apache server configuration file. It contains the configuration directives that give the server its instructions. See http://httpd.apache.org/docs/2.2/ for detailed information about the directives.
I want to focus on:
- Timeout 300
- KeepAlive On
- MaxKeepAliveRequests 100
- KeepAliveTimeout 1
These keep alive and timeout setting stop external request from eating up apache processes. The above setting will work for just about any setup.
Next let’s talk about the prefork module in apache2.conf, you want to start with
StartServers 4
MinSpareServers 4
MaxSpareServers 10
MaxClients 40
ServerLimit 30
MaxRequestsPerChild 2000
This tells apache how many workers to start with max amount of allowed connects. These numbers play a role in CPU and adjusting them and monitoring the changes overtime will help you optimize apache.
Now restart apache to load the new configuration.
/etc/init.d/apache2 restart
Now let’s move on to my.cnf
located in /etc/mysql/my.cnf this controls, among other things, the resource mysql will consume, most importantly RAM.
[mysqld]
key_buffer = 512M
sort_buffer_size = 8M
read_buffer_size = 8M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 40M
query_cache_size = 64M
skip-innodb
table_cache = 128
max_allowed_packet = 6M
thread_stack = 192K
thread_cache_size = 8
query_cache_limit = 1M
query_cache_size = 16M
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[isamchk]
key_buffer = 16M
The above is a good mysql optimization starting part. Only use skip-innodb if your using wordpress without innodb enabled, which is default for wordpress.
Now we move to PHP optimization.
You’re going to optimize the php.ini file located /etc/php5/apache2/php.ini
max_execution_time = 30
memory_limit = 64M
mysql.cache_size = 2000
session.gc_maxlifetime = 1440
mysqli.cache_size = 2000
The above is a good starting point for optimization on a standard size ubuntu server with average resources for a small to medium size server. PHP isn’t to bad of a memory hog but if your pages are dynamic created with PHP you’ll see CPU spikes on page loads if your server isn’t configured optimally or your resources are just extended to far due to server load.
Some other points of interest.
Are you using any type of caching with WordPress (such as WP-SuperCache)? You may be able to squeeze a bit more performance out of your Ubuntu server by implementing some caching for the websites themselves.
If this does not handle the caching efficiently enough there are more advanced solutions available including using Memcached and Varnish. Memcached is used to cache SQL queries, and Varnish is used to cache dynamic content websites to allow you to serve the content faster.
Along with that you may want to consider scaling your services horizontally. This would be to separate your web server and your SQL server to separate hardware. This would allow your web server and SQL database to operate on different hardware, therefore they would not need to contend with each other for system resources.