title: Rails 3.1 application deployment tutorial
body: <p>I'm looking for a good deployment tutorial for a Rails 3.1.1 application on a server. And by good I actually mean complete. The reason I'm posting this question is that although there are tons of tutorials out there on the web (google, blogs, books, other stackoverflow questions etc...) all of them seem to focus either on a problem with the deployment process or make some assumptions about the deployment environment that do not match with what I need.</p>
  
  <p>I realize that deploying a Rails app on a server requieres a variety of different programs and tools that need to be configured and somehow I always get stuck on apparently "little" things that make me very frustrated.</p>
  
  <p>So, let's begin from the start. What I have now is a server that I can access through SSH and a domain name, let's call it <strong><em>www.example.com</em></strong>. The server runs a fresh Ubuntu 10.04 x64 and has just a user installed, namely root (through root I access the server with SSH).
  Note! There is no Apache, Ruby, PHP, MySQL, cPanel or any other panel installed, just the bare minimum that comes with a fresh installation.</p>
  
  <p>Also the web server will host a single application and the database will run on the same machine.</p>
  
  <p>From my knowledge the process of deploying a Rails application follows the following scenarios:</p>
  
  <ul>
  <li><strong>Installing Ruby</strong></li>
  </ul>
  
  <p>I already did this by using RVM using the Multi-User install process (simply because I have just the root user and it does it automatically). This seems to work fine aftewards but I do have some questions:</p>
  
  <p>Would it make more sense to install Ruby directly and not through RVM (I'm thinking maybe in terms of efficiency - also RVM does a little bit of magic behind the scences modifying some bash_profile files in ways that I don't understand and this somehow seems invasive...)?</p>
  
  <p>Would it make more sense to install as a separate user through RVM (can there any safety problems arise)?</p>
  
  <ul>
  <li><strong>Necessary gems</strong></li>
  </ul>
  
  <p>Now that Ruby is installed what would be the best initial set of gems to install along side it?</p>
  
  <p>I installed just bundler, so that once I upload my application on the server I can run a bundle install command which will install in turn the required app gems.</p>
  
  <p>Question - Should I install the rails gem before hand? Are there any other gems that need to be installed?</p>
  
  <ul>
  <li><strong>Web server</strong></li>
  </ul>
  
  <p>This is where it gets tricky for me. I'm trying to use apache and mod-passenger for deployment (they seem to be the most popular). So I installed the apache web server and the passenger gem and all the related dependencies (libraries mentioned by passenger).</p>
  
  <p>Now, the problems arise. First thing is user related. How does Apache run? I mean under each user? If I installed it and (re)started it using the root user, will it permanently run under the root user? Is this a bad thing? If yes, should I create another user? If yes, how? In what groups should I put the new user in, what rights should he have (namely what folders should he have access to). How to start the apache in this case (under root, under that user, add a line somewhere in the configuration file, etc.)</p>
  
  <ul>
  <li><strong>Website configuration</strong></li>
  </ul>
  
  <p>Where to put the website configuration stuff? Is it OK to put it into apache.conf, or should I create a new file in sites-available? Or should I simply reuse the <em>default</em> file that is already present there? If a new user has been created at the previous step, what rights should he have in relation to the config files?</p>
  
  <p>Also... where to eventually put the rails application? In what folder would it be best? Somewhere under home? Maybe under /var/www? I want to know how would this affect the rights of the apache process serving the app? (Generally I have problems while serving an app, it complains that it doesn't have rights on a specific folder. Also, using the new asset pipelines - which I don't fully understand - asset files are being created and they seem not to inherit the parent folder rights...)</p>
  
  <ul>
  <li><strong>Database</strong></li>
  </ul>
  
  <p>As database I'm using MySQL and installing it is pretty straightforward. The question that I have here is again user related. Should I use a special user for the database? How does MySQL actually manage users (are they internal, or are they the Linux users or ...?). Should the database user be the same as the "web user" from above? Or should it be a different one?</p>
  
  <ul>
  <li><strong>Assets</strong></li>
  </ul>
  
  <p>Here I get really lost. I really have troubles making the assets pipeline works. I've checked the railscasts episode and also the rails website tutorial and I do seem to theoretically understand the thing, yet I have problems in practice. </p>
  
  <p>So the questions here would be - what commands should I run to precompile the assets? (Trying to run rake assets:precompile). Is it ok? What should I change in the production.rb file? How do the assets generated work in relation with the webuser or database users created above? I am personally getting a problem in this step namely the log files say that some css files are not accessible (for example I have jqplot library installed under the vendor/assets folder and its files cannot be accessed - should I add a manifest file here somehow?).</p>
  
  <p>It would be really great if someone could point me towards a nice article, or resource that explains all this stuff. The main area which I'm having problems in is how does Apache, Passenger, Ruby, Rails and MySQL interact with a Linux user. How to properly set up the permissions for the Rails app folder? How does the assets pipeline affect this user permission stuff?</p>
  
  <p>Thank you</p>