Tag Archives: php

Setting up PHP for development on Windows Subsystem for Linux in Windows 10

I have been working a little with PHP, for the first time for a while, and soon found it annoying not to have the convenience of instant application testing and line by line debugging. I have set up a PHP development environment before using XAMPP for Windows and Eclipse, but it was fiddly. I also prefer PHP on Linux, which is where my scripts will be running.

Since Windows 10 now has a Linux environment built-in, called Windows Subsystem for Linux (WSL), I decided to set this up to run Apache, PHP and MySQL and to try debugging my scripts there.

My PC is a recent installation and I had not yet installed WSL. To do so, you have to both download a Linux distribution from the Store (I chose Ubuntu), and enable WSL in Windows features. Then restart, launch Ubuntu, set a username and password, and you are up and running.

Note the Linux commands that follow should be run as root using sudo.

Before doing anything else, I got Ubuntu up to date:

apt-get update

apt-get upgrade

Then I installed the LAMP suite:

apt-get install lamp-server^

(the final ^ is intentional; see the guide here).

To check that everything is working, I created the file phpinfo.php in /var/www/html with the following contents:

<?php phpinfo(); ?>

and restarted Apache:

/etc/init.d/apache2 restart

Note: if you have IIS running in Windows, or another web server, Apache will not be able to listen on port 80. Change the port in /etc/apache2/ports.conf and in /etc/apache2/sites-enabled/000-default.conf

Then I opened a web browser on the Windows side and browsed to localhost:

image

and

image

We are up and running, but not debugging PHP yet. Remember the basic rules of WSL:

  • you cannot change Linux files from Windows.
  • you can access Windows files from Linux.

We want to edit PHP from Windows, so we’ll define a site that uses Windows files. Windows files are under /mnt/c (or whatever drive letter you are using).

So if you example you have your PHP website in a folder called c:\websites\mysite, you can have Apache serve files from that folder.

The quickest way to get up and running is to create a symbolic link in the Apache home directory, in my case /var/www/html. Change to that directory and type:

ln -s /mnt/c/websites/mysite mysite

Now you can view the site at http://localhost/mysite/

This worked first time for me, complete with PHP running. You could also set up multiple virtual hosts in Apache, and use the hosts file in Windows to map other host names to localhost.

