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:

<br />
sudo apt-get update<br />
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<br />
mkdir ~/tempruby<br />
cd ~/tempruby<br />
wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.4.tar.gz<br />
tar -xzf ruby-2.1.4.tar.gz<br />
cd ruby-2.1.4<br />
./configure<br />
make<br />
sudo make install<br />
rm -rf ~/tempruby<br />

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

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

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)

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

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

<br />
&lt;VirtualHost *:80&gt;<br />
    ServerName yourservermainhostname<br />
    ServerAlias www.yourubywebsitedomain.com<br />
    ServerAdmin [email protected]<br />
    DocumentRoot /home/yourname/ror/mywebapp<br />
    RailsEnv development<br />
    ErrorLog ${APACHE_LOG_DIR}/error.log<br />
    CustomLog ${APACHE_LOG_DIR}/access.log combined<br />
    &lt;Directory &quot;/home/yourname/ror/mywebapp&quot;&gt;<br />
        Options FollowSymLinks<br />
        Require all granted<br />
    &lt;/Directory&gt;<br />
&lt;/VirtualHost&gt;<br />

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

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

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

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

%d bloggers like this: