PHP Development Environment

What’s a Software Development Environment

As a server-side scripting language for web development, PHP scripts are executed on the server. So, at the very least, you will need to install a PHP-enabled web server on your development computer so that you can test your PHP scripts during development. In addition, you also need a software tool to write your PHP scripts on the computer. In other words, you need to set up a software development environment for PHP on your development computer.

A software development environment is the location where you write and test your software applications before uploading them to the staging environment and then to the production environment. The most common location for your development environment is your own computer. For the purpose of testing your PHP scripts, your target software development environment should at least comprise these three software components:

  • Web Server — Apache.
  • Code Interpreter — PHP engine.
  • Code Editor — any text editor of your choice.

What’s Web Server

A web server is a piece of computer software installed on a computer that hosts a website, which is a collection of web resources which may include HTML files, server-side script files, CSS files, JavaScript files, image files, and so on. Typically, you request a web resource file, e.g. index.php, from a web server by entering into the address bar of a browser an appropriate URL that points to that resource file on that web server, e.g. www.mywebsite.com/index.php. Upon receiving your request, the web server locates the resource file, call upon the appropriate server-side scripting engine to parse and execute any server-side scripts contained in that file, the results of which together with any HTML markup are then returned to your browser for rendering as a HTML formatted web page. Based on the HTML markup, your browser might then make further requests for other resources e.g. CSS, JavaScript, and image files.

There are different types of web server software available that supports PHP — Apache, IIS, Nginx, Lighttpd, etc. Among them, Apache is the most widely used web server software for PHP website.

Apache as Web Server

For the purpose of learning the PHP lanugage, it really doesn’t matter which web server software you use as long as it supports PHP. For practical reasons, however, you should use the web server software that your web host uses. For PHP websites, more often than not, your host is using Apache as the web server. Apache is the fruit of the open source Apache HTTP Server Project undertaken by the Apache Software Foundation with supports from volunteers all over the world. Apache’s popularity as web server software can be attributed to the facts that:

  • Apache has a long proven track record of reliability and performance.

  • Apache is open source thus it has no licensing fees or costs to bear.

  • Apache can be installed on most OS platforms, e.g. Linux, Mac, Windows.

  • Apache has been constantly maintained, upgraded, and supported by a network of volunteers.

  • There are plenty of documentation and helps available online.

  • Apache is one of the most feature-rich web servers available.

Setting up Apache and PHP

The easier and faster approach is to install them from some all-in-one software packages. One of the most popular all-in-one packages is this XAMPP available free for download and installation from Apache Friends. XAMPP stands for Xross-platform, Apache, MariaDB (a community-developed fork of the famous MySQL), PHP and Perl. Installing Apache and PHP using XAMPP is just a matter of a few mouse clicks away. Read the installation instructions and FAQs available at Apache Friends and proceed to install the XAMPP on your development computer.

By default, XAMPP for Windows is installed in C:\xampp directory while that for Mac is installed in /Applications/XAMPP directory. The default document root directories where web resources are kept are then C:\xampp\htdocs in Windows and /Applications/XAMPP/xamppfiles/htdocs in Mac respectively.

XAMPP comes with a control panel where you can start and stop servers easily. Start the Apache web server and keep it running, then proceed to set up a website.

Setting up a Website

A website is actually a folder that contains all web resources. By default XAMPP stores the website you create insides the document root directory with http://localhost as the top-level domain, and you can then access the website via the URL of http://localhost/website.

To set up a website called myfirstwebsite, for example, first create a sub-directory called myfirstwebsite inside the htdocs directory; then inside the myfirstwebsite directory, create a PHP file called index.php that contains the following PHP script using a text editor:

<?php
echo "<h1>My first PHP page from my first website</h1>";
?>

You have just set up a website called myfirstwebsite containing one resource file called index.php on the Apache web server. To visit the index.php, enter this URL http://localhost/myfirstwebsite/index.php or simply localhost/myfirstwebsite into the address bar of your browser and then hit the Enter key — do you see something that looks like the animated screenshot in Figure 1?

Figure 1: ~~My~~ Your First Website

Figure 1: My Your First Website

Isn’t that a piece of cake?

Note:

You can omit the `http://` as modern browsers will append it for you. You can also omit `index.php` as it is the default resource file that the web server will look for automatically if the resource file is omitted.

Wait a second…

This is not quite like the real stuff, isn’t it? In reality, your website will have a domain name, say www.myfirstwebsite.com, and is expected to be accessed via a URL that reads something like http://www.myfirstwebsite.com/, right? How can we simulate such a scenario on your development computer? Follow me if you dare…

Let’s use the XAMPP on a Windows computer as an example. There are two parts to the puzzle &mdash; setting up a virtual host pointing to the C:xampphtdocsmyfirstwebsite folder as another document root directory other than the default "C:xampphtdocs", and creating a local DNS entry for a custom domain called www.myfirstwebsite.com.

Setting up Virtual Hosts on Apache

  • Stop the Apache if it is running.

  • Locate the file called httpd-vhosts.conf which is usually found in the C:xamppapacheconfextra directory. It is prudent to make a backup copy of this file before attempting to make any changes to it.

  • Open and add the following content to the bottom of httpd-vhosts.conf:

<VirtualHost *:80>
  DocumentRoot "C:/xampp/htdocs"
  ServerName localhost
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot "C:/xampp/htdocs/myfirstwebsite"
  ServerName myfirstwebsite.com
  ServerAlias www.myfirstwebsite.com
  <Directory "c:/xampp/htdocs/myfirstwebsite">
  Order allow,deny
  Allow from all
  </Directory>
</VirtualHost>

Add a Custom Domain on Local Computer

  • Locate the file called hosts which can be found in the C:WindowsSystem32driversetc directory. Again, it is prudent to make a backup copy of this file before attempting to make any changes to it.

  • Open and add the following entry to hosts  using a text editor that is opened as administrator:

127.0.0.1  www.myfirstwebsite.com

Restart Apache, enter www.myfirstwebsite.com in the address bar of your browser and hit enter, do you get the output as animated in Figure 2?

Figure 2: ~~My~~ My First Realer Website

Figure 2: My Your First Realer Website

Congratulation, you can now test your website on a development computer using a more realistic domain.

PHP Configuration

Every PHP installation comes with default configuration settings contained in a configuration file called php.ini. The php.ini is read whenever PHP starts up and it contains a list of configuration directives whose values can be changed to affect the functionality and behaviour of PHP such as the default directories to look for files when a file path is not given, the maximum number of files allowed to be uploaded simultaneously, and the maximum file size permissible for each file upload, and so on.

You can view a complete list of the configuration settings in the php.ini file on a browser by calling the phpinfo() function. In the myfirstwebsite sub-directory of the htdocs directory, create a new PHP file called phpinfo.php that contains the following PHP script using a text editor:

<?php
  phpinfo();
?>

Enter this URL http://www.myfirstwebsite.com/phpinfo.php or simply www.myfirstwebsite.com/phpinfo.php into the address bar of your browser. Do you see this?

Figure 3: Output of `phpinfo()

Figure 3: phpinfo()

The php.ini file is a well commented text file. Before you make any changes to it, be sure to keep an original backup.

Tagged with:

Speak Your Mind