Mini httpd with Haserl and Lua: Difference between revisions

From Alpine Linux
(Created page with "{{DISPLAYTITLE:mini_httpd with Haserl and Lua}} [http://acme.com/software/mini_httpd/ mini_httpd] is a small HTTP server. Its performance is not great, but for low or medium t...")
 
(use cat template & remove sudo dependency)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:mini_httpd with Haserl and Lua}}
{{DISPLAYTITLE:mini_httpd with Haserl and Lua}}
[http://acme.com/software/mini_httpd/ mini_httpd] is a small HTTP server. Its performance is not great, but for low or medium traffic sites it's quite adequate. It implements all the basic features of an HTTP server.
[https://acme.com/software/mini_httpd/ mini_httpd] is a small HTTP server. Its performance is not great, but for low or medium traffic sites it's quite adequate. It implements all the basic features of an HTTP server.


[http://haserl.sourceforge.net/ Haserl] is a small program that uses shell or Lua script to create cgi web scripts. It is intended for environments where PHP or ruby are too big.
[https://haserl.sourceforge.net/ Haserl] is a small program that uses shell or Lua script to create cgi web scripts. It is intended for environments where PHP or ruby are too big.


[https://www.lua.org Lua] is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.
[https://www.lua.org Lua] is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.
Line 14: Line 14:


You need to modify mini_httpd.conf (to add cgipat)
You need to modify mini_httpd.conf (to add cgipat)
{{cmd|vi /etc/mini_httpd/mini_httpd.conf}}
{{Cat|/etc/mini_httpd/mini_httpd.conf|## do not leave empty lines in here!
<pre>
#host{{=}}www.example.org
## do not leave empty lines in here!
port{{=}}80
#host=www.example.org
user{{=}}minihttpd
port=80
dir{{=}}/www
user=minihttpd
cgipat{{=}}**.sh|**.cgi|**.lua
dir=/www
cgipat=**.sh|**.cgi|**.lua
nochroot
nochroot
</pre>
}}


mini_httpd service should be restarted because we have changed it's configuration
mini_httpd service should be restarted because we have changed it's configuration
Line 29: Line 27:


Creating sample Lua script
Creating sample Lua script
{{cmd|vi /www/test.lua}}
{{Cat|/www/test.lua|#!/usr/bin/haserl --shell{{=}}lua
<pre>
#!/usr/bin/haserl --shell=lua
Content-type: text/html
Content-type: text/html


<html>
<html>
<body>
<body>
<table border=1><tr>
<table border{{=}}1><tr>
<%  
<%  
t = {'Red', 'Blue', 'Yellow', 'Cyan'}
t {{=}} {'Red', 'Blue', 'Yellow', 'Cyan'}
for k,v in ipairs(t) do
for k,v in ipairs(t) do
io.write('<td bgcolor="'..v..'">'..v..'</td>')
io.write('<td bgcolor{{=}}"'..v..'">'..v..'</td>')
end
end
%>
%>
Line 46: Line 42:
</body>
</body>
</html>
</html>
</pre>
}}


Setting execution permission
Setting execution permission
Line 56: Line 52:
{{cmd|/www/test.lua}}
{{cmd|/www/test.lua}}
{{cmd|<nowiki>/usr/bin/haserl --shell=lua /www/test.lua</nowiki>}}
{{cmd|<nowiki>/usr/bin/haserl --shell=lua /www/test.lua</nowiki>}}
{{cmd|apk add sudo
{{cmd|su - minihttpd
sudo -u minihttpd /usr/bin/haserl /www/test.lua}}
/usr/bin/haserl /www/test.lua}}
{{cmd|<nowiki>apk add curl
{{cmd|<nowiki>apk add curl
curl http://localhost/test.lua</nowiki>}}
curl http://localhost/test.lua</nowiki>}}
[[Category:Web Server]]
[[Category:Lua]]

Latest revision as of 14:10, 25 August 2023

mini_httpd is a small HTTP server. Its performance is not great, but for low or medium traffic sites it's quite adequate. It implements all the basic features of an HTTP server.

Haserl is a small program that uses shell or Lua script to create cgi web scripts. It is intended for environments where PHP or ruby are too big.

Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.

mini_httpd setup

For setting up mini_httpd please see article mini_httpd

Haserl and Lua setup

Haserl and Lua packages are available in the Alpine Linux repositories. To install them run:

apk add haserl lua

You need to modify mini_httpd.conf (to add cgipat)

Contents of /etc/mini_httpd/mini_httpd.conf

## do not leave empty lines in here! #host=www.example.org port=80 user=minihttpd dir=/www cgipat=**.sh

mini_httpd service should be restarted because we have changed it's configuration

rc-service mini_httpd restart

Creating sample Lua script

Contents of /www/test.lua

#!/usr/bin/haserl --shell=lua Content-type: text/html <html> <body> <table border=1><tr> <% t = {'Red', 'Blue', 'Yellow', 'Cyan'} for k,v in ipairs(t) do io.write('<td bgcolor="'..v..'">'..v..'</td>') end %> </tr></table> </body> </html>

Setting execution permission

chmod +x /www/*.lua

Troubleshooting

For troubleshooting you can try running commands:

lua /www/test.lua

/www/test.lua

/usr/bin/haserl --shell=lua /www/test.lua

su - minihttpd /usr/bin/haserl /www/test.lua

apk add curl curl http://localhost/test.lua