Kostas Vlachos

Earth Observation

Personal Website

How to set up localhost Apache web server to run Python (in Laragon)

Share this:

What I’d like to show you in my very first blog post is how to run Python (2.x or 3.x) in a local Apache web server which runs under Laragon. Why bother using Python 2.x when Python 3.x is out, one might ask. Well, I wouldn’t have done this unless I knew that my web hosting provider supports Python 3.x. So out of my mistake I ended up learning interesting stuff about Python 2.x character encoding and how to use Python in a CGI (Common Gateway Interface) script .
Let’s start!

Install Python

First things first – what we should do is to install Python 2.x. interpreter in our Laragon installation. Thankfully, this is really easy for the newbies since Laragon itself makes it easy by giving relevant instructions. However, you can follow the instructions below and you’ll be good to go.

For Python 3.x:

  1. First go to the notification area in the taskbar and right click on Laragon icon > Tools > Quick Add > Python 3.x
  2. Close and re-open Laragon

For Python 2.x:

  1. Download zip file from here
  2. Unzip in C:\laragon\bin\python
  3. Close and re-open Laragon

Configure Apache web server

Follow steps below to -simply put- let Apache know that Python files can also be displayed by a web browser:

  1. Right click on Laragon icon > Apache > httpd.conf
  2. In the configuration file search for the AddHandler command
  3. If need be, uncomment it by removing the hash (#) symbol
  4. Add the .py extension at the end of the same line
  5. Search for the Options command
  6. Make sure the ExecCGI is included as an argument (or +ExecCGI assuming all of the arguments are denoted in the same way, i.e. with a + sign)
  7. Save and close the text file

Create and set up python file

What we need to do now is to create a python file and prepare it so it can be readable by your web browser. We achieve this by:

  1. Create a directory C:\laragon\www\python and a python file in it which you can name as you prefer. I will quickly name it as index.py
  2. Open the file, add the following lines and save it:
# -*- coding: utf-8 -*-

print "Content-Type: text/html"
print ''
print '<meta charset="utf-8">'

import cgi;
import cgitb;cgitb.enable()

print '<h1>Hello World!Γεια σου Κόσμε</h1>' 
  1. Now we should start the server by right clicking on Laragon icon > Apache > Start Apache
  2. To test whether the index.py can be “read” by your web browser or not, type localhost/python/index.py in your browser. What you should see is a “Hello World!Γεια σου Κόσμε!” header

That’s all for now. Thanks for reading and keep up with my next blog post!


[sourcecode language=”python” wraplines=”false” collapse=”false”]
# convert the line into a list of characters
# and feed the list to the ReadAhead generator
chars = ReadAhead(list(line))

c = chars.next() # get first

Share this:

Leave a Comment

Your email address will not be published. Required fields are marked *

fourteen + 11 =