Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Thu Oct 08, 2009 5:09 pm 
Offline
Senior Member

Joined: Thu Oct 08, 2009 5:07 pm
Posts: 99
Hi!

I just got my new Linode to host a few popular applications made with PHP, MySQL and Apache.

Does anyone have any advice for optimizing for high performance? I've been using xdebug to optimize my code, and I'm looking more for settings like httpd.conf and my.cnf.

Thanks!


Top
   
 Post subject:
PostPosted: Thu Oct 08, 2009 7:13 pm 
Offline
Senior Member

Joined: Fri May 02, 2008 8:44 pm
Posts: 1121
Some general suggestions, which may or may not apply to your setup:

1) Install a PHP accelerator, such as APC, XCache, or eAccelerator. You can get twice the performance, almost for free. It's a no-brainer!

2) Pay particular attention to the number of Apache processes you spawn (MaxClients, MaxWorkers, etc.) Don't know which linode plan you're on, but limiting MaxClients to 15 would be a good start. Some distros have insanely high defaults which quickly lead to resource exhaustion.

3) If you're using mpm_prefork with mod_php, you'd be loading up the whole PHP interpreter even when you serve simple static files (such as images, css, javascript). Consider switching to mpm_worker with mod_fcgid. Even better, offload static files to lighttpd or nginx. This is very important for memory-limited environments such as a linode.

4) If you have custom scripts that generate complicated SQL queries, cache the results so that you don't have to run the same queries every time. You can use files for this purpose, or perhaps something a little more elaborate such as Memcached. If you don't have any custom scripts, see if your "popular application" has a caching module/add-on, such as WP Super Cache for WordPress.

5) Give MySQL a generous amount of key_buffer, but don't give it too much or you'll run out of memory.

Cheers,


Top
   
 Post subject:
PostPosted: Thu Oct 08, 2009 7:26 pm 
Offline
Senior Member

Joined: Thu Oct 08, 2009 5:07 pm
Posts: 99
Thanks hybinet :)

I did not know that about it loading the whole of PHP for every static request.

Am I right in saying that using a reverse proxy with Squid for dynamic content is pointless?

Can you run lighthttpd together with apache on the same server?


Top
   
 Post subject:
PostPosted: Thu Oct 08, 2009 10:15 pm 
Offline
Senior Member

Joined: Mon Apr 27, 2009 7:36 pm
Posts: 59
Website: http://www.xenscale.com
Location: Boise, ID
anything you can get into memcached ...do it.


Top
   
 Post subject:
PostPosted: Thu Oct 08, 2009 10:36 pm 
Offline
Senior Member
User avatar

Joined: Fri Oct 24, 2003 3:51 pm
Posts: 965
Location: Netherlands
jonny5alive wrote:
Am I right in saying that using a reverse proxy with Squid for dynamic content is pointless?

Yes.

jonny5alive wrote:
Can you run lighthttpd together with apache on the same server?

Yes - many Linoders offload static content to lighttpd or nginx. Using Apache to do everything can chew up a lot of memory.

_________________
/ Peter


Top
   
 Post subject:
PostPosted: Fri Oct 09, 2009 7:19 am 
Offline
Senior Member

Joined: Sun Aug 02, 2009 1:32 pm
Posts: 222
Website: https://www.barkerjr.net
Location: Connecticut, USA
hybinet wrote:
3) If you're using mpm_prefork with mod_php, you'd be loading up the whole PHP interpreter even when you serve simple static files (such as images, css, javascript). Consider switching to mpm_worker with mod_fcgid. Even better, offload static files to lighttpd or nginx. This is very important for memory-limited environments such as a linode.
This would only happen when creating a new child, right? So, all the more reason to make sure your apache children is set to something higher like 50.


Top
   
 Post subject:
PostPosted: Fri Oct 09, 2009 9:44 am 
Offline
Senior Member

Joined: Fri May 02, 2008 8:44 pm
Posts: 1121
BarkerJr wrote:
This would only happen when creating a new child, right? So, all the more reason to make sure your apache children is set to something higher like 50.

In that case, you would be loading up 50 instances of the PHP interpreter when you start the server. Of course a lot of RAM is shared between those children, but still a helluva large waste of precious RAM. Think of it: 12-15MB of RAM dedicated to serving nothing but a 12-15KB css/js file? Besides, with MaxClients=50 on anything smaller than a Linode 1080, you'll probably end up running out of RAM as soon as you get enough visitors to make use of all those Apache chidren.


