Tuesday, January 13, 2009

Apache Tuning

Links:

(Apache)
http://phplens.com/phpeverywhere/tuning-apache-php
http://httpd.apache.org/docs/1.3/misc/perf-tuning.html
http://perl.apache.org/docs/1.0/guide/performance.html#the_crashme_Script
http://tldp.org/LDP/LGNET/123/vishnu.html
http://www.perlcode.org/tutorials/apache/tuning.html
http://drupal.org/node/215516

(PHP)
http://phplens.com/lens/php-book/optimizing-debugging-php.php

Apache is configured using the httpd.conf file. The following parameters are particularly important in configuring child processes:

Directive

Default

Description

MaxClients

256

The maximum number of child processes to create. The default means that up to 256 HTTP requests can be handled concurrently. Any further connection requests are queued.

StartServers

5

The number of child processes to create on startup.

MinSpareServers

5

The number of idle child processes that should be created. If the number of idle child processes falls to less than this number, 1 child is created initially, then 2 after another second, then 4 after another second, and so forth till 32 children are created per second.

MaxSpareServers

10

If more than this number of child processes are alive, then these extra processes will be terminated.

MaxRequestsPerChild

0

Sets the number of HTTP requests a child can handle before terminating. Setting to 0 means never terminate. Set this to a value to between 100 to 10000 if you suspect memory leaks are occurring, or to free under-utilized resources.

For large sites, values close to the following might be better:

MinSpareServers 32

MaxSpareServers 64

Apache on Windows behaves differently. Instead of using child processes, Apache uses threads. The above parameters are not used. Instead we have one parameter: ThreadsPerChild which defaults to 50. This parameter sets the number of threads that can be spawned by Apache. As there is only one child process in the Windows version, the default setting of 50 means only 50 concurrent HTTP requests can be handled. For web servers experiencing higher traffic, increase this value to between 256 to 1024.

Other useful performance parameters you can change include:

Directive

Default

Description

SendBufferSize

Set to OS default

Determines the size of the output buffer (in bytes) used in TCP/IP connections. This is primarily useful for congested or slow networks when packets need to be buffered; you then set this parameter close to the size of the largest file normally downloaded. One TCP/IP buffer will be created per client connection.

KeepAlive [on|off]

On

In the original HTTP specification, every HTTP request had to establish a separate connection to the server. To reduce the overhead of frequent connects, the keep-alive header was developed. Keep-alives tells the server to reuse the same socket connection for multiple HTTP requests.

If a separate dedicated web server serves all images, you can disable this option. This technique can substantially improve resource utilization.

KeepAliveTimeout

15

The number of seconds to keep the socket connection alive. This time includes the generation of content by the server and acknowledgements by the client. If the client does not respond in time, it must make a new connection.

This value should be kept low as the socket will be idle for extended periods otherwise.

MaxKeepAliveRequests

100

Socket connections will be terminated when the number of requests set by MaxKeepAliveRequests is reached. Keep this to a high value below MaxClients or ThreadsPerChild.

TimeOut

300

Disconnect when idle time exceeds this value. You can set this value lower if your clients have low latencies.

LimitRequestBody

0

Maximum size of a PUT or POST. O means there is no limit.

If you do not require DNS lookups and you are not using the htaccess file to configure Apache settings for individual directories you can set:

# disable DNS lookups: PHP scripts only get the IP address

HostnameLookups off

# disable htaccess checks

AllowOverride none

If you are not worried about the directory security when accessing symbolic links, turn on FollowSymLinks and turn off SymLinksIfOwnerMatch to prevent additional lstat() system calls from being made:

Options FollowSymLinks

#Options SymLinksIfOwnerMatch


MaxClients:

You can, and should, control the MaxClients setting so that your server does not spawn so many children it starts swapping. The procedure for doing this is simple: determine the size of your average Apache process, by looking at your process list via a tool such as top, and divide this into your total available memory, leaving some room for other processes.


MaxRequestsPerChild:

when using PHP's persistent database connections: don't set MaxRequestsPerChild too high so idle resources are released quickly

Tools:

http://2bits.com/articles/tools-for-performance-tuning-and-optimization.html

HTTPERF:
ftp://ftp.hpl.hp.com/pub/httperf/

