Running Lua on Heroku

Posted February 05, 2012 by leafo (@moonscript)

Since the release of Heroku’s Cedar platform they've opened up the opportunity for users to run web applications on any stack. Using something called a buildpack we can describe a template for deploying any kind of application. I saw this as a great opportunity to try using Lua on a cloud hosting platform (for free).


I put together heroku-buildpack-lua, a buildpack containing Lua 5.1 andLuaRocks (a Lua package manager) enable you to quickly deploy Lua along with any required dependencies that can be found on theLuaRocks server.

我做了一个heroku-buidlparck-lua的构建包,包里有lua 5.1和luarocks(LUA包管理器),充许你快速的部署一个lua程序,并且任何相关的依赖,都可以在LuaRocsks服务器上找到。

Here’s a tutorial on getting a very simple app running:


Feb 12 2012 — I’ve updated the buildpack and this post to simplify the process.

1.Creating An App 2.Describing Dependencies 3.Creating A Web Server 4.Deploying The Web Server 5.What’s Next?

1.创建APP 2.描述依赖 3.创建一个WEB服务器 4.部署WEB服务器 5.后续


Creating An App

Assuming you've installed heroku we start by creating a new app:


$ heroku create --stack cedar --buildpack 
Clone the repository it created and we're ready to begin. (stark-dust-4830 was the randomly generated name of my app, replace it with yours.)


$ git clone git@heroku.com:stark-dusk-4830.git
$cd stark-dust-4830


Describing Dependencies

Heroku manages a collection of app servers for us, called web dynos in their terminology. Each application server must expose itself to the outside world. This is done by running a web server on the dyno.

Heroku为我们管理服务器上的应用集合, 术语叫做web dynos,每个应用服务必须对外界暴露自己,这是运行在dyno上的WEB服务。

The Xavante project is simple web server written in Lua with a couple dependencies.

Xavate project是一个简单的用lua弄的web服务,两依赖。

Using LuaRocks bundled in the Lua buildpack, we can easily install Xavante and all its dependencies. We describe the dependencies of our Lua project by creating a rockspec for it.

LuaRocks被绑定在lua级的buidlpack中, 我们可以很容易的安装Xavante和所有她的依赖, 我们创建一个rockkspec的文件来描述我们lua工程的依赖。

A rockspec is a special Lua file ending in .rockspec that describes meta-data about a Lua module. This meta-data includes things like the project name, the maintainer. It also holds any dependencies and how to build the module.


The Lua buildpack understands the rockspec format, but only looks at the dependencies. Thus, for simplicity we'll only define the dependencies. Go ahead and create app.rockspec and place inside of it:


-- app.rockspec
dependencies = {
Xavante will be our only dependency. We're going to keep this tutorial short and leave out the web frameworks. Xavante’s API is flexible enough that it functions as a makeshift framework.

Xavante 是我们唯一的依赖,我们这教程不使用lua web框架。Xavante的API很灵活,是够应付一般函数的简易框架。

If you commit and push, you'll see the buildpack fetch and build all the dependencies. There will be a lot of output, don’t be be concerned.


$ git add app.rockspec
$ git commit -m "init"$ git push origin master

—–> Heroku receiving push —–> Fetching custom buildpack… done —–> Lua app detected —–> Copying lua to bin —–> Installing packages Installing http://www.luarocks.org/repositories/rocks/xavante-2.2.1-1.all.rock…

… output truncated …

—–> Discovering process types Procfile declares types -> (none) —–> Compiled slug size is 292K —–> Launching… done, v5 http://stark-dusk-4830.herokuapp.com deployed to

Our dependencies work, but we still haven’t set up a web server. This we'll do by writing some Lua.



Creating A Web Server

We'll use Xavante’s programmatic API to create and run our server through a simple Lua script Create a file, web.lua, and place in it:

我们用Xavnate的可编程API来创建和运行我们的简单的lua脚本。 创建一个web.lua文件。

-- web.lua

port =...

xavante.HTTP {
  server = { host ="*", port =tonumber(port) },
  defaultHost = {
    rules = {
        match ="/$",
        with =function(req, res)
          res.headers["Content-type"] ="text/html"
          res.content ="hello world, the time is: "..os.date()
          return res
      }, {
        match =".",
        with = xavante.filehandler,
        params = { baseDir ="static/" }

In this file we create a web server with two simple rules. If you go to the path / then we say hello and show the time. Otherwise, we default to trying to serve files from the static/ directory in our app.

在这个文件,我们创建一个web服务和两个简单的规则。当你访问路径’/’时, 会显示 ‘say hello’的字样,并且显示时间信息,另外静态文件是放在当前app的static目录 。

Go ahead and create the static/ directory now, and put something inside of it like a favicon or a html file.

继续,创建的 static目录 ,现在就可以把放一些图标和html文件放到里面。

If you have Xavante installed locally, we can test the app. (where 5000 is a port to bind to)


$ lua web.lua 5000
Xavante started on port(s) 5000
If not, go on to the next step.


4.部署 Web Server

Deploying The Web Server

Now that all the required code is written, the only thing left to do is to tell Heroku how to start it.


Heroku uses something called a Procfile to list the commands needed to start things like web severs and workers. We only need a single web server.

Heroku使用了一个叫Procfile的文件,列出了web server要执行的命令任务,我们只需要一个单独的web 服务。

Create a file called Procfile and place inside of it:


web: lua web.lua $PORT
Now we're ready to deploy. Commit and push once again.


$ git commit -a -m "..."$ git push origin master
We can check and see if our app is running by typing into the console:


$ heroku ps
You'll probably see nothing running! It’s because we deployed before without a Procfile. Tell Heroku to start up our web server:

你也看到了,什么都没运行! 因为之前的部署没有Procfile文件,来告诉Heroku是如何启动web服务的。

$ heroku scale web=1
Scaling web processes... done, now running 1

$ heroku ps
Process  State       Command                
-------  ----------  ---------------------  
web.1    up for 16s  bin/lua web.lua $PORT
If you still see nothing running you'll have to debug. Run heroku logs to see if anything failed.

如果你还是看到什么也没运行,执行一下heroku logs,看一下有什么错误发生。

Now our web server is running, navigate to the url of the app to see it live. Don’t forget to try out some of the static files you included.

此刻我们的Web服务在运行了,跳到的Url 证明app在运行中,别忘了测试一下static目录下的文件。


What’s Next?

What we've created here is fairly primitive. There are a lot of opportunities for expanding:

这篇比较糙,后续还有很多可以展开的题目 。

Use a Lua web framework like Orbit Talk to a SQL database with LuaSQL Talk to Redis with redis-lua Talk to CouchDB with luachia Make a website in MoonScript

使用Orbit 这种Lua WEB框架。 探讨SQL数据库和LuaSQL 探讨Redis和redis-lua 探讨CouchDB和luachia 使用Moonscript创建站点。

It’s also worth reading the the Lua buildpack’s README because it explains how and where Lua and it’s packages are installed.

更多有价值的信息可以看buildpack里面的README文件, 里面解释了lua和对应包是如何被安装的。

leafo.net 2015 · Generated Fri Apr 8 14:00:07 2016 by Sitegen


Lua On Heroku