Top
   
 Post subject:
PostPosted: Fri Oct 09, 2009 3:02 pm 
Offline
Senior Member

Joined: Sun Aug 31, 2008 4:29 pm
Posts: 177
hybinet wrote:
In that case, you would be loading up 50 instances of the PHP interpreter when you start the server. Of course a lot of RAM is shared between those children, but still a helluva large waste of precious RAM. Think of it: 12-15MB of RAM dedicated to serving nothing but a 12-15KB css/js file? Besides, with MaxClients=50 on anything smaller than a Linode 1080, you'll probably end up running out of RAM as soon as you get enough visitors to make use of all those Apache chidren.


MaxClients is the maximum number of Apache processes, not the number that is created when apache is started.

I would suggest that most of the memory used by an Apache process is in fact shared. On my systems I can watch Apache spin up 5 additional processes, while overall memory consumption increases by maybe 1 MB (at most).

The amount of memory consumed by an Apache PHP process is determined in large part by what it's doing. If it's processing a 'hello world' script then memory usage is minimal. If it's processing some monster script then memory usage is a heck of a lot more.


Top
   
 Post subject:
PostPosted: Sun Oct 11, 2009 7:05 pm 
Offline
Senior Member

Joined: Sun Aug 02, 2009 1:32 pm
Posts: 222
Website: https://www.barkerjr.net
Location: Connecticut, USA
Another thought. It seems that the Apache in the repository loads every possible module. Most of which you don't use. Maybe we should comment out unused modules.


Top
   
 Post subject:
PostPosted: Wed Oct 14, 2009 12:11 pm 
Offline
Senior Member

Joined: Thu Oct 08, 2009 5:07 pm
Posts: 99
Has anyone experience of RLimitMem in Apache?

http://httpd.apache.org/docs/2.2/mod/core.html#rlimitmem

The majority of my PHP hits are small but have to wait for an API for a second or two (unavoidable), so I'd like to set my ServerLimit fairly high. However there are some PHP hits that do some graphics work and can use 10MB extra, and if they happen to hit at the same time it will be swapdeath.

Anyone care to discuss if this is a good idea? One day everything will work great.[/url]


Top
   
 Post subject:
PostPosted: Wed Oct 14, 2009 5:09 pm 
Offline
Senior Member
User avatar

Joined: Tue May 26, 2009 3:29 pm
Posts: 1691
Location: Montreal, QC
That sounds like a good way to crash PHP. Shouldn't you be setting PHP memory limits in php.ini?

Or, switch to nginx/lighttpd and use fastcgi.


Top
   
 Post subject:
PostPosted: Wed Oct 14, 2009 5:14 pm 
Offline
Senior Member

Joined: Thu Oct 08, 2009 5:07 pm
Posts: 99
I believe the PHP memory limit is per PHP process.

If I have many visitors using a PHP script that unavoidably waits on an external API, but doesn't use much memory, I could run for example 100 of those without running out of memory. Occassionally, a visitor uses another PHP script that uses say 50MB of graphics memory to create a chart, so I couldn't run 100 of that script.

How would nginx/lighttpd and fastcgi help in this situation?


Top
   
 Post subject:
PostPosted: Thu Oct 15, 2009 12:01 pm 
Offline
Senior Member
User avatar

Joined: Tue May 26, 2009 3:29 pm
Posts: 1691
Location: Montreal, QC
It'd separate memory usage from the web server.

My concern was more, limiting memory usage by a PHP script should happen in PHP, not the web server. Otherwise PHP is going to try to allocate more RAM and the web server is going to deny it.

I've no idea what happens then, but it can't be good.


Top
   
 Post subject:
PostPosted: Thu Oct 15, 2009 12:55 pm 
Offline
Senior Member

Joined: Thu Oct 08, 2009 5:07 pm
Posts: 99
I tried it out, and it gives a nasty 500 Internal Server Error, that could be customized.

Setting up APC correctly has fixed my problems. I set up apc.php on my web server which gave graphs and statistics, and found increasing APC memory from 30MB to 60MB has resulted in processes being executed more quickly and CPU use down from 108% to 12% :)


Top
   
 Post subject:
PostPosted: Sat Oct 31, 2009 7:31 pm 
Offline
Senior Newbie

Joined: Mon Oct 12, 2009 3:30 am
Posts: 15
good read: http://www.mysqlperformanceblog.com/200 ... tallation/


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group