Archive

Archive for July, 2008

HyperVM openvz migration

July 23rd, 2008 Behzad No comments

You may faced situation when you hang to do a simple job. Yes, there are most of times, simple answers to resolve issues, but not mentioned anywhere. One of those is when you want to migrate a vps from one node to another with hypervm. You are lucky if you do this without getting any error, and if so, why do you reached this post? ;)

After 6 hours of hammering my head, I got it to work and I post this here to help you and others.

Before starting migration, you MUST do steps below if you want to success:

1. Make sure partition of /usr/local/lxlabs/hypervm/serverfile/tmp on source HN have enough space to dump vps into, or just remove it and create a symlink to another folder/partiotion which have enough space.

2. Make sure /tmp partition on destination HN have enough space, if not you can mount –bind it to another folder in another partition. (only if /tmp is partition, if not just create symlinks) for more info see: http://www.ibm.com/developerworks/library/l-fs3.html

3. Disable firewalls on both HN, so they do not block each other packets (simply disable/enable firewalls, openning ports does not give you the results always.)

4. Ssh to dest HN, from source HN, and make sure you can login without prompt of password. (Using public keys)

5. Now run

cd /usr/local/lxlabs/hypervm/httpdocs;

/usr/local/lxlabs/ext/php/php ../bin/common/switchserver.php ––priority=low ––class=vps ––name=VPS_NAME.vm ––v-syncserver=DEST_HN_IP

(change VPS_NAME and DEST_HN_IP with proper values)
6. check logs with command tail -f /usr/local/lxlabs/hypervm/log/shell_exec on both servers, and find any errors.

Take a cup of coffe and relax, I hope :)

After migration, you must change IP of new vps throught hypervm and cPanel/Plesk/DirectAdmin…

Top 20 Google Ranking Factors

July 20th, 2008 Behzad No comments

Websites are quickly becoming one of the most popular ways of advertising. Whether it be a business, its product or service or something completely different, everyone of all ages are turning to the web as a method of getting their message out there. With the popularity of this marketing medium increasing and the number of websites always growing, it is obvious that everyone wants to appear at the top of Google’s search engine rankings. Achieving such a task is never an easy feat, however with a bit of perseverance, one can definitely increase their chances of reaching that glorious first page result.

Given that there are a heap of websites out there who are on the first page, what is their secret? It is a little industry term called “SEO” and it stands for Search Engine Optimisation. SEO basically consists of the customisation of your website, its content and its internal and external links to assist in the overall indexing and ranking of your website in popular search engines. There are many contributing factors that are used in determining a website’s ranking and every search engine is different. This makes trying to optimise your site for Google, Yahoo, Live and the many others quite a pain staking task.

As most of us are aware, Google is currently the most popular search engine for the majority of Internet users. As such, it is only normal that we’d want to focus our sights on achieving a higher ranking within Google first with a hope that the rest will follow. To do this, we must start a journey that could potentially take months before we start seeing any real change, however we have to start somewhere.

Our journey begins by defining some of the key contributing factors that Google uses to determine a website and webpage’s ranking within its results. These factors range from keyword use to manipulating internal and external links and the list goes on. To get you started, we have listed the top twenty factors that you should focus on in order to help get your website that little bit closer to the top of the search engine results listing.

The following components relate to the use of the user’s search query terms in determining the rank of a particular page.

