Lesson 2 - Installing Apache + MySQL + PHP on Windows and making an app
In the previous lesson, Introduction to PHP and web applications, we explained how web applications work. We now know that PHP is a module that generates HTML pages on the server.
To be able to program in PHP, we'll need a web server, PHP, and a database.
All of the above are provided by a web host to which we'll upload our website. Since we don't want to debug our website on the web-host, we'll install said services on our computer. This way, we'll program our website and only upload it to the web-host when it's finished and properly tested. These services can be installed as a simple package.
Downloading and installing PHP
Web applications mostly use the Linux OS, Apache web-server, MySQL database and the PHP programming language. We usually abbreviate them as LAMP for short. They're all free of charge. If we run them on Windows, they may be referred to as WAMP.
Probably the most popular installation package including Apache, PHP, and MySQL is called XAMPP. Its installation is very simple.
Go ahead and download the XAMPP package from the official website http://www.xampp.org/
To do so, find the XAMPP for Windows link. There you will see a Download title that you will have to click on to download the Installer as shown in the image below:
It would be best if you run the installation as an administrator. To do that, right-click on the file and choose "Run as administrator". You'll probably see a message about UAC, just click OK.
You may get a notification message saying that you are missing a Microsoft Visual C++ runtime, one that is required by XAMPP. If you happen to see a message like that, just confirm it.
It'll open your web browser on the page where you can download the Microsoft Visual C++ runtime. Once you install it, you will be able to continue with the XAMPP installation.
The XAMPP installation is easy and you mostly just have to click "Next". You'll be able to choose what parts of the package you wish to install. Despite us leaving the default configuration, we'll take this moment to explain each of the components do:
Here we can choose exactly what we want on our server.
- Apache - The web-server that allows for use to communicate with the client. We will absolutely need this one.
- MySQL - A popular database that includes the user-friendly phpMyAdmin database administration tool. There are several other databases, SQLite for example, that are included in PHP.
- FileZilla FTP server - This one allows for us to create our own FTP server to which we can connect afterward. It requires a public IP address.
- Mercury Mail Server - Local SMTP server for email correspondence.
- Tomcat - A serverlet for Java that is useful for when you create websites in this programming language.
You can also choose what programming languages you'd like to use on the server.
- PHP - PHP is included by default.
- Perl - You can also run Perl.
Other tools offered by the XAMPP package include:
- phpMyAdmin - A user-friendly MySQL database administration tool.
- Webalizer - A website-access statistic tool.
- Fake Sendmail - Provides the sending of anonymous emails.
If there is a service you don't want on the server, simply un-select it, but I highly recommend that you install everything just in case. You can choose which services to run on the server and which to turn off so you won't have to use them if not needed.
In the next window, keep the default C:\xampp folder, there could be problems with writing privileges to the Program files folder otherwise.
During the installation, you might need to confirm to allow internet access to the services being installed.
When the installation finishes, you'll be asked if you'd like to start the XAMPP control panel. Go ahead and hit confirm.
The control panel
Once the application starts, we'll see the following form:
Here, we are able to view all of the installed services. The most important parts are the Start/Stop buttons for the Apache and MySQL services. Using these, we can start or stop these services. It's always a good idea to stop them when we aren't using them, as they'd use up your performance otherwise. You can also check whether each service should run as a system service using the checkbox.
READ CAREFULLY! If you're using Skype, you won't be able to start Apache! This is due to the fact that Skype isn't programmed properly and uses port 80 when it is not meant for it. You can fix that by changing Skype's port or by closing Skype - starting Apache - and starting Skype up again.
We can run administration tools or view logs for a given service using the other buttons.
If you got both services running, you're done with this part!
We now have the PHP environment ready for use. The only thing left to do is download an application that will allow us to write PHP code. We will not be using Windows Notepad. PHP is a dynamic language, so we would end up making plenty of mistakes there. We need something smart that will check our source code and auto-complete it.
I recommend using the PhpStorm IDE. It has high-quality tools and is used to develop the ICT.social information system. It comes with many functions that can save you a lot of time. You can download it at http://www.jetbrains.com/phpstorm.
The best free-to-use IDE is probably NetBeans. Although it isn't very "smart" and is kind of slow. You could download it at the following URL: http://www.netbeans.org, and choose the version for PHP development.
Click through the installation and start-up NetBeans.
We'll create a new project (File -> New project), choose PHP application, and name it HelloWorld. This name is used for when someone is making their very first application in a programming language. The sole purpose of HelloWorld applications is to print text. Which is exactly what we're going to program with the time we have left for today
The Project path should be the same as our PHP scripts folder. By default, it's C:\xampp\htdocs.
Last, of all, we'll choose to use the local webserver (our Apache from XAMPP) and confirm.
NetBeans will then generate a new HTML website for you. As you can see, it has the .php extension. You can find the PHP directive in the site's body:
The part of the website between these two tags will be executed as a PHP script. Let's try it out using the echo() function that prints text into a website. The source code should be as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php echo("This text was inserted to a website by PHP"); ?> </body> </html>
Start your project by clicking on the green "Play" button in your IDE or by entering the following address into your web browser: localhost/HelloWorld. You will then see the following result:
You can run the code also right in our online compiler by pressing the "Run code" button. Like this, you are able to study from everywhere even you don't have development tools installed.
The text is printed by PHP and is not part of the HTML website. In case something went wrong, try the same thing out on your web host directly.
In the next lesson, Variables and type system in PHP, we'll create something more interesting. We'll start by explaining PHP's language syntax. Every time we make a project I will make it available for download below the article, which will be useful if you happen to make a mistake.
Downloaded 56x (410 B)
Application includes source codes in language PHP
4 messages from 4 displayed.