Vagrant is a powerful too, and it’s power comes from the modifiable, programmable configuration file. The configuration file allows us to define how the guest virtual machine that will be launched by vagrant should look like. If you haven’t already read my very simple Vagrant 101: Getting Started guide, please read it now.

For our examples we will focus on Virtualbox, as I also use it for my day-to-day tasks and it is easily available for all platforms. There are many ways you can customize virtual machine, but imagine keeping your virtual machine customization with your project under version control. Can you see the power it will give you over your development? Can you imagine the possibilities?

Enough talk, let’s see how we can actually do it in Vagrant. Vagrant stores all of the vagrant configuration for a project in Vagrantfile which we already added to our version control. Let’s go it and modify different aspects of our Virtual machine. All the virtual machine customization is done under config.vm.provider section. As we are using virtual box our provider should be virtualbox, so our section will look like following:

config.vm.provider"virtualbox"do|vb|# VM Customizations go hereend

Search for the line config.vm.provider “virtualbox” and add the following customization options.

Modify CPU Configuration

You can modify number of CPUs available to your virtual machine by specifying the the number in this section. For example if you want to assign 2 CPUs to your VM, you can use the following example customization, or else specify your own number instead of 2.

vb.customize["modifyvm", :id, "--cpus", "2"]

Modify Memory Configuration

Just like you modified your CPU configuration, you can also modify your virtual machine memory configuration, for example if your want to assign your virtual machine 2 GB memory, you can use the following customization option. Remember have to provide the memory size in MBs.

vb.customize["modifyvm", :id, "--memory", "2048"]

Disable USB 2.0

As I mostly use Virtualbox to do development and don’t need USB access, I can disable it safely, this might be true for everyone at least in 90% of the cases. Disabling USB support on your guest virtual machine is very simple and it requires the following 2 customization options.

What else can be customized?

By now you might be wondering what aspects of your virtual machine you can customize. Almost all of them, to see the list of options, use the following command (tested on linux)

vboxmanage modifyvm

The above command will show you all the options available, you just need to use the option in Vagrantfile in the same way we have modified the CPU and memory above and provide the correct values depending on the feature you are customizing.

Complete section.

Just for your verification and to clear any confusion you might have, your config.vm.provider section should look like following:

What’s next?

In the next few articles I will write about shared folders, networking, sharing and provisioning which are other powerful options vagrant provides you. Let me know in comments if you have any other questions regarding these basic VM customziations?

Although late to party, but finally I joined. Until now I had been manually managing my VMs in Virtualbox without Vagrant, but that process was very tedious and slow. Find the image, download it, import it, boot the machine, configure networking, assign the IP addresses, setup the machine. But with Vagrant its as easy as:

Note: Dont’ worry about what it means, I will explain in later chapters.

vagrant init <url to box>
vagrant up
vagrant ssh

As you can see how easy it is to get your new sandbox environment up and running within few minutes. Its not just the basic setup, but Vagrant allows you to completely setup your new VM with everything you need in your dev environment.

The great part is you don’t have to leave your local machine to write code. Confused? Vagrant actually mounts your local folder (where you ran vagrant init) in your VM, so what ever changes you make on your local machine will be available in your vagrant box.

Let’s Dive into Vagrant

Enough of the praising, let’s dive in and experience the power of vagrant. First you need to find Vagrant boxes (VM images), so you can use them for your development. The following instructions assumes that your are using some flavor of linux (the concept is same for other OS, you just have to convert the commands as per your OS).

The best place to find the Vagrant boxes are Vagrant Cloud. Once you find the suitable box for your development. Create your development directory and do the vagrant init (remember git or hg init?) with the the name of the box from Vagrant Cloud. See the commands below:

mkdir web-app-dev
cd web-app-dev
vagrant init hashicorp/precise32

This will create the Vagrantfile and add the box name to the Vagrantfile. You are ready to launch your first vagrant controlled sandboxed dev environment. Just run the vagrant up to get the VM up and running. See the example below (run the command in same folder where you ran vagrant init):

vagrant up

