How can I start up a ruby server script when I start up my rails app? - ruby-on-rails

I'd like for a ruby server script that lives in my rails app root folder to accept connections from clients (the clients are not browsers--not that this really matters i guess) and update the db of my rails app while my rails app is running. Therefore, I'd like for it to be told somehow to start running whenever my web server starts serving up my rails app and stop running whenever my web server stops serving my rails app. How can I do this? Is this as simple as putting the name/path of my ruby server script somewhere?

You should look into Foreman, a gem specifically for launching multiple processes in your server environment.

Related

How to run rails applications on digitalocean using "rails s" (make it a development environment)

My issue is that I created a droplet to develop Rails apps in digitalocean .
I used the one-click rails droplet. And now I want to create more rails apps than the default rails app in this droplet.
The issue here is that it comes installed with nginx/unicorn .. And they're always on with path of default rails project in their config files.
Now let's assume I created another rails app(file) and I want to run it using "rails s" instead of default rails app that is created by the droplet. How can I do it?
Note: I don't want to change the file path in configs each time I decide to try another app
PS: I tried stopping the service of unicorn/nginx one at a time and both of them in the same time to use "rail s" to run the app .. But it didn't work .. Web pages were not loading
I know it might be a question of a rookie. But I'm kinda new to these stuff and I'd appreciate it if anyone could help me.
If you run it with rails s on the server, chances are it will be running with Puma, or if you're on an older version of Rails, Webrick. Unicorn is not involved in that case because Rails is using its own default web server. If you see that 'rails s' is not running in the right environment, it may be because RAILS_ENV is being set in your shell profile. You can override that by doing:
RAILS_ENV=development rails s
To launch your console.
That being said, rails s runs on localhost:3000 by default - and in the case you described it would be running on DigitalOcean's localhost - not yours. In order to get to it from your local machine, you would need to set up some sort of reverse proxy to allow connections to DO to get served from localhost. This is what nginx is doing for you - it's facilitating a reverse proxy.
If you want do use your DO server as a development machine for a second rails app you have, you're going to have to create that new rails app on the server, then create the reverse proxy settings in nginx to direct to it, then finally create the unicorn settings to serve it. This is an uncommon way of developing though. I recommend using your local machine to develop, and setting up Capistrano or some other deploy tool to deploy it to DO instead. You'd still need to add the settings in nginx/unicorn for the second app, but it will save you headache down the road.

Is one rails server per application?

I have two questions about rails server:
Do I have to start the server from within the application folder?
Is the server I started only for that application?
If they are true, this does not quite make sense to me, since why do I need to start multiple servers?
Or is there some kind of master configuration, so that one server can route to different applications? Is Capistrano for this purpose?
I'm going to assume you're talking about the rails server command, for running a local rails server to test your application, and that you're not talking about setting up a rails application on a remote server. Please mention if that is not the case.
Yes, you must execute rails server from within the root folder of your rails application.
Yes, the server you started is only for that application. It's a self-contained thing.
You should not need to start multiple servers. Even if you have multiple applications, you probably don't need to have more than one running at a time. So, you can shut down the rails server in one application (Ctrl-C) and then cd to your new application, and start a new rails server there with rails server.
If you do need to run two local rails applications at once, you can do so by running them on different ports. So, the first one, you can just execute rails server and it will make your site available at localhost:3000 (because port 3000 is the default port). The next one, you can specify a port other than 3000 - eg. rails server -p 3001 to get a rails app at localhost:3001.
Capistrano is for deploying your applications to a remote server, not for running them locally on your own computer. So, it is not relevant here. What you may be interested in is http://pow.cx/
Again, I've assumed you're talking about running your rails app locally on your own computer. If you're referring to deploying it to the internet on a server, then you can ignore this answer.

Setting up Rails on Hostmonster

I'm able to run rails s through ssh successfully and see the app start up just as it does on my own machine but I'm unable to access the app from the web. The app is directly under the home folder and I have a symbolic link pointing from public_html to the public folder of my rails app, just as this tutorial explains. I even tried setting up a subdomain and every other step in the tutorial to no avail. Any help would be highly appreciated.
You need an application server like Phusion Passenger, Unicorn or puma to run a Ruby app in a production environment. Typically, you'll integrate the application server into a web server's (Apache, nginx) environment.
I don't know about your hoster, but if you have root access, then you can probably use any of these application servers.
The built-in server you start by running rails server is only meant for testing purposes on your local machine. It has not been made with security, performance, stability or any other production-environment criteria in mind.

how to deploy an app on heroku running with rails/nginx/node/redis

I have an app running locally on Ruby on Rails, and nginx is my assets server. I have node server running for the real time stuff. The js file that runs with node is in the rails application folder. Redis subcription is used for the intermediate store between node and rails. So, it lies in the js file. It is subscribing the data from sql server trigger action. Redis Server is running as a service.
With this brief scenario, do any of you have rough idea, how I can go about deploying an app with all these in heroku?
Any opinion is highly appreciated.
Regards,
Sagar

Viewing project on browser

I am new to ruby. I can view my project on browser when I write ruby script/server otherwise I can't view my project on localhost.
What I have to do to view my project always on browser?
If you are using ruby on rails for the first time . Your answer is detached(deamon) mode .
Make sure your ruby version is more then 1.8.6 and rails version is less then 3.x.x
Script is
ruby script/server -d
And to know more
ruby script/server --help
When you use your application (using a browser) the application's code is run on a web server that receives the requests, execute the right code and respond to the user.
Now, rails provide a simple web server that you can run with rails server and you can use to test and develop your application. This web server is run on your machine and when you go to http://localhost:3000/ it's that local web server that responds.
If you want to access your application you need a webserver running. If you want to access your application anytime from everywhere the application must be in a server accessible from everywhere but you first need to deploy your application.

Resources