Simple webserver
YOU ARE HERE :: Main Page >> HowTo >> Simple webserver
- Relevant to ALL editions of PCLinuxOS.
A simple web server in Linux refers to a lightweight, minimalistic server used to serve web content (like HTML files, images, and other resources) over HTTP. There are several ways to set up a simple web server in Linux without needing complex configurations. Here are a few common methods:
1. Using Python’s Built-In HTTP Server
Python has a built-in web server module that can be used to quickly serve files from a directory. It's one of the easiest and fastest ways to start a simple web server, especially for testing or development purposes.
- For Python 3: To start the web server, navigate to the directory you want to serve and run the following command from a terminal window:
python3 -m http.server 8000'
This command starts an HTTP server on port 8000, and the content in the current directory will be available at http://localhost:8000.
- For Python 2:
python -m SimpleHTTPServer 8000
This command starts an HTTP server on port 8000, and the content in the current directory will be available at http://localhost:8000.
You should see your index.html file. If you do not have a index.html file in the directory you will see a listing of all the files in the directory.
Simple Server does NOT replace Apache or other servers such as lighttpd. It doesn't support php, but it is a quick way to test out your html, without setting up a bigger resource hungry web server.
2. Using SimpleHTTPServer in Node.js
If you have Node.js installed, you can create a simple HTTP server with a few lines of code:
- Install the http-server package
npm install -g http-server
Then, in the directory you want to serve, run:
'http-server
By default, this will serve content on port 8080, and you can access it at http://localhost:8080.
3. Using PHP’s Built-In Server
If PHP is installed on your system, you can use it to serve files via a simple built-in web server:
- To start, navigate to the directory where your files are located and run
php -S localhost:8000
This command will start a simple web server that serves the content of the directory over http://localhost:8000
These methods are ideal for testing, small projects, or sharing files over a local network.
Summary:
For quick and simple setups, using Python’s built-in http.server or PHP’s -S option are great choices. They are easy to use and require no additional installation for most Linux systems. If you're looking for more persistent or production-grade servers, tools like Apache or Nginx can be used with minimal configuration as well.