This is a note on building an HTTP server with Sinatra and handling POST requests.
I usually develop locally, and I was anxious about how to create a globally accessible Ruby HTTP server on a VPS or similar server with an assigned IP, but I was able to do it just by specifying the IP address with Sinatra.
First, I tried it with apache2 running.
# ruby app.rb -o 150.95.00.00 -p 80
Ignoring ffi-1.9.23 because its extensions are not built. Try: gem pristine ffi --version 1.9.23
Ignoring http_parser.rb-0.6.0 because its extensions are not built. Try: gem pristine http_parser.rb --version 0.6.0
== Sinatra (v2.0.3) has taken the stage on 80 for development with backup from Thin
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on 150.95.00.00:80, CTRL+C to stop
Stopping ...
== Sinatra has ended his set (crowd applauds)
Traceback (most recent call last):
11: from /root/gems/gems/sinatra-2.0.3/lib/sinatra/main.rb:26:in `block in <module:Sinatra>'
10: from /root/gems/gems/sinatra-2.0.3/lib/sinatra/base.rb:1459:in `run!'
9: from /root/gems/gems/sinatra-2.0.3/lib/sinatra/base.rb:1525:in `start_server'
8: from /root/gems/gems/rack-2.0.5/lib/rack/handler/thin.rb:22:in `run'
7: from /root/gems/gems/thin-1.7.2/lib/thin/server.rb:162:in `start'
6: from /root/gems/gems/thin-1.7.2/lib/thin/backends/base.rb:73:in `start'
5: from /root/gems/gems/eventmachine-1.2.5/lib/eventmachine.rb:194:in `run'
4: from /root/gems/gems/eventmachine-1.2.5/lib/eventmachine.rb:194:in `run_machine'
3: from /root/gems/gems/thin-1.7.2/lib/thin/backends/base.rb:63:in `block in start'
2: from /root/gems/gems/thin-1.7.2/lib/thin/backends/tcp_server.rb:16:in `connect'
1: from /root/gems/gems/eventmachine-1.2.5/lib/eventmachine.rb:530:in `start_server'
/root/gems/gems/eventmachine-1.2.5/lib/eventmachine.rb:530:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)
Well, an error.
Stop apache and re-run.
/etc/init.d/apache2 stop
It worked.
# ruby app.rb -o 150.95.00.00 -p 80
Ignoring ffi-1.9.23 because its extensions are not built. Try: gem pristine ffi --version 1.9.23
Ignoring http_parser.rb-0.6.0 because its extensions are not built. Try: gem pristine http_parser.rb --version 0.6.0
== Sinatra (v2.0.3) has taken the stage on 80 for development with backup from Thin
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on 150.95.00.00:80, CTRL+C to stop

If you send a POST request with curl as follows, it will display the data received on the server side.
curl http://150.95.00.00/ -X POST -d 'html=hogehogehoge'
Summary
This was a note on building an HTTP server with Sinatra and handling POST requests. The issue is that if you start Sinatra while apache2 is running, you’ll get an error, so you need to stop apache2 before starting Sinatra.
That’s all.