Categories
Programming

Magento – Set Meta Title, Description, Keywords and Canonical URL via Layout XML

This handy XML can be used anywhere – your custom modules, CMS pages, product pages, etc.

[xml]
<reference name="head">
<action method="setTitle" translate="title" module="forms"><title>Title</title></action>
<action method="setDescription" translate="title" module="forms"><title>Description</title></action>
<action method="setKeywords" translate="title" module="forms"><title>Keywords</title></action>
<action method="addLinkRel" translate="title" module="forms"><rel>canonical</rel><href>http://www.yoururl.com/</href></action>
</reference>
[/xml]

Categories
Programming

Add a Layout Select Field to the Admin Form of Magento Custom Modules

I am coding a bespoke CMS feature for a Magento project. I wanted the client to be able to change the template / layout of each page. This is similar to the Layout selection found in the standard Magento CMS page edit screen. I added the following within the _prepareForm() function. This will display a layout selection dropdown on the Magento admin form page.

Categories
Programming

Magento – Adding Canonical URL for CMS Pages via Layout XML

Magento does not automatically add canonical URLs for CMS pages. Maybe they will add this feature in the future. However, if you are setting up pages and wish to add a canonical URL using the layout update xml, you can add the following code into the Custom Layout Update XML.
[xml]
<reference name="head">
<action method="addLinkRel">
<rel>canonical</rel>
<href>http://www.yoururl.com/goes/here</href>
</action>
</reference>
[/xml]

Categories
Technology

Install, Configure & Optimise MySQL on Debian

Step 1 : Adding the Dotdeb repository

You can skip this step if you have already added the Dotdeb repository while installing NGINX and PHP-FPM on your Debian Squeeze Server. We need to grab the latest version of MySQL to benefit from all new updates and bug fixes. Read this article for information on adding the Dotdeb repository to your Rackspace Cloud Server.

Step 2 : Install the MySQL server and client

Installation is simple. Just use the following command.

[shell]apt-get install mysql-server mysql-client[/shell]

You will be prompted to key in the MySQL ‘root’ password during the installation. Make sure the password is hard to crack. Once the install is over, you can run

[shell]mysql_secure_installation[/shell]

to remove the ‘test’ database and test users. MySQL should be up and running by now!

Step 3 : Configuring and optimising the MySQL Server

MySQL can get resource hungry. There are many articles around the web that talk about MySQL optimisation. The few settings that I have shown here will help free up some memory (RAM). Edit the following MySQL configuration file.

[text]/etc/mysql/my.cnf[/text]

The settings that are already in the MySQL configuration file are pretty decent. You can add just the following to free up some additional memory.

[text]
skip-locking
skip-innodb
key_buffer = 8M
[/text]

You can use the Perl script found at https://github.com/rackerhacker/MySQLTuner-perl to further optimise the database server.

Step 4 : Creating a new database user and database

We will need to create a new database and user for the WordPress installation. The following command should do that.
[shell]mysql -h localhost -u root -pPassword[/shell]
[sql]
GRANT USAGE ON *.* TO ‘newuser’@’localhost’ IDENTIFIED BY ‘password’
GRANT ALL PRIVILEGES ON `newdbname`.* TO ‘newuser’@’localhost’
[/sql]

This command will create a newuser with a password and assign the user to the new database with the name newdbname.

Step 5 : Restart the MySQL server

[shell]service mysql restart[/shell]

That’s it! You now have a working MySQL database server.

Categories
Technology

Install, Configure & Optimise PHP5 with PHP5-FPM

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
Source : PHP-FPM Website

PHP-FPM is far better than the standard mod_php implementation of PHP and also easier to implement that spawn-fcgi. PHP-FPM works like an application that loads and kills PHP instances as needed. Of all the benefits that it offers, reduced memory usage is the most attractive one.

Step 1 : Add the Dotdeb repository

You can skip this step if you have already added the Dotdeb repository while installing NGINX on your Rackspace Debian Cloud Server. The version of PHP and PHP-FPM in the Dotdeb repository is up-to-date and stable. We need to grab the latest version of PHP to benefit from all new updates and bug fixes. Read this article on adding the Dotdeb repository to your Debian Squeeze Server.

Step 2 : Installing PHP and PHP-FPM

