I am on an exploration of new technology outside of my usual comfort zone. My first step is to get a python web development setup working on my Windows 8 Asus Zenbook. This article will show my first step, getting the current stable release of Apache and python installed, configured, and tested.
The first step is to get the latest stable version of the Windows installer for Apache Server. At this time of this writing that is version 2.2.24. If a later version is available, then I recommend you use that instead.
Apache can be found at http://httpd.apache.org/download.cgi. Specifically, the latest windows installer binary can be found at http://www.trieuvan.com/apache//httpd/binaries/win32/. You should read all the notes on this page. I downloaded file httpd-2.2.22-win32-x86-openssl-0.9.8t.msi listed on this page.
Next run the msi installer. I accepted all defaults except for the server information. Change the network domain and the server name to “localhost”. Also since I may run an IIS site on port 80, I opted to have Apache installed at port 8080. This option also changed startup to manual instead of installing it as a windows service. This gives you a shortcut to open Apache in a console and only when you need it running.
Once installation is complete, go to the start screen and right click on the “Start Apache in Console” tile and select “Run as Administrator”. Actually it will run without administrator, but it will not write to the logs in that case. I recommend running as administrator. You will see the console running but without any message in the console.
Now that it is running, open a browser and navigate to http://localhost:8080/. You should see a message.
This test message is the default as defined in the Apache htdocs folder. Feel free to change it.
C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\index.html
The first step is to get the latest stable windows installer for Python. I am running on 64 bit Windows 8, so I downloaded ‘Windows AMD64 / Intel 64 / X86-64 binary’ from http://python.org/download/. The latest version at this writing is Python 3.3.0.
Run the installer after download. I accepted the default options. This means installation directory of C:\Python33\. This is a version specific folder, so if you download a later version then your default folder will be different.
The Next step for features I left the defaults which included every feature except putting Python in the windows path environment variable. If I need this later, then I can add it manually.
After a minute the installation should be complete.
Now that both Apache and Python are installed, you need to configure Apache to enable Python support. Just open the httpd.conf file for Apache configuration. This is in the ‘conf’ folder under the installation folder. If you installed at the default location, then on a 64 bit OS it will be located at “C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\”
Find the line
Options Indexes FollowSymLinks
and add ‘ExecCGI’ at the end. I did not have any other options on that line, but if you do then you may want to keep them. My result was
Options Indexes FollowSymLinks ExecCGI
Find the line with
#AddHandler cgi-script .cgi
and uncomment the line. Add the .py extension as an option at the end of the line. If you have other options, you want to leave them in place also, but I ended up with
AddHandler cgi-script .cgi .py
When I saved my changes Notepad++ could not save over the existing file due to permissions. This is because user accounts do have access to write to files under the program files directory. I saved the file to my desktop and then copied it over the file in the correct directory. During copy I had to confirm the copy was authorized.
Now you can restart Apache. If you followed the same installation steps as me and only have the console, then end the process by pressing “CRTL-C” in the window. Then Apache will safely shut down itself and the console will close. You can check the Apache error.log to confirm that it was shut down properly. Startup and Shutdown steps are logged in the errors.log with level ‘notice’.
You are now ready to test Python and Apache. I credit the above steps to editrocket.com in the references below. However, the test.py in that article is out of date, so follow the below steps instead.
A simple test is to create a test.py file (name as you like) and then place it in the default documents folder for Apache. Again, for me on 64 bit Windows 8 that location is “C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\”
For Python 3.3 a good test file is as follows:
#!/Python33/python print("Content-type: text/html") print("") print("<html><head>") print("") print("</head><body>") print("Hello from Python.") print("</body></html>")
“#!/Python33/python” means find python at: “C:\Python33\python.exe” If you changed the install directory, then update that line accordingly.
Then open the url for that page and verify it works. http://localhost:8080/test.py
You should have a working page. Here is the resulting test log:
127.0.0.1 - - [29/Mar/2013:09:38:45 -0500] "GET /test.py HTTP/1.1" 200 67
You now have a working Apache/Python web development setup on Windows 8. If not then see some of the issues I ran across below.
Originally I followed editrocket’s directions and the print statements in my test python file did not have parenthesis around the method arguments. Error reporting was not working correctly, so all I saw was a generic error page, and there were no logs. So I went down other incorrect paths to resolve the problem.
#!/Python33/python print "Content-type: text/html" print print "<html><head>" print "" print "</head><body>" print "Hello from Python." print "</body></html>"
This resulted in an error. This is invalid starting since around Python 3.0 which made parenthesis around the print method arguments required.
Once I fixed the other issues in the following sections, here is the resulting Apache error log message.
[Fri Mar 29 09:21:11 2013] [error] 1 Premature end of script headers: test.py [Fri Mar 29 09:21:11 2013] [error] 1 File "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/test.py", line 2\r [Fri Mar 29 09:21:11 2013] [error] 1 print "Content-type: text/html"\r [Fri Mar 29 09:21:11 2013] [error] 1 ^\r [Fri Mar 29 09:21:11 2013] [error] 1 SyntaxError: invalid syntax\r
The first time I had an error, I checked the Apache logs folder and saw only an install.log. no errors or access logs were present.
C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs
I checked security of the logs folder. Since this is running under program files, it requires administrator grants to access any of the folders. Granting ‘Everyone’ full rights does not solve the problem. Instead you must run the Apache webserver console as Administrator. Then I started seeing the access logs getting hits. The logs show the apache index page returning successfully, and the test python page failing, as I see during testing. The 200 on the first line is the root index page success. The 500 on the second line for test.py is a server failure.
127.0.0.1 - - [29/Mar/2013:08:26:09 -0500] "GET / HTTP/1.1" 200 44 127.0.0.1 - - [29/Mar/2013:08:32:22 -0500] "GET /test.py HTTP/1.1" 500 542
Before fixing the security issue, the error.log did not show my Python test page error. The only error was that the favicon.ico file was missing. If you followed all the directions above during setup then you wouldn’t have this problem. This is because I had done something else to try and solve the problem first…
Since I was having some issues and had no logs, after I setup, I had followed another step in the Python 3.3 documentation that said:
Configuring Apache is much simpler. In the Apache configuration file httpd.conf, add the following line at the end of the file:
Don’t do that. After doing that anytime I hit the test.py test page A python console window flashed on the screen with no contents, and disappeared just as quick. It was so fast, that it took a number of tries to catch this screenshot.
The Apache on Windows documentation says:
Any errors during Apache startup are logged into the Windows event log when running on Windows NT. This mechanism acts as a backup for those situations where Apache is not yet prepared to use the error.log file
I did not see any logs in the Windows event viewer related to Apache. This was the case also during the ScriptInterpreterResource error above. I’m not sure if Apache ever writes errors to the windows event log? I’m not sure if the problem was because none of the errors were in the Apache pipeline or because something is misconfigured. If anyone finds how to enable writing to the Event viewer, then please let me know in the comments.
If you have any errors not listed in this article, then check the windows event log. Maybe you’ll have some luck in there.
Apache Windows Install Binaries
Using Apache HTTP Server on Microsoft Windows
Apache Configuration for Python 2.x on Windows
example test.py file is out of date, otherwise it is still accurate