Monday, May 21, 2012

Amazon EC2 Micro Instance Swap Space - Linux

I have a Amazon EC2 Linux Micro instance. Since Micro instances have only 613MB of memory, MySQL crashed every now and then. After a long search about MySQL, Micro Instance and Memory Managment I found out there is no default SWAP space for Micro instance. So if you want to avoid the crash you may need to setup a swap space for your micro instance. Actually performance wise is better to enable swap.

Steps below show how to make a swap space for your Micro instance. I assume you have AWS Account with a Micro instance running.

  1. Run dd if=/dev/zero of=/swapfile bs=1M count=1024
  2. Run mkswap /swapfile
  3. Run swapon /swapfile
  4. Add this line /swapfile swap swap defaults 0 0 to /etc/fstab  
Step 4 is needed if you would like to automatically enable swap file after each reboot. 

Some useful command related to SWAP space:

Sunday, May 20, 2012

Mount Amazon EBS Volume as an External Drive

This article shows the steps needed to mount an EBS volume to a running Linux instance. I assume you have AWS account and you have access to AWS Console.

List below are the needed steps:

  1. Create your EBS drive under volumes in your AWS Console
  2. Attach it to your instance and you should get a reference for it. e.g. /dev/sdf
  3. Connect to your instance via SSH
  4. ls /dev should now show the created volume
  5. Format your volume by mkfs.ext3 /dev/sdf
  6. Run sudo mkdir /ext
  7. Run sudo vim /etc/fstab. Instead of vim any other editor can be used.
  8. Add  "/dev/sdf /ext ext3 noatime 0 0" to end of the  /etc/fstab
  9. Run sudo mount /ext
  10. Run sudo df -h /ext and you will see you drive

This volume can be used as a external drive for daily or weekly backups or any other purposes.

LAMP Server on Amazon EC2

This article shows how to build a LAMP on Amazon Cloud.

I assume that you already have an Amazon EC2 account and Amazon Linux AMI is launched. After you launched your image you can connect to it via ssh and get access to your machine shell to run your commands.

List below shows the steps to build Apache, PHP, MySQL on your Linux:

sudo yum update
sudo yum install httpd
sudo yum install mod_ssl
sudo yum install mysql
sudo yum install mysql-server
sudo yum install php
sudo yum install php-mysql

After you installed them you need to start all the services like below:

sudo /etc/rc.d/init.d/mysqld start
sudo /etc/rc.d/init.d/httpd  start

If you reboot the system you have to restart them or you can do something like below:

sudo rm /etc/rc.d/rc3.d/K15httpd
sudo rm /etc/rc.d/rc3.d/K36mysqld
sudo ln -s ../init.d/mysqld /etc/rc.d/rc3.d/S30mysql
sudo ln -s ../init.d/httpd /etc/rc.d/rc3.d/S85httpd

You can find config files for services as needed :

sudo mcedit /etc/httpd/conf/httpd.conf
sudo mcedit /etc/my.cnf
sudo mcedit /etc/php.ini

There are some useful additional packages :

sudo yum install mysql-devel -> For Sphinx Search Engine
sudo yum install php-bcmath  -> For some PHP function like bcadd  
sudo yum install php-xml     -> For XML support in PHP      
sudo yum install mlocate gcc gcc-c++ autoconf automake 
sudo yum install mc            -> mcedit command        
sudo yum install poppler-utils -> pdftotext

After installation, you need to add HTTP or HTTPS to your instance security group from your AWS Cosole  to be accessible via web. Also you can assign your Elastic IP to any domain.

At the end I like to mention some useful commands for Linux shell :

Connect to MySQL:
mysql -p --password='yourMySQLPassword' -u dbUser;

Secure MySQL for the first time after installation:
sudo /usr/bin/mysql_secure_installation

Backup you MySQL Database into a file:
mysqldump -p --password=' yourMySQLPassword ' -u dbUser dbName > fileName.sql 

Move Files between servers:
scp -v -i yourInstanceKeyPair.pem fileToBeTransferd 
ec2-user@YourPublicDNSFromAwsConsole:/path/to/destination