Thursday, June 7, 2012

Setting up CentOS 6.x server for Magento 1.6 on fresh a Rackspace server

I was asked to create a new server on Rackspace to run Magento CE. It was me who had to take the decisions for suitable OS and hardware configuration. I went for CentOS 6.2 simply because I had worked on it before and there was lot of help available on internet for that. Actually I'm not particularly a server guy. I'm a web specialist. So I had to rely on whatever help I can get from internet. I eventually found everything I wanted but I hoped I could get everything under one single link. I learned this in reasonably hard way so decided to put it up for non-server type guys (like me). I know that PHP programmers are often rotten by the deadline and mostly they don't have time to understand why they are doing what they are doing - as long as it works for them. So I will try to make it as easy and as "laymanish" as possible.

So my first choice was simple. CentOS 6.2. Simply because I had 'some' experience on it. The second choice - of hardware configuration - was under my domain. Magento requires minimum 256MB memory limit and they recommend minimum 512M server RAM. Don't go by this number because they simply don't work. In practice you HAVE to have minimum 1GB RAM of server and 512M for PHP runtime. Even that is not enough if you expect sizable traffic on your site. So I created a server for 2GB RAM with 1GB intended RAM for PHP specifically. The rest of the post will assume that you have a CentOS 6.x server set up with 2GB RAM.

Enough with the story? Let's get to the action!!

Step 1 - Install LAMP

1. Login to your server using PuTty or any other terminal tool

2. Open this URL in a browser - http://blog.svnlabs.com/installing-apache-mysql-php-in-centos-5-5-on-rackspace-cloud/ . Follow each and every step exactly. This will install Apache, MySql, PHP and PhpMyAdmin to your server. While you are installing PhpMyAdmin will get stuck on the console after showing some log. Don't worry. Just abort the process by typing Ctrl+C. The PhpMyAdmin would have been installed successfully by then. Then follow the rest of the instructions.

3. Since the Apache is installed you will obviously try to open the IP in the URL to see if it shows the Apache's default page. This won't work straight away!! This is because the port 80 is not open. Type following command to open the port 80.

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

4. Since the PhpMyAdmin is installed you will try to open it in the browser. But it will require a password which you donot have. You have to set a password for root user of MySql to get in. For that run the following command.

mysqladmin -u root password NEWPASSWORD

For more information about changing the MySql password visit this link -http://www.cyberciti.biz/faq/mysql-change-root-password/

5. Php-mcrypt. is required for Magento. To install php-mcrypt you will have to first update the EPEL on your CentOS. By default php-mcrypt is not available in the centOS's repository so you have to get the extended package. In short, run the following command.

rpm -Uvh http://mirror.overthewire.com.au/pub/epel/6/i386/epel-release-6-7.noarch.rpm

NOTE: The epel releases' version keep changing. So in future the above link may not work. Go to http://mirror.overthewire.com.au/pub/epel/6/i386/ to find out the appropriate release. Copy the URL of that location and add to above command. For more information visit this link http://www.ctkn.net/tag/enable-epel-centos-6/

6. Now you can install php-mcrypt by running the command - yum install php-mcrypt

7. You need to install the SOAP as well

yum install php-soap

8. Open the /etc/httpd/conf/httpd.conf file and find for "AllowOverride". You will find this in two places. On both it would be set to "None". Change both setting to "All".

9. Run this command to set Mysqld service to auto start on reboot

chkconfig –-level 2345 mysqld on

Step 2 - Install Sendmail


1. By default the sendmail is not installed. So Magento will not send any system mails. You will have to install Sendmail on the server. Command...

yum install sendmail

Then follow the instructions provided in this link http://www.unixmen.com/configuring-
sendmail-smtp-server-on-centos-a-scientific-linux/

2. SMTP requires the port 25 to be open. Run following command to open it.

iptables -I INPUT -p tcp --dport 25 -j ACCEPT


Step 3 - Memory Limit

