09.08.2022

Customize php ini file. PHP installation. Choose one of the options


PHP is one of the most popular programming languages ​​for creating websites and web applications. It has developed many ready-made content management systems for blogs, company websites or even online stores. Despite the fact that this language has its drawbacks, it is quite easy to learn and therefore it is very often used to develop new sites.

The php interpreter can be supplied as a module for Apache, execute scripts from the command line, or as a standalone php-fpm service. These services differ in their capabilities and purpose, but for any kind of interpreter, you need to set basic settings, for example, a working folder, included extensions, error display, and so on. All these settings are set through the php.ini file. In this tutorial, we will look at how to set up the php.ini file on Linux operating systems, although all the information will also work for Windows.

If you have not yet installed the php programming language interpreter, then you can read the article.

For each version of the interpreter, the php.ini configuration file is located in a separate folder. But all configuration files are located in the /etc/php folder, for example, /etc/php5:

The conf.d folder contains general settings for various extensions and modules, we will not be interested in them now. More interesting are the following three folders - apache, cli and fpm. They contain the php.ini configuration files for each of these interpreters.

If you are going to use several of these interpreters, then you will have to specify the settings for each of them separately. You can make sure that each of the folders contains a php.ini file.

As for the syntax of the file, it is divided into sections, first comes the php settings section, which is divided into subsections depending on the type of settings, then there are sections for setting different modules. The syntax of the settings themselves is very simple, it corresponds to the usual syntax of ini files. The line starts with the name of the setting, followed by an equal sign, followed by the value:

setting_name = parameter_value

The symbols denote the name of the section, for example, , and the symbol; means a comment, it and all characters after it are not readable by the interpreter. And now let's look at how php.ini is configured and go over the most important parameters.

Setting up the php.ini file

For ease of orientation, we will break all the parameters into categories depending on their purpose. It will be enough for you to find the desired parameter and change its value. And now open the php settings file, for example, for the apache module and move on to the settings. To avoid errors, do not add new lines, but look for existing ones and change the values ​​to the ones you need:

sudo gedit /etc/php5/apache/php.ini

First, there is some information about the file itself in the form of comments, then the settings that interest us.

Error output in php

Setting up php 7 usually starts with error output configuration. All error output settings are located in the Error handling and logging section. By default, displaying errors on the screen during script execution is disabled. This is done so that users cannot see anything superfluous. Instead, all errors are written to a log file. If you use php on your home computer, then such measures are not needed and you can immediately display everything on the screen:

display_errors=off

Replace off with on. PHP uses various types of errors, such as fatal, warnings, syntax errors, with the error_reporting line you can enable the output of only certain types of errors:

error_reporting = E_ALL & ~E_DEPRECATED

If you need to combine several types of errors, then use the symbol &, and to disable the display, precede the type with a ~ sign. The above example displays all errors (E_ALL) except messages about deprecated functions (E_DEPRECATED). You can disable all types by using 0:

error_reporting=0

Turn on php error logging if you don't print them to the screen:

In order not to clog the log with messages of the same type, you can ignore repeated errors within the same execution:

ignore_repeated_errors = On

Resource limits

If php scripts were not limited in resources in any way, then they could easily overload the server and prevent it from working normally. Therefore, php sets hard limits by default, but you may need to loosen them a bit.

By default, the maximum script execution time is 30 seconds, let's make a minute:

max_execution_time = 30

If you specify 0, then the script can run indefinitely. You can also limit the time during which the script will download data to 60 seconds:

max_input_time=60

Maximum number of variables in GET and POST:

max_input_vars = 1000

The following parameter specifies the maximum amount of memory that one script can use during its execution, in megabytes:

memory_limit = 128M

The maximum size of data transmitted in a POST request is also limited, the default size is 8 MB:

post_max_size = 8M

You can limit the scope of php on the system with the openbase_dir option, it specifies a folder above which the script cannot access the file system:

open_basedir = /var/www/

With the disable_functions and disable_classes directives, you can disable the use of certain functions or classes in scripts, for example, this can be useful for web hosts. In this example, we disable the use of the ini_set function, which allows you to change php settings from a script:

disable_functions = ini_set

Default directories

