Loose discussion about accelerating of websites and web applications. More details on each topic you will find on Google.
PHP is a scripting language means every user request makes the need to compile a script into the language understood by the server’s processor.
Since version 5.5 PHP has built-in Opcache mechanism (not enabled by default), which skip compiling the script during website calls, resulting in faster operation.
Some recommendations for the programming:
1. Avoid loop or recursive queries to the database. Eg. when creating tree data structure – categorized multi-level menu. It is better to get all data by one query, and then in the loop and/or recursion to generate a structure based on the query result.
2. Do not repeat yourself:
$x = array(1,2,3,4,5,6,7,8,9);
for ($y = 1;$y <= count($x); $y++) echo $y;
9 times is called count() function. It would be better to assign the result to a variable, and this variable substitute into the loop. Means:
$x = array(1,2,3,4,5,6,7,8,9);
$r = count($x);
for ($y = 1;$y <= $r; $y++) echo $y;
CSS, HTML, JS
Do not load CSS and JS files into pages that do not require it.
3. Combine files of one type (css, js) in one complete file. This reduces the number of http requests of web browser.
4. Use the public repositories of popular libraries, eg. jquery. Then you do not have o keep them on your own server and you reduce the number of requests that each browser is limited to the maximum number of simultaneous requests. Google Cdn provides the popular libraries like jQuery or Angular Js. Use cdn carefully, because the time is taken also on the resolving of dns domain names.
5. Use HTML5 and CSS3 – compared to predecessors they have many facilities in the writing of the code, for example rounding corners.
6. If you have many small files, eg. icons for editing, deleting, use so-called CSS Sprite.
It is a combination of many small files into one large file and reading them on the page using css background-color attribute. A tool that completely automates these steps is for example css.spritegen.com.
1. Do not keep large images on your website. There are tools that reduce the volume of image files without losing graphics quality (for the human perception ability). An example is the Yahoo tool – smush.it.
2. If you have a single small graphics to be placed along with the text (the text of a similar size as the image), do not do it using the img tag. Apply instead the base64 function to encode the image and display it on the screen. Then you will reduce the number of http requests.
3. Watch out for stealing transfer, if you have a popular website with nice graphics or movies someone may wish to use your media. In this case, you may face of robbery even double: by copying the content without your permission and the manner in which the thief put your picture on his website – in the src attribute of the img tag, gave the address of your server with the image source. Thief’s website:
<html> <body> <img src=”path/to/image/on/server/that/I/found/myself/and/assigned/authorship/of/their/content”> </body> </html>
In this way you lose the transfer. There are several ways to protect against this, for example renaming periodically images or folders in which they are located. More details in google – “bandwidth theft” or “hotlinking”.
But you can do it legally, hosting images on other servers purchased by you, saving on the transfer of your main server.
1. Memcached – communication system with a database that keeps the query results in server memory. The principle is simple: make a query, the result is stored in the memory with a unique identification key for each query, next system checks whether the result of this query is available in RAM memory, if so it returns, if not – connects to the database again. It is a very fast cache system because reading from RAM memory is many times faster than reading from disk storage. We do not worry about the extinction of ram, because the servers are working non-stop, in contrast to our home computers.
2. Pull data from a database from numeric fields, make indexes on the field by which you search or group data.
1. gzip compression – html, js or css code have a lot of repeated elements such as tags name. Gzip compression converts the strings on the numbers, so the file weighs less. And now if the browser supports gzip compression, decodes the file sent from the server. This is another way to reduce the transfer.
2. mirrors – a copy of the website on another server. DNS can be configured to return a few IP addresses of computers on which you have the same website. Then the browser will draw which computer to connect to. That way you get balanced distribution of load and to keep customers comfortable, who quickly will load the page (once with one server, one from the other). You can also put the database on a single server, and several of the same front-ends to the other.
3. Varnish – proxy server between you and the target server. It caches HTTP requests. When you connect to a normal server, it prepares in turn what you asked of him: css, html, graphics, data from the database and sends you the result. And so every time. When you have a varnish – the result of your query will go to him and the next time it will get quicker response to the same http query.
4. Do you have too many http requests that your browser has made at one time to perform the content of your site? You can set up a separate sub-domain eg. cdn.domain.com and here upload photos. The browser treats it as a separate server.
BLOCKING IP ADDRESSES
If you run a local business, and only support local clients, such as hairdresser, you do not need to allow visiting your website spiders from China, etc. If you are using a popular CMS systems, you can search for special plugin for this purpose, or ask the administrator of your website . You relieve in this way your website from unnecessary traffic and decrease the risk of hacker attack, as well as the amount of spam on your email or contact form.
Set Expiry header in the .htaccess file. Image files, css, js can be stored by your browser on set in .htaccess file specific time. This way you reduce the load on the server.
If you are using WordPress cms (or any other), consider the use of plugins that optimize and accelerate action on your website.
W3 total cache – a very powerful tool to cache content of the website.
Db optimizer – plugin that optimizes wordpress database: removes comments marked as spam, page reviews and others.
That’s it. There is no need to urgently use all the techniques described above, it depends on the webside load. The more traffic you have the more you have to take care of it’s performance optimization.
enroll in the course of Laravel framework >>>> or affiliate program