Configuring PHP Settings on a Linux Server

Configuring PHP settings on a LEMP/LAMP server involves modifying the php.ini file. Here's a step-by-step guide to configure PHP settings for all parameters.

Pre-requisites

Ensure you have root privileges on the server and have basic knowledge of working with terminal commands.

Locate php.ini File

You can find the location of your php.ini file by using the following command:

php --ini | grep Configuration

Example Output:

On Ubuntu:

root@vps ~ #  php --ini | grep "Loaded Configuration File"
Loaded Configuration File:         /etc/php/8.3/cli/php.ini

On AlmaLinux:

root@vps ~ # php --ini | grep "Loaded Configuration File"
Loaded Configuration File:         /etc/php.ini

Edit php.ini File

Use your preferred text editor to open the php.ini file. For example, you can use nano or your favorite editor,

For Ubuntu / Debian:

nano /etc/php/{version}/cli/php.ini

Note: Replace {version} with your PHP version, such as 7.4, 8.0, etc

For AlmaLinux / CentOS / Rocky Linux:

nano /etc/php.ini

Adjust PHP Settings

Within the php.ini file, you'll find various settings. Modify the values according to your requirements. Here are some common parameters you may want to adjust,

  • memory_limit: Sets the maximum amount of memory in megabytes that a script is allowed to allocate.
  • upload_max_filesize: Sets the maximum size of files that can be uploaded.
  • max_execution_time: Sets the maximum time in seconds a script is allowed to run before it is terminated.
  • post_max_size: Sets the maximum size of POST data that PHP will accept.
  • error_reporting: Sets which PHP errors are reported.
  • display_errors: Determines whether errors should be displayed to the screen.
  • date.timezone: Sets the default timezone used by PHP.

Here's an example of how these settings might look in your php.ini file,

memory_limit = 256M
upload_max_filesize = 32M
max_execution_time = 60
post_max_size = 48M
error_reporting = E_ALL & ~E_NOTICE
display_errors = Off
date.timezone = "America/New_York"

Save and Exit: After making the necessary changes, save the file and exit the text editor.

Restart PHP Service

To apply the changes, you need to restart the web server. The command may vary depending on the OS:

For Restarting Apache web server:

On Ubuntu / Debian :

systemctl restart apache2

On AlmaLinux / CentOS / Rocky Linux:

systemctl restart httpd

For Restarting Nginx web server:

systemctl restart nginx

For Restarting PHP-FPM:

systemctl restart php-fpm

You have to mention the version if you have a specific version installed, example: systemctl restart php8.3-fpm

Verify Changes

After restarting the service, you can verify that the changes have taken effect by creating a PHP file with phpinfo() function and accessing it via a web browser. For example,

<?php phpinfo(); ?>

Save this file in your web server's document root directory and access it via a browser. You should see the updated PHP configuration settings reflected in the output.

images

By following these steps, you can configure PHP settings for all parameters on your LEMP/LAMP server.