Run php on apache windows

Run php on apache windows

IIS is built in to Windows. On Windows Server, the IIS role can be added via the Server Manager. The CGI Role Feature needs to be included. On Windows Desktop, IIS has to be added via the Control Panel’s Add/Remove Programs. The Microsoft documentation has » detailed instructions. For desktop web apps and web-development, IIS/Express or PHP Desktop can also be used.

Example #1 Command line to configure IIS and PHP

@echo off REM download .ZIP file of PHP build from http://windows.php.net/downloads/ REM path to directory you decompressed PHP .ZIP file into (no trailing \) set phppath=c:\php REM Clear current PHP handlers %windir%\system32\inetsrv\appcmd clear config /section:system.webServer/fastCGI REM The following command will generate an error message if PHP is not installed. This can be ignored. %windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers /-[name='PHP_via_FastCGI'] REM Set up the PHP handler %windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI /+[fullPath='%phppath%\php-cgi.exe'] %windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers /+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='%phppath%\php-cgi.exe',resourceType='Unspecified'] %windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers /accessPolicy:Read,Script REM Configure FastCGI Variables %windir%\system32\inetsrv\appcmd set config -section:system.webServer/fastCgi /[fullPath='%phppath%\php-cgi.exe'].instanceMaxRequests:10000 %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='%phppath%\php-cgi.exe'].environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']" %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='%phppath%\php-cgi.exe'].environmentVariables.[name='PHPRC',value='%phppath%\php.ini']"

Apache

There are several builds of Apache2 for Windows. The Apache builds of ApacheLounge are recommended, but other options include XAMPP, WampServer and BitNami, which provide automatic installer tools. PHP can be used on Apache through mod_php or mod_fastcgi. mod_php requires a TS build of Apache built with same version of Visual C and same CPU (x86 or x64).

Читайте также:  Python remove dist packages

Choose Build

Windows builds can be downloaded from » http://windows.php.net/download/. All builds are optimized ( PGO ), and QA and GA releases are thoroughly tested.

  • Thread-Safe(TS) — for single process web servers, like Apache with mod_php
  • Non-Thread-Safe(NTS) — for IIS and other FastCGI web servers (Apache with mod_fastcgi) and recommended for command-line scripts
  • x86 — for 32-bits systems.
  • x64 — for 64-bits systems.

Источник

How to Install PHP on Apache in Windows?

PHP is a Hypertext Preprocessor, earlier it known as Personal Home Page. It is an open-source server (available for free) side scripting language (everything is implemented on the webserver) that is embedded into HTML and used for web development. PHP script starts with . Some common features of PHP are:

  • Open Source
  • Platform Independent
  • Used to create dynamic web pages
  • Provides Security encryption
  • Cost-Efficient
  • Compatibility
  • Website function faster

Facebook, Wikipedia, WordPress, Tumbler, and yahoo are some of the major companies using PHP

Why PHP on Apache?

PHP and MySQL both are compatible with an Apache server. These two are open source and easy to set up. PHP runs on many platforms like Windows, Linux, and Unix. Because of these advantages, PHP is used on Apache servers. Before installing the PHP make sure that the Apache server is already installed on your PC.

How to Install PHP on Apache?

Follow the steps below in order to set up PHP on Apache without facing any difficulties.

Step 1: Visit the official website of PHP. Here you will find the two different packages called Non-Thread Safe and Thread Safe Packages. Download the zip folder under the Thread Safe section because we are working on Apache.

 Visit-the-official-website-of-PHP

Step 2: Extract the files and rename the extracted file to PHP8.1(It’s completely on the user’s wish) and move to the directory where the Apache folder is located. Add the path to the system variables. To add the copied address to the system variables, search for Control Panel > System & Security > System > advanced system settings > Environment variables > System Variables > Path > Edit > New and paste the copied address.

Note: Make sure that both Apache and PHP extracted files should be in the same folder

Step 3: To check whether the PHP directory is added to the path of system variables or not. Open PowerShell as an administrator. Change PowerShell mode to the command prompt by entering the command cmd. Then type path hit enter if the directory path is visible on the screen, then the path is successfully added to the system variables. To know the version of the PHP type php -v and then hit enter

Step 4: Now it’s time to configure Apache for this open C: > Apache24 > conf > httpd.conf

Scroll down the httpd.conf file to the end and write the below three lines. In Apache, PHP is loaded as a module so load the module using LoadModule, read PHP files with the file handler and finally add the PHP file located address within the double quotes

LoadModule php_module “C:\PHP8.1\php8apache2_4.dll”

AddHandler application/x-httpd-php .php

PHPIniDir “C:\PHP8.1”

Step 5: In the PHP8.1 directory there are two default configuration files “php.ini-development” and “php.ini-production. Copy-paste the “php.ini-development” and rename the copied file as “php.ini”

Step 6: Set ServerName parameter as localhost for this search (Ctrl+F) “ServerName” in “httpd.conf” file and set ServerName as localhost below the “#ServerName www.example.com:80” line ServerName localhost

Set-ServerName-parameter-as-localhost

Step 7: Open PowerShell in command prompt mode as an administrator and type “httpd –t” and hit enter. If everything is ok then it displays “Syntax OK”

Open PowerShell-in-command-prompt-mode

Step 8: Specify the default PHP page. For this search for ” DirectoryIndex ” in httpd.conf file. Inside Dir-module add “index.php”

Step 9: Open htdocs folder which is present inside the Apache24 folder and create a PHP file with the below code and save it as index.php (users wish) for testing. To test the program, open your favorite browser and then type localhost and hit enter.

Running PHP code on Apache server

Now Apache server is ready to run PHP codes. Let’s understand how to run the first PHP code on the Apache server. For this open an editor write an example PHP code and save it in Apache24 > htdocs > projectfolder > filename.php.

