Posts Tagged ‘apache’

Install and Serve a Rails Application from PHP Subdirectory Using Apache, Phussion Passenger and Ruby Enterprise Edition

Posted 25 Feb 2010 — by admin
Category Ruby on Rails

Here is how to install a Rails application out of a subdirectory (rather than as a subdomain) with the Apache web server(Apache2). In this example I’m going to use my own blog which is a Wordpress installation and serve a Rails application from the subdirectory “reader”. Note, I’m not going to keep my Rails application in the document root of my Wordpress Blog, which is a PHP application and therefore anyone could browse the ruby source code :( . I’ll keep it elsewhere on the filesystem and tell Apache about the location in the VirtualHost file.

You can visit the application by going to http://seanbehan.com/reader. The application just parses a bunch of RSS feeds and displays them.
It uses the Feedzirra library, which I’ve also written about http://seanbehan.com/ruby-on-rails/installing-feedzirra-rss-parser-on-ubuntu-8/.

I’m using Phussion Passenger and Ruby Enterprise Edition from the folks at Mod Rails. Installing both Phussion Passenger and Ruby Enterprise Edition is simple and a very well documented process. However, you’ll need to download and compile them from source. If you install Ruby Enterprise Edition (REE) it comes w/ Passenger so you won’t need to download it separately. I recommend going with REE. http://www.rubyenterpriseedition.com/download.html

Installing Ruby Enterprise Edition

Here are the commands to download and install (change the X.X.X to the package you’ve actually downloaded).

wget http://rubyforge.org/frs/download.php/68719/ruby-enterprise-1.8.7-2010.01.tar.gz
tar xzvf ruby-enterprise-X.X.X.tar.gz
./ruby-enterprise-X.X.X/installer

When you run the installer you’ll be prompted for an installation location. Just hit enter to install in the default location. Follow the instructions from there and remember to copy/paste any code that they give you.

Official Instructions on installation for Passenger on its own are available here http://www.modrails.com/install.html I’ve written about setting up an entire box w/ Passenger here http://seanbehan.com/ruby-on-rails/new-ubuntu-slice-apache-mysql-php-ruby-on-rails-git-and/ If you already have Passenger installed and want to use REE just download and install REE and it’ll recompile Passenger with REE support if you follow the instructions.

*** If you install REE you’ll need to either link or reinstall all your gems. I linked the REE gem with the one in /usr/bin so that I can run gem install and REE will be aware of it.

ln -s /opt/ruby-enterprise-X.X.X/bin/gem /usr/bin/gem

The VirtualHost

If you have Passenger and REE successfully installed you’ll need to modify your VirtualHost file and add Rails application information to it.

<VirtualHost *>
  # Normal virtual host info
  ServerName seanbehan.com
  ServerAlias *.seanbehan.com
  DocumentRoot /var/www/seanbehan.com/wordpress

  # Rails info goes here
  Alias /reader /var/www/seanbehan.com/reader/public
  <Location /reader>
    PassengerAppRoot /var/www/seanbehan.com/reader
    RailsEnv production
  </Location>
</VirtualHost>

The “Location” directive tells apache to forward requests starting with /reader to the directory
/var/www/seanbehan.com/reader/public which is the location of our Rails app. However, we need to add the PassengerAppRoot assignment so that it knows where the actual application lives.

Routing in Rails with Relative Path

And finally, your Rails application will need to be aware of the relative url prefix assigned to each route. Normally, you could do this w/ the :path_prefix at the individual route level like so

map.resources :feeds, :path_prefix => "reader"

This will work but you can add a line to your RAILS_ROOT/config/environments/production.rb file which will handle all your routes for you. This way you don’t need to setup a relative path on your development environment work.

# in RAILS_ROOT/config/environments/production.rb
config.action_controller.relative_url_root = '/reader'

Final Thoughts

Remember Ruby Enterprise Edition is the new Ruby Interpreter that your Rails web applications are using. If you
have any gem or rake issues make sure that you’re using the same interpreter that REE is using. Look in the location
installation of REE “/opt/ruby-ent…” bin/gem or bin/rake and see if that helps. I just linked those to the standard /usr/bin/gem and /usr/bin/rake and everything worked fine.

Also I’ve read some people have trouble using the alias with passenger. This may be an older issue but works for me without a problem on Ubuntu (latest).

Here are some useful resources I found along the way…

http://robots.thoughtbot.com/post/159806388/phusion-passenger-with-a-prefix

http://www.modrails.com/documentation/Users%20guide.html#deploying_rails_to_sub_uri

http://www.modrails.com/documentation/Users%20guide.html#RailsBaseURI

http://stackoverflow.com/questions/848258/server-prefix-and-rails-routes

Mod_Python and Web.py on Ubuntu

Posted 09 Dec 2009 — by admin
Category Python

Download

First install mod_python for Apache and then restart/reload the server.

apt-get install libapache2-mod-python
/etc/init.d/apache2 force-reload
apache2ctl restart

Next grab the web.py framework from webpy.org. You can grab the tar or use easy_install depending on your setup.

wget http://webpy.org/static/web.py-0.33.tar.gz
tar xzvf web.py.tar.gz
cd web
sudo python setup.py install
# or if you use easy_install
easy_install web.py

Download this file http://www.aminus.net/browser/modpython_gateway.py?rev=106&format=raw This python package can (with this version) go anywhere in your python path “sys.path”. However, I placed it in the wsgiref directory. I’m running python2.6 so I did the following

# after downloading
mv modpython_gateway.py /usr/lib/python2.6/wsgiref/modpython_gateway.py

It’s important to remember that in the VirtualHost file you create the PythonHandler directive will need to reference this module. If you place it outside of the wsgiref directory remember to change the directive as well.

The VirtualHost

Now we need to create a VirtualHost for our application. You can run web.py apps without apache and this is good for development mode. To do this the command is

python pcode.py 4567

where the pcode.py is your web.py application filename and the 4567 is the port number you want to start the server on. I think it defaults to 8080. If you’re running apache or another web server port 80 will be in use. This is where mod_python (or cgi or fastcgi) come into play.

<VirtualHost *>
  ServerName py.seanbehan.com
  DocumentRoot /var/www/python/myapp

   #Aliases can trip you up! Pay attention here
   Alias /myapp /var/www/python/myapp
   <Directory /var/www/python/myapp>
      <IfModule python_module>
        PythonPath "sys.path +['/var/www/python/myapp']"
        AddHandler python-program .py
        # modpython_gateway is a file you'll need to download and place in wsgiref
        PythonHandler wsgiref.modpython_gateway::handler
        # This is your python program
        PythonOption wsgi.application pcode::main
        PythonOption SCRIPT_NAME /myapp
        PythonDebug on
      </IfModule>
    </Directory>
</VirtualHost>

Web.py Application

Web.py applications can be very simple. Everything can be placed in one file. In this example I created a file “pcode.py” and saved it to “/var/www/python/myapp/pcode.py”. The contents look like this

import web #this is the web.py frameword
web.webapi.internalerror = web.debugerror #lets capture errors for debugging

# map paths to classes
urls = (
    "/", "hello"
)

# this is the main from the PythonOption wsgi.application pcode::main from our VirtualHost
main = web.application(urls, globals()).wsgifunc()

class hello:
    def GET(self):
        #  web.header... otherwise you'll be prompted to save the file rather than view it in the browser
        web.header('Content-type','text/html')
        yield "Finally got web.py to work!"

if __name__ == "__main__":
    main.run()

Save the file, reload apache and visit the location on the web (In my case py.seanbehan.com/pcode.py/). Remember to include that last “/” after the pcode.py. You might get a “not found” message from web.py otherwise.

Pretty URLs

You can get pretty urls like “py.seanbehan.com/hello/world” if you use the mod_rewrite apache module.

a2enmod rewrite

You can either set up the rewrite rules in your VirtualHost or in a .htaccess file. I’ll do the .htaccess so I don’t have to reload the server for the changes to take effect.

In the same directory as your pcode.py file create a file “.htaccess” and in it place this code

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_URI} !^/icons
  RewriteCond %{REQUEST_URI} !^/favicon.ico$
  RewriteCond %{REQUEST_URI} !^(/.*)+code.py/
  RewriteRule ^(.*)$ pcode.py/$1 [PT]
