The purpose of this tutorial is to host a simple web server using only Python on a Linux system. This produces a very lightweight web server and works well to host a microservice, a browser-based file manager, or to test a website without deploying a full web server such as Apache or NGINX.
Follow along with the steps below to learn how to use Python to set up a simple web server on any Linux distribution. We will be using Ubuntu Linux in our example, and the version of Ubuntu will not matter as long as you are using Python 2 or Python 3 (recommended).
In this tutorial you will learn:
- How to install Python 3 on major Linux distros
- How to host a web server using Python 3 or Python 2
- How to access the Python hosted web server in browser
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux system|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to install Python 3 on major Linux distros
The only prerequisite to follow this guide is that you have Python 3 installed on your system. Python 2 will also work, but will require a slightly different command. We will cover both below. But first, use the instructions below to install Python on your Linux system.
You can use the appropriate command below to install Python 3 with your system’s package manager.
To install Python 3 on Ubuntu, Debian, and Linux Mint:
$ sudo apt install python3
To install Python 3 on Fedora, CentOS, AlmaLinux, and Red Hat:
$ sudo dnf install python3
To install Python 3 on Arch Linux and Manjaro:
$ sudo pacman -S python3
Configure simple web server in Linux step by step instructions
To start a web server using Python 3, use the following command. With this syntax, our web server will be running on IP address
127.0.0.1 and port
9000. You can change this to anything you want, or omit the options entirely to have Python be hosted on the default IP and port.
$ python3 -m http.server --bind 127.0.0.1 9000
The Python 2 equivalent command would be:
$ python -m SimpleHTTPServer 9000
You should see confirmation in your terminal window that Python is now serving an HTTP server on the IP and port you configured.
Now you can open up a browser and navigate to the address you configured, which is
http://127.0.0.1:9000 in our case.
As you can see, by default the website just displays our home directory and all of the files inside of it. We can now use this as a file browser. This might prove useful if you want to download some of these files from another system on your local area network.
Of course, you can also move a Python file into this directory and access it through the web browser.
In this tutorial, we saw how to host a simple web server using Python 3 or Python 2 on a Linux system. This set of instructions will work for Ubuntu or any other Linux distribution where Python is installed. Python web servers are not as robust as Apache or NGINX but they are good for simple purposes or for other situations described here.