(dv) 4.0:Apache FCGI MySQL Configuration

  • This page was last modified on September 7, 2011, at 11:22.
The (mt) Community Wiki is a collaborative project. Any (mt) Media Temple customer or employee may contribute. Not all articles and/or content have been tested for accuracy by (mt) Media Temple.

For officially moderated and tested articles, be sure to visit our KnowledgeBase.

From (mt) Community Wiki

Contents

Overview

This wiki will go into some details about how to get the most out of your dv 4.0 vps, and getting the most out of the performance of your server with a smaller memory footprint. You will need to install Developer Tools and enable root access. (dv):SSH connection.

This article is not directly supported by (mt) Media Temple.

Although I try my best to give the most detail possible so you understand what you are editing, This article can cause your server problems. Only attempt this if you feel you have a good understanding of whats outlined in the article and are comfortable working in ssh.

Apache Modules

First disabling some modules in apache is one of the first things you can do. One quick way to do this is using the Parallels Power Panel. which should be available from: https://youripaddress:4643/vz/cp or you can replace the ip with the default domain name.

Using the File Manager in Power Panel, Navigate to /etc/httpd/conf.d/ You should see a list of files like below.

bw.conf
fcgid.conf
mailman.conf
perl.conf
php.conf
php_cgi.conf
proxy_ajp.conf
python.conf
ssl.conf
webalizer.conf
welcome.conf
zz010_psa_httpd.conf

renaming some of these config files to .off to disable them will help lower the memory consumption of apache. This will depend a bit on what type of sites you are running. for example I have the follow files renamed.

perl.off
proxy_ajp.off
python.off
welcome.off

You should also be able to disable php_cgi.conf under most configurations. Since this wiki is geared torward using fast cgi, you can safely disable this. Simply rename the file to php_cgi.off

You can also choose to turn off mod_php all together. However this may cause some systems in plesk to not work properly anymore like for example (webmail) Depending on what kind of setup you have this can save you a lot of memory. Since php is being loaded via fast-cgi you don't need mod_php eating up a bunch of apache memory for nothing. To give you an idea of how much ram this can save. With it on under my configuration I had about 12MB apache processes. Turning mod_php off reduced this to 7mb. So that's roughly 5mb per apache process. Depending on things are setup on your server this can be quite different. 5MB per process is quite a bit considering that will allow much more headroom under a loaded server.

Apache Configuration Part 2

Now that you have disable some unneeded modules. You can begin editing the apache configuration file. You will need to login to ssh.

Run this command to begin editing the apache config file:

vi /etc/httpd/conf/httpd.conf

Once you have the file open you should be able to safely disable the following modules. Simply add the pound sign # in front of the LoadModule statement.

#LoadModule authn_alias_module modules/mod_authn_alias.so
#LoadModule authn_anon_module modules/mod_authn_anon.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so
#LoadModule authn_default_module modules/mod_authn_default.so
#LoadModule authz_dbm_module modules/mod_authz_dbm.so
#LoadModule authz_default_module modules/mod_authz_default.so
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
#LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule speling_module modules/mod_speling.so
#LoadModule cgi_module modules/mod_cgi.so

Their is other modules you can disable but this will depend on what type of software you will use, and what features you really want to use. For example you could safely comment out all the cache modules if you don't want to use caching on your server.

#LoadModule cache_module modules/mod_cache.so
#LoadModule disk_cache_module modules/mod_disk_cache.so
#LoadModule file_cache_module modules/mod_file_cache.so
#LoadModule mem_cache_module modules/mod_mem_cache.so

Be sure to disable the disk file and mem cache if you disable mod_cache the other 3 depend on mod_cache so you will get an error if you don't. You can also optionally choose to have mem and file cache but not disk cache or any variation of that. mod_cache is always required if you use any kind of caching.

After you are done save the file with shift+: wq! than restart apache with the following command

/etc/init.d/httpd restart




FastCGI Configuration

I think this is one important step than many overlook when having memory issues with a dv 4. The Default configuration for fastcgi is configured for a much bigger server than a base 512MB DV. So tweaking this file is important. The options you have will depend on how many websites you will have running on the server. So I will try to explain a good way to go for a multi domain setup versus a single domain with a lot of traffic.

First open the fastcgi conf file with this command via ssh

vi /etc/httpd/conf.d/fcgid.conf

This is the configuration I use on a 512mb DV 4.0 However I will explain why and how you can best configure this for your needs. You may see performance issues if you use this on a higher traffic situation than what my server has. So keep this in mind.

# This is the Apache server configuration file for providing FastCGI support
# via mod_fcgid
#
# Documentation is available at http://fastcgi.coremail.cn/doc.htm

LoadModule fcgid_module modules/mod_fcgid.so

<IfModule mod_fcgid.c>

<IfModule !mod_fastcgi.c>
AddHandler fcgid-script fcg fcgi fpl
</IfModule>

FcgidIPCDir /var/run/mod_fcgid/sock
FcgidProcessTableFile /var/run/mod_fcgid/fcgid_shm

FcgidIdleTimeout 40
FcgidProcessLifeTime 30
FcgidMaxProcesses 5
FcgidMaxProcessesPerClass 1
FcgidIOTimeout 120
FcgidInitialEnv RAILS_ENV production
FcgidIdleScanInterval 10
FcgidMaxRequestsPerProcess 1000

</IfModule>

The Important variables in this file are

FcgidMaxProcesses 5
FcgidMaxProcessesPerClass 1
FcgidMaxRequestsPerProcess 1000