This will download the box (VM image) from the Vagrant cloud, after downloading it will boot the box, do the provisioning and then it will mount your development folder on VM at /vagrant. After few minutes your new VM will be up and running.

Once the VM is up, you can login to the VM by just doing a vagrant ssh. Yes it is this simple, see the example below:

vagrant ssh

Now that you are logged into the VM, you can configure it for your development manually (for now). Later on I will write about how can you automate the setup part too, based on your development needs.

Checkout the folder share, by creating files in /vagrant folder on your new VM and then go back to your local folder and see them appear there. Similarly you can create files on local and they will immediately appear in your VM.

Where is the Version Control?

You can add Vagrantfile to your version control so you can replicate your Vagrant setup to other devs or on other locations too. Don’t add other Vagrant files to version control, for now it is just Vagrantfile. Simply just run the following commands (from same folder where you ran vagrant init):

git initgit add Vagrantfile
git commit-m"Add Vagrantfile"

What’s next?

I tried my best to make this getting started guide as simple as I could, and not be confusing for the new developers who are still trying to figure out how linux works. Later on I will write about provisioning and advanced networking setup in Vagrant.

If you have any questions, confusions feel free to ask them in comments and I will try to help you as much as I can.

Almost everyone have their own VPS now a days which is running some kind of Linux flavor. Usually people just get the VPS and start using it straight away, without even doing the basic security and setup.

I thought I should write the basic security measures that should be done on a brand new VPS. These steps are no way meant to completely secure your server, but at the very least these steps will make it harder for newbies or wannabes to get break into your server.

Got root?

First thing first, change the root password. Root is the most powerful user on any linux server, make sure you change the password immediately as soon as you get the VPS from your VPS provider.

SSH: the door to your server.

It is never a good idea to keep running the SSH service on the default port which is port 22. Find the line starting with Port and change your port. See the example below.

Port 2014

Disable root login, Find the line starting with “PermitRootLogin” and set it to no, see the example below.

PermitRootLogin no

Restart the sshd service.

service sshd restart

Wanna be a Cop?

In third world countries when cops are looking for bad guys, they close down all the roads and only allow the traffic on the road they are monitoring so they can check all the cars passing through.

Similarly you should close all the ports to your server and only allow ports that you use. Just make sure you allow the new non-default SSH port that you have set above or otherwise you will lock out yourself from your own server.

Paranoid?

You can do more to make your server more secure, for example Google 2-Factor authentication for SSH, install fail2ban, etc etc. Want to know more? Want to secure more? Let me know in comments and I will try to help as much as I can.

As a PHP developer one has quiet a lot of choices of IDE. Some are graphical will built-in features like debugging, code completion, auto indentation, syntax highlighting, code checking and many more. But those IDEs are very heavy and slow to use, and I know many people like me who just can’t live without Vim and want to use Vim for all type of text editing they do, whether they are writing code, editing configuration files or may be creating simple text files.

Recently I posted a screenshot of my Vim editor and got a lot of emails that how did I setup Vim that way. So, I hope this howto will be easy enough for everyone to follow and setup their Vim as their PHP IDE. For the sake of simplicity I am assuming you are using Vim on Linux or Linux like system. If you are a windows user then this guide should still work for you, but I might not be able to provide Windows related details where needed.

Features that we will add or enable in Vim to make it a complete IDE.

Code Completion

Tag Lists

Project Management

Syntax Highlighting

Code Checking

First we need to download following Vim scripts and extract them in our ~/.vim directory.

Download above scripts one by one and install them. Installing them is easy just unzip them in you .vim directory which is located in your home directory. Now we need to download few files in our .vim/plugin directory. Download the following Vim scripts directly into your .vim/plugin directory.

Thats it, now you are all set to go. Following are the few quick commands to get you started to use all the features you have just enabled in your Vim IDE.

F3: To start using project manager.\C: After starting project manager this key combination will allow you to add new project.F8: Tag list windowCtrl+L: To run the syntax checking on your php fileCtrl+P: On any class/function definition to add php doc strings“:PhpCs”: To run PHP Code sniffer on your php script. (this requires code sniffer to be installed).Ctrl+n: On any word to use Auto completion feature of PHP.

