Recently I was helping out a family member who is doing a degree in software engineering to get a working web server that could run Python CGI scripts. I’ve never really done this myself so I needed to work it out. What I quickly discovered is that there is a lot of ways to do this as usual, but none that were particularly simple for a beginner. What I did realise was that I would likely be best with a basic Apache server with a few modifications. In the end, for the simplicity I opted for MAMP since it’s an easy web server to set up with some basic UI.
Note, there are ways to just install Apache locally, but I like the simplicity of MAMP for beginners.
MAMP and Python
To configure MAMP to run .py
files (Python scripts), you’ll need to follow a few steps to set up the necessary environment for CGI (Common Gateway Interface) scripts. MAMP, by default, is configured for PHP, but you can modify it to handle Python CGI scripts as well.
This tutorial is specifically aimed at setting up sub-project folders in MAMP’s web content root folder with Python scripts residing in a /cgi-bin
subfolder.
For anyone coming to this later, this was setup on MacOS Sequioa 15.1, with python3
and MAMP v7.1 (not Pro). The steps are slightly different for MAMP Pro since it includes features for enabling Python support directly.
Here’s how to set it up:
1. Create a /cgi-bin
Directory
Inside your MAMP document root (default should be Applications/MAMP/htdocs
), create a new directory for your project and a subfolder called cgi-bin
. This is where your Python scripts will reside. So you should have htdocs/my-project
and htdocs/my-project/cgi-bin
2. Configure Apache to Allow CGI Scripts
- Open the MAMP application.
- Edit the Apache configuration file:
- You will find the Apache configuration file at
MAMP/conf/apache/httpd.conf
. - Search for
<Directory "/Applications/MAMP/cgi-bin">
and make sure that theOptions
attribute is set to+ExecCGI
and notNone
. - Add the following lines (change the project folder name to yours) below that
<Directory
block to enable CGI for thecgi-bin
folder:
- You will find the Apache configuration file at
<Directory "/Applications/MAMP/htdocs/your_project_folder/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .py
DirectoryIndex index.py
</Directory>
- Enable CGI Module:
- In the
httpd.conf
file, make sure the CGI module is loaded. Look for the following line and ensure it’s uncommented (remove the#
if present):
- In the
LoadModule cgid_module modules/mod_cgid.so
3. Configure Python
- MAMP doesn’t include Python by default, so you’ll need to ensure Python is installed on your computer and accessible. MAMP typically uses PHP and MySQL, but Python can be used with a custom setup.
- Ensure Python is Installed: Open a terminal and type:
python3 --version
If it’s not installed, you can install Python through python.org or use a package manager like Homebrew on macOS.
4. Set File Permissions
- Make sure your
.py
files are executable. In your terminal, navigate to thecgi-bin
folder in your project and set the correct permissions:
chmod +x your_script.py
5. Write Your Python Script
- In your
cgi-bin
folder, create a Python script, for example,hello.py
:
#!/usr/bin/env python3
print("Content-type: text/html\n")
print("<html><body><h1>Hello, World!</h1></body></html>")
Ensure that the script has the correct shebang (#!/usr/bin/env python3
) at the top, which tells the system to use Python to execute the script. To do this, in your terminal, type the following:
which python3
If this returns /usr/bin/python3
you should be fine with the line suggested earlier #!/usr/bin/env python3
6. Restart MAMP
- After modifying the Apache configuration, restart the MAMP server to apply the changes.
7. Access the Script in Your Browser
- Now you can access your Python script in a browser by visiting http://localhost:8888/your_project/cgi-bin/hello.py (again, ensuring you update the path to your actual project folder name)
- If everything is set up correctly, you should see the output of your Python script.
Conclusion
Congratulations! You should now have a working MAMP web server that can run Python scripts. If you’d like to add more projects, create a new folder in the MAMP web root and update the Apache config to allow Python in that folder too.
There are many other ways you could do this, but sometimes you just need something functional to get started. One thing I’ve learned over the years is that the hardest thing about any project is getting started. Don’t obsess over the perfect environment while you’re learning, sometimes you just need a quick solution so you can try things out.
As you learn more, and progress in your experience, you’ll find plenty of other ways to set things up! For now, I wish you luck in your coding adventures.