Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a higher memory_limit, post_max_size, and for a specific script in Apache/PHP?

Tags:

php

memory

apache

I have a web app that redirects all requests to the virtual host to a controller, which then decides which files to include and run based on the URL. There is a web service that accepts large XML files through POST. I need the memory_limit and post_max_ to be ~32M for the app, except for service that accepts the XML, which will need their limits to be closer to 1024M. ini_set('memory_limit','1024M') works within the script, but won't work for the post_max_size.

I can't seem to figure out how to do it. I was trying something similar to this:

<VirtualHost *:80>
ServerName test.com
DocumentRoot /var/www/test.com/html


php_admin_value include_path .:/var/www/test.com/includes:/var/www/test.com/includes/libs

php_value session.use_only_cookie 1
<Location />
    php_value memory_limit 32M
    php_value post_max_size 30M
    php_value upload_max_filesize 29M
</Location>
<Location /services/big-service>
    php_value memory_limit 1024M
    php_value post_max_size 128M
</Location>

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteCond %{REQUEST_URI} !^/(.*)(html|gif|jpg|png|ico|css|js|dtd|swf|flv|xml)$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/(.*)$ /Controller.php/$1 [PT]

</VirtualHost>

So, with the above config, if Controller.php looks like

<?php
echo '<pre>';
var_dump(ini_get('memory_limit'));
var_dump(ini_get('post_max_size'));
var_dump(ini_get('upload_max_filesize'));

I'll receive string(3) "32M" string(3) "30M" string(3) "29M"

if I visit http://test.com, and http://test.com/services/big-service, but I'd like to see the higher limits for the later URLs.

Does anybody have any solutions to this that don't resort to using and accessing the service script directly through the URL?

like image 589
tandoan Avatar asked Nov 14 '22 22:11

tandoan


1 Answers

The problem is mod_rewrite is causing it to always go to /Controller.php, so the Location block with the services is never used. Using additional mod_rewrite rules

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/services/(.*)$
RewriteCond %{REQUEST_URI} !^/(.*)(html|gif|jpg|png|ico|css|js|dtd|swf|flv|xml)$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/(.*)$ /ControllerBig.php/services/$1 [PT]

And then having ControllerBig be a clone of Controller

<?php
require_once('Controller.php');

will work.

Edit: Full VirtualHost block:

<VirtualHost *:80>
ServerName test.com
DocumentRoot /var/www/test.com/html

php_admin_value include_path .:/var/www/test.com/includes:/var/www/test.com/includes/libs

php_value session.use_only_cookie 1
<Location />
    php_value memory_limit 32M
    php_value post_max_size 30M
    php_value upload_max_filesize 29M
</Location>
<Location /services/big-service>
    php_value memory_limit 1024M
    php_value post_max_size 128M
</Location>

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/services/(.*)$
RewriteCond %{REQUEST_URI} !^/(.*)(html|gif|jpg|png|ico|css|js|dtd|swf|flv|xml)$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/(.*)$ /ControllerBig.php/services/$1 [PT]

RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteCond %{REQUEST_URI} !^/(.*)(html|gif|jpg|png|ico|css|js|dtd|swf|flv|xml)$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/(.*)$ /Controller.php/$1 [PT]

</VirtualHost>
like image 170
tandoan Avatar answered Dec 21 '22 09:12

tandoan