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'] |
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.
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”.
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.
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!
Reblogged this on ITechonology.
You, sire, are a generous genius! Those command lines saved me a lot of trouble! Now happily previewing php stuff in VS 2013 community.
You can install this VS extension – PHP Tools for Visual Studio http://visualstudiogallery.msdn.microsoft.com/6eb51f05-ef01-4513-ac83-4c5f50c95fb5/ – which enables syntax highlighting, IntelliSense, debugging, etc etc for PHP. Also it automatically downloads and configures PHP from Web Platform Installer, and optionally setups site on IIS Express.
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.
IT’S NOT FREE.
Great article Sean! it got me set up in 10 minutes, Thanks!
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?
I’d take a look at the Microsoft Web Platform Installer. It does a great job at checking/installing any required dependencies, plus it provides “packaged” installs if you want to put WordPress/Umbraco/etc. on your computer down the road. The MySQL install should be pretty simple and quick through WPI: http://www.microsoft.com/web/downloads/platform.aspx
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.
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!
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
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.
thanks dillie it works..i need to install PHP and runphp helper.
thanks for your timely help
One more help..i need to host wordpress site in azure and configure active directory of my corporate. Is there any articles for this?
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.
you helped me thank you
Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
what the problem and thank you
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.
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 :-)
Hmm, I’m not quite sure about this. Seems a little odd. Are you putting your php_info statement in a web page or trying to run it from the command line? You might try reinstalling PHP using Web Platform Installer or you may need to register additional headers as outlined here:
http://forums.iis.net/t/1200092.aspx?PHP+doesn+t+work+phpinfo+does
Let me know if that helps.
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
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!
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.
Odd, I’ll have to look into that today. I don’t remember having that issue.
how to run php on visual studio 2015
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’]
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).
What version of IIS Express are you running? I wonder if something has changed.