i am new to sinatra application. i developed my first application with sinatra. Now, i want to deploy this application on my own private server. My own private server is Linux server and all required config (i.e., ruby, sinatra, passenger ) installed.
So, i would like to know how to deploy it?
Here's our company's basic setup for Ruby microservices APIs:
1) Put your Ruby apps in a special folder
We like /srv/applications/
2) Choose a port for your application
Don't use the default port. I prefer ones in the 2000 series (E.g., 2000, 2010, 2020, ...) for Ruby apps.
3) Setup a reverse proxy to access your application on that port
Depending on your server, there are lots of Tutorials for Apache or Nginx.
That way /user-api will redirect to your port. Ex. myserver.com/user-api goes to myserver.com:2020
4) Run your application
If you're not using bundler, use bundle to run the app. To run on a specific port, use "bundle exec rackup -p "
You should then be able to access the application. If you run into trouble, some common problems include 1) the port is being blocked to the outside world, 2) you tried to access it before the app was running and now you still get 503s even though the app is running (restart your webserver to fix it).
Bonus - running your app as a Unix service
This has enormous benefits. If you can run the Ruby app with another supervisor level app, then you can automatically start it on reboot, automatically restart the app on failures (which can lead to flapping when you have catastrophic problems).
Originally we used Monit which is typically used for service monitoring but can actually start/stop/restart (start on boot) for a service.
Now we use foreman and upstart. I highly recommend this pairing, as app supervision is the textbook use case for Upstart. You can skip using Foreman if you have a vanilla configuration, but it is incredibly useful if you need to start other services, set environment variables, etc.
Related
I'm an iOS developer primarily. In building my current app, I needed a server that would have a REST API with a couple of GET requests. I spent a little time learning Ruby, and landed on using Sinatra, a simple web framework. I can run my server script, and access it from a browser at localhost:4567, with a request then being localhost:4567/hello, as an example.
Here's where I feel out of my depth. I setup an Ubuntu droplet at DigitalOcean, and felt my way around to setting up all necessary tools via command line, until I could again run my server, now on this droplet.
The problem then is that I couldn't then access my server via droplet.ip.address:4567, and a bit of research lead me to discovering I need Passenger and an Apache HTTP Server to be setup, and not with simple instructions.
I'm way in over my head here, and I don't feel comfortable. There must be a better way for me to take my small group of ruby files and run this on a server, than me doing this. But I have no idea what I'm doing.
Any help or advice would be greatly appreciated.
bit of research lead me to discovering I need Passenger and an Apache HTTP Server to be setup, and not with simple instructions.
Ignore that for now. Take baby steps first. You should be able to run your Sinatra app from the command line on the DigitalOcean droplet, and then access it via droplet.ip.address:4567. If that doesn't work something very fundamental is wrong.
When you start your app, you will see what address and port the app is listening on. Make sure it's 0.0.0.0 and 4567. If it's 127.0.0.1 or localhost that means it will only service requests originating from the same machine
After you get this working, next step is to make your Sinatra app into a service. Essentially this means the app runs in the background, and auto-starts when the system reboots. Look into Supervisor which is very simple configuration to get this running.
Later you can install Apache or Nginx to put in front of your Sinatra app. These are proxies which simply forward requests from port 80 (default HTTP port) to your sinatra app, but can do additional things such as add SSL support, load balancing, custom error pages etc. - all of which you do not need right now.
I'd like to have a simple API running on a webserver (Windows Server 2008).
I've chosen Sinatra with its default Webrick backend.
What I'm not sure how to do is make my sinatra app visible to the outside world? I have remote desktop access to my server, I have the Sinatra app running, but I'm sure it's not configured correctly for response to outside requests.
I'd love any pointers in getting this going.
Figured this out.
Was pretty much as simple as ensuring sinatra was running with no ip (either run with -e production or use the -o 0.0.0.0 flag), and then most importantly, ensure the port the webrick server was running on was open on the windows server machine. Once the port was open, accessing it via ip:port works.
In heroku we could use the command heroku maintenance on or off... How can I do it in my AWS EC2 web services? I tried just to stop the server using sudo service nginx stop but I don't like the error page that was displayed. It says error in url. In heroku if i use the maintenance command, the error page will display "Under contruction" "Maintenance" or something like that.
How can I do it in Amazon web services? thanks
You have to do it yourself. AWS does not provide that.
Yeah, so heroku has routing magick, think of the default WebBrick server your rails app is running as running outside of rails. Idk, assuming you use passenger you would'nt really notice this but at a high level, nginx is proxying your apps' port 3000 to port 80 for certain requests. I appologize if you don't know what I mean by that.
So (YOUR APP) ---> (NGINX) -----> (Client)
the advantage here, is that nginx keeps running but during maintience mode they most likely start shooting you pages to static content. If you too run you rails app via a proxy rather than via passenger, than you solution is easy. Stop your WebBrick, mongrel, unicorn, thin (what ever app server here) and setup an error message page for bad gateway errors aka 502 route.
If you use passenger. You could write a location block, that would over ride all your routes to a maintence page and switch Passenger to off in that server block, now rather than serving your app you can serve a static maintence page.
Heroku wraps alot of technology and exposes nice tools, so you'll need to do a bit more ground work to automate your stack. But this is a place to start.
I want to use Faye on production server. how can I start faye server automatically or as a daemon process.
Because when I start faye server using SSH it shutdown as i close ssh connection. Please guide me its really urgent.
I can run faye server on local but how I can rub this on live site.
It gets stopped probably because you're running it in your session, and once that session is closed it receives a SIGHUP signal and quits.
A quick test to determine if that's really your problem is to log in, execute
nohup your_server_startup_command > /dev/null 2>&1 &
logout, and see if the server still runs. THIS IS NOT A PERMANENT SOLUTION THOUGH!
The normal way for a server is to create a management script in init.d, and then use the service management app of your Linux distro to have the service started in the appropriate runlevels. In OpenSUSE you'll use YAST for that, in Ubuntu there's sysvconfig.
There are subtle differences between Linux distros, have a look here to get a general idea of how an init.d script is supposed to look, or here for an absolute bare-bones example.
I use faye as my message server currently.
Perhaps you would want to make faye as a daemon.
I use this for my faye app.
http://rubygems.org/gems/daemons
just
gem install daemons
and edit ur own rake file or a plain ruby to run daemon up. that's all
There are lots of daemon tools for ruby.
You can also combine faye with sinatra or thin,
but it's a little bit hassle when you can use daemons and fire it up in 3 mins. :)
Using Foreman + Upsart should be the best way to do it.
What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites running on the host so i can't run jetty/mongrel on port 80. How should i handle multiple sites/applications running at the same host?
Use a web server such as Apache that runs on port 80 and use virtual hosts to direct the traffic to the right app server. So basically you would run each application server (jetty/mongrel, etc.) on a different port and then in each virtual host would have a different configuration to use something like mod proxy to forward the traffic to the app server. You could use a different web server such as lighttpd or nginx. For the sinatra app you could also look at Phusion Passenger, a.k.a mod rails, a.k.a mod rack, which theoretically works with any rack app, although I've only used it with Rails.
If you look into it some more you'll find that there are various schemes for forwarding traffic to the app server from a web server, but the basic mechanism for doing this kind of thing always boils down to having a web server that listens on port 80 that uses name-based virtual hosts to then forward the traffic to the appropriate app.
I've been doing this kind of thing with various standalone servers (e.g., AllegroServe) for years. I've found the best approach to be:
Run each server on a different, non-privileged port (such as 8080)
Run pound (or Nginx etc.) on 80, configured to map requests to each application.
Pound is great, and the configurations end up very simple (unlike Nginx). It will also do SSL fronting and HTTP sanitization for you, which takes the burden off your application.
Use passenger! http://modrails.com - it is a plugin for apache and nginx that lets you (very) easily run a ruby app as a virtual host