A simple Web server example implemented by Shell script _ linuxshell-PHP source code

Source: Internet
Author: User
This article mainly introduces an example of a simple Web server implemented by Shell scripts. the Web server implemented in this article is very simple and practical and can be used when you do not want to install nginx, apache, and other large WEB servers, if you need a simple Web server, refer to the next article, which mainly introduces an example of a simple Web server implemented by Shell script. the Web server implemented in this article is very simple and practical, it can be used when you do not want to install nginx, apache, or other large WEB servers. For more information, see

Suppose you want to test the web page and some CGI, and you do not want to bother Apache to install the complete package. This quick shell script may be just what you need.

In short, a web server is an application that sends requests from local text files over the network. If you want another program (such as inetd) to process the network, the web server can be reduced to only cat "file name" and sent to stdout. Of course, it is difficult to extract the HTTP request string of some file names: any Bash script cannot do it easily.

Script

We should add some definitions like any other script:

#! /Bin/bash
Base =/var/www


Inetd transmits the data received from the remote host to our script. The first line is a standard HTTP request followed by zero or more header files. We record the request and exit sleep:

Read request

While/bin/true; do
Read header
["$ Header" = $ '\ r'] & break;
Done

The most troublesome part: extract the URL from the request data and find the corresponding file in the local file:

Url = "$ {request # GET }"
Url = "$ {url % HTTP /*}"
Filename = "$ base $ url"

Returns the content of a file containing the header information.

If [-f "$ filename"]; then
Echo-e "HTTP/1.1 200 OK \ r"
Echo-e "Content-Type: '/usr/bin/file-bi \" $ filename \ "' \ r"
Echo-e "\ r"
Cat "$ filename"
Echo-e "\ r"
Else
Echo-e "HTTP/1.1 404 Not Found \ r"
Echo-e "Content-Type: text/html \ r"
Echo-e "\ r"
Echo-e "404 Not Found \ r"
Echo-e "Not Found
The requested resource was not found \ r"
Echo-e "\ r"
Fi

All right, the script is complete.

Install

To make it work, you must add the following lines to the/etc/inetd. conf file:

Www stream tcp nowait nobody/usr/local/bin/webd

Webd is the name of the script you just created.

Use/etc/init. d/inetd restart to make the script take effect, you can test it. In/var/www delegate some HTML files, open your favorite Web browser by entering the following address for testing: https://www.php1.cn/

Please note that if your computer is connected to this insecure network, this script may not be smart, because anyone can access files on your hard disk through port 80. A better solution is to use tcpd to ensure that only local connections are allowed. I will write some relevant information and use your imagination to do it!

What is CGI?

Like this, a WEB server is useless and cannot do anything, and you can use other methods to access your files. We need CGI support (even simple ).

Theory: without sending a text file over the network, we run an executable file and output it. Before that, we have created a QUERY_STRING variable for the HTTP request to be processed and output to the executable file.

To do this, you only need to replace the code in step 3 with this.

Url = "$ {request # GET }"
Url = "$ {url % HTTP /*}"
Query = "$ {url #*\?} "
Url = "$ {url % \? *}"

Filename = "$ base $ url"

If ["$ query "! = "$ Url"-a-x "$ filename"]; then
Export QUERY_STRING = "$ query"
Echo-e "HTTP/1.1 200 OK \ r"
"$ Filename"
Echo-e "\ r"
Exit 0
Fi

Of course, the performance of this gadget cannot be compared with that of Apache. this is just a gadget.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.