Skip to content

Instantly share code, notes, and snippets.

@codespore
Last active December 9, 2018 23:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codespore/4651731 to your computer and use it in GitHub Desktop.
Save codespore/4651731 to your computer and use it in GitHub Desktop.
Apache + Unicorn

Setting up Apache on Ubuntu to proxy to Unicorn requires the following installation commands:

  • apt-get install apache2 -y
  • apt-get install libapache2-mod-proxy-html libxml2-dev -y
  • a2enmod headers
  • a2enmod proxy
  • a2enmod proxy_http
  • a2enmod proxy_balancer
  • a2enmod rewrite
  • a2enmod ssl
  • /etc/init.d/apache2 restart

Add the following into /etc/apache2/apache2.conf

<VirtualHost *:80>
  ServerName     myhost.example.com
  DocumentRoot   /opt/example/app/public

  RewriteEngine On
  # Redirect all non-static requests to unicorn
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://unicornservers%{REQUEST_URI} [P,QSA,L]

  RewriteCond %{DOCUMENT_ROOT}/public/$0 -f
  RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]

  <Proxy balancer://unicornservers>
    Allow from any
    BalancerMember http://127.0.0.1:8080
  </Proxy>
</VirtualHost>

Source:

Add unicorn.sh file into /etc/init.d

Source: https://gist.github.com/4651758

Add Unicorn config 'my_app.unicorn.rb' into /etc/unicorn

APP_ROOT  = '/root/parlo'
RAILS_ENV = 'production'
preload_app true
timeout 30
listen 3000
pid         "#{APP_ROOT}/tmp/pids/unicorn.pid"
listen      "#{APP_ROOT}/tmp/sockets/unicorn.sock"
stderr_path "#{APP_ROOT}/log/unicorn_error.log"

working_directory "#{APP_ROOT}"
worker_processes 2

after_fork do |server, worker|
    ActiveRecord::Base.establish_connection
end

Start Unicorn via init.d script

/etc/init.d/unicorn.sh restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment