How to run PHP applications in Visual Studio 2012

A PHP app launched from Visual Studio 2012
A PHP app launched from Visual Studio 2012

Note: This post comes as a HT to this StackOverflow question, I’m just tweaking a few things to accomodate for VSMMXII…

I’m working on a project to convert a PHP application to a .Net MVC application. The initial prototype has some nice bootstrap/highcharts functionality, so I want to preview the site locally as I’m migrating the code over. Instead of starting up or installing a new editor, I wanted to take advantage of IIS Express that comes with Visual Studio 2012 to run the app. It’s really easy to setup and here’s how to do it.

1. Download PHP

You can get a windows optimized version from http://windows.php.net. Following the documentation, make sure to get the Non-Thread Safe version since we’re working with IIS. After downloading the file, extract it to your “C:\Program Files (x86)” folder or wherever you find convenient.

2. Map FastCGI to your PHP location

With previous versions of Visual Studio / IIS Express, you needed to update your configuration files, but with Visual Studio 2012, all of this has been taken care of for you! The only thing you need to do is let FastCGI know where your PHP executables are by running the following commands from the command prompt (not PowerShell):

"C:\Program Files (x86)\IIS Express\appcmd.exe" set config /section:system.webServer/fastCGI /+[fullPath='"C:\Program Files (x86)\PHP\php-cgi.exe"']
"C:\Program Files (x86)\IIS Express\appcmd.exe" set config /section:system.webServer/handlers /+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='"C:\Program Files (x86)\PHP\php-cgi.exe"',resourceType='Unspecified']
view raw gistfile1.txt hosted with ❤ by GitHub

Note that the quotes are specific to the command prompt. You may need to update your path to PHP depending on where you extracted it to.

3. Create an Empty Visual Studio Web Application

Yes, this step sounds confusing, even if you already have your PHP site code. Visual Studio won’t let you create a basic web application solution in an existing folder, so I create an empty Web Application project file.

Visual Studio Empty Web Application Project

4. Add your PHP Files.

Once the project is created, copy all of your files into the project folder. Then, make sure your project is set to view all files, and click the refresh icon. You’ll see all of your PHP files and you can right click on them and specify “Include in Project”.

Visual Studio Add PHP Files

5. Configure Web Application Startup Parameters

The final step is to configure the startup parameters for your site. Right click on your project name in the Solutions Explorer, select “Properties”, and then select the “Web” tab on the left hand side in the properties window.  Change the “Start Action” option to be “Specific Page” and enter “index.php” or whatever your starting page needs to be. In addition, make sure that the “Servers” option is set to use the “Local IIS Web Server” and that IIS Express is selected. Make sure to save your options before closing the window.

Visual Studio Project Settings

6. Build and Run!

Technically there’s nothing to build, but hit CTRL+F5 to start the project without debugging. This will launch IIS Express and start up your index file! You’re all set. If you want you can use Visual Studio to edit your PHP code as needed, but without additional plugins, you won’t have all the extra intellisense helpfulness.

Enjoy!

