Home > Backend Development > PHP Problem > How to configure PHP during installation

How to configure PHP during installation

PHPz
Release: 2023-04-06 11:54:01
Original
2613 people have browsed it

In the process of building a dynamic website, PHP is of course an important part that cannot be avoided. When installing PHP, you need to perform a series of configurations to ensure that it can run normally. However, for beginners, the installation and configuration of PHP may be a bit of a headache. This article will introduce several key parameters that need to be configured when installing PHP to help you configure it better.

1. PHP environment requirements

First of all, we need to understand the environmental requirements required by PHP. The current version of PHP is PHP 7.4. The following environment is the minimum requirement to support PHP 7.4:

  • Operating system: Windows 7 or higher; Linux (CentOS/RedHat/Debian/Ubuntu)
  • Web server: Apache2.x or higher; Nginx (1.14 or higher)
  • Database: MySQL 5.5 or higher; MariaDB 10.0 or higher; SQL Server 2012 or higher Version
  • PHP extensions: OpenSSL, PDO, Mbstring, Tokenizer, XML, JSON, Curl

The above are the minimum requirements for the PHP environment. Please ensure that your environment can meet these before installation. Require. At the same time, if your website needs to use other extensions, such as GD library, MySQLi, Imagick, etc., you also need to install their extensions when installing PHP.

2. Download the PHP installation package

Before downloading the PHP installation package, you need to determine the PHP version you are using. You can check the current mainstream PHP version on the PHP official website and download the installation package of the corresponding version. When downloading, you can choose the stable version or the development version. Which version to choose depends on your actual needs.

3. Install PHP

After downloading the PHP installation package, you can start the installation. Before installing PHP, you need to ensure that servers such as Apache or Nginx have been installed.

  1. Extract the downloaded PHP installation package to any directory, such as "/usr/local/php".
  2. Modify the PHP configuration file:

You can find the "php.ini-production" file in the decompressed directory, copy it and change the file name to "php .ini" (or rename it directly), then open the file through an editor for editing.

When editing the php.ini configuration file, you mainly need to configure the following parameters:

  • extension_dir: Specify the directory where PHP extensions are stored. By default, PHP will automatically install extensions to this directory when compiling and installing, so this path should correspond to the specific PHP version. You can find out the directory where the PHP extension is currently stored and set it by entering the command "php -i | grep extension_dir".
  • error_reporting: This parameter is used to set the PHP error reporting level. Commonly used ones are E_ALL and E_ERROR. E_ALL contains all error levels, and E_ERROR contains only fatal errors.
  • display_errors: Use this parameter to turn on or off PHP's error output. If turned on, PHP will directly output error information to the user when an error occurs; if turned off, PHP will record the error information to the log file to avoid direct exposure to the user.
  • date.timezone: This parameter is used to set the time zone of PHP. When setting the time zone, it should be set to the same time zone as the server's local time to ensure that the PHP date and time output is correct.
  • upload_max_filesize: This parameter limits the maximum size of uploaded files. It can be set according to actual needs, and usually does not need to be set too large to avoid affecting server performance.
  • post_max_size: This parameter limits the maximum size of the POST request. It can also be set according to actual needs.
  1. Configure Apache or Nginx to parse PHP

After installing PHP, you also need to configure the web server to parse PHP. The specific operation process is as follows:

Apache:

In the Apache configuration file httpd.conf, uncomment the following two lines of code and set them to the directory where the PHP execution path is located:

LoadModule php7_module modules/libphp7.so
AddHandler php7-script php
Copy after login

Nginx:

In the Nginx configuration file, add the following configuration:

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
Copy after login

Among them, fastcgi_pass specifies the PHP execution path, and fastcgi_param specifies the name of the script that needs to be executed.

4. Test whether PHP is installed successfully

After installing PHP, we can confirm whether PHP is successfully installed through a simple code test. Create the test.php file and copy the following sample code into it:

<?php
phpinfo();
?>
Copy after login

Then upload the file to the web root directory of the server (usually "/var/www/html"). Then enter "http://localhost/test.php" in the browser. If the browser can display PHP information normally, it means that PHP has been successfully installed.

To sum up, although the installation and configuration of PHP may give beginners some headaches, through the above steps, I believe you have understood how to configure PHP so that it can run normally. If you encounter other problems about PHP installation, you may wish to ask for help through a search engine or the PHP developer community. I believe you will get a satisfactory answer.

The above is the detailed content of How to configure PHP during installation. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template