Embark on Your Programming Journey with AMP

0
4999
Embark on Your Programming Journey with AMP
Php-on-WindowsHere is an insight into running Apache, MySQL and PHP (AMP) on Windows. The author leads readers through his own experience to point out where the pitfalls are. This article is particularly useful for newbies venturing into AMP.

The first time I tried to create a Web app was during my college days. It was to be an application that would act like a college management system. Since I was a newbie and the project was too ambitious, I discovered that I couldn’t figure things out—the app never really worked. Of the many things I do remember was the fact that I couldn’t set up LAMP on my OpenSUSE box. I was a power user of OpenSUSE, yet, after a couple of days, I simply couldn’t get the LAMP stack working. I then decided to stop trying and begin again on a platform that I didn’t particularly like to use for development —Windows.

Starting off
Linux is not always the best platform to start on, since most people are unfamiliar with it. Even power users can get confused and not be able to set up a development environment straight away. This is one instance where you ought to stick with Windows rather than go with Linux. As a beginner who has not forayed into Web development, on Windows you can at least understand the instructions and follow them, and almost everyone you meet has some understanding of it. The ready-made packages for Windows require you to just install them to start working.

There are two ways to start off with the AMP (Apache, PHP, MySQL) stack on Windows: 1) Use a prebuilt package, and 2) Install the individual components and configure them to work with each other, manually. The first option is the easier one, which I would recommend. It is difficult to configure AMP manually on Linux as well as on Windows. This is largely due to the number of configuration files that need to be edited and the steps that have to be taken. Briefly, here are the steps you need to follow while configuring AMP manually on your system:
1.     Install MySQL.
2.    Configure MySQL parameters by editing the my.cnf file.
3.    Install PHP.
4.    Install the MySQL extension for PHP.
5.    Edit the PHP configuration file (php.ini) to enable MySQL’s support for PHP.
6.    Install Apache.
7.    Configure Apache to work with PHP.

There are chances of you getting stuck at many stages in this process; hence, installing a preconfigured package is recommended for Windows. Preconfigured packages are software installers that do the steps listed above, for you. All you need to do is to install them and you are ready to go!

The one that is most widely used is XAMPP, which I recommend because it is popular—it means that if you encounter a problem, there will be enough help available on the Web. The second (and quite important) parameter is path names. Where you install software in Windows is an important factor, more so when you are running software originally built for UNIX-like platforms (or let’s just say ‘Linux’ instead). Windows and Linux deal with file system paths in different ways, which can be annoying. The installation paths for the components (Apache, PHP and MySQL) are conducive to the Windows environment. The third reason for choosing XAMPP is that it comes with a simple service controller. So you can launch Apache and MySQL when you need them and shut them down when you don’t. Fourth, XAMPP also comes with a nifty FTP client, which can be pretty useful when you need to upload your work to a server. The last reason is that XAMPP also comes with other necessary tools, which allow you to get started real fast; for example, the MySQL root password is set to blank, PhpMyAdmin (a Web or browser-based tool to manage MySQL databases) is already installed and there is a page showing ‘phpinfo()’. Please note that the setup created by XAMPP is nowhere close to being secure enough for use on a production server.

Moving on with PHP on Windows
Installing PHP on Windows is only the beginning, to be followed by installing extensions, which is much simpler. Most of the necessary extensions are already installed. All you have to do is to enable them in the php.ini file and they will start working. So connecting to your databases or encrypting your data using PHP isn’t a problem.

But as you move forward, it is important to keep in mind that PHP’s primary target is not Windows. PHP is UNIX-world software that ‘also’ runs on Windows. One of the biggest problems for new programmers using PHP on Windows is the path names. If you open a file and you copy-paste the path from your Windows Explorer’s address bar into your PHP script, it may not work—one of the most common problems.

File paths
On all UNIX-like systems, the directory separator is the ‘/’ character. On Windows, it is ‘\’. At many points, this might cause some confusion upfront. While ‘\’ might work at times when specifying the path in PHP, it should be avoided. The worst thing you can do with paths is to mix the directory separators in the same path, which will not work. PHP recommends that you use ‘/’ as the directory separator on Windows as well. So C:\xampp\htdocs\project should be written as C:/xampp/htdocs/project.

