How to start a basic web server on macOS or Linux
Table of contents
- Start by finding the right folder
- Start the basic Python web server
- Check your web content
- Stop the basic Python web server
If you are working on webpages or other web content, you may need a easy and quick way to start a basic web server. This guide will teach you a Python command to start a basic web server on your computer. As you follow these instructions, keep in mind that your computer’s files and folders likely will be different from the samples. If you already have a lot of experience with the terminal, check out the commands on the homepage for quick reference material.
In order follow this guide, you will need:
- Access to a Unix terminal on any macOS or Linux environment.
- To know how to open a terminal window. If you are not sure, visit the instructions for macOS or Linux (coming soon).
- To know how to navigate files and folders on a terminal. If you are not sure, follow this guide first to learn how.
- Python3 installed on your computer. This comes installed by default on recent versions of macOS and many Linux distributions.
Start by finding the right folder
First, you need to navigate to the folder where your web pages or web content is saved. If you are not sure how to do this, you may want to read this guide first.
Let us say that your web content is in the folder
/home/user/examples. We want to do three things:
- Make sure we are in the right working directory using
- Check if we have web content in the folder using
- Check if the web content is in the right format, such as HTML, using
Here is the combination of the steps:
In the above example, we use
pwd to verify that
/home/user/examples is our working directory. Then we use
ls to list the contents of the working directory to make sure we have an HTML file in there. You can note the hello.html file.
Finally, we view the contents of hello.html using
cat to make sure it is really HTML.
We are now ready to start our web server!
Start the basic Python web server
Since it is frequently installed on computers by default, we are going to use Python to run a basic web server.
We are going to use this command to start a web server:
python3 -m http.server 8000
Let us explore what this command does:
python3instructs the terminal to use Python 3 to do something.
-m http.serverinstructs Python to run a web server.
8000instructs Python to use 8000 as the port for the web server.
Let’s try running it:
If you observe the above, the web server is now running on your computer.
Check your web content
Next, we can check if our web content is showing in a web browser. We can open a local URL to browse the content:
In our example above, this is what we observe in a web browser:
In our example, we did not have a file called index.html so our web browser listed the available files. You can click on a specific file to open it in the browser. Let us click on the hello.html that we checked earlier:
We now observe the web page!
Stop the basic Python web server
When you are finished using the web server, you can stop it from the terminal. To do this, press
Ctrl-C on your keyboard. This means pressing the
C keys at the same time.
You will observe a result like this:
You have learned how to start a basic web server on your computer. This will help you with tasks like web development and Python programming. If you are interested in learning more or programming in Python, you can visit https://www.python.org.