Rails - switch environment to production automatically - ruby-on-rails

I am doing a project in rails, and i just run this project in console by following command:
$ rails server
But it runs in development mode. I want when i run "rails server", it runs in production mode, what script must i edit to set the environment ?

Easy:
rails server RAILS_ENV=production
Or:
rails s -e production
Or you meant without this extra thing? If that so, it depends on which server you use. You can install Puma for example, and add config file, in which you can specify the default environment.
This question could help in case of set rails env for ngnx or passenger.

All the possible operations on the rails server
-p port: Specify the port to run on
-b ip: Bind to a specific IP address
-e name: Use a specific Rails environment (like production)
-d: Run in daemon mode
-h: Display a help message with all command-line options

Related

Error: No value provided for option '--port' Ruby on Rails & PostgreSQL

I am studying a project which is build on Ruby on Rails, I am newbie... I run a ruby-on-rails web application on Ubuntu. It connects to PostgreSql. When I run start_server.sh file, receive below error:
/home/ubuntu/.rvm/gems/ruby-2.4.5/gems/thor-1.0.1/lib/thor/parser/options.rb:228:in `parse_peek': No value provided for option '--port' (Thor::MalformattedArgumentError)
start_server.sh:
#!/bin/bash
# AWS environment variables
export AWS_S3_BUCKET="testing.mytest.resources"
export AWS_REGION="ap-southeast-2"
# Host and Port
rails s -b $LOCAL_DB_HOST -p $LOCAL_DB_PORT
Can you please tell a solution for this error?

Run `rails c` on GCloud instace with appengine gem

I have a Rails 6.0.0.rc1 application (with the appengine gem install) that I deployed to GCP. Is there a way to log into a remote rails console on the instance that runs the application? I tried this:
bundle exec rake appengine:exec -- bundle exec rails c
which gives the following output:
...
---------- EXECUTE COMMAND ----------
bundle exec rails c
Loading production environment (Rails 6.0.0.rc1)
Switch to inspect mode.
...
so apparently it executed the command, but closes the connection right after.
Is there an easy way to do this?
As reference: On Heroku this would simply be:
heroku run rails c --app my-application
There's a few steps involved:
https://gist.github.com/kyptin/e5da270a54abafac2fbfcd9b52cafb61
If you're running a Rails app in Google App Engine's flexible environment, it takes a bit of setup to get to a rails console attached to your deployed environment. I wanted to document the steps for my own reference and also as an aid to others.
Open the Google App Engine -> instances section of the Google Cloud Platform (GCP) console.
Select the "SSH" drop-down for a running instance. (Which instance? Both of my instances are in the same cluster, and both are running Rails, so it didn't matter for me. YMMV.) You have a choice about how to connect via ssh.
Choose "Open in browser window" to open a web-based SSH session, which is convenient but potentially awkward.
Choose "View gcloud command" to view and copy a gcloud command that you can use from a terminal, which lets you use your favorite terminal app but may require the extra steps of installing the gcloud command and authenticating the gcloud command with GCP.
When you're in the SSH session of your choice, run sudo docker ps to see what docker containers are presently running.
Identify the container of your app. Here's what my output looked like (abbreviated for easier reading). My app's container was the first one.
jeff#aef-default-425eaf...hvj:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND NAMES
38e......552 us.gcr.io/my-project/appengine/default... "/bin/sh -c 'exec bun" gaeapp
8c0......0ab gcr.io/google_appengine/cloud-sql-proxy "/cloud_sql_proxy -di" focused_lalande
855......f92 gcr.io/google_appengine/api-proxy "/proxy" api
7ce......0ce gcr.io/google_appengine/nginx-proxy "/var/lib/nginx/bin/s" nginx_proxy
25f......bb8 gcr.io/google_appengine/fluentd-logger "/opt/google-fluentd/" fluentd_logger
Note the container name of your app (gaeapp in my case), and run container_exec bash.
Add ruby and node to your environment: export PATH=$PATH:/rbenv/versions/2.3.4/bin:/rbenv/bin:/nodejs/bin
cd /app to get to your application code.
Add any necessary environment variables that your Rails application expects to your environment. For example: export DATABASE_URL='...'
If you don't know what your app needs, you can view the full environment of the app with cat app.yaml.
bin/rails console production to start a Rails console in the Rails production environment.

Thin Server - Virtual Path

In production, we deploy our application to a virtual path:
https://hostname/my-virtual-path/
So the route '/users/' in development is actually accessed on https://hostname/my-virtual-path/ in production.
This means that routes are different between development (/users/) and production (/my-virtual-path/users/). Normally this is handled by setting the environment variable RAILS_RELATIVE_URL_ROOT or config.action_controller.relative_url_root so the paths Rails generates with its URL helpers is adjusted depending on the setting of these variables. Unfortunately, our assets are going to be pre-compiled BEFORE we know what this setting will be.
Is there a way to run a Rails server in development mode, using thin, that will serve files to a virtual path? For example, I want to type:
thin start --ssl -p 3000 --path whatever-i-want
and be able to access the root URL at https://localhost:3000/whatever-i-want in order to test my application.
I think you’re after the --prefix option. From the output to thin -h:
--prefix PATH Mount the app under PATH (start with /)
In your case something like this:
thin start --ssl -p 3000 --prefix /whatever-i-want
(Note you need to start the prefix with /.)

EC2 : Rails deployment gives blank pages

I have deployed my rails application on EC2. It runs on two servers. One for rails application and second for DB.
When I start application using "rails s -e production&" and if I stay connected using SSH,
I can see the webpages.
As soon as I disconnect SSH I can not see the pages.
There are no errors thrown. One weird thing is "Production.log" file does not have anything.
everything is spit out on console.
You are running rails in the current ssh session. Any programs you have running during that session will stop if you disconnect. You need to set up your rails app to run as a daemon using something like Phusion Passenger.
You are basically running the built in WEBrick server that is not really meant for production so it's likely that the process is getting killed after the parent process (your ssh process) gets terminated.
You can probably tweak the configuration to make WEBrick not quit, or you can simply run your session using screen or tmux
Screen:
$ screen
$ rails s -e production &
$ screen -d
When you want to reattach:
$ screen -r
Tmux:
$ tmux
$ rails s -e production &
$ # Hit <ctrl-b><ctrl-d> to detach
When you want to reattach:
$ screen attach -t 0
Or like #datasage mentioned you can run your Rails with an actual production web server like Passenger Phusion or Unicorn.

How to configure webrick port in eclipse?

Using Eclipse/Aptana, how can I change the default port for WEBrick from 3000 to anything else, using a configuration settings in the project? E.g. I want to be able to have multiple servers running for different projects on different ports.
I don't see where (in Eclipse or my project settings) I can set the port? I run the server by right-clicking the project and choosing "Run Server".
Using: Eclipse Kepler; Aptana Studio 3 (3.4.2)
In Aptana Studio Go to Window > Show View > Servers.
There you can select to delete and update servers related to your apps.
You can set the port for WEBrick with the -p parameter. You should be able to append the param to your run configuration.
# ruby script/server -p 8080
There's also some other params which you might find useful:
# ruby script/server --help
=> Booting WEBrick...
Usage: ruby server [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=ip Binds Rails to the specified ip.
Default: 0.0.0.0
-e, --environment=name Specifies the environment to run this server under (test/development/production).
Default: development
-m, --mime-types=filename Specifies an Apache style mime.types configuration file to be used for mime types
Default: none
-d, --daemon Make Rails run as a Daemon (only works if fork is available -- meaning on *nix).
-c, --charset=charset Set default charset for output.
Default: UTF-8
-h, --help Show this help message.

Resources