28 thoughts on “How to run PHP applications in Visual Studio 2012

    1. Thanks for the link Jakub! I was doing a quick and simple PHP run when I wrote this article, but I think I’ll have to give that plugin an install for more detailed inspection.

  1. Hay thank for the right up. I hate to add junk files to my computer so this is great to have. “clean” Now this is my first clean run of using php on this computer. Not doing a production of site yet so i am configuring the MySQL to run right on my computer here do you have any pointers?

      1. Thanks i do use web matrix3 and have used WPI a few times. Just got done with my server company . That is UNIX Try to enplane to them I use VS and will not use Dreamweaver. so i am doing php and i want to run the mysql with in VS so that I have all fills on my devp. computer. they was like you can do that VS is for Microsoft . I like ya i just run the server for mysql on my machine here use the built in ftp to up load never touching the db on their server. I really hate php today but all the work has been done and php for the last 2 years and now i have to fix what the other guys missed . thanks for the help.

        1. If you need to tweak with your MySQL queries at all, I highly recommend TOAD for MySQL as a tool to connect/query/etc. They have a freeware version available at http://www.quest.com/toad-for-mysql If you’re looking for something Visual Studio specific, I’m sure there are some plugins out there you can install. Best of luck!

  2. Hi,
    thanks for the post. I am working in windows 7 environment and trying to run wordpress site in my local. I ave downloaded the wordpress 3.8.1 source and need to work with visual studio 2012. I have installed the necessary components for the same and while running the site i am getting the error “Your PHP installation appears to be missing the MySQL extension which is required by WordPress”.

    I have tried all the possible solutions suggested in forums but nothing works.

    can anyone please help.

    I want to execute the wordpress site in my local using Visual Studio 2012.

    your timely help will be very much appreciated.

    Thanks,
    Pradeep

    1. Greetings Pradeep. Have you tried using Web Platform Installer to do your WordPress installation? If not, give that a try before opening things in Visual Studio. It will detect all the necessary dependencies and install them.

  3. One more help..i need to host wordpress site in azure and configure active directory of my corporate. Is there any articles for this?

    1. There are definitely articles on Azure about installing/configuring WordPress and pushing to Azure, and I’m guessing there are some about Active Directory. Try digging around the Azure site a bit, I’m sure some documentation/articles will show up. I don’t have any off the top of my head.

    1. Greetings Dakhamat. If you need MySQL, I’d recommend using Microsoft’s Web Platform Installer (see link in other reply) for the installation. It does a great job and making sure all dependencies are in place and gives you options on other things to install, if needed.

  4. Hi. Thanks for that info. Installed and runs, BUT it won’t run basic php commands, like ‘print_r()’ and ‘phpinfo()’, etc….in fact it runs very little. I can run a n echo command, so i know the php is basically installed, but it’s not running the php command set when testing in IIS Express – anything i need to enable? – i’ve tried everything. I have PHP Tools and Phalanger installed too. Any input/suggestions on this would be very welcome! Thanks, Lee :-)

      1. Hi
        I am running from a .php file as a web page. It won’t run any normal php commands. I have reinstalled PHP using the web platform installer.
        I suspect it’s the IIS not running the PHP correctly? I am running on Windows 7 btw.
        I will go through the setup in the forum article, although it pretty much outlines what i have already done. The only difference is my PHP folder is under Program Files (x86), and some people say it won’t work from there? I will try it in c:\PHP and reconfigure and see if that help! Will report back.
        Thanks

        1. That does make sense why it wouldn’t run from the x86 folder since sometimes there are issues resolving the path that has a space in it (yep, even in the new digital age that seems to cause problems. 8^D) Let me know if this works and I can update the article accordingly. Thanks!

  5. ok – runs from c:\php now :-)
    But funny thing is….only from localhost.
    The minute i try to run from another port ie: using Visual Studio to test the script like so;
    http://localhost:15853/index.php
    then it doesn’t work. If i manually copy the php file across to a test directory under localhost (inetpub/wwwroot/test): ie http://localhost/test/index.php…….then it works.
    Perhaps there’s a setting somewhere that enables usage under port mapping for visual studio? haven’t found anything.

  6. Awesome, you saved my day. Thank you so much. There’s tons of instructions out there but just yours work and simple a lot.
    P/S: The second command should be: ….resourceType=’Unspecified’]

  7. The first command worked but for the 2nd Command I got this error :

    ERROR ( message:Malformed collection indexer; format is [@position,name=’value’,name2=’value2′,…]. The @position specifier is optional, and be ‘@start’, ‘@end’, or ‘@N’ where N is a numeric index into the collection. )

    I have done exactly as you said. I have extrated it to a folder called PHP in C:\Program Files (x86).

Leave a reply to Pradeep Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.