Wednesday, January 28, 2009

Ubuntu Number of Files

With help of this command you could find the number of open files by each program:

lsof -n|grep -oE '^[a-z]+'|sort|uniq -c|sort -n

This command shows the Resources limitaions and number of fails:

cat /proc/user_beancounters

Wednesday, January 14, 2009

Winscp and ssh user with jail for Website

Look at this very useful page:

Download from there,

and add in the line useradd ... the desired group (e.g.: -g www-data).

Then run script as follows:

./ USERNAME /var/www/vhosts/YOURDOMAIN.COM/.bashrc /var/www/vhosts/YOURDOMAIN.COM

Change the access rights for desired folder(s):

chown 775 /var/www/vhosts/YOURDOMAIN.COM/htdocs -R

That's all!

Webpage Password Protection (.htaccess)

Edit the .htaccess file and add these lines to it:

AuthName "Member's Area Name"
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
require valid-user

Change the file mode:

chmod 755 .htaccess

Go to this site and generate your encrypted password.

Copy and paste it to .htpasswd file (It's recommended that, you create this file in another directory).

Go to web page, you must give your username and password to enter it!

Mysql Performance Tuning - 1

Command: mysqladmin -u root -p extended

The values making most sense to monitor are:

Slave_running: If the system is a slave replication server, this is an indication of the slave's health.
Threads_connected: The number of clients currrently connected. This should be less than some preset value (like 200), but you can also monitor that it is larger than some value to ensure that clients are active.
Threads_running: If the database is overloaded you'll get an increased number of queries running. That also should be less than some preset value (20?). It is OK to have values over the limit for very short times. Then you can monitor some other values, when the Threads_running was more than the preset value and when it did not fall back in 5 seconds.
Aborted_clients: The number of clients that were aborted (because they did not properly close the connection to the MySQL server). For some applications this can be OK, but for some other applications you might want to track the value, as aborted connects may indicate some sort of application failure.
Questions: Number of queries you get per second. Also, it's total queries, not number per second. To get number per second, you must divide Questions by Uptime.
Handler_*: If you want to monitor low-level database load, these are good values to track. If the value of Handler_read_rnd_next is abnormal relative to the value that you normally would expect, it may indicate some optimization or index problems. Handler_rollback will show the number of queries that have been rolled back. You might want to wish to investigate them.
Opened_tables: Number of table cache misses. If the value is large, you probably need to increase table_cache. Typically you would want this to be less than 1 or 2 opened tables per second.
Select_full_join: Joins performed without keys. This should be zero. This is a good way to catch development errors, as just a few such queries can degrease the system's performance.
Select_scan: Number of queries that performed a full table scan. In some cases these are OK but their ratio to all queries should be constant. if you have the value growing it can be a problem with the optimizer, lack of indexes or some other problem
Slow_queries: Number of queries longer than --long-query-time or that are not using indexes. These should be a small fraction of all queries. If it grows, the system will have performance problems.
Threads_created: This should be low. Higher values may mean that you need to increase the value of thread_cache or you have the amount of connections increasing, which also indicates a potential problem.

Command: mysqladmin -u root -p processlist

You can get the number of threads connected and running by using other statistics, but this is a good way to check how long queries that are running take. If there are some very long-running queries (e.g. due to being badly formulated) the admin should be informed. You might also want to check how many queries are in "Locked" state - these are not counted as running but are inactive, i.e. a user is waiting on the database to respond.

Command: mysql -u root -p -e "SHOW INNODB STATUS"

This statement produces a great deal of information, from which you should extract the parts in which you are interested. The first thing you need to check is: "Per second averages calculated from the last xx seconds". InnoDB rounds stats each minute.

Pending normal aio reads: These are InnoDB IO request queue sizes. If they are bigger than 10-20 you might have some bottleneck.
reads/s, avg bytes/read, writes/s, fsyncs/s: These are IO statistics. Large values for reads/writes means the IO subsystem is being loaded. Proper values for these depend on your system configuration.
Buffer pool hit rate: The hit rate also depends a lot on your application. Check your hit rate, when there are problems.
inserts/s, updates/s, deletes/s, reads/s: These are low level row operations that InnoDB does. You might use these to check your load if it is in expected range.

You can examine the state of the cache from the mysql command line:

mysql> show status like 'qcache%';

| Variable_name | Value |
| Qcache_free_blocks | 9990 |
| Qcache_free_memory | 34431360 |
| Qcache_hits | 2165383 |
| Qcache_inserts | 461500 |
| Qcache_lowmem_prunes | 113692 |
| Qcache_not_cached | 1894 |
| Qcache_queries_in_cache | 28203 |
| Qcache_total_blocks | 66628 |
8 rows in set (0.00 sec)

Here we can see that there have been 2 million cache hits and that about half of the allocated cache is still available (free_memory). The cache has been flushed (lowmem_prunes), so it might be useful to increase the value of cache_size in my.cnf slightly.

Tuesday, January 13, 2009

Change Crontab Editor

env EDITOR=vim crontab -e


add this line in ~/.bashrc:

export EDITOR=vim

Apache Tuning




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






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.



The number of child processes to create on startup.



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.



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



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:





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]


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.



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.



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.



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



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


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.


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



ab (Apache Benchmark)


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 = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = multi_admin
password = multi

Add new instance parameters:

socket = /var/run/mysqld/mysql3.sock
port = 3308
pid-file = /var/run/mysqld/
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