The php.ini configuration file allows you to specify paths in the default file system for various actions. You can specify folders where the system will look for scripts if you try to include them using the include statement:

include_path = ".:/usr/share/php5:/usr/share/php5/PEAR"

Folder with php modules:

extension_dir="./"

Folder for writing temporary files:

sys_temp_dir = "/tmp"

In order for users to upload their files to the server, for example, photos, you need to enable this function in php:

file_uploads = On

Maximum file upload size:

upload_max_filesize = 2M

The maximum number of files that one script can upload:

max_file_uploads = 20

The php.ini setup is almost complete, we only have extensions left.

Setting up extensions

Extensions allow you to greatly increase the functionality of php. For example, thanks to extensions, you can use the mysql, postgresql, mysqli, sqlite databases, the gd graphics library, and much more in your scripts. All of these are included in this section.

To enable an extension, just remove the comment before the line with its command, for example:

extension=php_mysql.so
extension=php_mbstring.so
extension=php_pgsql.so

Please note that for windows the extension will be in dll format, but for linux you need to use so. The following sections go through the configuration of each of the extensions, but we will not consider them because they usually do not require configuration.

conclusions

In this article, we looked at how php is configured on a server or a regular computer for developing websites. The php settings file has a fairly simple structure and is fairly easy to deal with. After completing all the settings and saving the changes, do not forget to restart the web server or the php-fpm service.

Generally speaking, php-fpm is a separate topic, because there are many additional settings, and perhaps we will consider it in one of the following articles. If you have any questions, ask in the comments!


Direct link: php-5.3.10-Win32-VC9-x86.zip
At the same time, immediately download the documentation in Russian in .chm format, you will need it when studying and working: php_enhanced_ru.chm

Unzip the archive to the desired directory (initially "C:\php" is suggested). Open the configuration file containing the recommended settings - "php.ini-development" (located in the distribution root), rename it to php.ini and make the following changes.

php.ini revision:

  1. Find the line:
    post_max_size = 8M
    Increase the maximum data size accepted by the POST method to 16 MB by changing it to:
    post_max_size = 16M
  2. Find the line:
    ;include_path = ".;c:\php\includes"
    Uncomment it by removing the semicolon before the line.
    (Attention exception! Backslashes when specifying a path):
    include_path = ".;c:\php\includes"
    Create an empty directory "C:\php\includes" to store the included classes.
  3. Find the line:
    extension_dir = "./"
    Set the value of this directive to the path to the folder with extensions:
    extension_dir = "c:/php/ext"
  4. Find the line:
    ;upload_tmp_dir =
    Uncomment it and specify the following path in the value:
    upload_tmp_dir = "C:/php/upload"
    Create an empty folder "C:\php\upload" to store temporary files uploaded via HTTP.
  5. Find the line:
    upload_max_filesize = 2M
    Increase the maximum allowed file upload size to 16MB:
    upload_max_filesize = 16M
  6. Connect, uncommenting, the data of the extension library:
    extension=php_bz2.dll
    extension=php_curl.dll
    extension=php_gd2.dll
    extension=php_mbstring.dll
    extension=php_mysql.dll
    extension=php_mysqli.dll
  7. Find the line:
    ;date.timezone =
    Uncomment and set the value to the timezone of your location (see the documentation for a list of timezones):
    date.timezone = "Europe/Moscow"
  8. Find the line:
    ;session.save_path = "/tmp"
    Uncomment and set the value of this directive to the following path:
    session.save_path = "C:/php/tmp"
    Create an empty folder "C:\php\tmp" to store temporary session files.
Save your changes and close the php.ini file.

Next, you need to add the directory with the installed PHP interpreter to the PATH of the operating system. To do this, follow the path "Start" ("Start") -> "Control Panel" ("Control Panel") -> "System" ("System"), open the tab "Advanced" ("Advanced"), click the button " Environment Variables", in the "System Variables" section, double-click on the "Path" line, add in the "Variable Value" field, in addition to what is already there exists, path to PHP installation directory, for example, "C:\php" (without quotes). Note that the semicolon character separates the paths. For the changes to take effect, restart the operating system.

Path string example:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\php;C:\Program Files\MySQL\MySQL Server 5.5\bin

Installing and configuring the PHP interpreter is now complete.

Description of the included libraries:

