1. Introduction

As web developers on a Linux system, we often have a bunch of files in a folder and want to serve them so they’re accessible in a browser. This is one of the key parts of a local web development environment. In this tutorial, we’ll look at different ways to start a web server in the current working directory of our Linux system.

2. Preparation

To start with, we’ll create a file named index.html in our folder with the following contents:

<!doctype html>
<html>
  <head>
    <title>HELLO WORLD</title>
  </head>
  <body>
    Hello World
  </body>
</html>

In addition, for demonstration, we’ll start our server on port 1102 in all the following examples. We must also note that any program starting a server on a port number less than 1024 (such as 80 or 443) requires root privileges. In addition, we’ll start the server in the folder $HOME/foo for demonstration.

3. Using BusyBox

BusyBox is a set of lightweight Linux utilities packaged as a single binary. Most Linux distros have it installed by default. In case it’s not already installed, we can use apt to install it on Ubuntu:

$ sudo apt install busybox-static

Once we have BusyBox installed on our system, we can start a web server by running its httpd utility:

$ busybox httpd -f -vv -p 1102 -h $HOME/foo

In the above command, we used the -f  argument to keep the server process in the foreground (the default is daemonizing). Further, the -vv argument instructs the command to print verbose logs, and we’ve specified the port number using the -p argument and the folder using the -h argument. After the server starts, we can visit http://localhost:1102 and see that the browser says “Hello World“.

In case we don’t have an index.html file in the folder, the above URL shows us a 404 error and we’d need to specify the full URL including the filename to access a file.

4. Using Python

Most Linux distributions come with Python installed by default. We can use python3 to quickly spin up a web server:

$ python3 -m http.server 1102 -d $HOME/foo

This command calls the http.server module to start a web server on port 1102, in the directory $HOME/foo specified using the -d argument. Now, we can visit http://localhost:1102 to see that “Hello World” is displayed. If we don’t have an index.html file in our folder, this module shows a list of files and folders in the folder by default.

We can also use the python command instead of python3 provided we have set the default Python to python3. If we’re using python2, the command to launch a web server is a little different:

$ cd $HOME/foo
$ python2 -m SimpleHTTPServer 1102

Here, we must note that we cannot specify the folder while starting the server, and hence we must change to the folder before we start the server.

5. Using PHP

If we’ve PHP installed on our system, we can use it to start a web server with a very simple command:

$ php -S 0.0.0.0:1102 -t $HOME/foo

We specified the IP address and the port number to start the server on, followed by the directory to serve using the -t argument. Now we can visit the address http://localhost:1102 to see that the browser shows “Hello World“.

6. Using Ruby

By using Ruby, we can execute httpd to start a web server:

$ ruby -run -e httpd $HOME/foo -p1102

In the above command, the -run argument followed by the -e argument executes httpd to start a web server. We then specified the folder path, followed by the -p argument to specify the port number. Depending on the Ruby version we have installed, this might require an additional gem installation:

$ sudo gem install webrick

Once we’ve managed to start the server, we can visit http://localhost:1102 and verify that the page says “Hello World“. If we don’t have an index.html in our folder, we’ll see a very detailed index of the folder.

7. Using a Node.js Module

If we have Node.js and npm installed on our system, we can install the http-server module and use it to start a web server.

Let’s start by installing the module:

$ sudo npm i http-server -g

Then, to run the server:

$ http-server $HOME/foo -p 1102

Once we run this, the server starts in the specified folder $HOME/foo. Now, we can see that the browser says “Hello World” when we visit http://localhost:1102. Presumably, we’ve used the -p argument to specify the port number. Like the Ruby method above, this module also shows a detailed index of the folder when index.html isn’t present.

8. Conclusion

In this article, we looked at different ways to start a web server in a folder. Of those, BusyBox and Python come pre-installed on most Linux distros, so no additional installation step is required. We also learned that Python is more comprehensive in showing a directory list index when no index.html is present.

We also looked at accomplishing the task with PHP, Ruby, Node.js. The PHP method is very simple, but shows no detailed directory listing. Meanwhile, the Ruby and Node methods need module installation in addition to the language packages being installed, but they show a very detailed directory listing.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.