Developing & deploying Rails app from same machine - ruby-on-rails

I have started developing a new Rails app on my server using RVM, Rails 3, & Ruby v1.9.2. I am using Git as my code repository. It's a simple app and I don't want to use an extra server. I just want to deploy my app directly from the same server I am developing on.
I've installed Phusion Passenger w/ Apache to serve my app, but have realized that I can't do that pointing to my development directory as my RAILS_ENV is set to "development". (I found I got file permission errors on the asset pipeline and other issues when I attempted to set RAILS_ENV to "production" and serve the app)
What's the simplest/easiest way to deploy the app? Can I simply:
1) Create a separate user to run rails production (Rails in dev currently runs as me on my Ubuntu server)
2) Clone my repo into a separate dir and configure Apache accordingly
3) Seed my database with the data needed for production (not much data needed here)
4) What else?
I've looked briefly at Capistrano, but it seems like overkill for this simple app. I only need to be able to provide a simple web interface for some data entry. Seems like git push should be sufficient, but I haven't done this before so maybe I'm wrong? Also, if I git push how do I ensure file permissions in the "production" directories are all set properly, particularly for any new files that get created in the originating app directory structure?
Thanks for any ideas.

No- you do not need Capistrano for the above; at this stage I feel it will only serve to confuse you further.
I'd suggest you first save your repo to a private Github or free BitBucket account. What you should do is keep one folder for 'development'.
Remember that Passenger is 'just' a module working with Apache. So what you need to do is setup a virtual host under apache and direct that to another folder on your system. For this example, consider:
~/rails/myapp_development/ and ~/rails/myapp_production/
Passenger always runs the app in production, so that should not be an issue. You can do bundle --without=production in your development setup to ignore any gems listed in the Gemfile under the production namespace, i.e. say you've got the mysql adaptor specified, you can ignore this and have Rails only rely on the SQlite gem.
You can now simply develop in the development folder, commit, push to BitBucket. Deploying will be as simply going into the production folder and doing a git pull and touch tmp/restart.txt.

Related

Rails server not working when copying back from staging deploy

I ssh copied a rails app from a staging server because the development repository has been lost. My goal is to create a new development code base using the deployed code as a source. So far I have removed a hidden .bundle folder and replaces several aliases with folders and files. I then ran bundle install. For the database I did a sql dump from staging and used it to build a development database. I think I'm ready to run rails server. But when I try to run rails server in the base directory. It gives me the rails command line help as if I was running rails s in a directory with no app.
I'm not even sure if it is possible to reverse a deploy this way. I've looked at the rails guide on the app initialization process and all the files seem to be in place.
Remember that for Rails to start up, you need bin/rails, bin/bundle, config/boot, etc.
If you restore those files, it should work again.

Rails 4.2 capistrano 3 deployment