The following command will do it all for you. I have included some popular PHP modules. You can choose and install the modules that you require.
[shell]
sudo apt-get install php5-cgi php5-cli php5-common php5-curl php5-dev php5-fpm php5-gd php5-idn php5-imagick php5-imap php5-json php5-mcrypt php5-memcache php5-mhash php5-ming php5-mysql php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-suhosin php5-tidy php5-xcache php5-xmlrpc php5-xsl php-pear php-soap
[/shell]
This command will install PHP-FPM and auto start the service. PHP-FPM will get added to the startup.

Step 3 : Configuring and Optimising PHP

As you will be using PHP-FPM to process PHP scripts, edit the php.ini file that can be found at /etc/php5/fpm/php.ini. There are a lot of PHP settings that you can configure to optimise PHP for performance, speed and security. The following three are a good starting point.
[text]
memory_limit = 64M
expose_php = Off
display_erros = Off
[/text]

Step 4 : Configuring and Optimising PHP-FPM

Remember, we are setting up a low-end (256MB) web server on Rackspace. We need to conserve as much memory as possible. Edit the file /etc/php5/fpm/pool.d/www.conf
[text]
pm = dynamic
pm.max_children = 10
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 4
pm.max_requests = 500
[/text]
This will bring down the number of children processes and free up some RAM.

Step 5 : Restart PHP-FPM

[shell]
service php5-fpm restart
[/shell]
This command will restart PHP-FPM.

Categories
Technology

Install, Configure & Optimise NGINX on Debian Squeeze

nginx (pronounced “engine-x”) is a Web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols, with a strong focus on high concurrency, performance and low memory usage. According to BuiltWith, it is used on 9.92% of the top 1 million websites, and its growth within the top 10k, 100k and 1 million segments is increasing.
Wikipedia

The purpose of this tutorial is to install and configure NGINX (to host WordPress sites) on a low end cloud server (256MB instance from Rackspace).

Step 1 : Add the Dotdeb repository

The version of NGINX in the Debian repository is slightly outdated. We need to grab the latest version of NGINX to benefit from all new updates and bug fixes. Read this article on Adding the Dotdeb repository to your Rackspace Cloud (Debian Squeeze) Server.

Step 2 : Install NGINX

The following command will do it all. It will also start the NGINX service and add it to the server startup.
[shell]apt-get install nginx[/shell]

Step 3 : Configure & optimise NGINX

The NGINX package from Dotdeb comes bundled with all the key modules. The configuration file is pretty good too. We’ll still need to make some changes as our cloud server instance is just 256MB. Open
[text]/etc/nginx/nginx.conf[/text] using your preferred editor. The following are some settings that you can change / add.
[text]
worker_processes 2
worker_connections 1024
server_tokens off;

# Timeout Settings

client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 5 5;
send_timeout 10;

# Gzip Settings – For improving (SEO) page speed

gzip on;
gzip_static on;
gzip_comp_level 6;
gzip_disable "msie6";
gzip_vary on;
gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss;
gzip_proxied expired no-cache no-store private auth;
gzip_buffers 16 8k;
gzip_http_version 1.1;
[/text]
The above are just a few critical ones. You can refer to the NGINX documentation if you would like to further optimise NGINX.

Step 4 : Create virtual hosts for multiple domains

Create the following folders to store the website’s files, scripts and logs. The third command will make NGINX (www-data user) own the website files.
[shell]
mkdir -p /var/www/domain/httpdocs
mkdir /var/www/domain/logs
chown -R www-data:www-data /var/www/
[/shell]
Create a domain vhost configuration file at this location.
[text]/etc/nginx/sites-available/[/text]
The filename can be anything. e.g. domainname.conf, domainname, domainname.tld, etc. Create a symlink to this domain’s vhost configuration file from [text]/etc/nginx/sites-enabled/[/text] using the following command.
[shell]ln -s /etc/nginx/sites-available/filename /etc/nginx/sites-enabled/filename[/shell]
The following content should go into the domain’s vhost configuration file.
[text]
server {
# This redirects the non-www version of the domain name to the www version
server_name domain.com;
rewrite ^ $scheme://www.domain.com$request_uri? permanent;
}
server {
server_name www.domain.com;
root /var/www/domain/httpdocs;
index index.html index.htm index.php;

access_log /var/www/domain/logs/access.log;
error_log /var/www/domain/logs/error.log;

location ~ \.php$ {
# The next two lines address a security flaw in NGINX.
# https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/domain/httpdocs$fastcgi_script_name;
}

# Disable index.php and make WordPress permalinks work
location / {
try_files $uri $uri/ /index.php?$args;
}

# Expires header to improve SEO (Google Page Speed)
location ~* \.(ico|css|js|gif|jpg|jpeg|png|xml|pdf)$ {
expires 1w;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
log_not_found off;
}

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}

