Posted by Ben Reubenstein
Sat, 23 Aug 2008 03:59:00 GMT
I have been using the magic of Rails for a lot lately but some of those apps are starting to take off and get slow. It isn't Rails however it is MySQL queries to big tables. You don't have to be a MySQL rocket scientist to really make a huge difference. Want to improve queries to your big tables? Add indexes for the columns you use most in your where clause.
1. Turn on slow query logging in your my.cnf (most likely in /etc/mysql/my.cnf). Most like you will just have to uncomment this line:
log_slow_queries = /var/log/mysql/mysql-slow.log
2. Examine your logs and then use the mysql
EXPLAIN query to see why it is performing slowly. This query needs to look at almost the ENTIRE table to find a result.
mysql> EXPLAIN select * from network_tests where user_id = 1;
+----+-------------+---------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | network_tests | ALL | NULL | NULL | NULL | NULL | 264559 | Using where |
+----+-------------+---------------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)
3. Add indexes to the columns that you are querying on most:
mysql> ALTER TABLE network_tests ADD INDEX(user_id,test_key);
Query OK, 285075 rows affected (1 min 7.14 sec)
Records: 285075 Duplicates: 0 Warnings: 0
4. Run explain again, see how many rows now need to be checked for this query? VERY NICE!.
mysql> EXPLAIN select * from network_tests where user_id = 1;
+----+-------------+---------------+------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+---------+---------+-------+------+-------------+
| 1 | SIMPLE | network_tests | ref | user_id | user_id | 5 | const | 118 | Using where |
+----+-------------+---------------+------+---------------+---------+---------+-------+------+-------------+
1 row in set (0.00 sec)
Tags indexes, mysql, optimizations | no comments
Posted by Ben Reubenstein
Sun, 10 Aug 2008 16:39:00 GMT
After my initial surfing on my iPhone for Olympic medal totals, I found no immediate results for an optimized mobile experience. I wrote up a quick site after which I immediately found NBC's mobile site. Mine is super simple, and can be found at iPhone Olympic Medal Counts. CHEERS.
Tags iphone, medals, olympics, rails, ruby | 1 comment
Posted by Ben Reubenstein
Tue, 24 Jun 2008 04:49:00 GMT

