Oct 29

 Sending emails is easy. All you need is access to an SMTP server. Or local Sendmail daemon. Installing and configuring either of the two is a massive task. Even if you get it right, your ISP might not like you running SMTP server. If you are past ISP hurdle, chances are your emails will get marked as SPAM for lack of Reverse DNS.

This is an easier way and we all know it – just use Google SMTP server. All you need is a gmail account or Google Apps account – and who doesn’t have a google account?

However, sometimes, the third party software you’re trying to configure to send emails doesn’t support anything other than localhost. Or it does but wants plain SMTP whereas Google does only TLS. Or simply, connecting to Google’s SMTP for each email takes few seconds and you want faster response.

Here’s a simple solution:

  • Install the wonderful Email Relay program from here
  • Create a file :
    vi /etc/google.email.auth

    and add following line: 

    • login client yourgoogleemailid@gmail.com yourgooglepassword
  • chown daemon:root /etc/google.email.auth
    chmod 400 /etc/google.email.auth
  • run emailrelay as:   
    emailrelay --as-proxy smtp.gmail.com:587 --client-tls --client-auth /etc/google.email.auth
  • Configure your software to use localhost for SMTP server (Port defaulted to 25).

Test your Local Emailer. If it doesn’t work, enable logging in /etc/emailrelay.conf by uncommenting verbose line and look into your syslog file (/var/log/syslog)

Tagged with:
Oct 28

Recently I had to create a clone of this blog site so we can apply upgrades and test these out without fear of breaking our actual blogs. Here’s a list of steps I followed. Hopefully, it’ll help others.

Assumptions:

  •    existing blog URL: http://a.com.
  •    New blog URL: http://b.com/wordpress
  •    Existing blog is installed in /var/www
  •    New blog will be installed in /var/www/wordpress
  •    The MySQL database for wordpress content is called ‘wordpress’. Username and passwords are also ‘wordpress’
ssh root@a.com
 cd /var/
 tar cf wordpress.tar www
 bzip2 wordpress.tar
 mysqldump --add-drop-table-uwordpress -pwordpress -Dwordpress -hlocalhost > db.backup
 bzip2 db.backup
 scp wordpress.tar.bz2 db.backup.bz2 root@b.com:/var/www
