How To Install Rails, Apache, and MySQL on Ubuntu with Passenger
Welcome to the Greenhost.Cloud blog! In today’s post, we’ll walk you through the steps to install Ruby on Rails, Apache, and MySQL on Ubuntu, paired with Passenger as the application server to serve your Rails applications. Whether you’re building a new project or deploying an existing one, this guide will help you set up your environment efficiently.
Prerequisites
Before you begin, ensure you have the following:
- An Ubuntu server (18.04 or later).
- A non-root user with sudo privileges.
- Basic understanding of command-line operations.
Let’s get started!
Step 1: Update Your System
First, make sure that your server’s package list is up-to-date. Log in to your server and run:
sudo apt update
sudo apt upgrade -y
Step 2: Install Required Packages
To install Ruby on Rails, we need a few dependencies. Install them with the following command:
sudo apt install -y curl gnupg build-essential libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt1-dev libcurl4-openssl-dev libffi-dev libmysqlclient-dev
Next, we’ll install Node.js and Yarn, as they are essential for JavaScript in Rails applications.
# Install Node.js
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs
# Install Yarn
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 update && sudo apt install -y yarn
Step 3: Install RVM and Ruby
RVM (Ruby Version Manager) enables you to manage multiple Ruby environments. To install RVM and Ruby, use the following commands:
# Install RVM
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
# Install Ruby
rvm install 3.0.2 # You can choose your desired Ruby version
rvm use 3.0.2 --default
# Verify the installation
ruby -v
Next, install Rails:
gem install rails
Step 4: Install MySQL
Now we need to install MySQL database server. Run:
sudo apt install -y mysql-server
After installation, secure your MySQL installation:
sudo mysql_secure_installation
Follow the prompts. You can set a password for the MySQL root user and configure other security settings.
Step 5: Install Apache and Passenger
We’ll use Apache as the web server and Passenger as the application server.
Install Apache
sudo apt install -y apache2
Install Passenger
To install Passenger, we need to add its PPA:
sudo apt install -y dirmngr gnupg
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo add-apt-repository "deb https://oss-binaries.phusionpassenger.com/apt/passenger/ $(lsb_release -cs) main"
sudo apt update
Then, install Passenger with Apache support:
sudo apt install -y libapache2-mod-passenger
Enabling the Passenger module:
sudo a2enmod passenger
Step 6: Configure Your Rails Application
Let’s create a sample Rails application to test the setup:
rails new myapp -d mysql
cd myapp
Edit the config/database.yml
file to configure the database connection:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: root
password: your_password
host: localhost
Replace your_password
with the password you set during MySQL secure installation.
Create and migrate the database:
rails db:create
rails db:migrate
Step 7: Configure Apache for Your Application
Create a new configuration file for your Rails app in Apache:
sudo nano /etc/apache2/sites-available/myapp.conf
Add the following configuration to the file:
<VirtualHost *:80>
ServerName your_domain_or_ip
DocumentRoot /path/to/myapp/public
<Directory /path/to/myapp/public>
Allow from all
Options -MultiViews
Require all granted
RailsEnv development
</Directory>
PassengerRuby /home/your_user/.rvm/gems/ruby-3.0.2/wrappers/ruby
</VirtualHost>
Make sure to change /path/to/myapp
to the actual path of your Rails application and your_domain_or_ip
to your domain or IP address.
Enable the new site and restart Apache
sudo a2ensite myapp
sudo systemctl restart apache2
Step 8: Access Your Application
Now, your Rails application should be accessible via your web browser. Open a browser and navigate to http://your_domain_or_ip
. You should see the Rails welcome page!
Conclusion
Congratulations! You’ve successfully set up a Ruby on Rails environment on Ubuntu using Apache and MySQL with Passenger. This is just the beginning; you can now start developing your application, implement functionalities, and deploy it!
Happy coding! 🚀