I am totally new to rails deployment. After googling, I still find it hard to understand how to deploy rails apps.
So, my questions are:
After setting up the VPS with all rails dependencies, where do I store my codebase? The root directory of the VPS or some specific locations e.g. www/ or public/?
Should I upload the whole rails app folder or just part of it? I have paperclip in my rails app, and paperclip creates a system/ directory in the public/ folder, so should I upload system/?
In Capistrano 3, there is a repo_url field, I know they support file://, https://, ssh://, or svn+ssh://, but most of the articles about capistrano put github repositories into that. However, I do not have such a github repo. What should I input then?
Thank you for your attention.
Answers to the specific questions raised:
After setting up the VPS with all rails dependencies, where do I store
my codebase? The root directory of the VPS or some specific locations
e.g. www/ or public/?
It will be deployed to the folder pointed by :deploy_to parameter. If not specified, :deploy_to defaults to /var/www/#{fetch(:application) See: https://github.com/capistrano/capistrano/blob/05f63f5f333bb261f2a4c4497174361c48143252/lib/capistrano/defaults.rb#L3
Should I upload the whole rails app folder or just part of it? I have
paperclip in my rails app, and paperclip creates a system/ directory
in the public/ folder, so should I upload system/?
Paperclip system folder is specific to the environment; each environment (development, production,...) will have its own system folder which will store the files uploaded on that specific environment. This folder should not be part of the code being deployed.
The recommended way of handing such folders is the keep them in a shared folder on the server, and create symlinks in the current version of the code so that the same folder is used for storing/retrieving attachments. See Section 3. Update custom links section in http://robmclarty.com/blog/how-to-deploy-a-rails-4-app-with-git-and-capistrano for more details about this.
As mentioned there, the same applies to config/database.yml file, and any other file containing environment specific configurations.
In Capistrano 3, there is a repo_url field, I know they support
file://, https://, ssh://, or svn+ssh://, but most of the articles
about capistrano put github repositories into that. However, I do not
have such a github repo. What should I input then?
Depends on where the code you are deploying is stored. If it is in a local folder, use the file::// format to specify where the files are located.
You can set up your own private git server, then in deploy.rb you can put something like
set :repo_url, 'ssh://user#server_ip/path/to/your_git_repo.git'
When you commit your changes to the git repo, you do not have to upload the app to the server. Capistrano will upload the app for you when you deploy.
where do i put my code base? This is determined by what you put in deploy.rb e.g
set :deploy_to, '/path/to/my_codebase'
Whether to upload the /system directory will depend on whether you want the paperclip images on your version control. If not you can add the directory to gitignore. Here is a tutorial on how to deploy on ubuntu 14.04 passenger and NGINX. if you are not using Passenger and Nginx you can skip to how to configure capistrano and make adjustments depending on your setup.
EDIT
You need to install git on your development machine and set up a git server on your VPS as explained on the link above, add your remote server to your local machine using
git remote add origin <server>
where 'server' is the url to your git repo in the VPS e.g.
ssh://VPS_user#VPS_ip/path/to/your_git_repo.git
Now when you commit and push your changes to the server, capistrano will deploy the latest version on your git server.
Here is a link with a guide on how to get started with git

Setup VPS (Bluehost) to deploy and update RoR apps (Heroku Style)

My goal is to setup my environment to be able to run $ git push myapp master on my local computer AND automatically deploy/update my RoR app on my VPS at Bluehost.
On my computer (Linux-Ubuntu) the RoR app is at: /var/www/myapp
I followed this GREAT explained tutorial and I have a plenty functional git installation on my VPS server at ~/git/repository/myapp
Ideally, my deployed RoR app will be located on Bluehost at ~/rails_apps/myapp
Bluehost automatically uses Pushion Passenger, and there is a symlink between ~/public_html/myapp folder and the public side of the RoR app, located at ~/rails_apps/myapp/public.
I am stuck with the post-receive git hook and I am not using Capistrano (should I?)
I have been reading many documents with similar goals, like this one, or this other one, I even found this gem.
There are different ways to achieve same goal and I am still confused because I have not found a post-receive hook example that fits my need (and have not grasp the logic).
I have been on git guides specific section for hooks and it tells what you can do but not how to do it, which is great for everyone but a rookie.
Any help with the scenario and the goal? Will it be easier if I use Capistrano and/or git-rails gem? Other suggestions?
THANKS.
ADDITION:
Hi, I am using Capistano now but have not acheved the goal yet.
I have my app at /var/www/my_app on linux local machine.
The git repository remote --bare and --share is at user#myserver:/server/user/git/repository/my_app.
I want to deploy to /server/user/rails_apps/my_app.
At deploy.rb I have
set:application, "my_app".
set :respository, "user#myserver:/server/git/repository/my_app.
set:user, "my_user_name".
set: domain, "mysite.com".
set :scm, :git.
set_scm_command, "/server/user/bin/git".
set :scm_passphrase, "my_phrase".
set :deploy_to, "/server/user/rails_apps/my_app".
set :deploy_via, :remote_cache.
set :tmp_dir, "/server/user/tmp".
When I run it, tels me: fatal: No remote configured to list refs from.
Can you give me some guidance? Thanks.
Yes, The standard way is Capistrano or Mina and provisioning a rails environment in a hosted directory. If you are hell bent on doing it the heroku way you can use Vagrant to provision your own heroku environment here: https://github.com/openruko/vagrant-openruko