Top 20 Factors

  1. Keyword Use in Title Tag – Placing the targeted search term or phrase in the title tag of the web page’s HTML header.
  2. Keyword Use in Body Text – Using the targeted search term in the visible, HTML text of the page.
  3. Relationship of Body Text Content to Keywords – Topical relevance of text on the page compared to targeted keywords.
  4. Keyword Use in H1 Tag – Creating an H1 tag with the targeted search term/phrase.
  5. Keyword Use in Domain Name & Page URL – Including the targeted term/phrase in the registered domain name, i.e. keyword.com plus target terms in the webpage URL, i.e. seomoz.org/keyword-phrase.
  6. Link Popularity within the Site’s Internal Link Structure – Refers to the number and importance of internal links pointing to the target page
  7. Quality/Relevance of Links to External Sites/Pages – Do links on the page point to high quality, topically-related pages?
  8. Age of Document – Older pages may be perceived as more authoritative while newer pages may be more temporally relevant.
  9. Amount of Indexable Text Content – Refers to the literal quantity of visible HTML text on a page.
  10. Quality of the Document Content (as measured algorithmically) – Assuming search engines can use text, visual or other analysis methods to determine the validity and value of content, this metric would provide some level of rating.
  11. Global Link Popularity of Site – The overall link weight/authority as measured by links from any and all sites across the web (both link quality and quantity).
  12. Age of Site – Not the date of original registration of the domain, but rather the launch of indexable content seen by the search engines (note that this can change if a domain switches ownership).
  13. Topical Relevance of Inbound Links to Site – The subject-specific relationship between the sites/pages linking to the target page and the target keyword.
  14. Link Popularity of Site in Topical Community – The link weight/authority of the target website amongst its topical peers in the online world.
  15. Rate of New Inbound Links to Site – The frequency and timing of external sites linking in to the given domain.
  16. Anchor Text of Inbound Link.
  17. Global Link Popularity of Linking Site.
  18. Topical Relationship of Linking Page.
  19. Link Popularity of Site in Topical Community – The link weight/authority of the target website amongst its topical peers in the online world.
  20. Age of Link

Negative Crawling/Ranking Factors

There are also some points we should make before you start getting your hands dirty. With any type of SEO marketing, there are some things that can actually have a negative impact on your ranking. These following components may negatively affect a spider’s ability to crawl a page or its rankings at Google.

  • Server is Often Inaccessible to Bots
  • Content Very Similar or Duplicate of Existing Content in the Index
  • External Links to Low Quality/Spam Sites
  • Duplicate Title/Meta Tags on Many Pages
  • Overuse of Targeted Keywords (Stuffing/Spamming)

It’s now time to get busy! Start prioritising your tasks, modifying your content and building your internal and external links to meet some of the above guidelines. Keep in mind that improving indexing is mostly a technical task, improving ranking is mostly a business/marketing strategy, what might work now may not work in the future and finally, it takes time. Loads of time. Still, with a bit of trial and error and a good dose of persistence, you can achieve the search engine ranking you’re after.

Tips for Optimizing PHP code

July 20th, 2008 Behzad No comments

Here is the list of short tips you can use for writing an optimized and more efficient PHP code:

  1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
  2. echo is faster than print.
  3. Use echo’s multiple parameters instead of string concatenation.
  4. Set the maxvalue for your for-loops before and not in the loop.
  5. Unset your variables to free memory, especially large arrays.
  6. Avoid magic like __get, __set, __autoload
  7. require_once() is expensive
  8. Use full paths in includes and requires, less time spent on resolving the OS paths.
  9. If you need to find out the time when the script started executing, @import url(“css/widgContent.css”); INSERT:CONTENT:END SERVER['REQUEST_TIME'] is preferred to time()
  10. See if you can use strncasecmp, strpbrk and stripos instead of regex
  11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
  12. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
  13. It’s better to use select statements than multi if, else if, statements.
  14. Error suppression with @ is very slow.
  15. Turn on apache’s mod_deflate
  16. Close your database connections when you’re done with them
  17. $row['id'] is 7 times faster than $row[id]
  18. Error messages are expensive
  19. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
  20. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.
  21. Incrementing a global variable is 2 times slow than a local var.
  22. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
  23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
  24. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
  25. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance.
  26. Methods in derived classes run faster than ones defined in the base class.
  27. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.
  28. Surrounding your string by ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string.
  29. When echoing strings it’s faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments.
  30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts.
  31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times.
  32. Cache as much as possible. Use memcached – memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request
  33. When working with strings and you need to check that the string is either of a certain length you’d understandably would want to use the strlen() function. This function is pretty quick since it’s operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick.Example:

    if (strlen($foo) < 5) { echo "Foo is too short"; }
    vs.
    if (!isset($foo{5})) { echo "Foo is too short"; }
    Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it’s execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string’s length.

  34. When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don’t go modifying your C or Java code thinking it’ll suddenly become faster, it won’t. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend’s PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
  35. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory.
  36. Do not implement every data structure as a class, arrays are useful, too
  37. Don’t split methods too much, think, which code you will really re-use
  38. You can always split the code of a method later, when needed
  39. Make use of the countless predefined functions
  40. If you have very time consuming functions in your code, consider writing them as C extensions
  41. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview
  42. mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%
  43. Excellent Article about optimizing php by John Lim