How To Increase PHP Memory Limit

Increase PHP Memory Limit

In this tutorial, we will show you how to increase the PHP memory limit on a Linux server. If you have seen an error like ”Fatal Error: PHP Allowed Memory Size Exhausted” in webserver logs or your browser, this means that PHP has exhausted the maximum memory limit. This tutorial will show two different ways how you can increase the PHP memory limit.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges.

Prerequisites

  • A server running one of the following operating systems: CentOS, AlmaLinux, or Rocky Linux.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Increase PHP Memory Limit

Step 1. First, let’s start by ensuring your system is up-to-date.

sudo dnf clean all
sudo dnf update

Step 2. Check PHP Memory Limit

You can check by creating a file called info.php in /var/www/html/ with the following content:

<?php
phpinfo();
?>

 Increase PHP Memory Limit

Step 3. Increase PHP Memory Limit on Linux system.

Method 1. Changing the memory limit from php.ini

First, find locate the php.ini file used by your web server.

nano /etc/php.ini

Search “memory_limit” in your php.ini, and change it. If no “memory_limit” is found, add the following line at the end of php.ini

memory_limit = 64M ;Maximum amount of memory a script may consume (64MB)

Restart Nginx or Apache webserver:

sudo systemctl restart httpd
or
sudo systemctl restart nginx

Method 2. Changing memory limit using .htaccess

Find the “.htaccess” in your root directory of the specified domain, and edit the .htaccess file in the webroot directory. Look for the section:

php_value memory_limit 64M ;Maximum amount of memory a script may consume (64MB)

Congratulations! You have successfully increased the PHP memory. Thanks for using this tutorial to change the PHP memory limit Linux system. For additional help or useful information, we recommend you check the official PHP website.

PHP Install Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “PHP and Webserver” installation, starting from $25 (Paypal payment). Please contact us to get the best deal!