php_bz2.dll– With this extension, PHP will be able to create and unpack archives in bzip2 format.

php_curl.dll- A very important and necessary library that allows you to connect and work with servers using a huge number of Internet protocols.

php_gd2.dll- Another indispensable library that allows you to work with graphics. You thought PHP can only generate HTML pages? But no! Almost everything can be done with PHP, including drawing.

php_mbstring.dll– The library contains functions for working with multi-byte encodings, which include encodings of Oriental languages ​​(Japanese, Chinese, Korean), Unicode (UTF-8) and others.

php_mysql.dll- The name of the library speaks for itself - it is necessary to work with the MySQL server.

php_mysqli.dll– This library is an extension of the previous one and contains additional PHP functions for working with the MySQL server version 4.1.3 and higher.

These libraries should be enough for the full-fledged work of PHP. Over time, if the need arises, you will be able to include additional libraries, but you should not include them all at once with the thought that you won’t spoil the porridge with butter, in this case, an excessive number of included libraries can noticeably slow down PHP.

«

PHP is one of the most popular programming languages ​​used to create Web sites.

Download the distribution kit from the official PHP website http://www.php.net ( I have - php 5.1.6 ). Unzip the archive to the C:\Program Files\php directory. Find the config file php.ini, containing the PHP interpreter settings, and make the following changes to it:

    register_globals directive php 5.1.6 enabled by default ( recommended):

    register_globals = on

    Set the maximum amount of data sent by the POST method to 16M :

    post_max_size = 16M

    Find the line:

    ;include_path = ".;c:\php\includes"

    uncomment it ( why remove the semicolon symbol ; at the beginning of the line) and fix to:

    include_path = ".;C:\Program Files\PHP\PEAR"

    You need to set the extension_dir parameter to the directory where the PHP distribution is located:

    extension_dir = "C:/Program Files/php/ext"

    Set the maximum size of uploaded files to 16M:

    upload_max_filesize = 16M

    The doc_root parameter must be passed the value of the DocumentRoot directive of the Web server:

    doc_root = "d:/main/html"

    If PHP extensions are required, remove the comment ( semicolon character ; ) for the lines:

    ; extension=php*.dll
    .
    .
    .
    ; extension=php*.dll

    The following extensions are enough for PHP to work properly:

    Php_mbstring.dll - the library is designed to work with multi-byte encodings, which include encodings of Eastern languages ​​( Japanese, Chinese, Korean), Unicode ( UTF-8) and etc.

    php_bz2.dll - the extension is used to create and unpack archives in bzip2 format.

    Php_curl.dll - allows you to connect and work with servers using various Internet protocols.

    Php_gd2.dll - the extension allows you to work with graphics.

    Php_mysql.dll - the library is required to work with the MySQL server.

    Php_mysqli.dll - The library is an extension of php_mysql.dll . It contains additional PHP functions for working with MySQL server version 4.1.3 and higher.

    Parameter

    error_reporting = E_ALL & ~E_NOTICE

    allows you to display all errors, except for remarks. This value is set by default, and we leave it.

    But PHP 5 ( unlike previous versions) does not display a series of errors in the browser window ( due to safety requirements).

    You can view all information about errors in log files. To place it in them, the log_errors parameter must be set to On :

    log_errors = On

    To debug Web applications with error messages displayed in the browser window, set the display_errors parameter:

    display_errors = On

    Setting display_errors and log_errors to off disables the display of error messages in the browser window and log file, respectively.

    Directive

    e r r o r _l o g = syslog

    allows you to log errors to the Windows system log.


    Find the line:

    session.save_path = "F:/main/tmp"

    and specify the path to the folder for storing temporary files. I have this folder Temp on drive C :

    session.save_path = "C:/Temp"

    To store temporary session files, you can create a separate Temp folder in the php directory. Then:

    session.save_path = "C:/Program Files/php/Temp"

    Apache web server configuration file httpd.conf add the following lines before the virtual hosts description block:

    AddType application/x-httpd-php phtml php

    Options ExecCGI

    Action application/x-httpd-php "/php_dir/php-cgi.exe"

Now you need to restart the Apache server and check if PHP is working.