Next, you probably want PHP to show error messages. To do this, replace the default php.ini with the development version (or tweak it according to your own preferences. At the time of writing, on Ubuntu, the default PHP version is 7.0 and php.ini-development is located in /usr/lib/php/7.0/php.ini-development. So I backed up the ini file at /etc/php/7.0/apache2, replaced it with the development version, and restarted Apache. My PHP form immediately showed me a non-fatal undefined index error, so it worked.

There is one small inconvenience. Apache in WSL will only run during the session. So before starting work, you have to open Ubuntu and type:

sudo apache2ctl start

Well, background task support is coming to WSL but I do not regard this as a big problem.

OK, this is cool, we can make changes in the PHP code in our favourite Windows editor, save, and view the results directly in the browser. But what about line-by-line debugging? For this, we are going to use Visual Studio Code with the PHP Debug extension:

image

Then on the Ubuntu side:

apt-get install php-xdebug

Restart Apache:

apache2ctl restart

Check that phpinfo.php now shows an Xdebug section. Then edit php.ini and add the following:

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

Restart Apache again and XDebug is ready to go.

Over in Visual Studio code there is a little more work to do. The problem is that although everything is running on localhost, the location of the files looks different to Linux than to Windows. We can fix this with a pathMappings setting. In Visual Studio code, open the PHP file you want to debug. Click the Debug icon and then the little gearwheel near top left; this will open launch.json. By default there are a couple of settings for XDebug. These are OK for a default setup, but we need to add path mapping so that the debugger knows where to find the files. For example:

image

Now you can set a breakpoint, start debugging, and open the page in your browser:

image

More guidance on the PHP Debug extension by Felix Becker is here.

Final thoughts

This is cool; but is it better or worse than an old-style VM running Linux and PHP? The WSL solution is lightweight and convenient, but unlike a VM it is not isolated and you may hit issues that are unique to WSL, because not everything runs. I did happen to suffer crashes in Visual Studio and in Outlook while WSL was running; it may well be coincidence, but I cannot help wondering if WSL might be to blame.

Still, a great feature of WSL is that when you exit your session, it goes away, so it is not too intrusive. I plan to use it for PHP debugging and will see how it goes.

PHP Developer survey shows dominance of mobile, social media and cloud

Zend, a company which specialises in PHP frameworks and tools, has released the results of a developer survey from November 2011.

The survey attracted 3,335 respondents drawn, it says, from “enterprise, SMB and independent developers worldwide.” I have a quibble with this, since I believe the survey should state that these were PHP developers. Why? Because I have an email from November which asked me to participate and said:

Zend is taking the pulse of PHP developers. What’s hot and what matters most in your view of PHP?

There is a difference between “developers” and “PHP developers”, and much though I love PHP the survey should make this clear. Nevertheless, If you participated, but mainly use Java or some other language, your input is still included. Later the survey states that “more than 50% of enterprise developers and more than 65% of SMB developers surveyed report spending more than half of their time working in PHP.” But if they are already identified as PHP developers, that is not a valuable statistic.

Caveat aside, the results make good reading. Some highlights:

  • 66% of those surveyed are working on mobile development.
  • 45% are integrating with social media
  • 41% are doing cloud-based development

Those are huge figures, and demonstrate how far in the past was the era when mobile was some little niche compared to mainstream development. It is the mainstream now – though you would get a less mobile-oriented picture if you surveyed enterprise developers alone. Similar thoughts apply to social media and cloud deployment.

The next figures that caught my eye relate to cloud deployment specifically.

  • 30% plan to use Amazon
  • 28% will use cloud but are undecided which to use
  • 10% plan to use Rackspace
  • 6% plan to use Microsoft Azure
  • 5% have another public cloud in mind (Google? Heroku?)
  • 3% plan to use IBM Smart Cloud

The main message here is: look how much business Amazon is getting, and how little is going to giants like Microsoft, IBM and Google. Then again, these are PHP developers, in which light 6% for Microsoft Azure is not bad – or are these PHP developer who also work in .NET?

I was also interested in the “other languages used” section. 82% use JavaScript, which is no surprise given that PHP is a web technology, but more striking is that 24% also use Java, well ahead of C/C++ at 17%, C# at 15% and Python at 11%.

Finally, the really important stuff. 86% of developers listen to music while coding, and the most popular artists are:

  1. Metallica
  2. = Pink Floyd and Linkin Park

Wow.

Zend’s PHP cloud: develop in the cloud, deploy anywhere

Zend has announced Zend Studio 9 beta, the latest version of its IDE for PHP. The feature that caught my eye is integrated support for the Zend Developer Cloud, currently in technical preview. Setting up a PHP development environment is not too difficult, but can be a hassle to maintain, and the idea of being able to fire up an IDE anywhere and start coding is attractive.

You do not need Zend Studio to use the Developer Cloud; they are independent projects, and you can use the free Eclipse PHP Development Tools (PDT) or another IDE or editor.

The PHP Developer Cloud is not just a shared hosting environment for PHP applications:

All applications are housed within a container on the Zend Application Fabric. This container is separate from all other containers and has its own database instance and is easily connected to your IDE.

The Zend Application Fabric is for deployment as well as development. It is a server framework that includes the Zend Framework and also the capability of scaling on demand.

Once you have developed your app, you can deploy to any cloud provider that supports the Zend Application Fabric, including Amazon Web Services, IBM SmartCloud,  a private “custom cloud”, or a resilient multiple cloud option which Zend calls RightScale. You can deploy to RightScale using both Amazon and Rackspace together, which I presume means your app will keep on going even if one of these providers were to fail.

Details on the site are sketchy, but if Zend has got this right it ticks a lot of boxes for enterprise PHP developers.

image

What’s coming in Delphi RAD Studio XE2: more details of 64-bit and Mac announced, introducing FireMonkey

Embarcadero’s David Intersimone has posted more details of what is coming in the new version of Delphi and RAD Studio XE2, to tie in with an international publicity tour.

One intriguing comment is a reference to FireMonkey:

with FireMonkey, the GPU-powered next-generation application platform, you’ll be able to create visually stunning HD and 3D business applications

Here is the teaser list of features:

  • Create GPU-powered FireMonkey applications
  • Build 64-bit Delphi applications
  • Create a single application and target both Windows and OS X
  • Extend your multi-tier DataSnap applications with new mobile and cloud connectivity in RAD Cloud
  • Connect any visual element to any type of data using LiveBindings™
  • Modernize the look and feel of your Windows applications with VCL styles
  • Create mobile-optimized web applications and standalone apps for iOS and Android devices using RadPHP

Hmm, so RAD Studio XE2 is targeting iOS and Android not with Delphi, but with RadPHP. That suggests some sort of HTML and JavaScript approach rather than a true native executable.

I was not greatly impressed with Delphi for PHP when I first looked at it. That was four years ago though, and since then Embarcadero has acquired Qadram, the third-party developer behind Delphi for PHP, so I would expect something more worthwhile in the forthcoming new version.

Update: Embarcadero’s Andreano Lanusse has posted more details about FireMonkey.

Would you consider running PHP on Azure? Microsoft faces uphill battle to convince customers.

Yesterday Microsoft announced Windows Azure SDK for PHP version 3.0, an update to its open source SDK for PHP on Windows Azure. The SDK wraps Azure storage, diagnostics and management services with a PHP API.

Microsoft has been working for years on making IIS a good platform for PHP. FastCGI for IIS was introduced partly, I guess, with PHP in mind; and Microsoft runs a dedicated site for PHP on IIS. The Web Platform Installer installs a number of PHP applications including WordPress, Joomla and Drupal.

It is good to see Microsoft making an effort to support this important open source platform, and I am sure it has been welcomed by Microsoft-platform organisations who want to run WordPress, say, on their existing infrastructure.

Attracting PHP developers to Azure may be harder though. I asked Nick Hines, CTO for Innovation at Thoughtworks, a global IT consultancy and developer, what he thought of the idea.

I’d struggle to see any reason. Even if you had it in your datacentre, I certainly wouldn’t advise a client, unless there was some corporate mandate to the contrary, and especially if they wanted scale, to be running a Java or a PHP application on Windows.

Microsoft’s scaling and availability story around windows hasn’t had the penetration of the datacentre that Java and Linux has. If you look at some of the heavy users of all kinds of technology that we come across , such as some of the investment banks, what they’re tending to do is to build front and middle tier applications using C# and taking advantage of things like Silverlight to get the fancy front ends that they want, but the back end services and heavy lifting and number crunching predominantly is Java or some sort of Java variant running on Linux.

Hine also said that he had not realised running PHP on Azure was something Microsoft was promoting, and voiced his suspicion that PHP would be at a disadvantage to C# and .NET when it came to calling Azure APIs.

His remarks do not surprise me, and Microsoft will have to work hard to persuade a broad range of customers that Azure is as good a platform for PHP as Linux and Apache – even leaving aside the question of whether that is the case.

The new PHP SDK is on Codeplex and developed partly by a third-party, ReadDolmen, sponsored by Microsoft. While I understand why Microsoft is using a third-party, this kind of approach troubles me in that you have to ask, what will happen to the project if Microsoft stops sponsoring it? It is not an organic open source project driven by its users, and there are examples of similar exercises that have turned out to be more to do with PR than with real commitment.

I was trying to think of important open source projects from Microsoft and the best I could come up with is ASP.NET MVC. This is also made available on CodePlex, and is clearly a critical and popular project.

However the two are not really comparable. The SDK for PHP is licensed under the New BSD License; whereas ASP.NET MVC has the restrictive Microsoft Source License for ASP.NET Pre-Release Components (even though it is now RTM – Released to manufacturing). ASP.NET MVC 1.0 was licensed under the Microsoft Public License, but I do not know if this will eventually also be the case for ASP.NET MVC 3.0.

Further, ASP.NET MVC is developed by Microsoft itself, and has its own web site as part of the official ASP.NET site. Many users may not realise that the source is published.

My reasoning, then, is that if Microsoft really want to make PHP a first-class citizen on Azure, it should hire a crack PHP team and develop its own supporting libraries; as well as coming up with some solid evidence for its merits versus, say, Linux on Amazon EC2, that might persuade someone like Nick Hine that it is worth a look.

Adobe announces Flash Builder for PHP, PhoneGap integration in Dreamweaver

Adobe has stepped up its support for mobile and Flash development with a couple of announcements today.

The first is that Dreamweaver 5.5, part of the new Creative Suite 5.5, has integrated support for PhoneGap.

image

PhoneGap lets you build apps for Apple iOS and Google Android using HTML and JavaScript, taking advantage of the WebKit runtime that is present in these devices. The apps are packaged as native apps and also have access to some device-specific features. This does not mean Adobe is abandoning Flash, but is part of a both/and strategy, which makes sense to me.

There is also a new 4.5 version of Flash Builder which has greatly improved mobile support.

image

 

Flex 4.5 compiles to AIR apps on Android, Blackberry and iOS, as well as desktop Mac, Windows and Linux.

You can debug directly on an Android device connected via USB, or using a new emulator built into Flash Builder:

image

Adobe has also announced Flash Builder 4.5 for PHP, in partnership with Zend. A great feature is that you can debug seamlessly from PHP code on the server to Flex code running in a Flash client, provided you are using Zend server.

The new Flash Builder products will ship within 30 days. The premium edition is part of the Creative Suite 5.5 bundle – an improvement over Creative Suite 5.0 which only bundled the Standard edition – or available separately, while Flash Builder for PHP is a separate purchase at $399 or €319 for Standard, and $799 or €629 for Premium.

I asked Adobe’s Adam Lehman, Flash Builder produce manager, how developers should decide between PhoneGap and AIR for Mobile, given that both are now in Creative Suite.

They’re coming from two different technical perspectives. If you’re going to come in with your HTML skills and try to build an application that way, PhoneGap is better than trying to go and learn ActionScript and Flex from scratch. But from a performance and functionality perspective we believe we’re offering a lot better solution with Flash Builder and Flex.

The other part of it is that our tooling is superior. The sort of workflow that we showed [Design and develop with round-tripping between Flash Catalyst and Flash Builder] isn’t going to be available on the PhoneGap side with our tooling. Dreamweaver might be able to support different layouts and things like that, but it is not going to be a full-fledged IDE. What you’re getting with the AIR runtime and the full tooling stack is far superior that if you were building the HTML-based PhoneGap app. You can always tell a PhoneGap app, you can tell that it is running in an embedded browser. There’s tons of inconsistencies between the devices because the browsers are very different. There’s a lot of advantages to going AIR where you know that the design is going to look exactly the same. But while we love Flash we’re not zealous about it to ignore the fact that you can build with these other technologies as well.

More information on Flash Builder 4.5 here.

Adobe announces Flash Builder for PHP, PhoneGap integration in Dreamweaver

Adobe has stepped up its support for mobile and Flash development with a couple of announcements today.
The first is that Dreamweaver 5.5, part of the new Creative Suite 5.5, has integrated support for PhoneGap. PhoneGap lets you build apps for Apple iOS and Google Android using HTML and JavaScript, taking advantage of the WebKit runtime that is present in these devices. The apps are packaged as native apps and also have access to some device-specific features. This does not mean Adobe is abandoning Flash, but is part of a both/and strategy, which makes sense to me.
Adobe has also announced Flash Builder 4.5 for PHP, in partnership with Zend. A great feature is that you can debug seamlessly from PHP code on the server to Flex code running in a Flash client, provided you are using Zend server.
Flex 4.5 compiles to AIR apps on Android, Blackberry and iOS, as well as desktop Mac, Windows and Linux.
The new Flash Builder products will ship within 30 days. The premium edition is part of the Creative Suite bundle or available separately, while Flash Builder for PHP is a separate purchase at $399 or €319 for Standard, and $799 or €629 for Premium.
More news on this and screenshots soon.

JetBrains WebStorm 2.0 and PHPStorm 2.0 First Look

I respect JetBrains, an IDE company which survives despite intense competition from free tools such as Eclipse and NetBeans. It does so because developers like the products, especially the IntelliJ IDEA Java IDE. The tools are focused on coding; there are few visual designers but lots of coding help, such as code completion, refactoring, find usages, and fast navigation. The tools are also relatively lightweight, so start up quickly, and generally run on Windows, Mac OS X and Linux.

The latest from JetBrains is two related web development IDEs. WebStorm 2.0 is for general HTML/CSS/JavaScript work, and PHPStorm 2.0 is a superset of WebStorm which adds PHP editing and debugging. There is also some support for Adobe’s Flex and ActionScript code.

New in PHPStorm is PHP 5.3 support, Zend Debugger support, improved SQL editing, support for the Mercurial source code management system, and updates for HTML 5 and EcmaScript 5.

I am interested in these tools since HTML development is increasingly important. Browsers are getting increasingly powerful, with HTML 5 and fast JavaScript engines challenging plugins as rich application runtimes, and PHP is less well served by IDEs than you would expect considering its popularity.

My first impressions though are mixed. I noticed a lot to like, in particular the work JetBrains has done on supporting mixed languages: HTML, JavaScript, CSS, SQL,XML and so on. One of its features is that code completion works even within quotes. For example, if your PHP is outputting HTML to the browser, code completion still works. Most editors treat anything within quotes as plain text rather than as code.

image

The IDE is highly configurable and also supports plugins. Live templates let you expand abbreviations to code fragments. There is support for generating documentation with JSDoc, ASDoc and PHPDoc. Testing with PHPUnit is integrated. These are products that will appeal to developers who are code-oriented – are there any others? – and who like the ability to fine-tune their tools to improve productivity.

That said, I did not find it easy to get started with PHPStorm. The main challenge is configuring your PHP environment to support debugging. I have not yet succeeded, though I have not spent all that long on it. Difficulty getting PHP debugging working seems to be the biggest single topic on the support forums currently.

I know this is tricky and it took me a while to get it working with Eclipse. It is important though, and anything the tools vendors can do to make this easier to setup would be welcome. There is a case for simply installing a dedicated LAMP or WAMP server with the product so that developers get immediate and reliable support for PHP debugging, with the option to configure for their own PHP server later.

Another thing I found annoying was the over-zealous spell checker, which seems inclined to check variable names as well as being unaware of PHP extensions like mysqli:

image

I expect there is a way to fix both issues by configuring the product.

In general JetBrains does not seem to suffer from the “not invented here” syndrome and uses open source libraries and tools; in fact it depends on Firefox for JavaScript debugging, and automatically installs its own plugin to improve integration.

image

However I picked up a note of disapproval in the description on the Zen Coding site about the way JetBrains has chosen to implement Zen Coding abbreviations:

These plugins are developed by third-party and has their own ZC engine implementation, which leads to different feature set and abbreviation syntax. Zen Coding team has no relation to this projects

WebStorm is modestly priced at £54.00 for individual developers or £115 per concurrent user for organisations.  PHPStorm is £77 or £154 on the same basis. Free licenses are available for education or for open source projects, on application.

Competing products include the free Eclipse PDT and NetBeans, the commercial Zend Studio, and for a more designer-friendly approach to PHP, Adobe Dreamweaver CS5.

No more Ruby support in NetBeans – the feature was little used, says Oracle

Oracle has announced the discontinuation of Ruby support in the NetBeans IDE. The reason? First, to free resources for JDK 7 support; but second (and more significant) – hardly anyone was using it.

There is hardly a shortage of Ruby IDEs. Ones that come to mind are the Eclipse-based Aptana, JetBrains RubyMine, the Visual Studio based Ruby in Steel, and Embarcardero’s 3rd Rail. Further, some Ruby developers prefer to work without an IDE.

I also suspect that Ruby has not quite hit the mainstream in the way it seemed that it might a few years back. Its influence has been huge, but in practice many developers still fall back to PHP, Java and C#.

Accelerating PHP with the Alternative PHP Cache

I decided to install the open source Alternative PHP Cache on this server in order to improve performance. Interesting exercise. This server runs Debian Linux, and there are several ways to install APC:

1. Install the official package with apt-get install php-apc or similar

2. Install with the PHP Extension Community Library which goes something like:

apt-get install apache2
apt-get install libapache2-mod-php5
apt-get install php-pear
apt-get install php5-dev
apt-get install make
apt-get install apache2-prefork-dev
pecl install apc

The advantage over (1) is that you get the latest stable build, version 3.1.6, instead of the Debian package which is 3.0.19

3. Download the source and do something like this to install.

I started with option (2) though I came to regret it. The first problem is that the pecl installer will build with your currently-installed Apache, and if you later upgrade Apache it might break. Sticking with the official package is safer, even though it is very out of date.

I could live with the idea of re-installing APC every time Apache was updated if necessary, but I had another problem. I was up and running with APC 3.1.6 and pleased with the results, until after a while everything stopped working and my blog became a screen full of messages saying “Unable to allocate memory for pool”.

It looks like this bug, which was said to be fixed in version 3.1.5, but if you look to the end of the comments there is one from today with the same issue, and no suggestions about how to fix it.

The ancient version, on the other hand, has performed perfectly so far.

Another point of interest: I found it challenging to discover the best settings for APC. By default the install does no more than to enable the extension; but the default setting is unlikely to be the best one. The documentation tells you what each setting does, but not how to choose the best values for those settings. Should the cache be the default 32MB, or something much greater? Another thing to note: if you compile with MMAP support, which is the default, the value of apc.shm_segments is ignored, and the value in apc.shm_size will solely determine the size of the cache.

I found this Moodle article on installing APC in Windows helpful. What you do is first to find the file apc.php which the install put somewhere like /usr/share/doc/php-apc – in my case it was also compressed -  and put this on your website, preferably in a password-protected folder. This tells you the status of the cache. The aim is to have the cache just big enough that it does not become full and highly fragmented. Here is what I get after a short run with 128MB, which may be a little too much:

image

Another tip is to set apc.stat to 0. This means APC will not check for changes in PHP files since they were last compiled and cached. The downside is that every time you change a file you have to restart the web server; but the benefit is better performance, which is the goal after all.