PHP Setup and Introduction
We will cover the following:
PHP Setup
We want to manually install PHP so that we can have more control over it’s configuration and the resources that come with the downloaded version.
Installation Guides
- Mac/Unix/Linux: You “should” have PHP already available to you as it now comes bundled with your operating system installation. We, however, need to verify that the version number is sufficient.
- Windows: Below is the best estimate of the steps necessary for installing and configuring your PHP installation. Note: we did not have a Windows device to test these steps.
- Download the Zip file under “Windows downloads” here. If you are questioning whether or not to use Thread Safe or Non-Thread Safe, choose Thread Safe.
- Unzip this file and put the contents such that the PHP folder has a path that mimics
C:\php
if there is not already a folder there. Your downloaded zip folder will have the version number and such in the folder name; rename this to just “php”.
Note: There is aphp.exe
file in this folder. That essentially is the PHP executable that you will be using via the command prompt. We just need to make some configuration changes and PATH changes so you don’t have to typeC:\php\php.exe
every time you want to run PHP. - Copy or rename
C:\php\php.ini-development
to becomeC:\php\php.ini
. This allows us to have a pre-made INI file (configuration file) for the PHP executable, but we still have more edits to it before we can use it. - Approximately line 751: change
;extension_dir = "./"
to beextension_dir = "C:\php\ext"
. Note that I removed the semicolon to uncomment this line in the INI file as well. (More Information) - Uncomment the following extensions:
- Line 906:
curl
- Line 908:
gd2
- Line 915:
mbstring
- Line 917:
mysqli
- Line 922:
pdo_mysql
- Line 939:
xmlrpc
- Line 906:
- We could change the INI file to allow us to use the PHP
mail()
function so we can send mail, but at this time, we do not want to try finding our ISP information and such to put there. - Adding PHP to your environment PATH:
- System Properties > “Advanced” Tab > Environment Variables
- Scroll to find “PATH” and then click on it and edit it.
- Add the following exactly at the end of this variables value line:
;C:\php
- Accept the changes and you might have to restart in order for this change to take effect.
Test for PHP
Most tutorails instruct you to write a PHP script, put a single line of PHP code in there, and then load it into your browser. There is a different way to do this and these 3 steps are not all that it takes to get a PHP script running.
- Test without writing a script: Open up your terminal/command prompt and try typing in
php -v
and press enter. You should see the following as a result if successful:PHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
- Using a file:
- Create a text/ASCII file named index.php
- Put the following code in this file:
<?php phpinfo(); ?>
cd
in your temrinal/comand prompt to the file’s directory location.- Run from the command line or terminal:
php -S localhost:8000
This starts a local PHP server on your local webpage. - Open a browser and navigate to: http://localhost:8000/ (note: you might need https)
- You should be seeing a page that displays your PHP server information.