To do this, create a test.php file in the d:/main/html directory. You can use a text editor to create a PHP file. Notebook, in which you should write any small script, for example:

e c h o (" H e l l o , PHP!");
?>

If, when typing in the address bar of the browser request http://localhost/test.php, the line appears

Hello PHP!

That installation was successful!

php has many settings, described in the php.ini file. This file must be available in the system search paths in order for php to use these settings. Usually it is enough to store this file in the same directory as php itself, but if php is configured to work as a web server module, then this file must be copied to a directory that is explicitly available in the system search paths. See the php installation section for more detailed instructions.

Here are the main php options divided into categories. The list of parameters mostly corresponds to php 4.0.6 version, however only the main parameters are considered here. A complete list of parameters with comments on each of them can be seen directly in php.ini, as well as in the corresponding section of the php manual.

The values ​​of the parameters with "yes/no" options can be as follows:

YES - 1, on, true or yes
NO - 0, off, false or no

Short_open_tag

Enabling support for a shortened version of php tags. If this option is disabled, then php code will only be recognized inside tags. If enabled, then tags are allowed. It is considered good practice not to use shorthand php tags.

Enabling support for asp tags<% %>as php tags.

Output_buffering

Enabling buffered output php. Using buffered output will allow you, for example, to use any functions that operate on http headers (header(), setcookie()) anywhere in your script, without worrying about not outputting anything before. You can control buffered output from your scripts (see the output control functions section in the php manual) Keep in mind that with output buffering enabled, the result of the script will only be returned to the browser after the script has finished running, which can lead to slower site performance.

Enable php safe mode. Safe mode prevents scripts from doing anything that is not safe for the server that php is running on. PHP's safe mode is discussed in great detail in the php manual.

Max_execution_time

Maximum php script running time (in seconds). After this time, the script will be forcibly terminated and an appropriate error will be generated. Avoids problems with looping scripts.

The maximum amount of memory that can be allocated for the needs of the script. It also avoids problems with looping scripts.

error_reporting

Mask for error messages to be generated by php. Specified as a boolean expression using a set of predefined constants that describe different types of errors. A list of these constants, as well as the rules for specifying expressions, can be found in php.ini.

Display_errors

Specifies whether to display error messages on the screen. Usually this option is turned on while writing php scripts in order to be able to see error messages, but on real servers on the internet it is usually turned off for security reasons.

If this option is enabled, then all errors will be logged to the log file specified by the following option.

Path and filename where all error messages generated by php will be written. For systems that support a syslog error log, you can set this parameter to syslog in order to redirect all error messages there.

If this option is enabled, then the last error text will always be available from the $php_errormsg variable.

Register_globals

This parameter determines whether the so-called. egpcs variables (environment, get, post, cookies, session) are available as global php variables. If this option is enabled, then variables will be accessed in the same way as any other global php variables:


echo "http protocol version: ". $server_protocol ;
echo "Query string parameter: ". $param1 ;
echo "Form element: " . $txtfirstname ;
echo "My cookies: " . $mycookie ;
echo . $mysessionvar ;

Or the same but with the option disabled:


echo "http protocol version: ". $http_env_vars["server_protocol"];
echo "Query string parameter: ". $http_get_vars["param1"];
echo "Form element: " . $http_post_vars["txtfirstname"];
echo "My cookies: " . $http_cookie_vars["mycookie"];
echo "My session variables: ". $http_session_vars [ "mysessionvar" ];

At first glance, this is less convenient, but disabling this option has its advantages:

Php starts to work a little faster, because. no extra time wasted creating multiple variables.
There is no risk that any two variables will have the same name, which will lead to errors in the script, which will be very difficult to catch due to their non-obviousness.
variables_order

This parameter is used in conjunction with the previous parameter and determines in what order the global egpcs variables are registered. For example, with the default value (egpcs), variables passed via get will be replaced by variables passed via post with the same name. post variables (as well as environment and get variables) can in turn be replaced by cookie values ​​with the same names, and so on.

Magic_quotes_gpc

Enabling this option will cause all data coming from the client side (via get, post or cookie) to be processed: all quotes (" and ") in them will be replaced by the combination " or " respectively. On the one hand, this is very convenient if you plan, for example, to insert this data into strings transmitted, for example, to sql server. On the other hand, this can be confusing, so it's usually safer to use the addslashes() and stripslashes() functions for this purpose.

