Running Apache on Windows 8 with Python

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.

Apache Setup

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.

Apache-Install-01-ServerInformation

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.

Apache-Test-01-StartConsole

Now that it is running, open a browser and navigate to http://localhost:8080/. You should see a message.

Apache-Test-02-TestPage

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



Python Installation

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.

Python-Install-01-Directory

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.

Python-Install-02-Features

After a minute the installation should be complete.

Python-Install-03-Done

Python Configuration in Apache

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.

Testing Python in Apache

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.

What can go wrong?

No parenthesis for print method

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.

Python-Test-01-Failure

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

No log files due to permissions

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…

ScriptInterpreterSource Registry

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:

ScriptInterpreterSource Registry

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.

Python-Test-02-Failure

No logs in Windows Event Viewer

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.

References

Apache Windows Install Binaries
http://www.trieuvan.com/apache//httpd/binaries/win32/

Python downloads
http://python.org/download/

Using Apache HTTP Server on Microsoft Windows
http://httpd.apache.org/docs/2.2/platform/windows.html

Apache Configuration for Python 2.x on Windows
http://www.editrocket.com/articles/apache_windows.html
example test.py file is out of date, otherwise it is still accurate

About the Author

Michael Lang

Co-Founder and CTO of Watchdog Creative, business development, technology vision, and more since 2013, Developer, and Mentor since 1999. See the about page for more details.

11 Comments

  • Avatar By Rick

    Thanks for the tutorial. What is meant to be the output of the test.py file. When I navigate to it, I simply get a web browser with the text that was contained in the test.py displayed. Is this wrong?

    • mlang74 By mlang74

      The output should be the executed result of your python file.

      After opening “start apache in console” running as administrator, then open a browser and navigate to http://127.0.0.1:8080/test.py and then you should see “hello from python”. At least that is the case if you used the test.py file contents above; the file was a series of print statements so it isn’t a literal output of everything in the file.

      • Avatar By rick

        Thanks! I think my configuration is off a bit then. I will have a play with wamp’s setting. If that doesn’t work, time to boot up my fedora machine!

    • Avatar By sann

      hi rick can you please tell me how you correct your problem because i have the same problem . my output on webpage is same statement written in python

  • Avatar By NaoNack

    #!/Python33/python
    print (“Content-Type: text/htmln”)
    print (“”)
    print (“”)
    print (“”)
    print (“Hello.”)
    print (“”)

    ligne 2 :

    il faut écrire Content-Type au lieu de Content-type

    • mlang74 By mlang74

      It may very well be more correct as “Content-Type”, but it still works as “Content-type” as done in this article. Thanks for your feedback.

  • Avatar By dvgb

    I just installed apache 2.2 on windows 7 with python 2.7 I found that when I added the line
    ScriptInterpreterSource Registry
    my python scripts executed without the #!/Python27/python at top of .py file.
    I also added
    PassEnv PYTHONPATH
    SetEnv PYTHONUNBUFFERED 1

    # got these lines from http://www.imladris.com/Scripts/PythonForWindows.html

    • mlang74 By mlang74

      Thanks for the update. Although I’m not sure why you choose an older version of python, 2.7. Did you have an issue installing version 3.3? Or maybe some code you were using only worked on python 2.7?

  • Avatar By hurd

    tanks for this nice tutorial
    my error in this section #!/Python27/python i test in terminal test script best result in this
    #C:Python27python and in web server nice result

  • Avatar By sladej10

    Didn’t work. As others mentioned, browser shows only the python code, not the execution result.

  • Avatar By sladej10

    So the tutorial is fine (thank you), but is missing an imporant instruction. After editing the httpd.conf file for Apache, you have to restart the Apache service to reload the config. Go to {APACHE_HOME}/bin in your cmd (or add it to PATH). Then execute httpd -k restart.