Is your Magento running slow? It would be definitely. The problem is the memory. You have got a server of 2 GB RAM (as per aforementioned spec) but your Magento is not utilizing it -  as yet. For that follow the below steps.

1. Increase the memory limit to 1GB in your php.ini file. This file is by default located in /etc/php.ini. And then run the phpinfo file. See if the master value changed to 1GB? Don't worry about the local value as it is coming from the .htaccess file. If master value is changed then you are good to move on to the next step.

2. Change the memory limit from .htaccess file. This will change the local value of memory limit in the phpinfo. Until this is done the Magento will not use anymore memory than the default 256M. Now check the site again. It would be significantly faster.

Step 4 - Install SSL

You can simply follow the instructions given in this link http://www.akadia.com/services/ssh_test_certificate.html or go by the following steps.

1. Generate the CSR file. Reference this link for more info http://www.rackspace.com/knowledge_center/index.php/Generate_a_CSR

2. Send this CSR file to the site owner and ask him to obtain a CRT file using this CSR file from a certificate authority. 

3. Once you have the CRT file use it to install the SSL on your server. For more info refer this link http://www.rackspace.com/knowledge_center/index.php/Installing_an_SSL_certificate

4. Run following command to open port 443 (for HTTPS) 
iptables -I INPUT -p tcp --dport 443 -j ACCEPT

After all these is done, install your Magento in the document root (by default in /var/www/html/). Your basic Magento should be running fine after performing all these steps. 

Good Luck!! You need it with Magento ... :)

Tuesday, October 4, 2011

Too many MySql connections - Magento

There was a weird problem I faced with Magento on Bluehost server. The site keeps throwing error saying there are too many connections with MySql database with a single user. On contacting the hosting provider they said they allow maximum of 15 concurrent connections for a single DB user. while 15 is not a good number by any means, it's not very bad for a newly started website either. It's less probable that 15 users are attempting connection to the DB at a moment. But the error log consistently showed me this error and it was far too often.

I tried to look at the phpinof.php file to see if there is anything abnormal related to the MySql settings. Yes, there was a surprise for me. The persistent connection was set to ON in that file. Apparently this had to be THE problem. When I checked in the php.ini file there was no such setting so I assumed that it comes from the default php.ini settings. So I set it to off in my PHP.ini file. Since then, I have not faced this problem.

Although this problem is solved for now, I'm sure it is going to come back at some point of time as 15 concurrent connection is not an adequate number. But it gives me a breath for the moment!

Wednesday, September 28, 2011

HTML 5 with Sencha and Adobe Edge

I'm venturing with HTML 5 these days. Currently I'm studying Sencha and Adobe Edge. These two are Flash like softwares which allow you to create animation through GUI program. Basically what you do is just creating number of frames that would combine up eventually to form an animation. Pretty much like flash. I'm really excited about this stuff. I'll keep posting my experiences. Thanks.

The reference links

Thursday, September 22, 2011

Flash Vs HTML 5

It really amuses me when people say that HTML 5 is the direct replacement of Flash. Internet chatters have started shouting like "HTML 5 is a miracle", "Who wants flash", "Flash is dead" and what not. So what really is this all about? Are they true? I'm sure anybody who has slightest of experience with Javascript programming will part his way from this bandwagon.

What is HTML 5 animation?

J Well, it’s just Javascript maneuvering. HTML has come up with a new tag element called “canvas” that is meant to be the medium of animation. Javascript is used to animate this canvas.

Can we do in HTML 5 whatever we can in Flash?

If we take it up as a separate project and decide to emulate whatever Flash can do then it could be possible on many cases. But it’s not feasible. Flash with all its powerful tools and utilities is a far superior and rapid option to create animation.

Why it is so hard?

Because you have to code it. Flash and HTML are meant for different purposes and have different philosophy. Flash does everything for you automatically while in HTML you have to actually code it. In most of the sites nowadays we see a banner rotator. Do they code it? No. They use JQuery. JQuery is a separate Javascript project which is built upon reams of coding over the years. But JQuery also has its own limitation. Even after all these years, JQuery has a limited set of pre-coded animations (that too basic ones) available. And here we are talking about replacing the boundless power of Flash. Because of this, barring some “prototype” examples we see for HTML 5 on net, it is not used for REAL animation anywhere.

