3 WAYS TO START A LOCAL WEB SERVER IN LINUX
When a web developer develops a website, he needs to see how it will work on the end-user side. But a question comes in mind- "What is the need of a web server if we can see the HTML files in the web browser?" The answer is- what if we need to test the dynamic content, so we need to setup a local web server. This can be accomplished very easily on Windows, macOS and Linux. But today we will see 3 ways to setup a local web server in Linux.
NOTE::
- Steps being used are almost similar to use on other operating systems. Do some research on your own also.
- Following servers can also be opened on other machines in the same network.
FIRST WAY- Using apache2
Apache is open-source web-server software that powers much of the web today. It is maintained by apache-http-project.
Steps to follow::
Step 1: Open Terminal
Step 2: Check if it is installed or not (mostly, already installed). If not, use the following command- apt install apache2 to install it
Step 3: To start it, use the following command- service apache2 start
Step 4: To use the web server, enter your machine's IP address in the web browser
We can also use the following addresses to access it- localhost, 127.0.0.1 and 0.0.0.0.
Step 5: To stop it, use the following command- service apache2 stop
SECOND WAY- Using Python
Python 2 simple-http-server and Python 3 httpserver modules are also used to start a local web server but it is different from apache2 web server in a way that it starts a web server on the particular folder in which it is started, as given below, I launched this server in /root folder, that is why it opened the root folder.
NOTE::
1. You should also have Python 2 or Python 3 and pip already installed.
2. To install Python 2 use, apt install python
3. To install Python 3 use, apt install python
4. To install pip, use apt install python3-pip or apt install python-pip
1. You should also have Python 2 or Python 3 and pip already installed.
2. To install Python 2 use, apt install python
3. To install Python 3 use, apt install python
4. To install pip, use apt install python3-pip or apt install python-pip
Steps to follow::
Step 1: Open Terminal
Step 2: Check if it is installed or not (mostly, already installed even in Windows). If not, use the following commands- pip install simple-http-server (For Python 2) and pip install httpserver (For Python 3) to install it
Step 3: To start it, use the following commands-
- python3 -m http.server (in Python 3)
- python -m SimpleHTTPServer (in Python 2)
Step 4: To use the web server, enter your machine's IP address in web browser with port number 8000 as by default it uses port number 8000.
We can also use the following addresses to access it- localhost, 127.0.0.1 and 0.0.0.0.
Step 5: To stop it, press Ctrl+C
More Options::
- We can also specify port number with this server
- -d flag can also be used with the above command to specify a directory. By default, it opens root directory
- Command- python3 -m http.server port_number -d directory_name
- Above options example:: python3 -m http.server 800 -d Desktop/
THIRD WAY- Using Twist
Twisted is an event-based framework for internet applications, supporting Python 2.7 and Python 3.5+. It can also be used to make a local web server.
NOTE:: For this also, you should have Python 2 or Python 3 and pip already installed.
Steps to follow::
Step 1: Open Terminal
Step 2: Check if it is installed or not (mostly, already installed). If not, use the following command- pip install Twisted to install it
Step 3: To start it, use any of the following commands-
- twist web --listen tcp:port_number --path=directory_name
- twist web --port tcp:port_number --path=directory_name
- twistd -n web --listen tcp:port_number --path=directory_name
- twistd -n web --port tcp:port_number --path=directory_name
- Example:: twist web --listen tcp:8080 --path=Desktop/
- Just use twist web OR twistd -n web to start with default configuration
- In above commands, --listen and --port flag (or switch) are to specify the port number to be used
- --path flag is used to specify the directory to be opened. By default, it opens root directory
Step 4: To use the web server, enter your machine's IP address in the web browser with specified port number (default port is also 8080)
We can also use the following addresses to access it- localhost, 127.0.0.1 and 0.0.0.0.
Step 5: To stop it, press Ctrl+C
Conclusion
As you have seen till now, the usage of the above servers is straightforward to understand as shown in the examples which are covered in this tutorial. Try all of them by yourself. And with that, we come to the end of this tutorial. I hope that you can now use above web servers on your Linux system without any difficulty.
If I have not mentioned any other way you may know, please do share it.
ReplyDelete