Total Blog Views: 67
Blog Status: publish
Created By: swaz_ahmed Created at: 07-17-2024
Tags:
Deploying a Rails App on Apache using EC2 Instance
Introduction
Deploying a Rails application can seem daunting at first, but with the right tools and guidance, it becomes manageable. In this guide, I'll walk you through the steps to deploy a Rails application on an Apache server using an EC2 instance.
Prerequisites
Before we start, ensure you have the following:
Step 1: Setting Up the EC2 Instance
1.1 Launch an EC2 Instance
Log in to your AWS Management Console.
Navigate to the EC2 Dashboard.
Click on "Launch Instance".
Choose an Amazon Machine Image (AMI). For this tutorial, we will use the Ubuntu Server 20.04 LTS(or latest).
Choose an instance type. The free tier eligible t2.micro instance is suitable for small applications.
Configure instance details (default settings are fine).
Add storage (default settings are fine).
Add tags (optional).
Configure the security group. Ensure you have rules to allow HTTP, HTTPS, and SSH traffic.
1.2 Configuring Security Inbound Rules
To allow traffic to your EC2 instance, you need to set up security inbound rules:
In the EC2 Dashboard, select "Security Groups" from the left-hand menu.
Find the security group associated with your instance and click on its ID.
Click on the "Inbound rules" tab and then "Edit inbound rules".
Add the following rules:
Click "Save rules" to apply the changes.
Note: When you will launch the EC2 instance at that time you will download a .pim file, we need that pim file for further process.
1.3 Connect to Your EC2 Instance
Once your instance is running, connect to it using SSH:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-dns
Step 2: Installing Dependencies
2.1 Update the Package List
sudo apt-get update
2.2 Install Ruby and Rails
sudo apt-get install -y curl gnupg build-essential curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt-get install -y nodejs curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update sudo apt-get install -y yarn gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \curl -sSL https://get.rvm.io | bash -s stable source /home/ubuntu/.rvm/scripts/rvm rvm install 3.1.2 rvm use 3.1.2 --default gem install rails -v 7.0.4
2.3 Install PostgreSQL
sudo apt-get install -y postgresql postgresql-contrib libpq-dev sudo -i -u postgres createuser --interactive
Follow the prompts to create a new PostgreSQL user.
createdb your_database_name exit
Step 3: Setting Up the Rails Application
3.1 Clone Your Rails Application
git clone https://github.com/your_username/your_rails_app.git cd your_rails_app
3.2 Configure Database
Edit the 'config/database.yml'
file with your database settings:
default: &default adapter: postgresql encoding: unicode pool: 5 username: your_postgres_user password: your_postgres_password host: localhost development: <<: *default database: your_database_name_development test: <<: *default database: your_database_name_test production: <<: *default database: your_database_name_production username: your_production_user password: <%= ENV['YOUR_DATABASE_PASSWORD'] %>
3.3 Install Gems and Set Up the Database
bundle install rails db:create db:migrate
Step 4: Setting Up Apache and Passenger
4.1 Install Apache and Passenger
sudo apt-get install -y apache2
4.2 Install Passenger
First, install the PGP key for the repository server:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
Create an APT source file:
sudo nano /etc/apt/sources.list.d/passenger.list
Insert the following line to add the Passenger repository to the file:
deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main
Press CTRL+X to exit, type Y to save the file, and then press ENTER to confirm the file location.
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
Update the APT cache:
sudo apt-get update
Finally, install Passenger:
sudo apt-get install libapache2-mod-passenger
Make sure the Passenger Apache module; it maybe enabled already:
sudo a2enmod passenger
4.3 Configure Apache for Your Rails Application
Create a new virtual host configuration file:
sudo nano /etc/apache2/sites-available/your_rails_app.conf
Add the following content to the file:
<VirtualHost *:80> ServerName your_domain_or_ip DocumentRoot /home/ubuntu/your_rails_app/public <Directory /home/ubuntu/your_rails_app/public> AllowOverride all Options -MultiViews Require all granted </Directory> PassengerRuby /home/ubuntu/.rvm/gems/ruby-3.1.2/wrappers/ruby </VirtualHost>
4.4 Enable the Virtual Host and Restart Apache
sudo a2ensite your_rails_app.conf sudo systemctl restart apache2
Step 5: Final Steps
5.1 Precompile Assets (for Production)
RAILS_ENV=production bundle exec rake assets:precompile
Ensure your application is started and running correctly. You can check the public IP address of EC2 instance on browser. AND HERE YOUR APP IS DEPLOYED!!
we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link