Report this

What is the reason for this report?

How to install Ruby on Rails onto an existing Apache server?

Posted on July 9, 2019

Hi, So I’ve had a webserver with Ubuntu and Apache on the LAMP stack for about a year. It’s mainly just a portfolio website.

I’ve just made an API using Ruby on Rails that is on my local machine and I’ve read up that you can host multiple different applications on the same droplet.

The idea is to make a subdomain such as www.api.Example.com My existing website I have is www.Example.com With this subdomain I wish to host my Ruby on Rails API, it uses PHP myadmin which I think I’ll have to install on the LAMP stack? And it also has gems to install such as mysql2.

My question is how do I do this?

I’ve looked at so many tutorials but they’re all saying a different thing and mentioning Nginx and I don’t know how to get a subdomain. I bought my domain from Google so do I do it on Google Domains or do I do it on Digital Ocean?

So altogether I’m asking how to… Make a subdomain such as www.api.Example.com? Do I install PHPmyadmin on the main Apache bit and will it be on both my subdomain and main domain? Is it possible to have a Ruby On Rails app and an existing Apache web server working concurrently? How do I move my API from my PC over? Is it simply a case of copying over files? (To the Sites folder I’m guessing?) How would I start it? (The usual rails s? or is it slightly different cause of the sub domain? MySQL is already installed through the LAMP stack so I can just forget about it right?

Is that everything? It’s been a good 7 months since I actually went on my server terminal so I’m probs a bit rusty with the commands.

Thanks in advance, I know I’m pretty bad at explaining myself but I think I did an alright job



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Yes, all the above is possible, you can run Ruby on Rails onto an existing Apache server and you don’t need Nginx server. Here’s a guide on how to set that all up:

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-passenger-and-apache-on-ubuntu-14-04

Before you start with the guide you can point your subdomain name to your Droplet’s IP address. If you are using Google’s namesevers you need to do that via your Google account, if you are using DigitalOcean’s nameservers you need to make this DNS change via your Digital Ocean domain control panel.

Also before you proceed with the rails setup make sure to backup your droplet, in case that anything goes wrong, you can simply revert to that backup.

Regarding the PHPmyAdmin installation you can follow the steps here on how to install PHPmyAdmin manually with an existing Apache server, start from step Step 3:

https://websiteforstudents.com/install-phpmyadmin-latest-version-on-ubuntu-16-04-18-04-with-apache2-mariadb-and-php-7-2/

Hope that this helps! Let me know if you need any help!

Bobby

Hosting multiple applications on a single DigitalOcean droplet, including setting up a Ruby on Rails API alongside an existing Apache web server, involves several steps. You’ve outlined a good series of questions, so let’s tackle them one by one.

1. Creating a Subdomain

The subdomain www.api.example.com should be set up through your DNS provider, which in your case is Google Domains since that’s where you bought your domain.

  • Log in to Google Domains.
  • Find your domain and look for the DNS settings.
  • Add a DNS record: You’ll typically add an A record pointing to your droplet’s IP address. The host name should be www.api if you want it to be www.api.example.com.

2. Configuring the Server

Since you already have Apache installed, you can continue to use it, or you can opt to install Nginx, which is often favored for Ruby on Rails applications due to its capability to handle concurrent connections more efficiently. However, for simplicity, you can stick with Apache.

  • Virtual Hosts: You’ll need to set up a new virtual host in Apache for your subdomain.
    • Edit Apache configuration files typically located in /etc/apache2/sites-available/.

    • You can copy the existing site configuration file and modify it for your API.

    • Example snippet for your virtual host:

<VirtualHost *:80>
    ServerName www.api.example.com
    DocumentRoot /var/www/api/public

    <Directory /var/www/api/public>
        Allow from all
        Options -MultiViews
        Require all granted
    </Directory>
</VirtualHost>
    • Enable the site using a2ensite www.api.example.com.conf and reload Apache service apache2 reload.

3. Ruby on Rails Setup

  • Install Ruby, Rails, and necessary dependencies on your droplet if not already installed. This includes installing Ruby using RVM or rbenv, then Rails, and other gems.
  • Database Configuration: If you’re using MySQL, ensure your Rails application’s database.yml is configured to use it. Since MySQL is part of your LAMP stack, you can use the same MySQL server, but you might consider creating a separate database for your API.
  • Migrate your Rails API to the server. You can use SCP or FTP to transfer files from your local machine to the server. Place them in something like /var/www/api.
  • Precompile Assets and run migrations:
    • rails assets:precompile
    • rails db:migrate RAILS_ENV=production

4. Starting the Rails Server

Instead of using rails s, which is typically for development, you should set up a proper application server such as Puma or Passenger which can be integrated into Apache.

  • Install Passenger:
    • sudo apt-get install libapache2-mod-passenger
    • sudo a2enmod passenger
    • Restart Apache.
  • Configure Passenger to run your Rails app in the virtual host configuration.

5. PHPMyAdmin

You can install PHPMyAdmin on the Apache server, and it will be accessible from any domain hosted on that server, provided you configure it securely. It can be accessed via a URL like example.com/phpmyadmin or api.example.com/phpmyadmin.

Final Considerations

  • Security: Ensure that your API is secured, particularly if it’s accessible from the internet. This includes setting up HTTPS, which can be done using Let’s Encrypt for free SSL certificates.
  • Testing: Make sure to thoroughly test your API on the new server before switching the DNS to point to it for public traffic.

By following these steps, you should be able to successfully deploy your Ruby on Rails API on your existing server without affecting your current Apache setup for your portfolio website.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.