Manually install PHP for Windows
- 25-07-2022
- Toanngo92
- 0 Comments
Mục lục
Manual PHP Installation for Windows
Choose Apache Web Server
There are several builds of Apache 2 for Windows. We recommend that users use Apache Lounge builds of Apache. Other options include BitNami, XAMPP, and WampServer. These three tools provide automatic installation tools. PHP can be used on Apache via mod_fastcgi and mod_php. Mod_php requires a Thread Safe (TS) Apache build built with the same version of Visual C and the same CPU (x86 or x64).
In a programming context, a build is a version of a program.
Select PHP’s Build
The latest PHP versions based on Windows can be downloaded from https://windows.php.net/download/
Apache 2.4 on Microsoft Windows
You should consult the Official Apache Documentation for a basic understanding of Apache 2.4 Server and then download Apache 2.4.
Once downloaded, users must first go through the Manual Installation Steps ( https://www.php.net/manual/en/install.windows.manual.php ) and then proceed with Apache integration and PHP.
There are three ways to set up PHP and make it work with Apache 2.4 on the ‘Windows’ platform – PHP can be run as CGI, as handler or under FastCGI.
Option 1: Install as Apache handler
To load the PHP module for Apache 2.4, the following lines in the Apache configuration file httpd.conf must be inserted as follows:
LoadModule php_module "e:/php/php8apache2_4.dll"
<FilesMatch .php$>
SetHandler application/x-httpd-php
</FilesMatch>
PHPIniDir "C:/php"
Important: The actual installation path for PHP should be replaced instead of c:/php/ in the examples. Make sure that the file referenced in the loadModule directive is in the specified location. Use phpSapache2_4.dll for PHP 8 .
Option 2: Run PHP as CHOLD
Users should refer to Apache CGI documentation for a brief and complete understanding of CGI implementation on Apache.
PHP-CGI files must be placed in a directory specified as the CGI directory using the scriptalias command, in order to execute the PGI as CGI.
ONE #! this line must be placed in the PHP file, this line must point to the location of the PHP binary.
For example:
#!C:/php/php.exe
<?php
phpinfo();
?>
When the server is deployed in CGI mode, it has many possible vulnerabilities. Users should read the CGI security section of the manual to know how to protect themselves from such attacks.
Option 3: Run PHP with FastCGI
There are several advantages of running PHP with FastCGI over running it as a CGI. It’s also simple and easy to set it up this way.
Download mod_fcgid from https://www.apachelounge.com. Win32 binaries are available for download from that site. The user must install the module according to the enclosed instructions.
The user must configure the Web server and also take care to adjust any paths. For example:
LoadModule fcgid_module modules/mod_fegid.so
# Xác định vị trí file php.ini
FegidInitialEny PHPRC "c:/php"
<FilesMatch .php$> /* code to configure PHP and will run program using SetHandler fcgid-script FastCGI wrapper*/
</FilesMatch>
FegidWrapper "c:/php/php-cgi.exe" .php
Once done, files with the .php extension will now be executed by the FastCGI PHP wrapper.
To install Apache with PHP 8.0 on Windows, one has to do the above steps. If the versions of Apache and PHP are different, the appropriate steps should be considered.
The Apache server must be downloaded fromwww.apache.org/dist/httpd/binaries/win32 . It is recommended to download the current version of the stable release with the no_sre.msi extension. Double click the installer file to install. C:Program Files default installation location. The installer will also prompt the user to run Apache as a service or from the command line or DOS environment. Therefore, users should not install it as a service, as that can cause problems on startup.
The PHP binary archive should be decompressed using the decompression utility. C:PHP is a common location used.
Some .dll files should be copied from the PHP directory to the system directory (usually C:Windows). Refer to the manual for which files. Users will require php8ts.dll for all cases. Copy the file corresponding to the web server module – C:PHPSapiphpS8apache.dll. into the Apache module directory.
You should copy the recommended php.ini-dist or php.ini to your Windows folder and rename it to php.ini.
This file must be edited to receive the configuration directive, so open it in a text editor. New users can put error reporting for E_ALL in the PHP scripts they will create on their development machine. This will result in all errors reported by PHP and will help new users debug or troubleshoot faster.
Next, the user has to configure and communicate with the Apache server from where to look for PHP files and what the PHP file extension will be. Normally, php is standard, but users can use .html, .phtml, etc.).
To do this, go to the appropriate HTTP configuration file directory and open httpd.conf with a text editor. For example, the user may have installed Apache in the Program FilesApache directory. In that case, the path for the configuration files would be C:Program Filesapacheconf .
Then search for the word DocumentRoot in the httpd.conf file. This word will appear twice. Change both paths to the directory from where the PHP files are loaded. The user must add at least one PHP extension directive as follows:
LoadModule php8_module modules/php8apache.dll AddType application/x-httpd-php .php .phtml
Alternatively, the user can also add the following line: addModule mod_php8.c
The user then restarts the WWW service as follows:
- Type services.msc in the Windows Run command or launch Services from Control Panel.
- Scroll down the list to Global Web Publishing Service, right click and select Stop.
- Then right-click and select Start to start over.
If required, the user can also restart the computer.
The user can then launch a text editor and write a basic script to test the PHP installation:
<? php phpinfo ();
Save this file in the Web server’s root directory as test.php and launch it in the browser in the format http://localhost/test.php or http://127.0.0.1/test.php . The user should never launch a PHP file directly from the local path, e.g. D:PhpCodetest.php should not be provided in the browser’s Address bar. This is because in that case it will not be considered an HTTP request required to process the file.