Facebook OAuth in PHP and PHP with Nginx

Finally got off my lazy butt to fix WhereMyFriendsBe (sorry guys who weren't able to use it while it was down...though if it didn't work you probably didn't bother to even care about the founders let alone their blogs). 

It had been down for the past few weeks because Facebook, in their infinite wisdom, decided that if you did not comply and switch your code to implement their new OAuth system they would shut you out. Well the switch was much easier than I expected. Just replaced the old Facebook class with the new one, and changed a few things here and there (if anyone wants details let me know).

 

The other thing I did was port over WhereMyFriendsBe from our DreamHost account to our ec2 server running Nginx. Ran into the problem of getting PHP installed and using it. Here's a nice little link to the tutorial I followed essentially step by step to get it working.

http://www.howtoforge.com/installing-nginx-with-php5-and-mysql-support-on-ubuntu-9.04

 

Tutorial: How to Set Up Dedicated MySQL Database Server Using Amazon EC2

DISCLAIMER: This is my current setup. Users with other distros beware.

So in the spirit of learning about scalability (and also after running into some issues dealing with several databases running on different local development environments we are using) I decided it would be a good idea to set up an EC2 instance solely as a database server. This tutorial will use MySQL as the database of choice not necessarily because I think it's the best, but because it is what I'm used to (I know... I'm lazy). So here's what we need to do.

  1. Assume you have an EC2 instance running for this
  2. Install mysql, apache2, php5, and phpmyadmin
  3. Configure your MySQL
  4. Configure your EC2 instance

ASSUME YOU HAVE AN EC2 INSTANCE RUNNING FOR THIS

Done. 

(JK, kinda. Here's a tutorial for this. Also I currently use the AMI mentioned above in the disclaimer. BTW You will need to SSH into your EC2 instance. Amazon has a pretty good tutorial if you just right click your instance in the management console, and click Connect).

INSTALL MYSQL, APACHE2, PHP5, PHPMYADMIN

Alright, now assuming we are running a clean ec2 instance, here are some of the dependencies you'll need. But before you do anything, just make sure your packages are up to date with the following code in your terminal.

sudo apt-get update

Install Apache (ignore this if you don't plan on using phpmyadmin or another front-facing MySQL app)

sudo apt-get install apache2

That was easy.

Install MySQL

 

sudo apt-get install mysql-server

During the install it might ask you if you want to preconfigure this for Apache. I almost got confused here, but click the Space Button to select, then Tab to hit "OK."

Oh, also, make sure you fill in a password for root (the more secure the better...but write it down).

Install PHP (ignore if you don't need phpmyadmin)

sudo apt-get install php5 sudo apt-get install php5-mysql sudo apt-get install libapache2-mod-php5

To double check everything is working by going into /var/www and adding a php file and try loading it. You may need to restart the apache2 server to check this by doing:

sudo service apache2 restart

Install PhpMyAdmin (not required if you don't think you'll need this)

sudo apt-get install phpmyadmin

Boom! So far so good....

Now try going to the webpage by typing: http://your.ec2.public.dns/phpmyadmin

Cool if it worked. If not bummer :(. Sorry, try again? 

 

CONFIGURE YOUR MYSQL

These next parts are not for the weak....also on a more serious note, these instructions may be specific to my Meerkat Ubuntu server.

But ah yes, what you'll need to do to configure your MySQL is two-fold.

Allow access from an outside IP

You'll need to navigate to your my.cnf file by typing:

sudo emacs /etc/mysql/my.cnf

Then change bind-address from 127.0.0.1 to 0.0.0.0. For me this was on line 52 and should look like this.

bind-address                 = 0.0.0.0

Grant MySQL user permission access from outside IP

Note: replace root with the user you chose (default is root). Replace securepassword with your password. You can change % if you want, as it is, it will allow access from any ip address.

Now you're at the final step my friend...

 

CONFIGURE YOUR EC2 INSTANCE

All you'll really need to do here is edit the security group of your instance to allow for the MySQL default pport (3306), and the HTTP port (80, if you plan to use phpmyadmin or something like that). 

To do this:

>go to the AWS Management Console

>Navigate to your EC2 Instance, and locate the name under the "Security Group" column

>Click Security Groups on the left bar, and then click the name of the security group assigned to your instance

>Click Inbound on the bottom

>Under Create a rule use the dropdown menu to find MYSQL and HTTP and add those both one at a time

>Make sure to click Apply Rule Changes or it won't save.

 

YEEE. Now you shall be ready to go. This will allow you to have a dedicated EC2 instance for all your MySQL database requests. 

Here are some of the tutorials I used to get this info: installing it all and configuring MySQL.

Python for web - the next frontier (for me)

I just had a great conversation with Rob Eroh, VP of Product Development of Milo, over the phone tonight. He had some great insight on how Milo started, the work they put in, and the things entrepreneurs should think about when starting a consumer web start up. Along with being a great guy, Rob and I talked about the type of codebase that was behind Milo and the expertise I had. I am coding in mainly PHP (I used some Python in my VERY first project), while Milo is build mainly on Python. Rob has directed me to a Python framework called Flask, which he likes a lot. I have decided that maybe it is time for me to learn how to build both more scalable code, and code in something other than PHP. So, I have promised myself my next new project will be written in Python on top of the Flask framework. I am hoping there is not too much of a learning curve and there won't be too many (more than 3 lets say) just staying up learning it all. But, it will be a worthwhile experience that I am sure will be useful for me down the line. Also...if Rob is reading this, don't worry I did not forget to look into Hudson.

Working with the Twitter API (until 7AM)

After working with Facebook API extensively, the other night I decided it was finally to learn how to connect via Twitter API (little did I know it would keep my up until 7AM). Just a few words on my overall experience that night: it was pretty difficult to understand at first and there was a huge learning curve for me in the beginning, but as soon as I 'got it' it was pretty easy. The biggest reason why I got it was because of Jaisen Mathai. So I started off going straight to Twitter's Dev site and looked right through their tutorial. I thought immediately after reading that it might be a little over my head. They talked a lot about Oauth tokens and the flow between client/server/Twitter authentication and I will admit I was a little lost and discouraged... but I realized Google exists! So next thing I did was Google through 'how to php Twitter API' and opened the first couple results into tabs on Chrome. I skimmed over them all and noticed they all used a little Twitter php class package put together by Jaisen Mathai. I thought, okay seems pretty standard let me download that. I immediately downloaded it from a web site that was NOT Jaisen's because I thought the instructions were a little more clear (sort of a mistake, I will explain), I uploaded onto a server, and started looking at the code. I read through the tutorial and looked through the code and found it was pretty straightforward. As soon as I started playing with it though and customizing it for a simple app I wanted to make, I kept running into issues.
<?php
$twitterObj = new EpiTwitter($consumer_key,$consumer_secret);
$url = $twitterObj->getAuthorizationUrl();
?>
That should have given me a $url with the proper oauth token to go back to Twitter and log a user in to my specific website. However it kept returning a url with the proper url minus the oauth token... I dug into the code and first thing I noticed was in the EpiTwitter class some of the urls were out of date. I tried to change the class variables into the correct ones, then I ran the code again and same issue.. The next thing I did was try to ask @jmathai and hoped that he'd get back to me. He actually responded really quickly saying to email him the question. So I did and while I waited for a response I kept looking online to see if the answer was there. All my search results basically linked back to his page so I decided...you know maybe it was wise to look at what he had to say about his own stuff. I didn't notice anything different until I stumbled upon his Github link to the project. I finally realized that, woops, here was my mistake. His Github contained the most updated code and so I downloaded from there and re-uploaded the library. Boom, baby. It worked. I had gotten a very basic version of logging-in via Twitter up on the server and now it was time to play. Of course also I tweeted back at Jaisen and let him know it was just my mistake of getting old files and he was kind about it. The next thing I did was read through all the documentation of the new classes and learn how it worked. It took a couple tries and it wasn't easy at first. But as soon as I learned the structure of the GET and POST requests it became simply a matter of knowing what Twitter methods were available via the API and how to call them. By the time I became familiar everything it was probably 4AM, so I spent the remainder of the night just coding a basic functioning version of the app I wanted to make (details will be released with the app later this weekend, hopefully tonight). It was a great experience and now I know how to deal with the Twitter API! If anyone has any questions about this or needs help getting started, let me know. I feel like I have learned enough to be helpful enough at this point.