</IfModule>

Troubleshooting

I ran into some trouble with the Alias in the VirtualHost file. It looks like this

Alias /myapp /var/www/python/myapp

This will map incoming urls to the full path on the filesystem so that your python program will run properly. I kept getting the not found error message from web.py without it. There might be a better way to set this up but this is how I got it to work.

Here are some additional resources I found helpful
http://wiki.slicehost.com/doku.php?id=install_mod_wsgi_on_ubuntu_gutsy

http://webpy.org/install
http://dready.org/blog/2009/01/29/webpy-with-mod_python-on-apache/
http://www.devisland.net/help/webpy.shtml

Fun with Apache2 – httpd not running, trying to start (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down

Posted 16 Oct 2009 — by admin
Category apache

Have you ever gotten this error message when trying to (re)start Apache

httpd not running, trying to start
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down

If you are running with an SSL certificate on your box, Apache cannot start up without the correct passphrase. I’ve run into this problem a couple times. Once when I rebuilt my virtual server from Mosso and the reboot process (obviously) required Apache to be started up when the machine came back up. Not being there when the start up phase required the correct passphrase Apache was up but not handling requests. The other time my server crashed when it ran out of memory. It was a 256 meg O’RAM box running a Rails app. Go figure!

Fortunately, there is an easy solution. Kill Apache and start it up again. If you’re in the console you’ll be prompted for the passphrase. Enter it correctly and everything will be fine. Here’s how I did it.

ps aux | grep apache
root      3948  0.0  0.2   4028   668 ?        S    17:48   0:00 /bin/sh -e /etc/rc2.d/S91apache2 start
root      3959  0.0  0.2   4028   616 ?        S    17:48   0:00 /bin/sh /usr/sbin/apache2ctl start
root      3964  0.0  2.4 133564  6472 ?        S    17:48   0:00 /usr/sbin/apache2 -k start
root      4069  0.0  0.2   6268   660 pts/0    R+   17:49   0:00 grep apache

I took at a look at the running processes and grep for apache. The one I wanted was the 2nd to last.
Look for the process id and kill it with the -9 option. The process id will be in the second column to the left

kill -9 3964

After it’s dead you can restart Apache like so

apache2ctl start

You’ll get the prompt for the passphrase. Enter it and everything should be fine.

Using sendmail to send mail on ubuntu box

Posted 07 Aug 2009 — by admin
Category Linux

I normally install postfix for my MTA. However, I’ve never really used sendmail so I’d decide to give it a whirl for a new application I’m working on. I don’t use it for anything but handling the mail that the application needs to send out, like new user welcome emails, password resets, etc.

apt-get install sendmail

Sendmail, unlike postfix, won’t work out of the box. Postfix will prompt you for the necessary config setup when running the install. Sendmail won’t, and therefore it’s not ‘out of the box’. You’ll have to make some modifications on your own. Nothing major but this is what I’ve found in order to get it to work, reliably and quickly. The first thing I did was add the fully qualified domain name to my /etc/hosts file

#vim /etc/hosts
127.0.0.1 www.mydomain.com

After this I added the fully qualified domain name to my apache default configuration file

#/etc/apache2/sites-available/default
ServerName www.mydomain.com
#vhost info etc...

Reload and restart…

/etc/init.d/apache2 force-reload
/etc/init.d/sendmail restart

You can test sendmail like so

sendmail email@example.com
hello
from
me
. 

This should deliver a message to you (the “.” on a new line, followed by a new line, closes the message).

Dot htaccess file for wordpress installation (.htaccess)

Posted 11 Jun 2009 — by admin
Category Programming

Login to your wordpress installation and scroll to and click the second to last link set “Settings”. You’ll need to configure wordpress to handle your new links as “permalinks”. Make a selection and copy the code into your .htaccess file. Wordpress will attempt to do this for you, but only if your webserver can read/write to the directory.

Permalink Settings

Permalink Settings

Permalink Settings

Permalink Settings

Add the .htaccess file to the root directory of your wordpress installation if wordpress informs you that it can’t get it to work. The file should be named “.htaccess” (w/out quotes).  This is a ‘hidden’ file so if you don’t see it, run the ls -lha command which will list all the files in the directory including hidden files.

Next you’ll want to edit your VirtualHost file. I used the default VirtualHost under /etc/apache2/sites-available/default You’ll need to make one modification to this file. You need to tell it to allow overrides, since we’ll be giving the .htaccess file the right to change configuration options. We need to change the line that says AllowOverrides None to AllowOverrides All

AllowOverrides All

AllowOverrides All