Rails 3.1 application deployment tutorial

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.
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.
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 www.example.com. 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.
Also the web server will host a single application and the database will run on the same machine.
From my knowledge the process of deploying a Rails application follows the following scenarios:
Installing Ruby
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:
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...)?
Would it make more sense to install as a separate user through RVM (can there any safety problems arise)?
Necessary gems
Now that Ruby is installed what would be the best initial set of gems to install along side it?
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.
Question - Should I install the rails gem before hand? Are there any other gems that need to be installed?
Web server
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).
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.)
Website configuration
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 default 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?
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...)
Database
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?
Assets
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.
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?).
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?
Thank you
Here's my way of deploying Rails:
Installing Ruby
I would not use RVM because it's very tricky to get it running properly in combination with stuff like cron jobs. Doable (with wrappers for example), but a bit of a hassle. If you don't need other Ruby versions on that machine, just install it from source yourself.
Gems
Just let Bundler work its magic. Remember to install with the flags --without test development --deployment. I wouldn't do that up front though. Just make sure you have bundler.
Web server
Using Passenger (with either Apache or Nginx) is a fine and easy choice. When you install Apache from apt, it will run in a special user.
Apache is configured correctly automatically on Ubuntu. It will start on reboot.
Website configuration
All configuration be in /etc/apache2. Place your virtual host configuration in /etc/apache2/sites-available and use a2ensite to enable it.
Put your app in /var/www, since that is the default location in Ubuntu. I usually make a deploy user.
Make sure that user can access your application by assigning your app to the www-data group.
Database
MySQL has its own user system. Log in as root user and create a new user with SQL: GRANT ALL ON 'application_production'.* TO 'deploy' IDENTIFIED BY 'some password';
Assets
Assets should be generated as the user owning the application. You should add any css and javascript files to production.rb. For example:
config.assets.precompile += %w(backend.css)
Conclusion
It helps to use a deploy tool like Capistrano. When you run capify, uncomment the appropriate line in the Capfile to get assets compilation. Here's mine:
ENV['RAILS_ENV'] = 'production'
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
load 'deploy/assets'
load 'config/deploy'
require 'capistrano/ext/multistage'
require "bundler/capistrano"
require 'capistrano_colors'
This is just how I normally install my rails apps. I hope this will get you going. Recently I've written a gem for integrating Chef-solo with Capistrano, called capistrano-chef-solo. It's very alpha and might be a bit too complicated if you're just starting with deployment, but it might help you.

Rails w/ HopToad -- Deploying?

I have a deployed Rails application that I'd like to use with Hoptoad (an online exception notification service). It comes packaged in a gem that I installed on my deployment server, but I need to run the script/generate hoptoad every time I deploy to configure it because the Hoptoad config files/changes exist only on my production application (and not on my development, so when I deploy, they all get erased).
Is there an easy way to run that script/generate on the production server every time I deploy? I feel like you could do this with Capistrano tasks, but I don't know how to do that.
What do people usually do for this? Sorry if this post is a little hard to understand, I'm not quite sure how to explain the issue.
Thanks!
You can add the Hoptoad configuration file to your source code repository so that it gets deployed as part of the Rails application.
Alternatively, if you don't want that information within your repository, then upload the config file to your production server (outside of Capistrano's normal releases directory structure) and have Capistrano create a symbolic link to it as part of the deployment process. I provided an answer to a similar question here which should give you the idea.

Resources