How to run a Rails app in production mode locally with Puma

How to run a Rails app in production mode locally with Puma

Sometimes, you need to debug an error with assets generation y/o in the context of production environment, for such cases, here, I’m sharing a few steps you can follow in order to run your Rails application in production mode.

Prepare your https/ssl certificates:

openssl req -x509 -sha256 -nodes -newkey rsa:2048 -days 365 -keyout localhost.key -out localhost.crt

Skip most of the info if you want and after that you will get these two files(copy them into a specific folder file):
– localhost.crt
– localhost.key

Prepare your secret or any other environment variables that you need for your application

RAILS_ENV=production be rake secret
export SECRET_KEY_BASE=fb8a46298cb2eaa1b33bd18b8c41273d74902ec34aabd20dd4ce9211e7168fdc4f41e8309cf69589e10c3969922d63f23b94c47837a6ac0f0e99409409404

Update the `config/database.yml` for production file according to your user/pass

username: 'xx'
password: xxxx

Create your database and run migrations:

RAILS_ENV=production be rake db:create
RAILS_ENV=production be rake db:migrate

Clean old assets:

 rm -rf public/assets

Precompile and install assets:

RAILS_ENV=production bundle exec rake assets:precompile

NOTE: If you are making any changes to your assets, before running the precompile command, remember to clean-up assets before:

RAILS_ENV=production be rake assets:clobber

In case you want to re-use the development data you can export/import that in the following format (If you are using Postgresql):

pg_dump --no-owner --no-acl -d my_database_development > yourdatabasedump
psql -d my_database < yourdatabasedump

Run the server in production mode:

be puma -b 'ssl://127.0.0.1:3000?key=/Users/heridev/.ssh/localhost.key&cert=/Users/YourUserName/.ssh/localhost.crt' -e production

And allow Chrome to visit that using the advanced option (non secure visit)

https://127.0.0.1:3000

In the case that you want to go to the Rails console, you can do so by running:

RAILS_ENV=production rails console

Having troubles with this guide? follow me and let me know in my twitter account @heridev

Cheers

No Comments

Post A Comment