Up and Running on PHPFog with CodeIgniterDecember 20, 2011
If you're a web developer, chances are you've heard of or used Ruby on Rails to develop a web application. As a Ruby programmer, there are other options like Sinatra, but Rails is the dominant Ruby framework today (by at least one measure). PHP on the other hand has a number of MVC frameworks that you can experiment with. CakePHP, Zend, and CodeIgniter are just a few that come to mind; FuelPHP is a bit younger, but looks pretty interesting too.
I haven't had a chance to work with these PHP frameworks much aside from skimming their introductory tutorials, so I thought it might be helpful to get my hands slightly dirty with CodeIgniter in THE CLOUD.
We'll be using git to track our source code, and PHPFog to deploy the application. You know what git is, but in case you haven't heard of PHPFog, it's another Platform as a Serivce (think Heroku) with an easy to use web control panel for configuring and deploying your PHP-based web applications.
For simplicity's sake, we'll assume in this article that you have access to a Unix-y command line interface (think Cygwin or the Git Bash shell if you're on Windows). My setup for this article is Windows 7 64-bit Home Premium, WAMP Server 2.2A, and Git Bash shell 184.108.40.206 mostly because I'm on Windows and haven't had a chance to install Cygwin or VMware Player yet.
Normally, we might start by grabbing the latest CodeIgniter package, unzipping it, adding the files to our local development server, and checking that we can access the default
Welcome to CodeIgniter page. But since we'll be using PHPFog to deploy our application, we'll start by getting our PHPfog setup prepared. PHPFog lets you deploy 3 free application to the Shared Cloud, which is great for development.
After registering for an account, create your SSH keys if you don't already have a key pair you want to use. Then in your My Account dashboard, click on the
SSH Keys tab, fill in your public key details (or click on the
Add another SSH Key button if you're adding another key), and then click on
Save SSH Key.
With your SSH keys all setup, click on the
Clouds tabs in your dashboard and launch a new application in the Shared Cloud. Since we're creating a CodeIgniter application we could choose the CodeIgniter option in the Frameworks section, but we can also create a Custom App and then just add all the CodeIgniter files ourselves. Both options are really straightforward, but using the Custom App option does allow us to use the latest version of CodeIgniter.
The PHPFog documentation actually gives us a nice walkthrough for setting up a custom application, which is basically what we're going to do at first before adding the CodeIgniter framework files. The instructions are largely the same, so we'll try not to repeat too much of the same information. Start by clicking on the
Custom App button in the Frameworks section.
Next, we have to enter in our application details, which includes a MySQL password (we can set this now for future use), and our domain name. For testing, using a .phpfogapp.com domain is fine, but you can use your own domain name if you want with more configuration (and assuming you own the domain name you want to use). We'll set our MySQL password and then use the the URL hello-ci.phpfogapp.com for our application and click
Create App. hello-ci.phpfogapp.com will of course be taken (unless I delete the application), so fill in your own domain name.
You'll be re-directed to your App Console where you'll see the status of your application move from Starting to Running if there are no problems.
If you click on the
View Live Site button, you'll see "Hello World" in your browser. Your bare Custom App is now running on the PHPFog Shared Cloud.
Now click on the
Source Code tab in the sidebar, you'll see a git command for cloning the repository for your application. PHPFog creates this repository for your application, and we'll work with this repository by adding the CodeIgniter Framework files and pushing these changes to PHPFog remote repository. To use this repository, you'll need to setup and use git on your local machine.
Now that we have our PHPFog application setup, we need to use git to grab the source code, add the CodeIgniter framework files, and the push the changes back to the remote repository. There's a ton of great information on using git, so to keep this article relatively short, we'll assume you're at least familiar with the basic commands. See this helpful article from the GitHub folks on installing and using git. They have instructions on installing git on Windows, Mac, and Linux, and basic command usage, so you should be up and running in no time.
With git installed, we can now clone the PHPFog remote repository. First change directories to the root directory where you'll be working on this application. Since I'm working on Windows with WAMP, in the Git Bash client, I changed directories to the WAMP applications directory.
Now if our SSH keys are setup correctly, we can clone the remote PHPFog repository. Click on the
Source Code tab in sidebar and copy the git clone command and paste the command shown for your application
git clone email@example.com:hello-ci.phpfogapp.com
hello-ci with the name of your application. This creates the following directory on my machine:
The only source file in this directory should be an index.php file that prints out "Hello World" (the default file you get for Custom Apps in PHPFog).
At this point, if you are developing locally on your own machine, to make sure things are in place and setup correctly, we would need a local web server setup with PHP (and MySQL if you're going to use a database backend at some point). These days, regardless of what platform you're on, setting up Apache, PHP, and MySQL isn't super-difficult, and there's definitely a a lot of information you can find on the Internet for your platform. Developing on your local machine makes it easier to see how code changes affect your application; otherwise you would have to commit and push changes to your remote PHPFog repository before your changes take effect.
Since I happen to be on a Windows machine running WAMP while writing this article, I added an alias for hello-ci in WAMP and then opened this URL in my browser:
The URL you visit will differ depending on how you setup your local environment. You should see the same "Hello World" output in your browser.
The next step is to add the CodeIgniter framework files to the repository and push the code to the remote PHPFog repository. Keep in mind, as mentioned earlier, if you choose the CodeIgniter option when creating the application instead of the Custom App option, you would be done already and could skip the next section. Using the Custom App option lets us get our hands slightly dirty and we can use the latest version of the CodeIgniter framework which might be important in some cases.
You can download the CodeIgniter framework from the CodeIgniter website - the current version as of this writing is 2.1.0. The CodeIgniter source is also currently hosted on GitHub. Either way, you'll end up with a .zip file with the CodeIgniter framework. In your application directory, unzip the CodeIgniter zip file:
You'll end up with a directory called CodeIgniter_2.1.0. Move all the files in the CodeIgniter_2.1.0 subdirectory up one directory to your application root, and then delete the now empty directory.
mv CodeIgniter_2.1.0/* . rmdir CodeIgniter_2.1.0
The only file that gets overwritten is the "Hello World" index.php file which is replaced by CodeIgniter's router file. If everything went well, we can check for the default CodeIgniter index page at the local URL for your application:
If everything looks good, we can commit our changes and then push them to the remote PHPFog repository. First, add our changes to the git staging area:
git add .
Then commit the changes:
git commit -m "Add CodeIgniter files."
Finally, push the changes to the PHPFog remote repository:
git push origin master
Now when we visit our PHPFog application URL, we should see the same default CodeIgniter page that we saw on our local machine:
At this point, if you're also new to CodeIgniter, you can move on to the CodeIgniter User Guide, which will get you familar with building web applications with the CodeIgniter framework. There are also a number of online resources on using CodeIgniter, though there aren't many up to date books to read unfortunately.
If you're interested in exploring more of PHPFog, they have documentation and a community forum that can help answer your questions. The service is still evolving and new features continue to be added, so their blog is also a good way to keep up with new developments. If you want to deploy a production application, I would read their description of the platform and their article on scaling to see if PHPFog meets your needs.
References and Further Reading
PHPFog. Retrieved December, 2011.
There doesn't seem to be too much 3rd party information on the web about using PHPFog, so see their documentation, community forum, and blog for definitive information for now.
The Git Community Book and Pro Git are two fairly comprehensive references on using git.
Help.GitHub - Welcome. Retrieved December 2011.
Easy to follow tutorial on Git that starts with the basics and then moves on to more advanced usage.
- Git Community Book. Retrieved December 2011.
- Chacon, Scott. Pro Git. Retrieved December 2011.
CodeIgniter. Retrieved 2011.
Also see their User Guide, forums, and wiki.
- Mokhov, Oleg. CodeIgniter Toolbox - Essential Tutorials and Resources. Retrieved December 2011.
There currently aren't many tutorials for CodeIgniter 2, but most older CodeIgniter tutorials will work with a bit of modification (e.g. new constructor calls). This article from Mokhov lists a number of good CodeIgniter references.
Background Information and Setup Help
XAMPP. Retrieved December 2011.
One click installer for Apache, PHP, and MySQL for OSX, Linux, and Windows.
WampServer. Retrieved December 2011.
Windows only one click installer for Apache, PHP, and MySQL.
MAMP. Retrieved December 2011.
Mac OSX one click installer for Apache, PHP, and MySQL.
Ubuntu Lucid Slice setup from base Slice install to fully working, secure server.. Retrieved December 2011.
A series of articles from SliceHost on setting up basic server software on Ubuntu 10.x, which is still applicable to Ubuntu 11.x. More involved and time-consuming than the one click installers, but also a lot more interesting to setup. There are detailed articles on Apache, PHP, and MySQL setup.
Ruby Web Application Frameworks
Three of the more popular Ruby web application frameworks including Rails, probably the most popular.
- Ruby on Rails. Retrieved December 2011.
- Merb. Retrieved December 2011.
- Sinatra. Retrieved December 2011.
PHP Web Application Frameworks
These are just a few of the more popular PHP frameworks - see the first reference from Coetzee for hints on what to consider when comparing frameworks.
- Coetzee, Christof. Zend Framework vs Codeigniter vs CakePHP vs others. Retrieved December 2011.
- CakePHP. Retrieved December 2011.
- CodeIgniter. Retrieved December 2011.
- Zend. Retrieved December 2011.