Installing and Working with PHP Without any Server
It's very tempting to think that PHP programs cannot be run without server applications like Apache or Nginx. Absolutely incorrect. Beginner PHP programmers install XAMPP, MAMP, WAMP, etc. stack to start programming with PHP. PHP can be used standalone without any server stack or server application. As we are going to learn PHP like computer scientist, we are not going to use PHP as ordinary people do.
Installing and Configuring PHP
Some system come with PHP pre-installed with proper settings. If you have PHP installed on your system I encourage you to uninstall that and start from the beginning.
Downloading, Installing and Configuring PHP on Windows
Usually we install windows applications with the help of a setup package with a
.msi extension. We double click on that, a wizard opens up that optionally prompts us to choose zero or more options and we can easily install a windows application. For PHP, it's different. On http://windows.php.net/download/ you will get different versions of PHP listed that can be downloaded as
zip files. Download any version of PHP 7 from there. Create a directory called
php at the root of
C drive. Extract the contents of the zip file in that directory. It may ask you for administrative privilege. It is also possible to extract in other directories or other drives. But to do that you will have to understand and edit PHP's configuration file. For now, we are not diving into those complexities. Contents in the
php directory will look like the following.
PHP needs a configuration file, usually called
php.ini, to start. Without this file PHP will not work. In the
php directory you will find a file named
php.ini-development. Rename this file to
Making PHP available from command line:
We just extracted or copied and pasted the contents of the zip file downloaded from
php.net. So, if we open up command line, write
php and press enter, windows will report that no such command exist. So, we need to create a new entry in the list system paths of environment variables of our piece of Windows. Press the windows button and type
environment variable, in the search result you will see
Edit system environment variables and
Edit environment variables for your account. If you are the administrator of the PC then choose the first one or the second. In the window click the button titled
Environment Variables. It will show you a list of environment variables. Double click on
Path. If you are on windows version 10 you will see
Delete, etc. buttons. Click on new and put
C:\php and click
ok. If you have any older version of windows then you will see a long text in one line. Go to the end of the text put a semicolon at the end if there is not one present already, put
Press ok and close the window. Open up a command line, write
php -v a press enter. You should see output like below:
It is showing the version info of the PHP executable we opened.
Downloading, Installing and Configuring PHP on Linux
On Linux distributions, you can compile PHP from source and install it. But there is no need to go through that much hassle for now and install PHP with the package manager available on your system.
On Debian based distro of Linux like Ubuntu, Linux Mint, etc. you can install PHP with the following command:
sudo apt-get install php
On Redhat or CentOS you can install it with the following command:
yum install php
Unlike windows you do not need to provide a
php.ini file by yourself. We will talk about
php.ini more when we will need to configure it further.
If you have other type of Linux distro then find installation instruction on the official website or from other places online.
Downloading, Installing and Configuring PHP on Mac
There are more options to install PHP on Mac. As a programmer you might like the homebrew way of installation. I am not going to describe the whole process here. Please have a look at this github repository: https://github.com/Homebrew/homebrew-php.
Here is a little summary:
brew tap homebrew/homebrew-php brew install php71
In place of
php71, you can use any of php54, php55, php56, php70, php71, php72.
To test whether your piece of PHP is working or not, you can execute the following command from the command line:
It will show you version information of the currently installed and configured PHP on your system.
I hope you are able to properly install and configure PHP on your system. If you failed for some reason, then please email me with screen shots of the error you are encountering. In the next guide we will start coding in PHP in the way of computer scientists, instead of web developers.