Archive for category Web
15Oct/091
How to speed up loading of your website
Is your website slow? Do you even realize that it could load faster?Every webmaster tries to make web sites faster, so people will get better user experience. But there are few tricks that not everyone is aware of.
How to improve loading speed?
- cache static content (pictures, javascripts, css files)
- remove ETag from pictures
- compress content
First thing is to cache static content so that it can be loaded from visitor's computer and not from server on every visit. Caching will also decrease bandwidth. It's good to set expire tag for at least a year.
Every picture sent by a server include ETag by which browser can check if a picture from a server match picture from browser's cache. This is not needed on static content, which will never change (for example, uploaded pictures).
And don't forget about compressing content. Loading will be faster because a browser needs to download a compressed file which is smaller and it will save a little bit of bandwidth. Compression is good for static and also dynamic content (.htm/.html/.css/.js/.php).
Example .htaccess file:
Header unset ETag
FileETag None
<FilesMatch "^.*\\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
<FilesMatch "\\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
You need to put this file in a root directory of your website. Be careful, file doesn't have any name, only extension "htaccess". If you're using nice/SEO URLs then there is a good chance of you having this file already in a root directory. Only thing you need to do is to open existing file and copy this example code on the bottom of the file. It will only work with Apache HTTPD Server (most popular http server).
Compressing dynamic PHP file
To compress PHP file you need to add following code on the top of the file:
<?php
ob_start("ob_gzhandler");
?>
Or a bit safer method, that will turn off GZIP compression if visitor's web browser doesn't support it:
<?php
if(!ob_start("ob_gzhandler")) ob_start();
?>
You can check site's performance with YSlow add-on for Firefox's developer plugin named Firebug.
Download: http://developer.yahoo.com/yslow/
7Oct/096
Convert video to FLV with PHP
How to convert video to .flv with php so that it can be played by Flash Player?This is a simple approach to converting videos to .flv with PHP. For example, users are allowed to upload videos, and then you can play it by Flash Player.
Installing FFmpeg
Probably you already have PHP installed, so let's continue with FFmpeg. Following commands works only in Debian/Ubuntu distribution.
To install FFmpeg you simply need to type:
apt-get install ffmpeg
FFmpeg is a library for converting video and capturing screenshots from video.
Now you need some sort of a plug in to allow PHP to access ffmpeg. So we need ffmpeg-php, which can be installed by following command:
apt-get install ffmpeg-php
Now you should be able to use it.
Example PHP script:
<?php
if($_FILES['file']) {
if (!$_FILES["file"]["error"] > 0) {
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
$movie = new ffmpeg_movie($_FILES["file"]["name"]);
$srcWidth = $movie->getFrameWidth(); //Movie width
$srcHeight = $movie->getFrameHeight(); //Movie height
$srcFPS = $movie->getFrameRate(); //Movie frame rate
$srcAB = intval($movie->getAudioBitRate()/1000); //Audio bit rate
$srcAR = $movie->getAudioSampleRate(); //Audio sample rate
exec("/usr/bin/ffmpeg -i ".$_FILES["file"]["name"])." -ar 22050 -ab 32 -f flv -s ".$srcWidth."*".$srcHeight." new_movie.flv");
//Convert a movie from original type to FLV
exec("/usr/bin/ffmpeg -i new_movie.flv -an -ss 00:00:05 -an -r 1 -vframes 1 -y new_movie%d.jpg ");
//Make screenshot of a movie at 5th second
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="file" /> <input type="submit" value="Upload" />
</form>
This script saves an uploaded file, converts it to FLV and makes a screenshot of the movie at 5th second.
3Oct/091
VPN or how to access US sites abroad
If you live outside US you cannot watch Hulu/CBS/etc. videos on demand because you need US IP. The other reason to use VPN is a secure connection to VPN provider. So your ISP doesn't know what are you browsing until you are connected to VPN.There is some free and some payable services. Payable usually have no problems with speed and blocked IPs.
1. AnchorFree Hotspot Shield
It is a great service and it's free too. Downsides are that their IPs are blocked from VoD (video on demand) web sites and connection is not the fastest. Another downside is that you need to install their program in order to get service to work.
2. Payable service HappyVPN
I decided to try a payable service and I heard a lot good about HappyVPN, so I decided to give it a try.
It's 14.99$ per month and you will get unlimited access. Nice offer. So I payed for the first month and watched a few of episodes of various TV shows. Sometimes I got disconnected but I weren't worry about it too much. And after a week or a liitle bit more I am not able to even connect to this service.
So I decided to cancel my subcription to them.
You can try if you want. They offer payment by credit card or PayPal.
3. The best option is it's own server or VPS (Virtual Private Server)
They are from 20$ per month and you will really get unlimited access. There is only one downside, they have limited bandwidth.
So I bought one node from VPS.Net and try it out. You can pay with PayPal (I prefer this method, however they do not offer automatic payment through PayPal, like GoDaddy does) or credit card.
How to buy a node and create VPS at VPS.net
1. Go to http://vps.net/, select one node (or more if you need more bandwidth or multiple VPS for some reason) and click Buy Now!
2. Now you need to type your information and select your payment type. Then pay for it.
3. Go to your account and select Create new VPS.
4. Type VPS label and host name. For host name you can use your domain or sub domain. You don't actually need a domain.
5. Then select your cloud. UK to get access to UK sites (like BBC iPlayer) or US cloud to get access to US web sites (like CBS or FOX).
6. Select Debian 5.0 (Lenny) x64 and in the bottom box select VPN image. Then click Create.
7. You will be redirected to a page with your VPS's information. Wait a minute or two and then hit refresh.
8. Your VPS should be ready now. Use SSH program from Terminal (Mac/Linux) or Putty for Windows.
9. Connect to IP of your VPS and login as root with default password written on VPS's information page.
10. Type following command to edit username/password to access VPN server:
nano /etc/ppp/chap-secrets
11. Go to the end of file and type something like:
username pptpd password *
(change username and password with your desired username/password)
12. Save file and exit (Ctrl + C and then type "Y" (without quotes))
13. Type following command to reboot pptpd service in order to make VPN work:
/etc/init.d/pptpd restart
That's it for server side.
How to connect to the VPN network with Mac
Now that you bought VPN access or created VPS with VPN service you need to prepare your Mac to send all your Internet connections trought your VPN.
1. Go to System Preferences.
2. Go to Network and press on "+" button in the bottom left corner. Then change interface to VPN, select PPTP as VPN Type and enter your desired VPN Service Name.
3. Now type your IP address and username:
4. Click on Advanced and set up like in following picture:
5. Click Ok and then Apply. Now you need to type your password and hope everything is allright.
Everything should work now. You can check by going to http://www.ip-adress.com/ and if IP on site match your VPS's IP then you're good.
I'll write more about connecting to VPN on Windows in next blog post.
29Sep/090
Essential security tips for servers
In this tutorial you will find out some of the necessary steps to secure you VPS or dedicated server.1. First thing you need to do is to change your root password. Connect to server with SSH and type:
passwd
Then type in your new password, press enter, and retype it again.
2. It's not safe to allow root user to login throught SSH. So you need to create a new user:
useradd user_name
Then set the password for this user with:
passwd user_name
3. Now let's go to SSH settings.
Open file by typing:
nano /etc/ssh/sshd_config
And find/change following:
PermitRootLogin no
X11Forwarding no
AllowUsers user_name
Port 10000
It's important to change your SSH port to higher number (for example 10000).
4. Configure iptables:
iptables-save > /etc/iptables.rules
nano /etc/iptables.rules
Example rules:
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -i ! lo -j DROP
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10000 -j ACCEPT #Change this port to SSH server's port
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-A INPUT -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -j ACCEPT
COMMIT
Be careful to change your SSH port to the one you set in sshd_config, otherwise you wouldn't be able to log in to SSH server.
Import rules to iptables:
iptables-restore < /etc/iptables.rules
5. Set new iptables rules to reset during reboots:
sudo nano /etc/network/interfaces
...
auto lo
iface lo inet loopback
pre-up iptables-restore < /etc/iptables.rules
...
6. And now reload SSH server:
sudo /etc/init.d/ssh reload
These are only essential steps to secure you server. Your server should now be a little bit more secure, but there is no such thing as 100% security.
27Sep/090
Setting Charset on an Apache Server
How to demand from an Apache Server to serve pages with a specific encoding?You will ask this question sooner or later if you are making web sites. Probably sooner if you are dealing with non-ASCII characters.
There are a lot of charsets in the world, from ones that support most languages to ones that are very specific. In web development business UTF-8 is a standard, specially on non-english web sites. It's not that big of a problem on HTML sites, but it can be a hell of a mess if you have server with first encoding, PHP with second encoding and MySQL with completely different encoding.
In this post, I will show you how to tell Apache to use specific charset (in our examples I'll use UTF-8).
The easiest way to set encoding is by following meta tag in HTML:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
But this is not always enough. So to be sure you need to specify charset in the Apache configuration file or in .htaccess.
You can use one of the following methods:
<FilesMatch "\\.(htm|html|css|js|php)$">
ForceType 'text/html; charset=UTF-8'
</FilesMatch>
or
AddType 'text/html; charset=UTF-8' html
or
AddCharset UTF-8 .html
or
AddDefaultCharset UTF-8
I recommend last option because I had no problems what so ever with it so far. It always works fine. (Other should too.)