Basically what this means is I cannot have more than 5 php-cgi processes running at once, and each domain cannot have more than 1 process. This does not include subdomains or add-on domains, and lastly they will live for 1000 requests and than be recycled. Depending on the load you have on your server you may want to not recycle processes as often. So increasing the 1000 to maybe 2000 to start would be more efficient.

This is configured on a server that receives roughly 25000 raw page views per day. I would say it's configured well for at least twice that. So if you have much higher traffic first you might want to increase the max processes slowly. Only do this if you are having some noticeable slowness on occasion as well as increase the 1000 number slightly.

Lastly if you have more memory you can likely run this higher from the get go. So I would start off with doubling the max processes on double the memory. So a 1GB DV 4.0 start off at 10 Max Processes and tweak from there. You may also want to allow 2 Per Class. Don't be afraid to experiment a bit. :)

If your running a single domain, and went for a vps for performance of a high traffic website. Than you'll likely want to leave Max Processes at 5 (512mb dv) but increase the PerClass of 1 to maybe 5 As well. So that your domain uses all 5 processes. If you require more power than that you can increase it from this point. I would say however if your site required more power than that, you would probably be using a bigger server.

After you are done configuration of fastcgi. Save the file, and restart apache.

/etc/init.d/httpd restart


suPHP Configuration

If performance is not a big factor for you, and you need more of a one size fits all solution than suPHP will be a better option. I highly suggest you read this wiki below, as setting up suPHP on Plesk is a bit of a process. (dv)_4.0_-_Making_It_Better_::_Using_mod_suphp_with_Plesk

This will drastically lower your memory usage, could save you as much as 100mb of ram, and will allow you to run PHP scripts as the domain user. The Performance aspect will really only come into effect in a high load situation where fastcgi will preform better than suPHP by a lot. The Issue with Fastcgi is it's very difficult to get a setup that will work in all situations, and suPHP is one of the best ways around that.


The Problem with fast-cgi is sometimes it gets into a state where it's allocating memory that's not actually being used yet. (usually more than your standard php application page) So this is wasted memory, and may only be useful on a site by site basis where you want that extra horse power.

Under a suPHP configuration you can easily get memory into the 270mb to 300mb range for the entire server with little load. This gives you a lot of room for a big traffic spike.

MYSQL

Configuring MYSQL can be tricky sometimes, because it depends largely on how much database stuff your websites are doing. This is what I use for mysql hungry set of websites that I use. For example running a message board like PHPBB with 5000 members on it.

You can edit your mysql config by running this command

vi /etc/my.cnf

And Below is my configuration file

###############################################################################
### /etc/my.cnf - MySQL configuration
### For: MySQL 5.1 on a server which is not dedicated to MySQL
###
### This file was initially designed for (mt) Media Temple
###     (dv) 4.0 512MB
###
### Version 1.0
###
### Any line with configuration after a '#' indicates the default setting,
### but is included in this way for easy modification (and for reference).
###
##############################################################################
###
### To activate this configuration, you must first create the slow query log
### file and then restart mysql.  You only need to do this once after you
### begin using this configuration file.
###
### Copy and paste this command:
###     touch /var/log/mysql.slow-queries.log && /etc/init.d/mysqld restart
###
##############################################################################

[mysqld]
datadir      = /var/lib/mysql
socket       = /var/lib/mysql/mysql.sock
user         = mysql

max_connections = 100
max_allowed_packet = 32M

# Comment this out if you need LOAD DATA INFILE, but disable again
# when you're done.  This is a security risk!
local-infile = 0

# tmp_table_size/max_heap_table_size: Default is 16MB, but this causes many
# temporary tables to be written to disk, and is ineffecient.
# Keep tmp_table_size and max_heap_table_size the same!
tmp_table_size      = 64M
max_heap_table_size = 64M

### query-cache settings ###
query-cache-type  = 1
query-cache-size  = 32M
query_cache_limit = 4M

### Buffer size settings ###
#sort_buffer_size = 2M
#join_buffer_size = 128K

# key_buffer_size is important for MyISAM tables.  Should be larger than size of index data.
key_buffer_size = 40M

thread_cache_size = 8

# table_cache: Approximate by taking max_connections and multiplying by the
# largest number of tables that can be open for any join.
table_cache = 1024

### Timing options ###
interactive_timeout = 100
wait_timeout        = 20
connect_timeout     = 15

### Log slow queries ###
# Make sure you create this file before you start mysql or it will not work!
# Use the this command:   touch /var/log/mysql.slow-queries.log
slow-query-log      = 1
slow-query-log-file = /var/log/mysql.slow-queries.log

# Duration a query must run (in seconds) to get logged
long_query_time     = 1

### InnoDB settings ###
# See http://dev.mysql.com/doc/refman/5.0/en/innodb-configuration.html
# for InnoDB settings; They largely depend on your application.
innodb_buffer_pool_size = 8M

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

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

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

I highly suggest reading this wiki for information on how you can fine tune mysql. Personally I don't think there is a better way to know what to tweak without using [MYSQLTuner]

Lastly a good rule to go by for the following variables

query_cache_size=32M #32M for every 1GB of RAM
key_buffer=64M ## 64MB for every 1GB of RAM
sort_buffer_size=1M ## 1MB for every 1GB of RAM
read_buffer_size=1M ## 1MB for every 1GB of RAM
read_rnd_buffer_size=1M ## 1MB for every 1GB of RAM

Therefore you may want to half these values for a Base 512 server, and evaluate the server from this point.

Hopefully this helps some people get memory under control on a 512mb DV4.0. I will add more to this as I find somthing that can be generally useful. Anyone else would like to share information feel free to edit this wiki.:)