nginx + mongrel + redmine – quick setup notes

My quick setup notes for nginx as a frontend to a mongrel redmine server.

This is intended for situations where redmine needs to be served in a subdirectory via a nginx that is also used for other serving needs.

Some basic prerequisite knowledge of nginx and redmine will likely be needed.

Instructions are tested on redmine-svn and Ubuntu-9.10.

  • Install dependencies
    • sudo apt-get install subversion libmysql-ruby rake rubygems mongrel nginx
  • Setup directory structure
    • sudo adduser --system --group redmine
      sudo mkdir /opt/redmine
      sudo chown redmine:redmine /opt/redmine
      sudo -Hu redmine svn checkout http://redmine.rubyforge.org/svn/trunk /opt/redmine
  • Create the MySQL database for redmine
    • Example for mysql-server on localhost:

      sudo apt-get install mysql-server
      mysql -u root -p
      create user 'redmine'@'localhost' identified by 'password';
      grant all privileges on  `redmine_%` . * to  'redmine'@'localhost';
      create database redmine_production character set utf8;
      quit;
    • (or use phpmyadmin)
  • Copy config/database.yml.example to config/database.yml and edit the database configuration
    • sudo -Hu redmine cp /opt/redmine/config/database.yml.example /opt/redmine/config/database.yml
      sudo -Hu redmine vim /opt/redmine/config/database.yml
    • Example database spec:
      • production:
          adapter: mysql
          database: redmine_production
          host: localhost
          username: redmine
          password: password
          encoding: utf8
  • Generate redmine session store secret
    • cd /opt/redmine
      sudo -Hu redmine gem install -v=2.3.5 rails
      sudo -Hu redmine rake generate_session_store
  • Create the database structure and insert the default data
    • sudo -Hu redmine RAILS_ENV=production rake db:migrate
      sudo -Hu redmine RAILS_ENV=production rake redmine:load_default_data
  • Setup redmine to be served from /redmine/
    • cd /opt/redmine/config
      sudo -Hu redmine cp additional_environment.rb.example additional_environment.rb
      echo 'config.action_controller.relative_url_root = "/redmine"' | sudo -Hu redmine tee -a additional_environment.rb
  • Setup email delivery as desired
    • cd /opt/redmine/config
      sudo -Hu redmine cp email.yml.example email.yml
      • Example smtp spec
        • sudo -Hu redmine vim email.yml
          production:
            delivery_method: :smtp
            smtp_settings:
              address: smtp.example.com
              port: 25
              domain: example.com
  • Start the mongrel server
    • cd /opt/redmine; sudo -Hu redmine mongrel_rails start -e production -p 9001 -d
  • Edit /etc/nginx/sites-enabled/default
    • sudo vim /etc/nginx/sites-enabled/default
    • Add to the active server section:
      •         location /redmine {
                        alias /opt/redmine/public/;
                        try_files  $uri/index.html $uri.html $uri @mongrel;
                }
                location @mongrel {
                        proxy_set_header  X-Real-IP        $remote_addr;
                        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
                        proxy_set_header  Host             $http_host;
                        proxy_redirect    off;
                        proxy_pass        http://127.0.0.1:9001;
                }
  • Restart nginx:

    sudo service nginx restart
  • Test the website. eg: http://localhost/redmine/
  • If successful, set mongrel to start on boot
    • sudo vim /etc/rc.local
      • cd /opt/redmine; sudo -Hu redmine mongrel_rails start -e production -p 9001 -d
  • Retest site after a reboot
This entry was posted in Uncategorized. Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.

5 Comments

  1. Wade
    April 3, 2010 7:29 am

    Glad to find this article, but… as i open http://localhost/redmine, all file in redmine/public can not be loaded.

    some errors like:
    ActionController::RoutingError (No route matches “/themes/basecamp/stylesheets/application.css” with {:method=>:get}):

    I think it’s somethings wrong in nginx.conf, because when i change back additional_environment.rb back, then i can access to http://localhost/, it works fine.

    • April 3, 2010 1:05 pm

      I can reproduce your error by commenting out the alias line in location /redmine {}.

      There are two types of requests when serving a rails app in nginx.
      First the static content which is located in /opt/redmine/public and should be server from under your project root. Ie
      /opt/redmine/public/themes/basecamp/stylesheets/application.css should be served at http://localhost/redmine/themes/basecamp/stylesheets/application.css
      This is taken care of by the alias line in the location section.

      Second. If static content is not able to be found then request gets forwarded to mongrel. By using the config in additional_environment.rb you are able to serve this content from /redmine instead of just /.

      Does explaining that help you find your error any easier?

  2. April 4, 2010 10:55 am

    Ok, I see how your trying to debug by starting with something small, but you should not need to both alias and proxy_pass in /redmine/themes.

    The themes folder is only static content, so it should only have an alias statement.
    If you lookup the alias documentation alias defines the root content for the current folder (as opposed to the root statement which defines root based on the root of your site).
    So your location block should be:
    location {
    alias /opt/redmine/public/themes/;
    }
    by using try_files statement in location /redmine {} you are telling nginx to try to serve static content first before passing to the mongrel proxy. Which should have given you everything working in the first place without having to resort to location hacks.
    I’m not sure why your having so much trouble. Have you tried it on a clean install in a VM or the like?

    • Wade
      April 5, 2010 11:37 pm

      I read “alias document” and “HowTo Install Redmine in a sub-URI”, still have no idea on it.
      As some reasons, i don’t want to reinstall nginx, so Maybe oneday i reconfig my server can solve the problem… or like you said install in a VM environment.

      Anyways, thianks your help and your article, at least i can use redmine in a subdirectory.
      ^^

2 Trackbacks