I am a Software Architect at IBM India Software Labs and part of the BPM product development team.In my spare time, I like to research on upcoming technologies and like to share my knowledge and experiences through blogs,articles, books and various forums.I am also a Master Author with IBM developerWorks,have co-authored books on Spring framework, the first book on Apache CXF and multiple IBM Redbooks.You can reach me on my website www.naveenbalani.com Naveen is a DZone MVB and is not an employee of DZone and has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

WebMatrix: Why PHP Developers Should Pay Attention

05.11.2011
| 26375 views |
  • submit to reddit
Gone are the days when PHP developers had to turn away business because the clients used Windows Servers. If you are a PHP developer and have been looking for a cost-effective and easy way to begin developing web sites for clients on Windows, WebMatrix is your new best friend. With WebMatrix, Microsoft has created a new point of entry into the world of Windows web development. WebMatrix allows you to create web sites quickly and in your preferred web programming language. This article discusses what WebMatrix can do for you as a PHP developer.

Note from the authors:
Rajeev Hathi and Naveen Balani co-authored this article.
Rajeev Hathi works as a software consultant for the J2EE platform. His interests are architecting and designing J2EE-based applications. Rajeev attained SUN certifications in Java and J2EE technologies (Web, EJB, Architect). He has contributed to IBM developerWorks by writing articles about Ajax, Web services, DB2, and XML. He is the co-author of the first book on Apache CXF framework.He can be reached at http://www.rajeevhathi.com

Introduction to WebMatrix

WebMatrix is a free web development tool from Microsoft that includes everything you need for web site development. WebMatrix provides an integrated environment for building, testing, and deploying web sites. This integrated environment includes an embedded IIS web server, database, and programming template that lets you get started with web site development,  which includes PHP and .NET applications. If you use any of the widely used blog and content management solutions based on the PHP programming language (such as WordPress, Joomla, or Drupal) or .NET-based solutions (such as DotNetNuke), WebMatrix allows you to configure and download these applications quickly through its Open Gallery feature.

In this article, we will describe two ways to get started with PHP development using WebMatrix. The first approach includes starting from scratch by creating a new web development project. The second approach includes integrating an existing open-source application like WordPress and customizing the web site with WebMatrix. This second approach is similar to importing any existing PHP application and starting development on it.

Installing WebMatrix

To get started with WebMatrix, go to http://www.microsoft.com/web/webmatrix/ and click Install WebMatrix.  This launches the Web platform installer that will guide you through the process of installing WebMatrix. It is recommended that you accept the defaults to install the required components..

When you launch WebMatrix, the following screen opens.

The Site from Web Gallery option allows you to get started with pre-configured web software applications like WordPress or Drupal quickly. The Site from Template option allows you to create an empty website (as shown below). The Starter site, Bakery, Photo Gallery, and Calendar options currently provide .NET application templates; these sample applications can be used as reference or extended to create end-to-end applications. The My Sites option allows you to work with existing WebMatrix projects that you have created. The Site from Folder option allows you to import existing PHP (or .NET based) projects.

Creating a PHP Web Site

As an example, we will use an empty site to build a sample PHP application.  Click the Empty Site icon and enter the site name “SampleApp”. Next, enable the PHP support for the site by clicking Settings and selecting the Enable PHP button as shown below. This opens a pop-up window that asks you to select and install a PHP version. Select the desired version and click Install.

.

Developing and testing the PHP application

Once PHP is installed, click Files and then New. Select the PHP file type from Common options, provide the name ‘techfeeds.php’, and click OK. techfeeds.php will open up in the editor.  Now you can begin coding the PHP application.

WebMatrix offers PHP code syntax highlighting capabilities so that you can easily identify pieces of PHP code and syntax errors quickly. Let’s create our sample application by adding the following lines of code between the <body></body> tags as shown below. This reads the latest RSS feeds from naveenabalani.com. <body>


<?php

function load_file($url)

{ $ch = curl_init($url);

#Return http response in string

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$xml = simplexml_load_string(curl_exec($ch));

return $xml;

}

$feedurl = 'http://naveenbalani.com/index.php/feed/';

$rss = load_file($feedurl);

foreach ($rss->channel->item as $item)

{

echo "<h2>" . $item->title . "</h2>";

echo "<p>" . $item->description . "</p>";

} ?>

</body>

Once you have pasted the above code in the editor, you will see the PHP syntax highlighting capability go to work. Next, click the Run icon in the top menu to run the application. This will show you the RSS feed in the browser. As you can see, you are up and running with a sample PHP application within minutes.

Publishing the PHP application

Once you have created your PHP application, you can publish your application on your web site using the Publish feature. To do this, click the Publish icon and provide the options in the Publish settings. In this example, I used the FTP protocol option and provided the site path inside of www root folder (i.e., wwwroot/util/php). Click Validate connection and you should see a message that says, “Connected Successfully”. Click Save. You will see a message as shown below. 

Click Yes.

The message I received said that the compatibility couldn’t be determined, but since I knew that I have a hosting server which supports PHP 5.3, I went ahead and published the changes.

In the preview window, you will see a list of files being uploaded. Select the file and click Continue. You should see a success message along with the URL (for instance - http://naveenbalani.com/util/php/techfeeds.php) .  For subsequent publishes, WebMatrix keep tracks of what local files are changed so that you can upload only changed files on the server. Within a few minutes, you have deployed your PHP application on your hosting environment. Any subsequent changes can be tested on a local environment and published to the hosting environment quickly.

Development using an Existing Web Application

This section focuses on developing a simple web site using WordPress with the help of WebMatrix. WordPress is an open–source, widely used blogger tool that can be used to create a blog web site where users can view and post comments. WordPress comes as part of the Web gallery in WebMatrix. To build the site using WordPress, follow these steps.

1.      Select WordPress from the Gallery.

When you launch WebMatrix, you can choose open-source applications, including WordPress, from the Web Gallery. To set up WordPress, click the Site From Web Gallery option. You will see a list of open-source web applications supported by WebMatrix. Click the WordPress icon and enter the name of the site that you wish to create. The default site name is ”WordPress”. In this example, we will change this to “My Simple WordPress Site”.

 

2.      Select DB engine.

The next screen prompts you to select the database engine. You can only select the MySQL database engine because WordPress (by default) supports only MySQL. Click Next. You will be prompted for your admin information. By default, the admin user name is “root”. Enter the password and make sure to note it down somewhere so that you do not forget.

3.      Download and install WordPress and MySQL.

The next screen prompts you to accept or decline the user licensing agreement. Click I agree and WebMatrix will start downloading and installing WordPress and MySQL. It will also download and install PHP and the MySQL Connector. The total download size will be approximately 115MB and should take a short time to install, but obviously the download time will depend on your internet broadband connection speed.

The installation of WordPress will require you to set up the database. The next screen prompts you to create a database where WordPress will store all your site related information.

By default, the Database Name is “wordpress” and the Database User Name is ”wordpressuser”. You will need to enter the database user password and then re-enter it. You also have to provide the password for your admin root user, which is the password that you set up in the previous step. You can leave the rest of the fields as their default values. Click Next to install WordPress. 

WebMatrix launches your site’s home window where you can manage your files, database, and reports. Click the Files tab to see the list of files that represent the content of your web site. You can edit these files to modify your site content.

4.      Setting up your WordPress site.

Now you can set up your site details. Click Run on the ribbon menu to run your site. The WordPress index page launches. Enter your Site Title, Username (admin role), Password, and email ID. We will give the site the title: ”My Sample WordPress Site”.

The username and password are used to administer the site from WordPress. Once you provide the necessary information, your site is ready for use. You can enter your site administration dashboard, which is the central area where you can create and manage your site blogs and certain other features, using the following URL: http://localhost:8397/wp-login.php. Enter your username and password. You will notice that WordPress has created the default blog page called “Hello World”. You can view your sample blog and site home page using the following URL: http://localhost:8397/.

5.      Modifying your website with WebMatrix

Now that you have your web site set up, you can easily modify it with WebMatrix. Go to your site design home in WebMatrix. Click the Files tab to see the list of WordPress PHP files. As stated earlier, these files represent the overall content of your website. As a WordPress user, you can tweak or change these PHP files to customize your web site. Double-click any of the PHP files to open them in the WebMatrix editor. The editor distinguishes PHP files from other web formats. After you make changes to the file, click Save and then refresh your web site. There are no extra steps for deployment.

Conclusion

WebMatrix allows you to develop and test PHP applications. Additionally, you can easily publish your applications and subsequent updates in the hosting environment very quickly. PHP developers have nothing to fear anymore when developing on a Windows environment.  WebMatrix seamlessly handles all of the complexities in the background and handles a good deal of tedious coding for the developer.

 

Published at DZone with permission of Naveen Balani, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gene Osteen replied on Mon, 2011/08/22 - 8:29pm

Today is the first time I have touched PHP. The sample you provided did not work. I was able to run the following.
<?php
phpinfo();
?>

When I ran the sample I received the following.

HTTP Error 500.0 - Internal Server Error The page cannot be displayed because an internal server error has occurred.

Most likely causes:
IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.
IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.
IIS was not able to process configuration for the Web site or application.
The authenticated user does not have permission to use this DLL.
The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.


Things you can try:
Ensure that the NTFS permissions for the web.config file are correct and allow access to the Web server's machine account.
Check the event logs to see if any additional information was logged.
Verify the permissions for the DLL.
Install the .NET Extensibility feature if the request is mapped to a managed handler.
Check the failed request tracing logs for additional information about this error. For more information, click here.

Detailed Error Information:
Module FastCgiModule
Notification ExecuteRequestHandler
Handler PHP53_via_FastCGI
Error Code 0x00000000
Requested URL http://localhost:37498/techfeeds.php
Physical Path C:\Documents and Settings\Geneo\My Documents\My Web Sites\SampleApp\techfeeds.php
Logon Method Anonymous
Logon User Anonymous
Request Tracing Directory C:\Documents and Settings\Geneo\My Documents\IISExpress\TraceLogFiles\SAMPLEAPP

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.