So what's in the future?

Until someone comes up with a software that writes all the javascript automatically from a graphical interface for HTML 5 (just as Flash does it) it is going to remain an over hyped concept. The iPad/iPhone programmers will continue to use video files as the flash replacement.

In short, if you are not a web programmer, you will find the Flash Vs HTML 5 conversation interesting. Otherwise, you will just laugh at it and continue doing what you have been doing for years.

Monday, April 18, 2011

SEO For Google

Hi,

For some unknown reasons people do believe that as a website gets older it gets higher rank on Google SE. Because of this belief people start believing that just because more number of users are using the site, it gets upper rank. While this is true but the reasoning behind this is not correct. If it was like that, then all the web admins would do nothing but keep refreshing the page all the time!

I wonder, why the believers of this theory are not confronted by the question "how do Google know that how many people are using my site?". Think about it.

The most essential thing is the content of the site and the number of people who are interested in reading/searching that content on Internet. What happens is, when you search for something on the web and find something of your interest, you will click on that link. Google records your all the clicks against the search term you entered. Now as more and more people search for that term and start clicking on your link, Google ranks your site higher, simply because you have the content which interests the users. Simple!

Now, Google has introduced the +1 function for each search. So when people search for a keyword and find your site interesting, people will do +1. Need I say that this would boost your SEO greatly? ... :)

So here is the rule. it really doesn't matter how old your site is. What matters is your content which would interest people. Smart way, isn't it?

Thursday, January 20, 2011

When to use Stored Procedures?

Hello,

Before some days, I talked about system calls. While including a file to the script is a system call, connecting to database server is also a system call. So obviously it will make the site slow.

There is a popular belief that SPs are helpful in getting faster database performance. But very few know why. Most people believe that it is because the code is pre-compiled. This is not true. The fact that you can pass variables to a SP, it has to be dynamic and cannot be pre-compiled. So. is SP really faster as they say? The answer is Yes and No. A SP is fast is it is used for appropriate reason. Otherwise there is no difference. So what is the right use of SP?

As I said in the beginning, all the database calls are system calls. So the site becomes slow when you fire more and more queries on the database. In most occasions this is inevitable. But in some cases there is an alternative. Sometimes when you are doing database operations, you fire a query to get some result. Now based on this result you have to fire another query to get some more result. Also, this process may not be possible joins. So in this case, you would be firing two system calls. So instead of firing two queries, just create a stored procedure that would fetch the first value from database, do the necessarily operation, fire the next query and give you the result as the output. Thus you make only one system call. The fact that the stored procedure is the part of the database engine, it would not make extra system call when it is firing a query. So here you go, you get two query worth output with only one system call. so it is now faster!

Hope I made sense here.

Monday, January 10, 2011

What makes your site slow the most? System Calls.

Hello,

Today I will talk about a highly discussed topic. But I'm not going to talk about the usual reasons given for this. I'm going to talk about a much unknown and and un-talked factor. The system calls.

Believe it or not, the most time taking process in site execution is the "System Calls". Whenever you "require" or "include" a file, it makes a system call on server to include that file in your script. So it depends upon how many files you are including in your script. In today's world of MVCs, there is literally no limit of the files you include. This is not to take away the fact that MVCs are marvelous platforms to build any web application (for many reasons) but they do have impact on the speed. The reason behind Magento being slow is much more this.

If you want a phenomenal speed, write the whole code on a single file! I know it sounds ridiculous but the applications who have a HUGE amount of load everyday, do exactly this! Example SourceForge and GForge. They have millions of lines of code in a single file.

However, MVCs do have the work around for this problem. They do caching. Using caching they evade this short coming greatly. And we all know the power of MVCs.

So guys, keep using MVCs for all the reasons. But it's just that when you create a SourceForge or GForge, avoid them!