ab (Apache Benchmark)


PHP:

Enable the compression of HTML by putting in your php.ini:

output_handler = ob_gzhandler

Tuesday, January 6, 2009

Mysql Multiple Instance

Add mysqld_admin part in /etc/mysql/my.cnf:

[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = multi_admin
password = multi

Add new instance parameters:

[mysqld3]
socket = /var/run/mysqld/mysql3.sock
port = 3308
pid-file = /var/run/mysqld/mysql3.pid
datadir = /var/lib/mysql/data3
language = /usr/share/mysql/english
server-id = 5

Build new Data directory:

mysql_install_db --user=mysql --ldata=/var/lib/mysql/data3

Restart mysql server:

/etc/init.d/mysql restart

Start new instance:

mysqld_multi start 3

Check that is runing:

mysqld_multi report 3

Set the root password for new instance:

mysqladmin -uroot password "NEWPASSWORD" -S /var/run/mysqld/mysql3.sock

Check new instance is available:

mysql -uroot -p -S /var/run/mysqld/mysql3.sock

Wednesday, December 31, 2008

Change RAMDISK size in Debian

Edit /boot/grub/menu.lst (or grub.conf or lilo.conf) and add ramdisk_size=512000 to this line:
kernel /vmlinuz root=/dev/md1 ro console=tty0 console=ttyS0,57600

kernel /vmlinuz root=/dev/md1 ro ramdisk_size=512000 console=tty0 console=ttyS0,57600

it defines 512 MB Ramdisk.

Reboot system.

To create an ext2fs file system, the following command is used :

mke2fs -m 0 /dev/ram0

The -m 0 option keeps mke2fs from reserving any space on the file system for the root user. This makes the complete ramdisk space available to any regular user.


To mount the ramdisk :


mkdir /mnt/RAMDISK
mount /dev/ram0 /mnt/RAMDISK

Run mount to check whether the RAMDISK has been mounted.
Now the RAMDISK can be used as a regular directory. You can read, write, delete and modify files on the RAMDISK as if you are working on a regular hard disk. Linux would handle it as if it were handling a regular directory on the disk. The difference between RAMDISK and normal DISK would be invisible to a regular user.

The only problem is that if the system is either rebooted or crashes. In that case, all data in the RAMDISK is lost. You will need to recreate the RAMDISK and redo all that has been done.

Wednesday, November 26, 2008

Keyboard problem in VMware under Ubuntu 8.10

In VMWARE Server on my fresh install of Ubuntu 8.10,after powering on a Windows XP vm, I realized that I could not use the some keys properly. On hitting the left arrow key, start menu was being displayed and other arrow keys had similar issues. Also, using VMware player was completely impossible as ctrl-alt-del wasn’t working.

To fix this issue, you simply need to run the following command,

echo 'xkeymap.nokeycodeMap = true' > /etc/vmware/config

Thursday, November 13, 2008

Install Mysql Activity Report on Ubuntu

Install these two packages:

apt-get install librrd2-dev libmysqlclient15-dev

Download the Mysql Report Activity:

wget http://freshmeat.net/redir/mysqlard/...d-1.0.0.tar.gz

tar -xvzf mysqlard-1.0.0.tar.gz
cd mysqlard-1.0.0

./configure
make
make install

Edit /usr/local/share/mysqlard/mysqlar.php and change username and password for your Mysql Database.

Give access right to www-data user for vim /usr/local/share/mysqlard/ :

chown www-data.www-data /usr/local/share/mysqlard/mysqlar.php

Add the alias to apache config file ( I added a new conf file in /etc/apache2/conf.d/ ):

vim /etc/apache2/conf.d/mysqlard.conf

and add these lines to it:

Alias /mysqlard /usr/local/share/mysqlard

"Directory /usr/local/share/mysqlard"

php_flag register_globals off

Options Indexes FollowSymLinks

"IfModule mod_dir.c"

DirectoryIndex mysqlard.php
"/IfModule"

"/Directory"


(change "" with <>).

Create a user in MYSQL:

GRANT USAGE ON *.* TO mysqlar@localhost;

and then change the user in mysqlar.php.

Check too all paths in these files:

mysqlard.server

mysqlard.cnf
mysqlard_graph

Copy the following files in appropriate crontab folders:
mysqlar.daily
mysqlar.weekly
mysqlar.monthly

You need to add a cron job to generate the graphs. For example :
*/5 * * * * root hourly=1 daily=1 weekly=1 monthly=1\
/usr/bin/mysqlar_graph > /dev/null

Restart apache2 service:

/etc/init.d/apache2 restart

You can access Mysql Activity Report with following url:

http://localhost/mysqlard/mysqlar.php

Apache Server Status

The Status module allows a server administrator to find out how well their server is performing. A HTML page is presented that gives the current server statistics in an easily readable form. If required this page can be made to automatically refresh (given a compatible browser). Another page gives a simple machine-readable list of the current server state.

The details given are:

  • The number of worker serving requests
  • The number of idle worker
  • The status of each worker, the number of requests that worker has performed and the total number of bytes served by the worker (*)
  • A total number of accesses and byte count served (*)
  • The time the server was started/restarted and the time it has been running for
  • Averages giving the number of requests per second, the number of bytes served per second and the average number of bytes per request (*)
  • The current percentage CPU used by each worker and in total by Apache (*)
  • The current hosts and requests being processed (*)
The lines marked "(*)" are only available if ExtendedStatus is On.

To enable status reports only for browsers from the foo.com domain add this code to your /etc/apache2/apache2conf configuration file:

"Location /server-status"
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from foo.com

"/Location"

(change " with <>)

You can now access server statistics by using a Web browser to access the page http://your.server.name/server-status

You can get the status page to update itself automatically if you have a browser that supports "refresh". Access the page http://your.server.name/server-status?refresh=N to refresh the page every N seconds.

A machine-readable version of the status file is available by accessing the page http://your.server.name/server-status?auto. This is useful when automatically run, see the Perl program in the /support directory of Apache, log_server_status.

Wednesday, November 12, 2008

Mount a remote folder in Ubuntu

SSHFS (Secure SHell FileSystem) is a file system for Linux capable of operating on files on a remote computer.

apt-get install sshfs

(Remote host must run the ssh daemon)

sshfs
remoteuser@remotehost:/path/to/remote_dir local_mountpoint

Thursday, November 6, 2008

Zoneminder in Ubuntu

In Ubuntu run:

apt-get install zoneminder

apt-get -f install

modprobe -r zc0301
modprobe -r gspca
modprobe gspca

add this line to /etc/modprobe.d/blacklist:
blacklist zc0301

Give access right to Video Devices:

chmod 777 /dev/video(x)

Thursday, October 23, 2008

Friday, October 17, 2008

Enable SSH in VMware ESX 3.x

ESXi 3.5 does ship with the ability to run SSH, but this is disabled by default (and is not supported). If you just need to access the console of ESXi, then you only need to perform steps 1 - 3.

1) At the console of the ESXi host, press ALT-F1 to access the console window.

2) Enter unsupported in the console and then press Enter. You will not see the text you type in.

3) If you typed in unsupported correctly, you will see the Tech Support Mode warning and a password prompt. Enter the password for the root login.

4) You should then see the prompt of ~ #.

Edit the file inetd.conf (enter the command vim /etc/inetd.conf).

5) Find the line that begins with #ssh and remove the #. Then save the file.

6) Once you've closed the vi editor, run the command /sbin/services.sh restart to restart the management services. You'll now be able to connect to the ESXi host with a SSH client.

Monday, September 22, 2008

Disable Beep in Ubuntu Hardy

Open the file /etc/modprobe.d/blacklist as root in a text editor:


vim /etc/modprobe.d/blacklist

Add this line to the end of the file:

blacklist pcspkr

The next time Ubuntu boots up pcspkr will be blocked from loading and you will no longer hear the beep. Peace and quiet at last!

But if you only need a temporary solution to disable the beep, just run this command (as root):

modprobe -r pcspkr

Thursday, September 18, 2008

Make Hostname permanent in Strato - Debian

Chnage this line in file /etc/hostname.sh:

[ -f /etc/hostname ] && HOSTNAME="$(cat /etc/hostname)"

to:

[ -f /etc/hostname ] && HOSTNAME="YOURHOSTNAME"