Host Ruby on Rails website in Apache – PHP configured server

What if you need to migrate existing or new created Ruby on Rails web application to your PHP-Apache configured server?

If you are not using VPS or dedicated server and you have simple shared php web hosting – of course this article is not for you.

If you are in localhost, so everything is clear, you can use different webservers, different ports, you can damage your webserver, then reinstall it etc. But when you in production server, you can’t do such risks and you can’t play with webserver settings, ports.

So we need the way safer.

If you know what ssh, linux server are, let’s get started.

This quick tutorial will show how to do that in Debian based Linux systems, but it can also be implemented in another Linux distros.
Open your terminal(or putty), type commands below:

sudo apt-get update
sudo apt-get install build-essential libssl-dev libyaml-dev libreadline-dev openssl curl git-core zlib1g-dev bison libxml2-dev libxslt1-dev libcurl4-openssl-dev libsqlite3-dev sqlite3
mkdir ~/tempruby
cd ~/tempruby
wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.4.tar.gz
tar -xzf ruby-2.1.4.tar.gz
cd ruby-2.1.4
./configure
make
sudo make install
rm -rf ~/tempruby

It is done with ruby. Now let’s start the module which will link our ruby to existing Apache.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
sudo nano /etc/apt/sources.list.d/passenger.list
deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main
sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
sudo apt-get update
sudo apt-get install libapache2-mod-passenger
sudo a2enmod passenger
sudo service apache2 restart
sudo rm /usr/bin/ruby
sudo ln -s /usr/local/bin/ruby /usr/bin/ruby
cd ~
sudo gem install --no-rdoc --no-ri rails

Now just copy paste your existing ROR web app to any directory. (or you can create new app with “rails new testapp –skip-bundle” command)
Let’s say that you have pasted your web app to /home/yourname/ror/mywebapp directory. Then do the following (don’t forget to change directory and app name in below code)

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mywebapp.conf
sudo nano /etc/apache2/sites-available/mywebapp.conf

Terminal editor wil be opened. You need to edit copied config and to get such result.

<VirtualHost *:80>
    ServerName yourservermainhostname
    ServerAlias www.yourubywebsitedomain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /home/yourname/ror/mywebapp
    RailsEnv development
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory "/home/yourname/ror/mywebapp">
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

Press ctrl+o and Enter. It will be saved. Exit with ctrl+x. Then enable new created website with the following commands.

sudo a2ensite mywebapp
cd /home/yourname/ror/mywebapp
bundle install
sudo service apache2 restart

That’s all. Now all your PHP and RoR websites should work together in the same machine and under the same port.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.