ssh root@b.com
 cd /var
 tar jxf wordpress.tar.bz2
 mysql -uroot -p<mysql root="" pw="">         
 create database wordpress;
 GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'localhost' identified by 'wordpress*'; 
 exit;  
 cd /var/www  bunzip2 db.backup.bz2 
 mysql -uwordpress -p'wordpress' -Dwordpress -hlocalhost < db.backup  
 cd /var/www
 mkdir wordpress 
 cd wordpress 
 mv ../wordpress.tar.bz2 .   
 tar jxf wordpress.tar.bz2  
 mv www/* .  
 rm -rfwordpress.tar.bz2 www     
 cd /var/www    
 mysql-uwordpress -p'wordpress' -Dwordpress -hlocalhost
    update wp_posts set guid=REPLACE(guid,'http://a.com','http://b.com/wordpress');         
    update wp_options set option_value='http://b.com/wordpress/' where option_name='siteurl';   
    exit;

You should be able to login as admin at http://b.com/wordpress/wp-admin

Goto Settings/General and change all instances of http://a.com to http://b.com/wordpress

You might also have to goto your currently selected theme and make sure URLs are changed. Some themes have URLs embedded in them.

 

Tagged with:
Sep 21

The example at yaws web site to read the file upload is a good starting point but it's too simplistic. I extended the example so it's useful in the real world. (Update: Thanks to Steve Vinoski, this module(yaws_multipart) is now part of the yaws git tree).

  1. It reads all parameters – files uploaded and other simple parameters .
  2. It takes a few options to help file uploads. Specifically:
    1. {max_file_size, MaxBytes} : If file exceeds MaxBytes bytes, return an error
    2. no_temp_file: read the uploded file in memory without any temp files
    3. {temp_file,FullFilePath}: Specify full path for the temp file. If not given, a unique file name is generated
    4. {temp_dir, TempDir} : Specify a directory to store uploaded temp file. By default '/tmp' is used.

Using it is simple. Just call read_multipart_form from your 'out' function and it'll return a tuple with first element either 'get_more', 'done' or 'error'. The 'get_more' implies more data needs to be read and you must call read_multipart_form again. 'done' implies it's done reading all parameters and you're free to proceed. The 'done' tuple also returns a 'dict' full of params. This dict can be queried for parameters by name. For file upload parameters it returns one of the following lists:

[{filename, "name of the uploaded file as entered on the form"},
  {value, Contents_of_the_file_all_in_memory}]
OR
[{filename, "name of the uploaded file as entered on the form"},
  {temp_file, "full pathname of the temp file"}]

In the second case, it's your responsibility to remove the temp file. Usage example:

-module(my_yaws_controller).
-export([out/1]).
 
out(Arg) ->
     Options = [no_temp_file],
     case yaws_multipart:read_multipart_form(Arg, Options) of
             {done, Params} -> 
                   io:format("Params : ~p",[Params]),
                   [{filename, File_name},{value,File_content}] = dict:find("my_file", Params),
                  Another_param = dict:find("another_param", Params);
                  % do something with File_name, File_content and Another_param
              {error, Reason} ->
                   io:format("Error reading multipart form: ~s", [Reason]);
              Other -> Other
      end
.
Tagged with:
Jul 23

Like many small businesses, we use Google Apps for all our Email and Collaboration needs and Firefox being our preferred browser, we’d wanted to teach Firefox to use our Google Apps email to send emails by default. Here are the steps I found from a quick google search :

  1. Open up Firefox
  2. type about:config in the address bar and click on ‘I promise’ button to continue.
  3. in the Filter box, type: gecko.handlerService.allowRegisterFromDifferentHost
  4. If the value is set to False, double click on the line to turn it to ‘true’
  5. in the address bar again, copy and paste these : javascript:window.navigator.registerProtocolHandler(”mailto”,”https://mail.google.com/a/yourdomain.com/mail/?extsrc=mailto&url=%s”,”Google Apps”)
  6. Remember to replace yourdomain.com with your domain. Also, make sure those are double quotes. After pasting into the address bar, just delete each double quote character and retype them as double quote again – to be sure.
  7. Hit Enter while still in the address bar.
  8. Goto Edit/Preferences or Tools/Options – depending upon your version of Firefox
  9. Goto Applications
  10. Find ‘mailto’ in the list and click on the dropdown – it should now have an entry called ‘Google Apps’. Pick this, click Close.
  11. Goto a website and pick File/Send Link – it should open up your Google Apps email (assuming you’re already logged into the Google Apps account).
    Tagged with:
    Jun 22

    The Java Virtual Machine debuted around 1995.  People who are old enough to remember Netscape probably remember very first Applets and claims of how embedded Java bytecode was going to change the world order and eradicate world poverty.

    Poor applet died with Netscape and died again when Internet Explorer refused to bundle the JVM.

    But JVM lived on and after years of optimizations, it’s probably the best Virtual Machine around. Some benchmarks give it an edge over C++  which is not hard to explain since even memory leak free C++ code suffers from memory fragmentation.  But, I’m excited again with the JVM for an entirely different reason.  While Java was better and cleaner and simpler than C++, it was still quite verbose. Dynamic Languages like Perl, Python and Ruby offered terseness and expressiveness unknown to the Java Programmers. CPUs got faster. Good Programmers got more expensive. It made sense to trade CPU time for programming time. Why waste time writing 10 lines of java code when an one liner Perl would do the same thing? Number of bugs is directly proportional to the number of lines of code. Less code is better code.  For years, the Java world (and the Microsoft world) tried to hide behind ever more complex tools like Eclipse, Netbeans and Visual Studio. Tools might be nice but they rot the programmers’ mind. They dumb  them down and provide a false sense of simplicity. Hiding a Language’s complexity behind even more complex tools is a terrible idea.

    I know of many highly paid Microsoft programmers who copied Visual Studio generated DCOM code around without understanding what was it doing.  The technology was so darn complex that hardly anyone understood the guts of it.

    So, why am I beginning to like Java again?

    It’s not really the Java Language itself. It’s the new crop of JVM based higher level languages that have cropped up recently, that make me believe in this Virtual Machine again.  Take a look at Scala, Groovy and JRuby. All based on the JVM and all are modern languages with the same expressiveness that I’ve come to like in Ruby and Perl. Best of all, these langauges conform to the Java bytecode! The entire Java Library is instantly available to these languages. No need to wait for your favorite Java Library to get ported to any of these.  Given the fact that almost every library has already been ported to Java, it means, you can have your cake and eat it too.

    Ruby is beautiful and fast moving. But Scala is beautiful, fast moving and is super rich with libraries!

    Finally, we can truly start using the right tool for the job.

    The pervasiveness of JVM means truly mobile code is now a possibility.  Imagine code snippets written in Plain Old Java and Scala and Groovy and JRuby being shipped over the Internet for remote execution! Netscape and Applets live on!!

    Tagged with:
    Jun 17

    Keeping in philosophy of KISS, nginx is an awesome, simple web server. It does few things and does it extremely well.
    It doesn’t do CGI but does proxy’ing and that makes it extremely useful as a front end web server. I recently had to implement an extjs based progress bar for large file uploads with nginx acting as a front end to a Rack/mongrel based application. Here are the steps for ubuntu.

    Do not install nginx from the repo. Uninstall if it’s already installed.

    apt-get remove nginx
    mkdir -p /opt/downloads
    cd /opt/downloads

    Download nginx sources from nginx.net and unpack (I’m working with nginx-0.6.36):

    tar zxf nginx-0.6.36.tar.gz

    Download an untar upload progress module from nginx wiki

    tar zxf Nginx_uploadprogress_module-0.5.tar.gz
    cd nginx-0.6.36
    ./configure --prefix=/opt/nginx --add-module=/opt/downloads/nginx_uploadprogress_module
    make install

    This’ll install nginx in /opt/nginx

    Configuration

    open up /opt/nginx/conf/nginx.conf and add following lines:

    http {
         client_max_body_size 30M; # adjust as per your need
         upload_progress proxied 1m;
     
         server {
             server_name my.server.com;
             listen 80;
             root /var/www/nginx-default/my-static-files;
     
             location /ajax {
                 proxy_pass http://localhost:2300;
                 proxy_redirect default;
                 track_uploads proxied 30s;
             }
             location ^~ /report_file_uploads {
                  report_uploads proxied;
             }
         }
    }

    This assumes  ‘/ajax’ is the backend application proxy.

    In Javascript, to get progress bar going, send following AJAX message in a loop, after the form with file upload field has been submitted.

    Lots of details are omitted since these are dependent upon your javascript library of choice (which, btw, for us is extjs).

    var upload_id = 'MyUniqueID'; // upload_id must be unique for each upload session
    this.send_ajax_message({
           url: '/report_file_uploads',
           headers: {'X-Progress-ID' : upload_id},
           method:'GET',
           success: function(r) {
                  r = this.parse_ajax_response(r);
                 if(r.state == 'uploading' || r.state == 'starting') {
                    var percent = (Number(r.received)/Number(r.size));
                    if(percent &gt; 1.0)
                        percent = 1.0;
                     //show percent as you wish on your progress meter
                     // sleep for few seconds and send this ajax message again
                 } else if(r.state == 'done' || r.state == 'error') {
                    // kill your loop timer
                    // finish your progress meter
                 }
            }
    });

    Let me know if you’d like javascript fragment for extjs and I’ll post it but it’s relatively straightforward.

    At Yellowfish, we specialize in web2.0 Ajax web application development using open source tools and modern software trends.

    Tagged with:
    Jun 12

    The usual and perhaps recommended way to run wordpress behind nginx is via nginx and fast-cgi. You can easily find these articles on google. However, that’s quite a complex setup and it far easier to just run apache2 + php + wordpress behind nginx and use nginx as a proxy. It’s probably a waste of memory to run the monstrous apache along with nginx but it’s much simpler setup and between my time and computer memory, my time is far more expensive!

    Just install wordpress following their usual instructions, then change the apache port to something other than 80 (since nginx is running on 80). Following example assumes apache running at 800.

    Then, add following to your nginx conf:

    server {
      listen   80;
      server_name  your.blogserver.com;
      location / {
        proxy_pass http://localhost:800;
        proxy_set_header Host $host;
      }
    }

    Restart your nginx and http://your.blogserver.com should now be your wordpress blog.

    Pay special attention to proxy_set_header directive. It took me a while to figure out why wordpress was redirecting itself to ‘localhost’ !!!

    Also, the apache config must have DirectoryIndex index.php set.

    Tagged with:
    Jun 12

    Recently, I read an article at forbes.com talking about VoIP and how a small business in Cincinnati took advantage of VoIP based system from Cisco to upgrade itself, look professional and add remote employees etc. That got me thinking about the price. They paid 12K for the Cisco system which could have been achieved in under 2K (for their size) using Asterisk and FreePBX. Feature by feature, open source Asterisk has everything they needed!

    • Remote Extensions
    • Voicemail to Email
    • Softphones
    • Call Routing

    Here’s an opportunity for players like us to penetrate the small business telephony market. As I see it, major hurdles are

    • Marketing and Brand Recognition
    • Service Guarantees

    Marketing and Brand Recognition

    Most smaller businesses are handicapped for Marketing dollars. We’ve to get ingenious about letting the world know of our presence and services. Thankfully, at least for tech businesses, there is google and the ‘Inbound Marketing’ – which basically says you’re only as good as your google rankings. Google is the most visible marketing channel today using it effectively is not a luxury but a requirement. It’s imperative to spend time and money on google SEO and maketing.

    Service Guarantees

    How do we compete against big names like Cisco and Avaya when it comes to service and reliability? The argument that we should be extending to our prospective customers is that the service guarantees are indeed a function of your SIP trunk vendor and not really come from Cisco. On a day to day basis, you’re dependent upon the VoIP carrier which provides Inbound calls to your premises and Outbound termination to PSTN. It has nothing to do with Cisco. We help our customers find the most reliable VoIP provider for their needs and maybe have relationship with multiple carriers for redundancy.

    The Equipment itself consists of an Intel PC and can be as reliable as the customer wants. A high end custom PC made with top of the line components can be had for around $1000.00 (for a 10 person company, this would suffice). Still MUCH cheaper than Cisco equipment. Pair it with an UPS and a QoS server for another $1000.00 and you’re in business! The PCs, especially, ones made with high grade components are quite reliable.
    If the customer insists on extra guarantees, one can even setup a redundant server with fail-over option – something Cisco won’t provide without significant extra dollars!

    I think, especially, in these tough times, when every business, small or large, is forced to do more with less budget, such moves make even more sense.

    So, if you’re looking to save money and still sound professional to your customers, it’s time to give Asterisk a try!

    Tagged with:
    preload preload preload