In the previous PHP Introduction chapter, we got a brief introduction to PHP, what is it, a bit of its early history, its customers and general statistics, and so on.
Now it's time to get going and set up the whole environment for working with PHP on our respective machine.
In this chapter, we'll walk you through the installation process of PHP on Windows and Linux, introduce a couple of terms such as CLI, configure and set up VSCode for working with PHP, and a lot more.
This is by far the most important chapter of this course because, practically speaking, it'll make all that we'll do in the following chapters possible. Without the environment properly setup, we'll not be able to execute PHP programs.
So let's start.
XAMPP on Windows
There are multiple ways of installing PHP on Windows:
- Downloading source binaries directly from php.net.
- Downloading an installer to walk us through the installation process of PHP.
- Downloading a software bundle that not only installs PHP, but some other pieces of software as well, such as Apache.
Option 1 is a bit more technical and requires a lot of knowledge about compilation, executables, configuration files, shell files and so on. It's usually used by experienced software engineers who want to customize their build of PHP. We'll not use this method simply because of its technicality and less practicality to us.
Option 2 is good and an easy way to get started with PHP. However, it's merely the PHP engine itself — nothing else comes with it. In the latter part of this course, we'll use PHP in the context of a web server and that's when we'll need a full-fledged web server. For PHP, the most common choice of a web server is Apache.
Indeed, it's much better to install and setup both of these — PHP and Apache — right at this stage even though in our early days of learning, we won't be much concerned with Apache at all.
For this very simple reason, choice 3 naturally suits us and to many developers who tend to favor bundles of different pieces of software over installing each one separately.
The bundle we'll use for PHP is XAMPP.
XAMPP is an abbreviation for cross-platform Apache, MariaDB, PHP and Perl (although this is not an official full-form).
As the name suggests, it's a cross-platform software that allows us to easily set up PHP and Apache on any platform, be that Windows, OS X, or Linux.
What is MariaDB and Perl?
MariaDB is an RDBMS (Relational Database Management System) similar to MySQL, PostgreSQL, Oracle, and so on. The name 'Maria' comes from the name of the second daughter of the lead developer of MariaDB, Michael Widenius.
On the otherhand, Perl is yet another server-side scripting language rooted in the 1980s. It's possible to use Perl alongside Apache on a web application but there is relatively less usage of Perl these days for web applications, although it's another extremely powerful and capable language like PHP.
So without wasting anymore time, let's get to the installation of XAMPP.
Headover to apachefriends.org and select the Windows installation button to download the installer for Windows.Downloading XAMPP on Windows.
This will automatically start the download of the installer in the background.
Once the download is complete, run it. A dialog box similar to the following shall open up.XAMPP installer dialogue (Windows).
Click Next > to move to the next slide.
Here we're able to customize the installation of XAMPP. Since, we only want to work with PHP and Apache for this course, we'll uncheck everything except for these two.XAMPP installer dialogue (Windows).
After this, click next.
A couple more slides will be shown, such as choosing the directory where to dump all data, or choosing the language. Go with the defaults, and soon installation will begin.
Once the installation completes, click the Finish button with the checkbox checked.XAMPP installer dialogue (Windows).
This will launch the XAMPP control panel.XAMPP control panel.
Great! At this stage, we are done with the installation of XAMPP.
Now it's time to set up the editor for writing PHP. You can skip the next section, which details installation of XAMPP on Linux, and advance to setting up Visual Studio Code on Windows in the next-to-next section.
XAMPP for Linux
Akin to Windows, there are multiple ways to install PHP on Linux:
- Downloading the PHP source code, and then manually compiling and building it.
- Downloading PHP via the operating system's package manager, i.e.
- Using a software bundle to download everything in one go.
As before, option 1 is extremely tedious, leaving us with option 2 and 3.
The package manager of the Linux operating system is a handy place to download stuff on Linux. In some cases, the package manager is the easiest way to get stuff set up.
Even in the case of PHP, it's easy but the problem is that each package has to be installed separately — Apache is a separate package, PHP is a separate package, and there are even more packages to install besides these.
What's even more important is that we have to integrate PHP and Apache ourselves, using yet another package.
So while the package manager is surely an easy way to get going with PHP, if we want more stuff alongside PHP, which we almost always do, then we have to do the work ourselves.
Hence, we'll go with option 3, which is of a software bundle.
And you guessed it — it's XAMPP.
Here's how to install XAMPP on Linux. The distro we're using in these examples is Ubuntu.
Headover to apachefriends.org and select the Linux installation button to download the installer for Linux.Downloading XAMPP on Linux.
This will automatically start the download of the installer in the background.The browser may warn you that the file may harm your computer, and ask you whether you want to keep it or discard it. Don't get panicked by this alert — it's made because these kinds of
.runfiles (that are executable) downloaded from just any unknown vendor out there can be dangerous. However, fortunately they are safe in the case of apachefriends.org. Be rest assured!
It's not a problem with the installer from apachefriends.org — rather it's merely a safety measure before downloading just any executable file.
Once the download is complete, head over to the location of the downloaded file. Typically, it's in the Downloads directory.
Open the location of the file in the terminal. This can easily be done by right-clicking inside the directory containing the downloaded file and then selecting the option Open in Terminal.
Once the terminal window is opened up, enter the following two commands, one after another.~/Downloads$ chmod 755 xampp-linux-*-installer.run~/Downloads$ sudo ./xampp-linux-*-installer.run
The second command, since its a
sudocommand, requires you to enter your password. After entering the password, press Enter and you'll see the following window.XAMPP installer dialogue (Linux).
Click next to move to the next slide.
Here we're able to customize the installation of XAMPP, but just a little bit.XAMPP installer dialogue (Linux).
Uncheck the option XAMPP Developer Files since we don't need it for the time being, and then click next.
A couple more slides will be shown, such as choosing the directory where to dump all data. Go with the defaults, and soon installation will begin.
Once the installation completes, click the Finish button with the Launch XAMPP checkbox checked.XAMPP installer dialogue (Linux).
This will launch the XAMPP control panel.XAMPP control panel.
By default, the Apache server is started automatically. As stated before, we don't want it running right now. Hence, we must stop it and save computation resources.
Go on and press the Stop All button to stop all servers (in this case, there is only one server running and that is Apache).
Once that's done, all server entries would have a red signal shown before them, as follows:XAMPP control panel.
Great! We are done with the installation.
The next step is to set up Visual Studio Code on Linux and start writing PHP.
Visual Studio Code for editing PHP
Back in time when I began working with PHP, there were a few text editors capable of providing me with the power, configurability, extensibility and simplicity of Visual Studio Code.
Still relatively quite a recent text editor, it's wrong to say to that it's just an editor. Clearly not!
Rather, it's a huge collection of tools integrated into one another of which the editor is just one, and perhaps the most important one.
Here's how to install Visual Studio Code if you've not done that already.
After installing Visual Studio Code, it'll be time to create our very first PHP file, saying 'Hello' to the world. This will be accomplished in the next chapter.