Magic_quotes_runtime

This parameter is similar to the previous one, except that it affects data coming from external sources located on the server side (for example, data coming from the sql server or the results of external programs).

Magic_quotes_sybase

Enabling this option will cause single quotes ("") to be doubled (""). This is necessary for some sql servers that only support this way of inserting quotes into strings (interbase, ms sql, sybase and some others).

Auto_prepend_file

Allows you to set the path and file name, which will be automatically added to the beginning of each php script. Used only if it has a non-empty value.

Auto_append_file

The same as the previous parameter, but the contents of the file are added to the end of each php script.

The path to the directory where the php modules are located. This is usually the extensions subdirectory under the php root directory.

Of course, I'm not a super expert in all sorts of things that relate to setting up web servers, apache, php and everything else, so I still haven't got myself a separate server for projects. Nevertheless, sometimes in the work there are situations when you still have to get into the wilds and nuances of the settings - today there will be a post about one of them. The background is quite trivial: I was developing a site on typo3, and there you need to install ImageMagick to work with images. I’m turning to the hoster, they installed it, but for some reason it still doesn’t work. Then I find the forbidden exec function, which is exactly what ImageMagick needs to work - again I turn to the hoster. And then there’s the complicated communication procedure, where I first write a letter in the hoster’s admin panel, they answer it to the client’s mail, and only then can I read the message :)

In general, somehow I suffered for 3 or even 4 days, after which I was directed to the right help page, where I found all the necessary information. As it turned out, I could specify my PHP settings for hosting through the php.ini file, which, in principle, was a bit unusual.

So, php.ini is a PHP settings and configuration file. It contains a number of directives that define the various behavior of PHP, and, therefore, the site. The name of the file must be just such that the interpreter can find it. They write on the Internet that it first looks for the php.ini settings file in the current directory, if it is not there, then it goes to the directory specified in the PHPRC environment variable, and lastly it checks the path that was set when PHP was compiled. Somehow, the information is interesting, but more theoretical :)

Let's get back to real examples. When working with different hosters, I can say that I had several options and nuances for setting up PHP.

1. If cPanel is used as the admin panel, then there we find PHP configuration section”, where a list of PHP parameters for hosting is published. Sometimes you can change the PHP version there if the server allows you to work with versions 4 and 5.

Perhaps cPanel provides options for changing PHP settings (except for the version), but so far I have not come across this. Therefore, most often I resorted to the second option.

2. Quite logical for a user who is not very versed in all this ask the host for help. Sometimes this is the only possible solution. Here, apparently, a lot depends on the configuration and principles of the service for different companies - some give users more freedom, others, on the contrary, try to control all settings.

3. Some hosts trust their customers enough to allow use your settings in php.ini on servers. As I understand it, a number of PHP settings are set by default, but in some places there are situations when certain systems, scripts may need additional functionality - as, for example, in my case with the exec function or when it comes to wordpress, you can recall the memory_limit variable.

On one of the hosts I work with, found instructions on how you can still override some php.ini settings to suit your needs:

In .htaccess add the following lines:

#!/bin/sh exec /usr/bin/php -c /home/support/php.ini

Here /home/support/php.ini is the path to your php.ini (of course the php.ini file itself will need to be created there, make sure the path is correct). After that, we assign permissions 755 to the php5.fcgi file (chmod 755 php5.fcgi). The PHP settings will now be processed based on your php.ini file.

There are 2 additions to this guide. When creating a php5.fcgi file, you need to use unix newlines i.e. n. And if a 500 error occurs, check the error log, where you will most likely find what the problem is.

By the way, one of the users added a comment to this algorithm - and very useful! You can organize everything a little easier. All in the same .htaccess file, add the path to your php.ini through a special setting:

We save and copy it to the FTP hosting in the root directory of the site, and then run it. All PHP settings should be displayed on the screen, where you can find the value of certain functions to fix in php.ini. If you have something to add - write in the comments.

P.S. Guard. Optimization, promotion and promotion of the site in search engines Google, Yandex.
If you need high-quality website development, then you can contact the LTD-studio.


2022
maccase.ru - Android. Brands. Iron. News