Skip to content

Latest commit

 

History

History
191 lines (141 loc) · 4.78 KB

deploying_to_AWS.md

File metadata and controls

191 lines (141 loc) · 4.78 KB

AWS EC2

Working with an instance from its creation to a full-fledged working application. Phusionpassenger guide.

  1. Create instance with ubuntu (Free tier eligible)
  2. Generate PEW key to connecting to them
  3. Download this key
  4. Change file access using chmod
chmod 600 ~/Downloads/pemfile.pem
  1. Import it into your SSH store
ssh-add /path/to/pemfile.pem

Connect to your instance

ssh [email protected]
#or
ssh -i default.pew [email protected]
  1. Install rvm, ruby, nginx, nodejs, redis
sudo apt-get install software-properties-common
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt-get update
sudo apt-get install rvm
echo `source "/etc/profile.d/rvm.sh"` >> ~/.bashrc
rvm install ruby-2.5.5

sudo apt-get update
sudo apt-get install -y nginx
sudo service nginx restart

sudo apt install redis-server
sudo systemctl status redis # testing redis-server

# fix rvm/bin path
echo $rvm_bin_path
mkdir ~/.rvm/bin
ln -s /usr/share/rvm/bin/rvm .rvm/bin/rvm

Create AWS S3 Bitbucket

Create new bitbucket in your AWS Simple Storage Service Create access to this services

region and bucket name you can find in S3 -> Buckets -> Name of new bucket -> Access point Change file storage.yml to:

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.aws[:bucket_access_key_id] %>
  secret_access_key: <%= Rails.application.credentials.aws[:bucket_secret_access_key]  %>
  region: 'us-east-2'
  bucket: 'projector-kv065'

Go to Permissions -> CORS configuration and past

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>http://youre_ip_addres</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

Create AWS SES

Create new Simple Email Service Create new access key to enable access using IAM

Test your configuration inside you rails c

ses = AWS::SES::Base.new( :access_key_id => 'abc', :secret_access_key => '123',
:server => 'email.eu-west-1.amazonaws.com', :message_id_domain => 'eu-west-1.amazonses.com')

ses = AWS::SES::Base.new( :access_key_id => 'AKIATTTHFGKFIDRMFZPR', :secret_access_key => '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', :server => 'email.us-east-2.amazonaws.com', :message_id_domain => 'us-east-2.amazonaws.com' )

ses.send_email( :to => ['[email protected]'], :source => '[email protected]', :subject => 'Subject Line', :text_body => 'Internal text body')

You can find you domain inside information about domain( if you have one), or email address

'SocketError: Failed to open TCP connection to email.us-east-2.amazonses.com:443' Enable 433 port at the EC2 server > Security > Click to unique name of Security group.

Run capistrano

cap production deploy

Reboot Ubuntu

sudo reboot
# or
/sbin/reboot

Test or reboot Passenger

passenger-status
sudo passenger-status --show=requests
sudo passenger-config restart-app
# error messages if start doesn't work is in folder tmp

Passenger write all requests and errors in the file production.log

tail -f /var/www/yourappname/current/log/staging.log

Edit Nginx configuration file

cd /etc/nginx/sites-enabled/
sudo vim projector.conf
# put this inside the file:
server {
    listen 80;
    #listen [::]:80 ip_address;
    server_name default_server;
    #listen 80 default_server;
    #listen [::]:80 default_server;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /home/ubuntu/deploy/projector/production/current;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /home/ubuntu/.rvm/gems/ruby-2.5.5/wrappers/ruby;
}

# copy settings
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

# Then restart Nginx
sudo service nginx restart

Create folders for capistrano

mkdir /home/ubuntu/projector/deploy/production
cd /home/ubuntu/projector/deploy/production
mkdir shared releases

Copying master.key and database.yml files manually

scp ~/Documents/projector/config/master.key ubuntu@ip_address:/deploy/projector/production/shared/config

scp ~/Documents/projector/config/database.yml ubuntu@ip_address:/deploy/projector/production/shared/config

Handle Sidekiq error

Sidekiq write log in the file

tail -f /var/www/yourappname/current/log/sidekiq.log

Stop sidekiq

cd ~/deploy/projector/production/current
bundle exec sidekiqctl stop tmp/pids/sidekiq-0.pid 0