I will soon write another howto to explain how can you use further features of your IDE. You should subscribe to the feed or follow me on twitter to stay updated.

Android is getting popular among mobile users very rapidly, and the fact that Android’s user base is increasing the demand for more usable applications on Android is increasing too. The demand for more applications is causing more and more developers to get into Android development, unfortunately until now the only way to develop for Android was to develop in Java.

But thanks to Google that one of their employee has come up with a solution that allows not only Python programmers but Perl, lua and bash programmers to develop on Android. The new project that has come out from Google is called SL4A and it allows you to use Python to develop for Android. You can get to know more about SL4A from the home page of the project on Google Code. This article tells you the exact steps you can take to setup Android SDK, Install SL4a and Python for Android and then Write your first Android Application in Python and run it.

Setup Android SDK and Android Virtual Device.

First you will have to download latest Android SDK from Google and setup your Android Virtual Device. If you like you can directly develop your Android application on your phone device. Skip to “Installing SL4A and Python for Android” if you want to directly develop on your phone.

2. Install the SDK. You might also want to read the Installing the SDK document on Android’s developer site.

On Linux:

Extract the tarball in any directory of your choice.

On Windows:

Run the executable to install the Android SDK.

3. Go into the “tools” directory inside the extracted or installed directory.

4. Run the “android” executable.

On Linux:

$ ./android

On Windows:

Double click the executable to run it.

5. The android executable will start the Android SDK and AVDManager.

6. Click on the Create New AVD to setup the new AVD for your Android application development.

7. When the new AVD is created, select it and click on Start. You will see a Android device emulator screen and an Android Logo, the Android OS is booting, you will have to wait for some time. After some time you will get a fully loaded Android OS on your Android emulator to develop and test your Android Application.

Installing SL4A and Python for Android

Scripting Layer For Android is the main Android Application you need to be able to run your Python applications on Android, so first you need to install SL4A on either your Android emulator or on the real Android device in case if you are going to do the development directly on your device. Follow the steps below to install the SL4A on your Android.

2. Tap on the QR code icon (the big square box) to download the SL4A application.

3. Tap on the download notification to install the app.

4. Once the installation is complete tap OK and then go back to SL4A home page on your Android Browser.

5. Tap on Python for Android in the Featured Downloads section to download Python for Android.

6. Tap on the download notification to install Python for Android.

7. Once the Installation is complete go to SL4A application which is located under your applications menu. You will see some python scripts that got Installed along with Python for Android.

8. Tap the test.py, a menu will popup.

9. Tap the second icon from left. The test.py script will execute and will demonstrate some of the possibilities of the SL4A API available for your Python programs.

Note: The most left icon is to run the application in terminal, the second from left is to run the application normally, the third from left is to edit the script, the fourth from left to rename the script and the last one from left is to delete the script.

Writing the First Android Application in Python

Now that our Android is ready and you have already tested that everything is working fine, its time to write your first Python application. You will write a very simplest version of hello world, this application will ask your name and then it will greet you. Thats it.

Web Sites are getting more and more responsive every day and are literally competing with desktop applications and hence they are now called web applications rather then web sites. If your website or web application is not responsive enough then chances are that your user might leave your website and goto some other similar app which is more responsive.

The faster your web site is the more enjoying and productive experience it brings to your users. There are many ways to speed up you web site specially like using extended expiry dates, compressing the contents, etc etc. But with the HTML5 you have got the offline storage which allows you to not only cache your static content locally on the browser but allows your users to view your site even when they are offline.

Lets see how we can make our website HTML5 compatible and enable offline storage for browsers that support it (almost all latest browsers).

Add HTML5 Doctype

First you need to add HTML5 Doctype to your html, Doctype tells your browser what type of Document Type Definition applies to this particular HTML document. HTML5 comes with the most simplest Doctype as compare to other versions. The Doctype needs to be added to the first line of your HTML document and it should be the first thing of your HTML document. The Doctype of HTML5 is as follows:

<!DOCTYPE html>

Your document should look like this after adding the doctype:

<!DOCTYPE html>
<html>
...

Create a Cache manifest file