Line endings and IDEs
Development involves using other people’s work—a library, a patch or maybe even a complete project. Linux uses the ‘\n’ character for newline while Windows uses ‘\r\n’. This means that if you are using a piece of code which was originally written for Linux, it may not look pretty on Windows; at least, not natively. If you have been using a primitive text editor (such as Notepad) for coding, make sure that you get yourself an IDE or at least a decent text editor with more developer-friendly features. Notepad++ and Sublime Text are very developer-friendly text editors. Eclipse for PHP is a good free IDE while PHPStorm is by far the best IDE you can get for PHP, with many of its features currently not seen in most other IDEs. If you like Visual Studio, there is a plugin by Microsoft that allows you to write PHP with Visual Studio as well. Get yourself a decent IDE/text editor. These not only fix the viewing problems associated with line-endings but also come with some goodies that help you work much faster.

Servers
PHP is a scripting language for the Web. You do need a Web server to make it function and there are a number of them. On Windows, you have at least one extra option compared to other platforms – IIS. This stands for Internet Information Services and is the Web server developed by Microsoft itself. There are two things about it that need to be remembered:

1.     It is switched off, by default.
2.     If you are not going to work on C# or other .NET languages for Web development on Windows, it is better to use Apache rather than IIS. It does exactly what Apache does—serve Web pages. However, IIS supports Microsoft technologies, which Apache does not do too well. However, when it comes to PHP, the amount of help you get online with Apache is much more than what you get for IIS. The reason being that Apache works on Linux too, and with the exception of file paths, most solutions that work on Linux work on Windows as well.

And like I have already said, it is better to go for packaged AMP installations such as XAMPP which come with Apache. Enabling IIS would create a conflict with them.

Apache
Apache is the most famous Web server there is. What separates Apache from the rest is the amount of customisation that you can do. Having a threaded server or a non-threaded one, support for a very large number of languages as modules, handling multiple websites, managing permissions, customisable security levels are some of the most attractive functions that one can tweak the server to deliver. All of this means that there are chances that you can get one of those wrong and that would damage your installation. So first things first –- if the server is working the way you want it to, try not to change it (unless of course you are trying to learn more about Apache Web server itself).

Do remember that for developmental purposes, it is better to keep the security a little loose, while on the production server, it must be as tight as possible. If you are able to run multiple Web projects using just ‘localhost’, so much the better. If you have two projects named ‘college’ and ‘personal’, it is better to run them from http://localhost/college and http://localhost/personal when possible.

However, this is not always feasible and we might want separate domain names to test things out. To create a VirtualHost in Apache is not a very easy task if one has not done it previously. To make a new VirtualHost in Apache, follow the steps mentioned below (which have been tested with Apache installed with XAMPP; the configuration file governing the VirtualHosts might be different for other installations):
1.     Open C:\xampp\apache\conf\extra\httpd-vhosts.conf file in a text-editor.
2.    Add the following lines of code to the end (explained later):

<VirtualHost *:80>
    ServerAdmin youremail@host.com
    DocumentRoot "C:/xampp/v-htdocs/drupal.example.com"
    ServerName drupal.example.com
    ErrorLog "logs/drupal.example.com-error.log"
   CustomLog "logs/ drupal.example.com-access.log" common
  </VirtualHost>

3.     Save the file.
4.    Open file: C:\Windows\System32\drivers\etc\hosts in Notepad and add this line at the end:
a. 127.0.0.1 drupal.example.com
5.    Save the file and restart the Apache server.

The lines you added in Step 2 play the following roles:

  • <VirtualHost *:80>: This starts the definition of the virtual host and says that it should be reachable from all addresses on port 80.
  • ServerAdmin: This is the email which is displayed in error messages and should be of the person who maintains the server.
  • DocumentRoot: The path of the folder from where the files of this VirtualHost will be served.
  • ServerName: The domain name to which this VirtualHost will respond.
  • ErrorLog: This is the path of the file where the error log should be stored. It is relative to the Apache installation directory.
  • CustomLog: Other error logs should be stored in the file indicated here.
  • It is also important to make the changes in your hosts file later so that you can use the domain name of this virtual host on your machine.MySQL
    MySQL comes with XAMPP. If you are installing it outside of XAMPP, the wizard takes all the right steps. However, there are a few things to keep in mind:

    1.     The MySQL root password should not be forgotten.
    2.    The MySQL root password is set to blank by XAMPP, by default—this is not good for production servers.
    3.    You should try to have another user, even for developmental purposes because if you lose access to the root user, it would put you in trouble.

    Though I have listed some important points, you will encounter other problems along the way. Remember that Google is a friend and the official forums are great when it comes to asking for help with each of those products. Happy coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here