In today's rapidly evolving software landscape, Platform as a Service (PaaS) offerings like Heroku have gained immense popularity for their convenience and ease of use. They allow developers to deploy applications without worrying about the intricacies of server management. However, this convenience often comes at a hefty price, making it a less attractive option for developers and small businesses operating on a tight budget.
For those who prefer to maintain complete control over their application stack and want to avoid recurring PaaS costs, hosting a Rails app on a dedicated server remains a viable and cost-effective solution. Yet, finding reliable resources that guide you through the manual setup process can be daunting. This guide is designed for users who are ready to roll up their sleeves and dive into the intricacies of server management, bypassing the need for pricey service fees that come with PaaS. If you're someone who isn't afraid of getting your hands dirty and want to deploy a Rails app manually using Nginx and Phusion Passenger on Ubuntu 20.04, this detailed walkthrough is just what you need.
Step-by-Step Guide to Host Your Rails App on Ubuntu
1. Update System Packages
Start by ensuring your system is up-to-date. This is crucial for security and stability.
sudo apt update
sudo apt upgrade -y
2. Install RVM and Ruby
Ruby Version Manager (RVM) is essential for managing Ruby installations and gemsets efficiently.
sudo apt install curl gpg
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install ruby
rvm use ruby --default
3. Install Rails
Once Ruby is installed, proceed to install Rails, the framework used to build your application.
gem install rails
4. Install Node.js and Yarn
Rails requires a JavaScript runtime like Node.js for its asset pipeline, and Yarn helps in managing JavaScript dependencies.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs
npm install --global yarn
5. Install MySQL (or Your Preferred Database)
Set up the database system of your choice; MySQL is used here for demonstration.
sudo apt install mysql-server libmysqlclient-dev
6. Install and Configure Nginx
Nginx will serve your Rails application to the web, providing fast and efficient delivery.
sudo apt install nginx
7. Install Passenger
Phusion Passenger is an application server that integrates seamlessly with Nginx to handle Ruby applications.
sudo apt install dirmngr gnupg
sudo apt-key adv --keyserver hkps.pool.sks-keyservers.net --recv-keys 95C1DD26C60FA986 4B2C1C7E1088DA93
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger focal main > /etc/apt/sources.list.d/passenger.list'
sudo apt update
sudo apt install libnginx-mod-http-passenger
Configuring Nginx and Passenger
Edit your Nginx configuration to enable Passenger:
- Open Nginx's main configuration file
/etc/nginx/nginx.conf
and uncomment the following line:
include /etc/nginx/passenger.conf;
- Create a server block for your Rails application in
/etc/nginx/sites-available/your_app_name.conf
:
server {
listen 80;
server_name your_domain_or_IP;
root /path/to/your/app/public;
passenger_enabled on;
passenger_ruby /usr/share/rvm/rubies/ruby-x.y.z/bin/ruby; # Update to your Ruby version
}
- Enable the site by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/your_app_name.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
SSL with Certbot
Secure your site with SSL using Certbot, which automates the process of obtaining and renewing SSL certificates.
sudo apt-get install python3-certbot-nginx
sudo certbot --nginx
Conclusion
By following these steps, you bypass the high costs associated with PaaS solutions and gain complete control over your Rails application's deployment. This approach might require a bit more effort, but the freedom and cost savings are well worth it. If full ownership of your server environment sounds appealing, this guide empowers you to host your Rails app with confidence. Enjoy the flexibility and financial freedom that come with self-hosting your applications!``
Top comments (0)