Installing PHP
Let's get started with PHP by setting up the development environment. You always have the option of using a hosting company, FTP your files there and test on the remote server. But setting up the environment is very easy and will make your life easier in the long run. Basically it's right way to do development, you work locally and once you're sure everything works well, you deploy to a live server.
Maybe you've heard of LAMP, it means Linux, Apache, MySQL, PHP. Now we'll setup all these but on Windows, instead of Linux.
All the components (Apache, MySQL, PHP) can be installed separately one by one, but there's an easier way - it's called XAMPP. This is a package that contains all the components we need (plus more), all in once.
Download and unzip XAMPP
To download XAMPP, visit this page and in the "Downloads" section follow the XAMPP Lite link. As the name suggests XAMPP Lite is not the full XAMPP package but it already has all we need to start building PHP applications. Take the "EXE (7-zip)" download (as it's the smallest) and save it anywhere on your hard drive. Then double-click the downloaded file and it will prompt you where do you want it uncompressed. Select C:\
Start the environment
So there's no installation, it's all taken care of by simply unzipping. Now you should have a directory on your hard-drive called C:\xampplite
Now let's start Apache and MySQL. If you look in your C:\xampplite directory you'll find the following files (among others)
- C:\xampplite\apache_start.bat
- C:\xampplite\mysql_start.bat
- C:\xampplite\xampp_start.exe
If you double-click the first one, it will start the Apache web server. The second one will start the MySQL database server. Instead of starting them one by one, alternatively you can use xampp_start.exe, it will start them both.
Stopping services
The three files from the list above have their *_stop.* counterparts. Use these when you want to stop the services you've started.
Testing that it all works
Now that you've started Apache and MySQL, let's write a quick PHP script and test if it works, which will mean that your environment is setup correctly. (BTW, MySQL is not required for this example)
Create a file in C:\xampplite\htdocs\ like C:\xampplite\htdocs\isitworking.php with the following content:
<?php echo 'Oh, yes, it is!'; phpinfo(); ?>
Now point your browser to http://localhost/isitworking.php and it should display "Oh, yes, it is!" and a long table full of PHP settings. Great! You're all set!
Working directory
You created your first PHP script and placed it in C:\xampplite\htdocs so that it's accessible using the URL http://localhost/. So this means that C:\xampplite\htdocs is your "web root" and any subdirectory you create inside it will be visible as a subdirectory in the URL. For example C:\xampplite\htdocs\tests\mypage.html will be visible as http://localhost/tests/mypage.html
Custom root and data directories
Note: This step is optional.
Working with the default root directory that XAMPP Lite suggests is totally fine, same is true for the MySQL data directory (the directory where all the database tables are stored). But if you want to look a bit in the future, you might decide to substitute XAMPP Lite with the full XAMPP, or may decide to upgrade one of the services (say MySQL) on your own. In this case you'll be more "portable" if you keep the files you create separate. This can also ease your backups (you do create backups, don't you, you're not like me) since using the setup suggested below you can have only one directory to back up.
So I'd say, create a directory C:\mywebstuff with two subdirectories:
C:\mywebstuff\htdocs- the web document rootC:\mywebstuff\dbdata- MySQL database tables
In order to setup the web root, open C:\xampplite\apache\conf\http.conf and change the two occurrences of "/xampplite/htdocs" with "/mywebstuff/htdocs". Now move isitworking.php to the new directory (so it becomes C:\mywebstuff\htdocs\isitworking.php), restart Apache and check if it works.
To setup the MySQL storage directory, open the file C:\xampplite\mysql\bin\my.cnf (with a text editor) and in the section [mysqld] change the setting datadir to read:
datadir="/mywebstuff/dbdata"
Then copy the directories mysql, phpmyadmin and test from C:\xampplite\mysql\data (the default storage directory) to C:\mywebstuff\dbdata (the new data directory)
Restart MySQL and you should be ready to rock!
Note that when I say "restart Apache" it means run apache_stop.bat followed by apache_start.bat. Similarly "restart MySQL" means run mysql_stop.bat followed by mysql_start.bat, all these .bat files are located in C:\xampplite. Alternatively you can simply use xampp_restart.exe
Troubleshooting a potential issue
It's possible that Apache won't start, because you already have a web server running. Don't ask how this happened, it's your PC, isn't it
The thing is that some Windows versions might start IIS (Internet Information Services, which is Microsoft's web server, Apache competition) without you even realizing it. Or you might be running IIS intentionally f you do some ASP or .NET development. Anyway, if you get a message that says something like:
(OS 10048)Only one usage of each socket address (protocol/network address/port) is normally permitted. : make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
In this case you can configure the Apache web server not to listen for requests on port 80 (the usual) but on some other, like 8080. In order to do this, open the file:
C:\xampplite\apache\conf\httpd.conf
And replace "Listen 80" with "Listen 8080"
You also need to change the port for secure requests, which is normally 443. So open the file:
C:\xampplite\apache\conf\extra\httpd-ssl.conf
and change "Listen 443" with "Listen 444".
Then try starting Apache again, it should be fine.
In this case when you request pages in your browser, you should by using the port appended to the hostname, like http//localhost:8080/isitworking.php instead of http://localhost/isitworking.php
Tags: installing php, mysql, php setup, xampp