PHP is still one of the most popular options you have to run a website. It is simple, fast to write, and fast to execute. Furthermore, it can run virtually on any platform. Because of that, some people love PHP, while others hate it. Doesn’t matter which side you pick, you need to know how to run PHP. It is one of the IT skills that will come handy sooner or later. Thus, in this article, we explain how to set-up a PHP environment on a Microsoft Windows Machine. Just read on, and learn how to install PHP on Windows with XAMPP.
Install PHP On Windows
Introducing XAMPP
XAMPP is the best way to go if you want PHP on Windows. It is an all-inclusive suite that installs you PHP, Apache, MySQL, and optionally FileZilla server and Tomcat. In other words, everything you need to run a website on your PC. XAMPP is good to install PHP on both desktop computers and production servers. What do you get exactly?
- PHP Interpreter – Your PC will understand PHP scripts, and you can even call them from the terminal. This is included in Apache (see next).
- Apache – This is the webserver. It can listen to HTTP or HTTPS requests and process them with PHP scripts
- MySQL – A popular type of SQL database to store the data of your website in a standard manner
- XAMPP Control Panel – Intuitive dashboard to control the other services and restart them
So, the first step to install PHP on Windows is to get XAMPP from Apache Friends. Just go to their website here and download the Windows version. XAMPP is also available for Linux and MAC, in case you want it. The latest XAMPP version will come with the latest stable PHP version.
Installing XAMPP
Download XAMPP from Apache Friends as said above.
You will get an executable file from your download. Just run it, and follow the step-by-step setup. By default, XAMPP will install itself into C:/xampp, but you can change that if you want. Inside that folder, you will find all the files you need to configure your web server. Here are some example files you will find in that folder.
Running your Web Server
If you want to install PHP on Windows, it is probably because you want to run a web server. That’s simple, all you need to do is launch the web server from the XAMPP control panel. Just search for XAMPP Control Panel in your start menu and open it, you will see the following screen.
At this point, you only need to start Apache, but I recommend starting MySQL as well. To do that, simply use the Start button on the Apache and MySQL lines. If everything goes well, the module name will be highlighted in green, like below. If not, we need to check the logs below and do some troubleshooting.
Don’t see that? Don’t worry, it’s hard to have everything running on the first attempt. We got you covered, just read below.
XAMPP Troubleshooting Apache
The most common problem is Apache that won’t start. However, if we want to install PHP on Windows we need to make it work. Probably, the most common reason is a conflict on port 80 or port 443. XAMPP uses those ports to serve the website, but it means that those ports must be free. If another program is using them, you will have a problem. If you are lucky enough, you will see the program name in the XAMPP log. If not, we need to check the Windows Event Viewer. Just search for Event Viewer in your Start Menu, then select the Application Logs inside the Windows Logs from the left menu. You will see a list of logs, and hopefully some details about XAMPP problem right on top.
For example, Skype is a program that can create conflicts. Luckily, you can change your Skype configuration to avoid using port 80 or 443, removing the conflict. Of course, you could do that on the Apache side as well, but we don’t recommend that to new users of Apache. Once you cleared all conflicts, you will be able to start the services and see them in green.
Serving the wesbite
A website is a collection of static and dynamic files available over the web. By default, XAMPP puts those files in C:\xampp\htdocs. You can empty this folder and put your HTML, CSS, and PHP files here. Then, you can access them by browsing at http://localhost or http://127.0.0.1 (they are equivalent). For example, you can create your example Hello World script in a file named hello.php
. In it, write the following code:
<?php
echo 'Hello, world!';
?>
Then, navigate to http://localhost/hello.php and you will see Hello, World! on the page.
PHP From the Command Line
At this point, we know how to install PHP on Windows and we have a fully functional setup. However, we might want to run some PHP scripts directly from the CLI. This is very important if you want to use complex frameworks like Laravel to build your applications.
To do that, we simply need to add our php.exe to our System Path. Search for This PC in the Start Menu, then click Advanced System Settings on the left and the Environment Variables on the bottom-right of the window that will pop-up. From the User variables of the top box, select the variable named Path and double-click on it. Then, click on New and write the path to your php.exe file. If you installed everything with default settings, it will be C:/xampp/php/php.exe
. OK your way out of the windows you opened and you are set to go.
Now, open the command prompt and type php --version
. You will see something like this.
You can execute php <script name>
to run a PHP file as well. Plus, Apache doesn’t need to be running to do that.
Wrapping it up
In this article, we saw how to install PHP on Windows. Now, you can create your PHP websites and run them on your Windows PC or Server. To do that, you only need to install XAMPP. Furthermore, you can add PHP to your command terminal by adding php.exe to your System PATH variable.
What do you think about XAMPP and PHP? Let me know in the comments!