Deploy Rails 5.1 app with Webpack, React Puma and other
Recently I’ve been working on my inchef-app on localhost, app is almost done, but it was working only on my localhost. My previous projects I was running with unicorn and simple rails and jquery – nothing fancy. So I had my configuration up and running – just needed copy paste, change paths and that it. When yyou play with something new, and dont have any experienced friend around – you have to dive into ocean by your own – and I hate it, so deoploying I postponed as long I could… I like when I have time to focus, and read, play, find exact solution which will fit and will work. So today – Monday – this was good time. I’ve spent bunch of hours to test and check. So my server is running on debian wheezy, old one but good ;), first problem was that webpacker needs to run node > 4.x and I had 0.12 or sth. So simple `apt-get install node` didn’t worked, because node doesn’t support wheezy ;(. So what I did – I’ve installed node 5.x, than installed npm and than upgraded node to version 8.x 馃槈
# Installing Node.js 5.x on Ubuntu / Debian
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs
than install or use npm
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo ln -sf /usr/local/n/versions/node/<VERSION>/bin/node /usr/bin/node
To upgrade to latest version (and not current stable) version, you can use
sudo n latest
To undo:
sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/node
sudo n rm 6.0.0 # replace number with version of Node that was installed
sudo npm uninstall -g n
found here: https://askubuntu.com/questions/426750/how-can-i-update-my-nodejs-to-the-latest-version
then I needed yarn:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
Ok, so we have lastest node, npm
npm -v # => 5.0.0
node -v #=> v8.0.0
Now we need deploy our app:
Assuming that we are in app directory, we need:
webpack config in config/webpacker.yml
development: dev_server: host: 0.0.0.0 port: 8080 https: false production: source_path: frontend source_entry_path: packs public_output_path: packs
Puma config in config/puma_production.rb
workers 4 #according to qty of server processors threads_count = ENV.fetch("RAILS_MAX_THREADS") { 6 } threads threads_count, threads_count rails_env = ENV['RAILS_ENV'] || "production" environment rails_env app_dir = File.expand_path("../..", __FILE__) shared_dir = "#{app_dir}/tmp" bind "unix:#{shared_dir}/sockets/puma.sock" stdout_redirect "#{shared_dir}/puma.log", "#{shared_dir}/puma.err.log", true # Set master PID and state locations pidfile "#{shared_dir}/pids/puma.pid" state_path "#{shared_dir}/pids/puma.state" activate_control_app # Allow puma to be restarted by `rails restart` command. plugin :tmp_restart # Change to match your CPU core count on_worker_boot do require "active_record" ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env]) end
Next I had to run npm install
to install needed js libs
Now precompile assets and webpack according to https://github.com/rails/webpacker it should be done automatically with rails assets:precompile
however this didn’t work – maybe due to missing file config/webpacer.yml
. So I executed `bundle exec rails webpacker:compile
and it did work 馃槈
Next step… I was able to run puma server from app directory puma -C config/puma.rb
and it worked, now we need configure nginx to talk to puma, everyone uses sockets to we also will use socket, my very simple nginx configuration looks like:
upstream app_server { server unix:/path/to/puma.sock fail_timeout=0; } server { server_name domain.name; root /path/to/app/public; location / { if (!-f $request_filename) { proxy_pass http://app_server; break; } } }
I use init.d so had to download proper scripts from https://github.com/puma/puma/tree/master/tools/jungle/init.d
wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/init.d/puma
wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/init.d/run-puma
Installation:
# Copy the init script to services directory
sudo cp puma /etc/init.d
sudo chmod +x /etc/init.d/puma
# Make it start at boot time.
sudo update-rc.d -f puma defaults
# Copy the Puma runner to an accessible location
sudo cp run-puma /usr/local/bin
sudo chmod +x /usr/local/bin/run-puma
# Create an empty configuration file
sudo touch /etc/puma.conf
Managing the jungle
Puma apps are held in /etc/puma.conf by default. It’s mainly a CSV file and every line represents one app. Here’s the syntax:
app-path,user,config-file-path,log-file-path,environment-variables
You can add an instance by editing the file or running the following command:
sudo /etc/init.d/puma add /path/to/app user /path/to/app/config/puma.rb /path/to/app/log/puma.log
The config and log paths, as well as the environment variables, are optional parameters and default to:
- config: /path/to/app/config/puma.rb
- log: /path/to/app/log/puma.log
- environment: (empty)
Multiple environment variables need to be separated by a semicolon, e.g.
FOO=1;BAR=2
To remove an app, simply delete the line from the config file or run:
sudo /etc/init.d/puma remove /path/to/app
The command will make sure the Puma instance stops before removing it from the jungle.
Of course there was a problem with nginx… with my configuration, so very helpful was nginx -t
it will show you cause of the problem.
OK, now I can run sudo service puma start
and it will start my apps… and that its few hours of solving problems, now I have my app up and running globally not only on my localhost ;D
Najnowsze komentarze