Why Are PHP Settings Critical for Your Website?
Many WordPress, Joomla, and other content management system websites run on PHP. If you've ever encountered the error Fatal Error: Allowed Memory Size Exhausted or Upload: Failed to Write File to Disk, the root cause lies in incorrect PHP settings. In this article, we will practically teach you, with real-world examples, how to change the PHP version, increase memory limit (memory_limit) and upload size (upload_max_filesize), and enable essential extensions. This guide is applicable for users of shared hosting (cPanel or DirectAdmin) and virtual servers (VPS).
Changing PHP Version: Why and How?
Each PHP version has different features, security improvements, and compatibility. For example, WordPress 6 requires PHP 7.4 or higher. If you use older scripts, you may encounter errors with newer versions. Below, we review two common methods.
Changing PHP Version in Shared Hosting (cPanel)
- Log into your cPanel account.
- Go to the Software section and select Select PHP Version.
- From the dropdown menu, choose the desired version (e.g.,
8.1or8.2). - Click Set as current.
- On the same page, you can check or uncheck active extensions.
Important Note: If your website uses old code, first test it in a staging environment. Suddenly changing the PHP version may cause a White Screen of Death.
Changing PHP Version on a Virtual Server (VPS) with Nginx
On virtual servers using Nginx and PHP-FPM, you need to edit the site configuration file. Assume the current PHP version is 7.4 and you want to migrate to PHP 8.1:
# First, install the new version (on Ubuntu/Debian)
sudo apt update
sudo apt install php8.1-fpm php8.1-mysql php8.1-xml php8.1-mbstring
# Then, edit the site configuration file
sudo nano /etc/nginx/sites-available/example.com
# Find the following line and change the version:
# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# To:
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
# After saving, restart Nginx
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm
Common Mistake: Don't forget to enable the new PHP-FPM service (sudo systemctl enable php8.1-fpm) and disable the old one after the change.
Increasing memory_limit and upload_max_filesize
Two key parameters in PHP settings that directly affect website performance are memory_limit and upload_max_filesize. Common errors like Allowed Memory Size Exhausted usually occur due to a low memory_limit value.
Method 1: Editing the php.ini File (Root Access)
If you have root access to the server, locate the php.ini file. The typical path on Ubuntu/Debian is:
sudo nano /etc/php/8.1/fpm/php.ini
Then, search for and change the following values:
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 68M # Should be slightly larger than upload_max_filesize
max_execution_time = 300
max_input_time = 300
After saving, restart the PHP-FPM service:
sudo systemctl restart php8.1-fpm
Method 2: Using the .htaccess File (Shared Hosting)
On shared hosting where you don't have access to php.ini, you can use the .htaccess file. This method works for Apache:
# Increase memory_limit
php_value memory_limit 256M
# Increase upload size
php_value upload_max_filesize 64M
php_value post_max_size 68M
# Increase execution time
php_value max_execution_time 300
Add these lines to the end of the .htaccess file in the site root. If the file doesn't exist, create it.
Common Mistake: Some users set post_max_size lower than upload_max_filesize, causing upload errors. Always set post_max_size 2 to 4 MB higher than upload_max_filesize.
Method 3: Using the wp-config.php File (WordPress)
In WordPress, you can apply settings directly in the wp-config.php file. Add these lines before the /* That's all, stop editing! */ line:
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M'); // For admin pages
This method only affects WordPress and does not work for other scripts.
Enabling Essential PHP Extensions
Many errors during plugin or theme installation are due to disabled PHP extensions. Below, we introduce the most important extensions.
Common Extensions for WordPress and Joomla
- mysqli: For connecting to MySQL/MariaDB databases.
- curl: For communicating with external APIs and automatic updates.
- gd or imagick: For image processing (generating thumbnails, resizing).
- mbstring: For supporting multi-byte characters (Persian, Arabic).
- xml and simplexml: For processing RSS and XML feeds.
- zip: For installing plugins and themes via ZIP file upload.
- openssl: For secure connections (SSL/TLS).
Enabling Extensions in cPanel
In Select PHP Version, after selecting the PHP version, you will see the list of extensions. Check the required extensions and click Save. Usually, no restart is needed.
Enabling Extensions on a Virtual Server (Command Line)
To install extensions on Ubuntu/Debian:
sudo apt install php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-zip
sudo systemctl restart php8.1-fpm
To check active extensions, create an info.php file with the following content:
<?php
phpinfo();
?>
Then, open it in a browser and check the Loaded Configuration File and Extensions sections. After finishing, be sure to delete this file to prevent server information from being exposed.
Troubleshooting Common PHP Settings Errors
In this section, we address the most common errors and their solutions.
Error: "Fatal Error: Allowed Memory Size of X Bytes Exhausted"
This error indicates that memory_limit is insufficient. The default value is usually 32M or 64M. Increase it to 128M or 256M. If the problem persists, a faulty plugin or theme may be consuming memory.
Error: "Upload: Failed to Write File to Disk"
This error usually occurs due to low upload_max_filesize or post_max_size. It could also be that the /tmp directory is full. Check disk space with the df -h command.
Error: "Headers Already Sent"
This error occurs when output (such as whitespace or BOM) is sent before the header() function. Save PHP files with a proper editor (like VS Code or Notepad++) and avoid using BOM (Byte Order Mark).
Security Tips in PHP Settings
After applying PHP settings, be sure to follow these security measures:
- Disable error display in production: In the
php.inifile, setdisplay_errors = Offandlog_errors = On. - Limit dangerous functions: Use
disable_functionsto disable functions likeexec(),system(), andshell_exec(). - Use open_basedir: Restrict PHP access to specific directories.
To apply these settings on shared hosting, you usually need to contact support. On a virtual server, you can directly edit php.ini.
Summary
In this article, we practically taught you how to change the PHP version, increase memory_limit and upload_max_filesize, and enable essential extensions. By applying these settings, you can prevent common errors and improve your website's performance. If you use shared hosting and don't have access to advanced settings, you can benefit from ServerNet's web hosting services, which support the latest PHP versions. Remember to always back up your website files and database before making any changes.