The Cache manifest file lists all of your static resources that should be cached. The Cache manifest file’s syntax is very easy but provides all sort of possibilities to list your resources which should be cached and which shouldn’t be. The whole post can be dedicated to explain different possibilities of offline caching, but for this very first post on the topic we will stick to the basic cache manifest file which will list few css, javascript and image files to be stored offline.

Create a cache manifest file named ‘offlinecache.manifest‘ and add the following content:

CACHEMANIFEST
# version: 01
# List files that should be cached under CACHE
CACHE:
images/logo.jpg
images/background.jpg
js/jquery.js
js/slider.js
css/style.css
css/morestyles.css
# List the resources that shouldn't be cached and requires network
# Note you can use * to specify all the resources that are not listed under CACHE
NETWORK:
*

Add Cache manifest Mime-Type

You can name your cache manifest file anything the only requirement is that it should be sent to browser with the mime type “text/cache-manifest“. Assuming that your web site is hosted on Apache, and you have named your cache mainfest file as “.manifest”, you will need to add the following to the your .htaccess file:

AddType text/cache-manifest manifest

Publish your Cache manifest file

Now that your cache manifest file is ready to be served to the browser, you need to tell your browser about the manifest file location. The manifest file location needs to be added to the opening tag of html element with manifest attribute like following:

<html manifest="offlinecache.manifest">

You can specify absolute path or relative paths in the same way as you specify other URIs, if you have store your manifest file in the root of your document tree then your html file might look something like:

<!DOCTYPE html>
<html manifest="/offlinecache.manifest">
...
</html>

Congratulations!

Congratulations!!, If you have followed everything till here, your website will now make use of HTML5 offline storage as cache for static resources.

The famous Zen of python was written by Tim Peters. As now I will be writing posts here about Python too, so I thought I should start with the Zen of Python.

Small Easter Egg

If you have Python Interpreter you might want to try out the following.import this

The Zen of Python

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea — let’s do more of those!

Are you are WordPress developer and want to show twitter follower count on your wordpress blog or you want to show your Twitter follower count on your PHP based site? You might be wondering how to get that number from twitter. So here is the code snippet with details so you can write your own or you can just copy paste this one.

The new Twitter API allows you to read user information in XML and JSON format, so all you have to do is get the information in XML format and read the value between <followers_count> element.

Here is the code snippet for reading twitter followers count in PHP.

<?php$username="hameedullah";// Set this to username whose follower count you want to read.$twitter_api_url="http://api.twitter.com/1/users/show.xml?screen_name=$username";$api=curl_init($twitter_api_url);curl_setopt($api, CURLOPT_HEADER,0);curl_setopt($api, CURLOPT_RETURNTRANSFER,1);$xml=curl_exec($api);curl_close($api);if(preg_match('/followers_count>(.*)</',$xml,$match)!=0){$followers_count=$match[1];}echo$followers_count;?>

If you have any issues with the code snippet let me know in comments, or if you are looking for a code to get something else from the Twitter API let me know in comments too.

Blogging like a pro is the wish of every blogger. If you are a blogger or you want to start blogging you will definitely want to blog like a pro. Zemanta is the service that lets you blog like a pro by helping you find the related images, articles and videos to your posts.

Zemanta provides browser addons for almost all the browsers out there. All you have to do is just install the addon and goto your blog admin panel (I have tested with WordPress 3.0.2 but it works with all blogging platforms). As soon as you will goto the “Add New Post” i.e post editor of your blog you will find a new panel “Content Recommendations” on the right hand side of the editor.

Using Zemanta is as easy as 1, 2, 3. Below are the steps:

Step 1: Start writing your blog post.

Step 2: Zemanta will find the related content and show them to you in the Content Recommendation box.

Step 3: Add the images, videos, links from Zemanta recommendations to your blog by dragging them or click them.

Here is how it looked while writing this post.

Zemanta will not work with the post editor in HTML mode, you will need to switch to Visual editor to be able to use Zemanta.

What methods you use to find the related content to your post? Do you think Zemanta will help boost the blogging experience for bloggers? Write a comment to let us know your views.