sometimes a cigar is just a cigar
Header image

My server errors and optimization steps

Posted by ALonon in Linux

After 10K user for arabuldinle.com, i understood that optimization is very very important. In this article, I’ll explain optimization error that i have faced.  Also i researched some monitoring command such as top,mytop,free,iostat so you can find information about these command in detail.

Session Error

Error code: Warning: session_start() [function.session-start]: open(/var/lib/php5/ …)

Because i’m using lots of session_start() command in a page (it’s a big mistake), php was creating too much sessions and could not delete them. So that my server used all inodes.  (var/lib/php5). Moreover, my server has began to exceed disk io rate. (To delete all sessions: http://www.alonon.net/rm-argument-list-too-long-solution/)

What is inode ? You can think it as a pointer. It point begining of the file.  Each file has different inode: http://www.angelfire.com/myband/binusoman/Unix.html#inode for more information.

As a solution, i tried to store sessions in databases (http://www.devshed.com/c/a/PHP/Storing-PHP-Sessions-in-a-Database/). But because of using ajax, there was a problem about updating sessions. According to me, you can try storing php sesssion in database as a solution. After that i analyse my web sites code. I deleted unnecessary session code and change user group and chmod options of /var/lib/php5. Now It’s ok.

CPU Usage Error

We’re saving all searched data in arabuldinle.com. After more than 500.000 records, inserting a new record took about  2 seconds.  I found an app to view mysql query named mytop. By typing mytop on shell you can display mysql query. It’s working like “top”. In ubuntu you can install mytop with:

sudo apt-get install mytop

With typing

sudo mytop -u mysql_user -p mysql_pass

Mytop display something like that (More about mytop http://jeremy.zawodny.com/mysql/mytop/mytop.html)

With those information i discovered that,  i had to make some changes in my mysql table. Here is the table structure:

id  (primary_key , btree) | title | secondary_id

Problem Query: select id from MYTABLE where title = ‘$title’. It was searching by linear search which takes lots of time (http://en.wikipedia.org/wiki/Linear_search). I define  index to title field. So it started to search on btrees (http://en.wikipedia.org/wiki/Btree) which takes less time. After that operation my cpu usage decreased 40%.

However with more user , i faced with cpu problem again. With “top” command,  which used for displaying cpu process, user www-data was using 80% of cpu.  With changing some variable in apache2.conf , cpu usage decreased again. You can find changes here: http://library.linode.com/troubleshooting/memory-networking. There’s one command that i want to mention: “iostat”. This command is coming with sysstat.

sudo apt-get install sysstat

With typing

iostat

You can view cpu and i/o statistics for devices. For more information http://sebastien.godard.pagesperso-orange.fr/features.html.

Lastly, there is an another command named free. With typing:

free -m

You can display, ram and swap usage for your computer.

free -ms 10

With this you can view memory usage by  10 seconds periods . For more information about free: http://www.linfo.org/free.html

With all these error, i find out lots of new command and learned lots of thing.

I hope with this article helps you.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

2 Responses



Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>