May 12, 1937 – June 22, 2008
no comments
Posted by Ben Reubenstein
Tue, 10 Jun 2008 01:47:00 GMT
After almost a year of collecting data and half a million results recorded,
Xcellent Creations, Inc. has released the next version of the iNetwork Test web application. The iNetwork Test web application for iPhone has been moved to
http://www.inetworktest.com/iphone_content. The main
iNetworkTest.com is now a portal to
Mobile Network Testing Results.
User's can now create an account which allows them to save all of their results and create a personal results map. This build out will also support new versions for the
Android platform and the
iPhone.
Posted in Ruby on Rails | Tags apple, iphone, network, speed, testing | no comments
Posted by Ben Reubenstein
Fri, 02 May 2008 00:24:00 GMT
If you are reading this you are hitting the new server. I separated my sites out into two servers, one running a LAMP stack and the other running a LNMR (Linux Nginx MySQL Rails) stack.
Tags announcement, server | no comments
Posted by Ben Reubenstein
Sun, 13 Apr 2008 21:06:00 GMT
After getting passenger up and running I realized that I didn't have PHP cranking with the freshly compiled Apache. Here are the steps I took to get PHP going:
Compile PHP from source, be sure to check out ./configure --help for more compile options. Also as always adjust to your systems paths as necessary.
wget http://www.php.net/get/php-5.2.5.tar.gz/from/this/mirror
tar -zxvf php-5.2.5.tar.gz
cd php-5.2.5
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-curl
make
sudo make install
I am not sure why but the php bin file ends up being called php.dSYM. There might be a compile option I missed to fix this or it could be the Mac OS X compiler. I fixed the issue by creating a symlink.
sudo ln -s /usr/local/php/bin/php.dSYM /usr/local/php/bin/php
Add /usr/local/php/bin to your path in .bash_login and reload your terminal so that it is in your path. You can confirm that you are getting the right php by issuing:
which php
#The command should return:
/usr/local/php/bin/php
Add directives to the httpd.conf for Apache to find the PHP module and handle .php files
LoadModule php5_module modules/libphp5.so
AddHandler php5-script php
Restart Apache
/usr/local/apache2/bin/apachectl restart
Put a file in the root of your server or in virtual host directory called info.php with the phpinfo command. Visit the file via your web browser to confirm your install.
<?php phpinfo() ?>
Be sure to leave comments with any issues you have, or updates I can make to this document.
Tags apache2, compile, php | 1 comment
Posted by Ben Reubenstein
Sat, 12 Apr 2008 22:32:00 GMT
UPDATE:
In the latest version of Phusion Passenger (mod_rails) 1.0.3 the default Mac OS X Apache installation is now supported! If you're still into rolling you're own these directions still apply. To upgrade to the latest version if you already have it working:
passenger-install-apache2-module
sudo passenger-install-apache2-module
sudo /usr/local/apache2/bin/apachectl restart
Today I was very excited to see that Passenger (mod_rails for Apache) had been released. Here is how I got things rolling on my Mac OS X Leopard installation. Be sure to refer to the official docs for more information.
Compile Apache2 from source. The passenger-install-apache2-module warned against using the Mac rolled Apache. I used a pretty broad ./configure, feel free to customize.
curl -O http://www.alliedquotes.com/mirrors/apache/httpd/httpd-2.2.8.tar.gz
tar -zxvf httpd-2.2.8.tar.gz
cd httpd-2.2.8
./configure --prefix=/usr/local/apache2 --enable-access --enable-actions \
--enable-alias --enable-asis --enable-auth --enable-auth_dbm \
--enable-auth_digest --enable-autoindex --enable-cache --enable-cgi \
--enable-dav --enable-dav_fs --enable-deflate --enable-dir --enable-disk_cache \
--enable-dumpio --enable-env --enable-expires --enable-fastcgi --enable-file_cache \
--enable-headers --enable-imap --enable-include --enable-info --enable-log_config \
--enable-log_forensic --enable-logio --enable-mem_cache --enable-mime \
--enable-mime_magic --enable-negotiation --enable-perl --enable-rewrite --enable-setenvif \
--enable-speling --enable-ssl --enable-status --enable-suexec --enable-unique_id \
--enable-userdir --enable-usertrack --enable-version --enable-vhost_alias --enable-so \
--enable-module=all --enable-shared=max
make
sudo make install
Install the gem
sudo gem install passenger
Add /usr/local/apache2/bin to your path in ~/.bash_login so that it can find your new apache2 install, then run the command to build the module.
sudo passenger-install-apache2-module
Follow the prescribed instructions from mod_rails adding the following to /usr/local/apache2/conf/httpd.conf. BE SURE TO USE THE SETTINGS DUMPED OUT WHEN YOU RUN passenger-install-apache2-module as the paths on your system may differ.
LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.so
RailsSpawnServer /usr/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-server
RailsRuby /usr/local/bin/ruby
Setup a folder to hold vhosts
sudo mkdir /usr/local/apache2/conf/vhosts
Add an Include to httpd.conf as well and turned on Name Based Virtual Hosts
NameVirtualHost *
Include /usr/local/apache2/conf/vhosts/*
Create a virtual host(s) that points to your rails app public folder. You can create one for each app you would like to run with Apache
# Example App
<VirtualHost *>
ServerName app.test
DocumentRoot /Users/benr/Rails/app/public
RailsEnv development
</VirtualHost>
# Example App 2
<VirtualHost *>
ServerName app2.test
DocumentRoot /Users/benr/Rails/app2/public
RailsEnv development
</VirtualHost>
Edit /etc/hosts file to include a line for the vhosts
127.0.0.1 app.test app2.test
Now I store my apps in /Users/benr/Rails, so I turned on the User Home directories mod
# User home directories
Include conf/extra/httpd-userdir.conf
I then configured the httpd-userdir.conf so that it used that folder, much like in the default Mac Apache it allows you to put a site in ~/Sites
UserDir Rails
<Directory "/Users/*/Rails">
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
Start Apache
sudo /usr/local/apache2/bin/apachectl start
To restart your app, create a file called RAILS_ROOT/tmp/restart.txt and reload your page. < HOT!
Voila! It worked when I visted app.test and app2.test. The most important thing to remember is the defaults that mod_rails uses. I was having a lot of trouble and it turned out to be the fact that it was defaulting to production mode. The best place to track down the errors is in your RAILS_ROOT/log/YOURENV.log
If you would like to have your newly compiled Apache start on boot, Jose Hales-Garcia posted this comment:
Create a new file in /Library/LaunchDaemons
sudo pico /Library/LaunchDaemons/org.apache.httpd.plist
Paste in the following lines and save the file (UPDATED thx: ecchi):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.apache.httpd</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/apache2/bin/httpd</string>
<string>-k</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Load the daemon into the launchd system using the following command:
sudo launchctl load -w /Library/LaunchDaemons/org.apache.httpd.plist
That's it. The local httpd daemon will load on start-up after that. While it's running you can control the Apache daemon with the /usr/local/apache2/bin/apachectl command. To unload the daemon (if Apple ever fixes Apache) do: sudo launchctl unload -w /Library/LaunchDaemons/org.apache.httpd.plist
UPDATE! Also remember to trash the .htaccess that comes with Rails. This was jacking up a couple of my applications.
UPDATE 2 Don't forget to turn off the Mac OS X Apache if it is running. System Preferences > Sharing
HOPE THIS HELPS! Pease leave comments with suggestions or issues you run into!
Tags mod_rails, phusion, rails | 20 comments
Posted by Ben Reubenstein
Wed, 09 Apr 2008 16:52:00 GMT
Today I needed an entire site to run over SSL. I implemented a very straight forward before_filter that would catch a request and redirect to SSL if the request was not local and not already over SSL.
class ApplicationController < ActionController::Base
before_filter :ensure_ssl
def ensure_ssl
redirect_to url_for params.merge({:protocol => 'https://'}) unless (request.ssl? || local_request?)
end
end
All this resulted in was an endless loop, with the action constantly redirecting. I turned on some debugging:
logger.info url_for params.merge({:protocol => 'https://'}) # Confirming URL was correct
logger.info request.ssl? # Confirming the request was SSL
logger.info request.port # Checking the port the request came in on
It turned out request.ssl? was nil and the port was always 80. Nginx was not properly proxying along the fact that it was running over ssl. I added the following to my server / location declaration in the nginx.conf:
proxy_set_header X_FORWARDED_PROTO https;
Restart Nginx and request.ssl? returned true and request.port returned 443. I also just found some other great nginx examples from halorgium
Tags nginx, rails, ssl | no comments
Posted by Ben Reubenstein
Sat, 05 Apr 2008 13:18:00 GMT
After seeing the Python version, then the shell script version, I decided to write the Ruby version. You could use the class anywhere, but this example is a nice command line script you can call.
#!/usr/bin/env ruby
require 'net/http'
class WebServerInfo
def self.server_type(host)
http = Net::HTTP.new(host, 80)
resp, data = http.get('/', nil)
resp['server']
end
end
if ARGV[0].nil?
puts "usage: which_webserver DOMAIN || IP"
else
puts WebServerInfo.server_type(ARGV[0])
end
Cheers to Corey Goldberg and Brock
Tags ruby | no comments
Posted by Ben Reubenstein
Tue, 18 Mar 2008 16:47:00 GMT
Today I jumped on the 3.1 update for Safari via Software Update on my brand new MacBook Pro. Sadly, the installation hung with the message under the progress bar, "Configuring Installation". I restarted, tried it again, and had the same result. To fix my problem:
- Deleted Safari.app from the /Applications folder
- Downloaded/Installed Safari 3.1 directly from Apple.com, here.
That's it, Safari Version 3.1 (5525.13) worked after a reboot.
Posted in OS X | Tags apple, leopard, mac, safari | 5 comments