a projectfolder is your folder with your projectname and filename.php is your filename which should store inside your projectfolder. Now it’s time to run the example program. For this open your favorite browser and then type “localhost/projectfolder/filename.php”. then it will show the below output

Источник

How to install Apache 2.4 and PHP 8 on a Windows Machine

In this tutorial, you will learn how to install and configure Apache 2.4 and PHP 8 on a Windows machine. We all know Apache HTTP Server is an open-source cross-platform and free webserver to run web applications and similarly PHP is a free and open-source scripting language used to develop web applications.

To run the PHP code on a Windows machine, first, you’ll need to install and configure a web server (Apache) that executes the PHP application. And on this page, you will find the step-by-step guide on “How to install Apache 2.4 and PHP 8 on a Windows Machine”.

P.S. Tested with Apache 2.4 and PHP 8.1.4 on a Windows 10 machine.

1. Prerequisites

Download the Apache 2.4.x and PHP 8 from its official websites, extract the downloaded file and move it to the C drive.

1.1 Download Apache 2.4.x – Depending on your system build download the binary zip file accordingly.

Download Apache 2.4 - Websparrow.org

1.2 Download PHP 8 – Similarly, depending on your system build download the Thread Safe version of PHP.

Download PHP 8 - Websparrow.org

Before jumping to the main configuration part, be sure you installed latest 14.31.31103.0 Visual C++ Redistributable Visual Studio 2015-2022 : vc_redist_x64 or vc_redist_x86 software.

2. Install Apache

To install the Apache HTTP Server on a local Windows machine move the downloaded and extracted Apache 2.4 binary files to the C drive and follow the below steps:

Step 2.1: Go to the Apache 2.4 bin directory path C:\Apache24\bin (it might be different in your case) and set it into your system environment variable path.

Set Apache 2.4 in system environment variable- Websparrow.org

Step 2.2: Open the command prompt with admin privileges and run the httpd -k install command to install the Apache services.

C:\WINDOWS\system32>httpd -k install Installing the 'Apache2.4' service The 'Apache2.4' service is successfully installed. Testing httpd.conf. Errors reported here must be corrected before the service can be started. AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::ccb2:109d:6198:a489. Set the 'ServerName' directive globally to suppress this message

Step 2.3: Start the Apache service with the command httpd -k start or directly through the Task Manager » Services » Search for Apache2.4 » Right-click on the service » Start .

Start the Apache 2.4 service - Websparrow.org

By default, Apache HTTP Server runs on port 80, make sure no other services/servers running on this port.

Step 2.4: Open the web browser and hit the default Apache localhost URL i.e localhost or localhost:80 . If everything is configured correctly, you will see the default index.html web page contents and it is located inside the C:\Apache24\htdocs directory.

Apache 2.4 configuration successful - Websparrow.org

3. Install PHP 8

The next step is to install PHP 8. To do that, similarly, move the downloaded and extracted PHP 8 binary files to the C drive and follow the below steps:

Step 3.1: Copy the PHP 8 home path i.e C:\php-8.1.4 and set it into your machine environment variable.

Set PHP 8 path in system environment variable- Websparrow.org

Step 3.2: Open the command prompt and type php -v to check whether the path is set correctly or not. If the PHP path is set correctly, it will print the PHP version.

C:\WINDOWS\system32>php -v PHP 8.1.4 (cli) (built: Mar 16 2022 09:33:31) (ZTS Visual C++ 2019 x64) Copyright (c) The PHP Group Zend Engine v4.1.4, Copyright (c) Zend Technologies

4. Configure Apache and PHP

Now it’s time to configure Apache HTTP Server with PHP 8.

Step 4.1: Go to the C:\Apache24\conf directory, inside the conf directory edit the httpd.conf file. Go to the end of the file and add the below configuration because, in Apache, PHP is loaded as a module.

# Note: Repalce php_module location with your PHP path and # if the php8apache2_4.dll is not available, # download the non thread safe version of PHP. LoadModule php_module "C:\php-8.1.4\php8apache2_4.dll" AddHandler application/x-httpd-php .php PHPIniDir "C:\php-8.1.4"

Step 4.2: Go to the PHP home directory C:\php-8.1.4, and you will find two configuration files php.ini-development and php.ini-production . Create a copy of php.ini-development and rename it to php.ini

PHP configuration file php.ini - Websparrow.org

Step 4.3: Again open the httpd.conf file and search for ServerName, uncomment and edit the ServerName with localhost .

# ServerName gives the name and port that the server uses to identify itself. # This can often be determined automatically, but we recommend you specify # it explicitly to prevent problems during startup. # # If your host doesn't have a registered DNS name, enter its IP address here. # ServerName localhost

And in the same file also search for DirectoryIndex, and append the default index.php file.

# DirectoryIndex: sets the file that Apache will serve if a directory # is requested. # DirectoryIndex index.html index.php 

Step 4.4: Open the command prompt and run the httpd -t command to validate whether everything is configured correctly, it will return Syntax OK if everything is configured correctly.

C:\WINDOWS\system32>httpd -t Syntax OK

5. Test the configuration

To test the Apache 2.4 and PHP 8 configuration, restart the Apache2.4 service from the Task Manager and rename the index.html file to index.php from the C:\Apache24\htdocs directory and add the below piece of code.

Apache 2.4 and PHP 8 configured successfully-Websaparrow.org'; echo ' Your current PHP version is: ' . phpversion(); header("Refresh:10; url=https://websparrow.org"); ?>

Open the web browser and hit the localhost in the URL bar, you will get the following details:

How to install Apache 2.4 and PHP 8 on a Windows Machine

References

Источник

Оцените статью