项目作者: dereknguyen269

项目描述 :
Deploy Your Rails App to AWS :rocket: :rocket: :rocket:
高级语言:
项目地址: git://github.com/dereknguyen269/Deploy_Your_Rails_App_To_AWS.git


Deploy Your Rails App To AWS

As developers, we are usually concerned about the development part of any application. We don’t think much about the deployment part as we consider it to be a responsibility of the SysAdmins. But many times, we don’t have a dedicated SysAdmin available, so we have to put on the SysAdmin hat and get things done. There are many options to deploy your Rails application. Today, I will cover how to deploy a Rails application to Amazon Web Services (AWS).

1. Deploy Your Rails App To AWS With Passenger and Apache on Ubuntu.

Connect your AWS Instance via Terminal

  • First type :

sudo apt-get update && sudo apt-get -y upgrade

  • Install git :

sudo apt-get install git

  • Install Ruby via RVM:
    ```
    gpg —keyserver hkp://keys.gnupg.net —recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    \curl -sSL https://get.rvm.io | bash
    source ~/.rvm/scripts/rvm

rvm install 2.4.0
rvm list
rvm —default use ruby-2.4.0

  1. - Install Nodejs:

sudo apt-get install nodejs

  1. - Create your deploy foler:

cd /home/ubuntu
sudo mkdir deploy
sudo chown -R ubuntu:ubuntu deploy
cd deploy & clone your project from SVN.

  1. - Config your rails app:

gem install bundler
bundle install

  1. - Test your rails app :

rails s -p 3001 -b 0.0.0.0

  1. Your app will run : your_ip:3001
  2. - Install apache :

sudo apt-get install apache2

  1. - Install Passenger :
  2. First, install the PGP key for the repository server:
  3. `sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7`
  4. Create an APT source file:
  5. `sudo nano /etc/apt/sources.list.d/passenger.list`
  6. Insert the following line to add the Passenger repository to the file:
  7. `deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main`
  8. Change the owner and permissions for this file to restrict access to root:

sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list

  1. Update the APT cache:
  2. `sudo apt-get update`
  3. Finally, install Passenger:
  4. `sudo apt-get install libapache2-mod-passenger`
  5. Make sure the Passenger Apache module; it maybe enabled already:
  6. `sudo a2enmod passenger`
  7. Restart Apache:
  8. `sudo service apache2 restart`
  9. Now, we need to create a virtual host file for our project. We'll do this by copying the default Apache virtual host:
  10. `sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/testapp.conf`
  11. Open the config file:
  12. `sudo nano /etc/apache2/sites-available/testapp.conf`
  13. Edit it or replace the existing contents so your final result matches the file shown below. Remember to use your own domain name, and the correct path to your Rails app:


ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /home/rails/testapp/public
RailsEnv development
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Options FollowSymLinks
Require all granted

  1. Basically, this file enables listening to our domain name on port 80, sets an alias for the www subdomain, sets the mail address of our server administrator, sets the root directory for the public directory of our new project, and allows access to our site. You can learn more about Apache virtual hosts by following the link.
  2. To test our setup, we want to see the Rails Welcome aboard page. However, this works only if the application is started in the development environment. Passenger starts the application in the production environment by default, so we need to change this with the RailsEnv option. If your app is ready for production you'll want to leave this setting out.
  3. If you don't want to assign your domain to this app, you can skip the ServerName and ServerAlias lines, or use your IP address.
  4. Disable the default site, enable your new site, and restart Apache:

sudo a2dissite 000-default
sudo a2ensite testapp
sudo service apache2 restart

  1. Now your app's website should be accessible.
  2. `http://your-instance-ip`
  3. If you want using port `81` for your applicaiton. You need add
  4. `Listen 81`
  5. at `/etc/apache2/ports.conf`
  6. `Listen xx` where xx is a valid port number
  7. Add/Update Virtual Host
  8. - Create a virtual host that listens to 81 or the port number that Apache is listening to.
  9. - Enable the site with `sudo a2ensite name of the conf without .conf`
  10. - Restart the apache `sudo service apache2 restart`
  11. Note : Your Security Group of Instance need config same as :
  12. ![](https://github.com/minhquan4080/Deploy_Your_Rails_App_To_AWS/blob/master/Screen%20Shot%202017-02-04%20at%2015.59.20.png?raw=true)
  13. To update Passenger and Apache, you will need to run a basic system update:
  14. `sudo apt-get update && sudo apt-get upgrade`
  15. After the update process, you will need to restart the web server:
  16. `sudo service apache2 restart`
  17. ## Login with user & password:
  18. - Add new user:

sudo adduser USERNAME

  1. If user has been exist:
  2. `sudo passwd USERNAME`
  3. Enable password authentication by `editing /etc/ssh/sshd_config`: change `PasswordAuthentication no` to `PasswordAuthentication yes`
  4. Use command as **root user**
  5. By default, a new user is only in their own group, which is created at the time of account creation, and shares a name with the user. In order to add the user to a new group, we can use the usermod command:

usermod -aG sudo USERNAME

  1. Open:

sudo vi /etc/sudoers

  1. and add:

USERNAME ALL=(ALL:ALL) ALL

  1. `sudo /etc/init.d/ssh restart`

$ ssh USERNAME@ec2-__.compute-1.amazonaws.com
USERNAME@ec2-__.compute-1.amazonaws.com’s password:
```

Add your domain with instance

We will use Cloudflare

  1. After you register and login your account, add site with your domain, config DNS. We use public ip of instance : `52.90.195.71` :