You can install PHP on a Windows machine in multiple ways. It all depends on your needs/requirements.
XAMPP and WampServer are great for beginners or people that want to build something really quick and have less experience with PHP and Apache environments. By installing them you get a ‘ready to go’ development environment that includes: PHP, Apache, MySQL and more…
In most cases though, you might just be interested to install just PHP, to use it in different development environments. Below I will show you step by step what you have to do to install PHP 7.2 on a Windows machine.
- How to install PHP on Windows?
- Test PHP is installed
- What extensions do I need?
- How to enable extensions?
- Check what PHP extensions you have installed
- How to start a localhost on Windows
How to install PHP on Windows?
First, go and download PHP 7.2 .zip file from here. Choose the version you need for your machine, x64 or x86. I usually install the Non Thread Safe version.
Read this for more info about What is thread safe or non-thread safe in PHP?
Choose where you want to install PHP and make a new folder called PHP. I usually install it in /C.
Export the php zip contents into the /PHP folder you have just made.
Go into the /PHP folder, make a copy of the php.ini-development file (literally, copy and past that file again into the folder). Rename the file you just copied to php.ini.
Open php.ini file in any code editor and search (ctrl + f) for extension_dir = “ext”. Uncomment that line by removing the semicolon, as below:
Open Environment Variables (Windows Search for ‘env’) and open Edit the system environment variables. Click Environment Variables.
Under System Variables, select PATH and click Edit.
Add two new entries in that variables list. Click New and add the location of your /PHP folder (C:\PHP) in my case. Next, add the location of your extension folder (C:\PHP\ext) in my case. Click OK.
Under System Variables click New and add PHPRC for the name and C:\PHP for the value of this new system variable. Click OK and then OK again.
The next thing you want to do is to reboot/restart your computer.
Test PHP is installed
Once your machine restarted, the simplest way to check if PHP was installed correctly is to:
Open cmd by windows searching for cmd. This will open your terminal.
Run php -v and you should see a similar output, depending on the version of PHP you have installed.
You can also run php -i for a lot more information about the PHP you have installed or create a index.php file on your localhost containing the following code:
<?php echo phpinfo(); ?>
What extensions do I need?
You don’t need all the extensions to be enabled. Every developer will use different extensions, based on what they develop. Here is a good resource when it comes PHP extensions.
There are a few basic extensions that you will need to enable though:
- Depending on what database service you want to use, enable pdo_sqlite; pdo_mysql or pdo_pgsql.
- Enabling curl and mbstring is also something I usually do.
How to enable extensions?
Open php.ini in any code editor and search (ctrl + f) for ‘;extension=curl’. Or just scroll down until you see the list below:
NOTE**: Make sure you enable openssl as well.
Uncomment the extensions you wish to enable and save the file.
Check what PHP extensions you have installed
In your terminal (cmd) run the following command. That will show you what modules/extensions you have enabled and loaded.
php -r "print_r(get_loaded_extensions());"
How to start a localhost on Windows
Open terminal (cmd) and cd (go to) the folder where you want to start the localhost. This is usually project folder or where your index.php file is located.
Once you’re there, run the following command in the terminal:
php -S localhost:8000
More info from the documentation here.
Check if your localhost works by opening this link into the browser:
As you have probably figured out, installing PHP on Windows is not as hard as it may seem. Just make sure you don’t miss a step along the way and everything should work perfectly fine by the end of this tutorial. If you face any problems or errors, just comment below your error code and I’ll help you through this process.