Increase WordPress Memory Limit and PHP Memory Limit – PHP 5.3 with DreamHost

by Hiroshi on July 3, 2012

in WordPress

As a website grows in articles and attracts more visitors, it face low php memory or connections limit problems. You move from shared server to virtual private server and then dedicated server but problem remains same (Yes – for some reason). Often your website hits internal server error, or client exceeded concurrent connection limit problem. This means you lose traffic and valuable users. That drives you insane.I had the same issue but I have figured it out (at least for now). I was on the verge of changing my host. But the problem is not the host. Sometimes little research does the trick. What I wanted to do is to increase wp memory limit to 256M and increase php memory (from php.ini) to 256 as well. Now php memory can be increased from php.ini and wordpress memory can be increased by tweaking script in wordpress. I have dreamhost and a dedicated managed server. I don’t have access to the php.ini but here is a way to sort this out.

Install ‘TPC! Memory Usage‘ plugin to see what is current php memory and wp memory limits for your blog.

To Check PHP variables on your server you will need a file.

Create a simple php file (e.g. phpinfo.php) and paste the following code in it.

<?php phpinfo(); ?>

Now browse this file, e.g. yourwebsite.com/phpinfo.php

Search for the following lines in that file and see their values.

max_execution_time
max_input_time
memory_limit (should be 32M, 64M, 90M, 128M or 256M or even more)
display_errors (should be set to off)
allow_url_fopen (should be set to off)

You have to change their values if required. In this case i wanted to change memory_limit value to 256M.

php.ini Changes for PHP Memory Limit

Find php.ini file in your host and search for the following:

memory_limit

Modify its value.

In my case i modified it to 256M.

e.g.

memory_limit = 256M;

But my case was not simple as I could not find php.ini file in my host because I use Dreamhost and in managed hosting they do not allow user to mess with the php configuration files.

I had to do few things to get php.ini working for me.

I had to change my php verion to 5.3 and then create directories (/.php/5.3) under my user and then created a file named as phprc with code I wanted to modify in my php.ini file. You can create those folders using FTP.

I added the following line in the file.

memory_limit = 256M;

Then I restarted my MySQL Machine and Dedicated Server to see changes.

Further read custom php.ini in DreamHost for dedicated server and php 5.3.

Increase WordPress Memory Limit by Changing Code in WordPress File

In wordpress installation folder go to wp-includes folder and search for default-constants.php file.

From line number 19 to 29, look for WP_MEMORY_LIMIT value and change it to 256M.

Original Code:

	if ( !defined('WP_MEMORY_LIMIT') ) {
		if( is_multisite() ) {
			define('WP_MEMORY_LIMIT', '64M');
		} else {
			define('WP_MEMORY_LIMIT', '32M');
		}
	}
 
	if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
		define( 'WP_MAX_MEMORY_LIMIT', '256M' );
	}

Change above code to:

	if ( !defined('WP_MEMORY_LIMIT') ) {
		if( is_multisite() ) {
			define('WP_MEMORY_LIMIT', '256M');
		} else {
			define('WP_MEMORY_LIMIT', '256M');
		}
	}
 
	if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
		define( 'WP_MAX_MEMORY_LIMIT', '256M' );
	}

The above method worked for me.

WP-CONFIG.php Code to Change WordPress Memory Limit

Add this code in wp-config file, to increase the memory limit of WordPress.

define('WP_MEMORY_LIMIT', '256M');

Alone this will not work. It looks like it has no effect in changing memory at all. It did not work for me. But modifying default-constants.php file worked for me.

Increase PHP Memory Limit using .HTACCESS file

Add the following line in .htaccess file in root directory of your website.

php_value memory_limit 256M

.htaccess is hidden in FTP, so you will have to unhide it by changing FTP settings so that it may show hidden files as well.

But this method made no difference for me as well.

Further Reference

Related Posts

Previous post:

Next post: