Lesson 4 of 6
Run a local web server
Start a local web server that serves the site from a folder on your machine.
10 minDevOps
After this lesson, you will be able to run a local web server that serves your static site from a folder on your machine, and you will understand the difference between serving files and opening them directly.
A web server for static files
So far, you have built a small site and packaged its files into an archive. To see the site as a visitor would, you need a web server. A web server is a program that listens for requests from a browser and sends back files. For a static site, the server just reads files from a folder and returns them.
You can open an HTML file directly in a browser, but that is not the same as serving it. When you open a file directly, the browser shows the file from your disk. A web server gives the site a proper address, such as http://localhost:8080, and handles requests the way a real deployment would.
Python includes a simple web server in its standard library. You do not need to install anything extra. The command to start it is:
python3 -m http.server 8080This tells Python to run the http.server module and listen on port 8080. A port is a number that identifies a service on your machine. The server will serve files from the folder where you run the command.
Start the server
Make sure you are in the my-site folder. Run the command:
python3 -m http.server 8080The server starts and prints a message like Serving HTTP on 0.0.0.0 port 8080. It keeps running and does not return you to the shell prompt. Leave this terminal window open.
Open a web browser and go to http://localhost:8080. You should see your site's home page with the heading "Welcome to My Site".
The server serves files from the current folder. If you go to http://localhost:8080/about.html, you will see the about page if you created it in the lesson 1 exercise. If you did not, the server will return a "404 Not Found" error.
To stop the server, go back to the terminal where it is running and press Ctrl+C. That stops the server and returns you to the shell prompt.
Serve the packaged site
The server serves whatever is in the folder. If you have run ./package.sh, there is a site.tar.gz file in the folder. The server will happily serve that file too, but that is not what you want. A visitor should see the HTML pages, not the archive.
You can create a separate folder for the files you want to serve. This keeps the archive and other files out of the web root. The web root is the folder the server uses as the top level of the site.
Create a folder called public and copy your HTML files into it:
mkdir public
cp *.html public/Add public/ to .gitignore so Git does not track the copied site files. Open .gitignore and make sure it contains these lines:
.DS_Store
*.log
site.tar.gz
public/Now start the server from the public folder:
cd public
python3 -m http.server 8080Open http://localhost:8080 again. You should see the same home page. The server is now serving from public, so it will not show site.tar.gz or your scripts.
Stop the server with Ctrl+C and go back to the my-site folder:
cd ..A script to start the server
You will use this server often, so it is helpful to put the command in a script. Create a file called serve.sh:
touch serve.shOpen serve.sh and add these lines:
#!/bin/bash
set -e
cd public
python3 -m http.server 8080This script changes to the public folder and starts the server. The set -e line makes the script stop if any command fails.
Make the script executable:
chmod +x serve.shNow you can start the server with one command:
./serve.shTest it by opening http://localhost:8080 in your browser. Then stop the server with Ctrl+C.
The script assumes the public folder exists. If you run it before creating that folder, it will fail. In a later lesson, you will write a deploy script that sets up the folder automatically.
Commit the server script and ignore the public folder
Add serve.sh and the updated .gitignore to Git and commit them:
git add .gitignore serve.shgit commit -m "Add server script and ignore public"The post-commit hook runs test.sh, which should pass. The server script is not part of the site content, but it is part of your project, so it belongs in the repository. The public folder stays ignored because it is a generated copy.
You now have a way to serve your site locally. In the next lesson, you will write a script that packages the site and deploys it to a server folder automatically.
Key takeaways
- A web server sends files to a browser over a network, using a port number.
- Python's
http.servermodule serves static files from the folder where it is run. - Keep the web root separate from your project files so you only serve what visitors should see.
- A script makes it easy to start the server with one command.
- Add generated output folders like
public/to.gitignoreso Git status stays clean.
Try it
Create a new page called gallery.html in the my-site folder with a heading that says "Gallery" and a short paragraph. Copy it into the public folder using cp gallery.html public/. Start the server with ./serve.sh and open http://localhost:8080/gallery.html in your browser. You should see the new page. Stop the server with Ctrl+C. Commit gallery.html with a suitable message. The post-commit hook should pass, and git status should show a clean working directory because public/ is ignored.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.