# Don’t log and deny access to files which end with ~, as these are usually backup files.
location ~ ~$ {
access_log off;
log_not_found off;
deny all;
}
}
[/text]
The above settings should be sufficient to run a WordPress based website. We have also taken care of some key page speed improvements that Google recommends.

Step 5 : Restart NGINX

Once you have completed the above steps, you will have to restart NGINX for the new settings to take effect. You can use the following command to restart NGINX.
[shell]service nginx restart[/shell]

Step 6 : Point the domain’s A record to the server’s IP address

For the domain and the hosting to work together, you need to edit your domain’s DNS records and point the A records (@ and www) to your server’s IP address. Once the DNS change gets propagated around the web, you will be able to enter your domain name in the address bar of the browser and reach your website stored under the folder (/var/www/domain/httpdocs).

Categories
Technology

How to Add the Dotdeb Repository to Debian Squeeze

Dotdeb is a repository containing packages to turn your Debian boxes into powerful, stable and up-to-date LAMP servers
– Dotdeb About Page

The Dotdeb repo for Debian based Linux servers contains the latest stable versions of NGINX, PHP and MySQL. All three of these packages combined can provide us with a powerful yet light weight web server to host static and dynamic websites. The aim of this tutorial is to setup NGINX, PHP5-FPM and MySQL on a Rackspace Cloud Server to host a WordPress based website.

To add the Dotdeb repository to any Debian Squeeze server, edit the file

[text]/etc/apt/sources.list[/text]

and add these two lines

[text]
deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all
[/text]

at the bottom of the file. You will now need to fetch the GnuPG key for this repository. You can download the GnuPG key using wget and then add it to the server.

[text]
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add –
[/text]

Run [text]apt-get update[/text] to complete the repository addition.

Source : Dotdeb Website. Please visit the Dotdeb website for instructions on setting up the repository for Debian Lenny servers and updates on all the available packages.

Categories
Technology

How to Launch a New Rackspace Cloud Server Instance

In this tutorial I will demonstrate how to setup a Rackspace Cloud Server instance. The intended use of this sever is to host a low traffic WordPress website. All settings and configuration detailed in this tutorial are tailored for a 256MB Rackspace Cloud Server slice.

Categories
Et Cetera

Twitter Spam : Really horrible blog about you, might want to read this…

Sitting in my inbox today was an email from a friend on Twitter. The direct message read, “Really horrible blog about you, might want to read this…“.

Twitter Spam Direct Message

I am aware of emails that go out to people with a very tempting statement and then request that they click a link to read about it. Even though I am aware of such practices, I was tempted to click this link. I do blog from time to time and the fact that someone might be saying horrible things about me was quite shocking. I was very tempted to click the link. However, I decided to go into Twitter and check out my friend’s wall. The friend had posted a status where she mentioned that someone had hacked her account and sent these nasty messages. What a relief! I was so close to clicking on the link.

Updates – Similar Messages:

  • Hey guess what? i got to test and keep an iPhone 4S this rocks u should get one!
  • Bad blog going around about you, have you read it yet?
  • Someone is posting a pic of you all over Twitter

Spam! Delete and disregard it.

Categories
Programming

PHP : Convert or Cast Array to Object & Object to Array

I love PHP Objects. Given an option of returning any data as an Array or Object, I would go for Objects.

PHP Objects are clean and easy to write.

[php]
// Echoing a PHP Array value
echo $array[‘value’];

// Echoing a PHP Object value
echo $object->value;
[/php]

Now to the conversion (casting) of a PHP Array into a PHP Object. This is very simple. I just type cast the Array as an Object when returning it.

[php]
function array_to_object($array) {
return (object) $array;
}
[/php]

The above is just an example. You do not need a PHP function to convert an Array into an Object. The (object) function will do that to any PHP Array. If you ever need to change an Object into an Array, then use the (array) type casting function.

[php]
function object_to_array($object) {
return (array) $object;
}
[/php]

Now, back to some Codeigniter coding :)