Magento PHP tutorial

In this artice you will find out how to set required PHP options in Magento 2. Firstly, you need to make the following prerequisites before configuring the required PHP options:

Step 1. Make sure PHP is installed by entering the following command:

php -v


Step 2. Check installed extensions by entering the following command:

php -m


You need following modules to be installed:

  • ext-bcmath
  • ext-ctype
  • ext-curl
  • ext-dom
  • ext-gd
  • ext-hash
  • ext-iconv
  • ext-intl
  • ext-mbstring
  • ext-openssl
  • ext-pdo_mysql
  • ext-simplexml
  • ext-soap
  • ext-xsl
  • ext-zip
  • ext-sockets

Step 3. Check PHP settings:

  • Set the system time zone for PHP
  • Set the PHP memory limit.
  • Increase the values for the PHP realpath_cache_size and realpath_cache_ttl to recommended values:
realpath_cache_size=10M
realpath_cache_ttl=7200
  • Enable opcache.save_comments. This is required for Magento 2.1 and newer.

It is recommended to enable PHP OPcache for performance reasons.

Now follow these steps to update the required PHP settings:

Step 1.  When configuring Magento 2 PHP at this point, you need to find the php ini file to proceed with the settings.

Run the phpinfo.php file in your web browser and find the downloaded config file like this:

php settings

To locate the PHP command-line configuration, enter the following command:

php --ini | grep "Loaded Configuration File"


Now you need to find OPcache configuration settings:

These files are usually located in php.ini or opcache.ini. Their location may depend on your operating system and PHP version.

Here's how to find them:

  • Apache web server:

For Ubuntu, OPcache settings are usually located in php.ini.

For CentOS with Apache or Nginx, OPcache settings are usually located in /etc/php.d/opcache.ini.

If not, use the following command to find it:

sudo find / -name 'opcache.ini'
nginx web server with PHP-FPM: /etc/php/7.2/fpm/php.ini

  • nginx web server with PHP-FPM: /etc/php/7.2/fpm/php.ini

Modify all of the opcache.ini if you have more than one.

Step 2. Setting PHP options.

  • Open php.ini in a text editor.
  • Find your server's timezone in the timezone settings
  • Find the next parameter and uncomment it if necessary:
date.timezone =

  • Add the time zone from step 2.
  • Change the memory_limit value. Recommended one:
    memory_limit=2G
  • Add or update the realpath_cache configuration to match the following:
;
; Increase realpath cache size
;
realpath_cache_size = 10 MB

;
; Increase ttl cache realpath
;
realpath_cache_ttl = 7200

  • Save the changes and close the text editor.
  • Open another php.ini file (if different) and repeat all the actions.

Step 3. Set OPcache options.

Open the OPcache configuration file in a text editor:

  • opcache.ini (CentOS)
  • php.ini (Ubuntu)
  • /etc/php/7.2/fpm/php.ini (Nginx web server (CentOS or Ubuntu))
  • Find the opcache.save_comments file and uncomment it if necessary.
  • Make sure its value is set to 1.
  • Save the changes and close the text editor.
  • Restart your web server:
    • Apache, Ubuntu: restarting apache2 service
    • Apache, CentOS: restarting httpd service
    • nginx, Ubuntu and CentOS: restarting the nginx service

What about running external PHP files in Magento 2?
Due to one of the security features of Magento 2, you cannot run the external PHP files by default. 

The file names need to be added in the Nginx configuration to execute or you can allow all external files to run but it can break the security of Magento 2. So we don’t recommend doing it.
Want to get a custom solution without having to design and configure everything yourself? Use our Magento Custom Development Service. Experts will help you develop a high-quality individual solution for your business. Our developers are proficient in a wide stack of technologies, including PHP: Zend (2), YII (2), Laravel (2), Symfony (2), and other well?known frameworks.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2021 Amasty. All Rights Reserved.