Wednesday 28 March 2012

Resetting MySQL root password

Set / change / reset the MySQL root password on Ubuntu Linux. Enter the following lines in your terminal.
  1. Stop the MySQL Server.
    sudo /etc/init.d/mysql stop

  2. Start the mysqld configuration.
    sudo mysqld --skip-grant-tables &

  3. Login to MySQL as root.
    mysql -u root mysql

  4. Replace YOURNEWPASSWORD with your new password!
    UPDATE user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit;
Note: This method is not regarded as the securest way of resetting the password. However it works.


References

MySQL 5.0 Reference Manual: How to Reset the Root Password

Friday 23 March 2012

Launching and Tearing down AWS wordpress instance


Introduction

Most blogs and small business web sites are hosted on shared hosting services, where you pay a fixed monthly fee in exchange for a predefined set of features and installed software. Most of the time, it’s a perfectly valid option, but running your blog in Amazon’s cloud comes with benefits that make it an option worth considering:

Easy to upgrade. Creating a new server machine in Amazon’s cloud can be accomplished in a matter of minutes. So by using the service, you are always prepared to handle instant jumps in traffic. If a post jumps to the front page of Digg or Reddit, you can add more servers and balance the load. accordingly.
    Cost-efficient power for large scale services. If your site attracts a lot of users, and eventually outgrows the limits of your regular hosting, Amazon Web Services gives you free hands in designing a server architecture that matches your needs, in a cost-efficient way, without needing to hire a complete network operations team to do the setup for you! This is how many of the most popular Facebook developers have built their servers.
    Total control over the setup: Though this point might not be too important when running WordPress, if you want to get adventurous and build your next project using something more exotic than a combination of PHP and MySQL, you don’t have to ask your web hosting company. You install the software you wish, just like you would on a server of your own.

Whether or not hosting your WordPress site on Amazon is the right choice depends on the traffic your blog receives, as well as how much you value the opportunity to configure your server exactly to your needs.

Step 1 - Create an Amazon Web Services Account

Before you can start using Amazon’s cloud for your web hosting needs, you need to create an account with Amazon Web Services (AWS). Point your browser to aws.amazon.com and click on the “Sign Up Now” button.
If you’ve used Amazon.com before, you can use your same e-mail and password to login to AWS. You can also create a new account by choosing “I am a new user.
At this stage, you will need to enter your contact information and credit card number so that Amazon can bill you for any services you use. Creating an account is free, though everything else will cost you money.
The pricing model is rather simple — you pay for every hour your server is running — but because of the variety of options, calculating the actual price can get quite a bit more complex than that. When running your own site at Amazon, it’s best to be careful and double check your price calculations before leaving the servers running unattended so you don’t get any surprises at the end of the month.
Building our WordPress setup shouldn’t last but an hour, and we will stop everything when we’re finished, so the price for completing the tutorial will be close to zero.

Step 2 - Launch Your First Server Machine

Our server setup will consist of two parts:   

Application server hosted on Amazon’s Elastic Computing Cloud (EC2), running Apache and PHP, with WordPress installed on it.
MySQL database hosted on Amazon’s Relational Database Service (RDS).
    Another cheaper, but more difficult, option would be to manually install the MySQL server on the same machine with Apache and PHP. In this tutorial, I decided to go with the Relational Database Service because of its ease of use, as well as the fact that, this way, Amazon will take care of backups and maintenance for the database, and you won’t have to worry about that level of additional security.
    Let’s start by creating our first application server instance.
    Click on the EC2 tab in the AWS Management Console. As this is your first time using the EC2 service, you will need to separately sign up for it. Click on the “Sign Up For Amazon EC2″ button:
    As a special step, Amazon will verify your identity by placing a call to your phone. The whole process is automated: you type a code from the web page into your phone and, once the system has verified that the codes match, you will be redirected to the next page.
    When you are finished with the sign up process, return to the Management Console and the EC2 tab. You will see a page that looks like this. Click on the “Launch Instance” button.
    A popup with a list of Amazon Machine Images (AMIs) opens.
    (AMI's are snapshots running computers)

    Most consist of an operating system and a few utilities, but you can create AMIs with as much complexity as you wish — even containing your full application, ready to be started on a virtual machine with a click of a button.
    We will pick the most basic option: “Basic 32-bit Amazon Linux AMI 1.0.” Click on “Select” next to the AMI and you are ready to go.
    After selecting the AMI, our next step is to decide what kind of hardware we will use to run it. Depending on whether you picked a 32-bit or a 64-bit AMI, you will see a different list, the 32-bit AMIs offering the cheaper options.
    For testing, we don’t need much memory or processing power, so we will start with a Micro instance. Select the instance type and click “Continue.”
    In the next screen, the defaults do just fine. Click “Continue.”
    When you have many machines with different roles, it’s easy to get lost with the machines and end up spending a lot of time searching for the right machine. Naming the machines helps a lot. In our case, naming is not too important, as we will run only one EC2 instance, but let’s give it a name anyhow. Enter your desired name and click “Continue.”
    Next, you will be asked to create a key pair file for securely connecting to the machine. The key replaces a password when connecting to the machine with SSH. Name your key pair file, and click on “Create & Download your Key Pair.” Copy the key pair file to a safe location (A good place, if you are on a Unix system such as Mac OSX or Linux, is ~/.ssh). After copying the file, change its permissions to 400:
    1. chmod 400 wordpress_demo.pem   
    If you are using Windows, you probably use PuTTY for SSH. PuTTY uses its own key format (ppk), so you will need to convert the key to that format by using the Puttygen tool available at the PuTTY download page.
    After you’ve created the key pair, you will still be asked to configure the firewall for your new machine. By default, no one is allowed to connect to the machine — every port is closed. Let’s open ports 22 (SSH) and80 (HTTP), so we can configure the machine over SSH and access the blog from a browser. Select the type of connection from the drop down list at the bottom of the popup and click on “Add Rule.” When you are done with the setup, click “Continue.”
    Review the configuration, and, if everything appears to be correct, click “Launch.”
    Now the machine is launching! Close the popup to return to the dashboard. At first, you’ll notice that the instance’s status is set to “pending,” and within a couple of minutes it switches to “running.” This means the server is up and running — you can start using it.
    When the machine is running, it can be accessed through the ports you defined in the firewall configuration. Let’s use SSH to connect to the machine and finish its setup by installing Apache, PHP, the MySQL extension to PHP, and finally WordPress.

    Step 3 - Install Required Server Software

    Right click on the name of the machine, and select “Connect” from the drop down list. A popup with an SSH command will open.
    Highlight the command, and copy it to a console if you are using a Mac or a Unix machine. The command will be similar to the following:
    1. ssh -i wordpress_demo.pem root@ec2-....compute-1.amazonaws.com   
    If you saved your pem key to ~/.ssh, update the path in the command. Also, you’ll need to change rootinto ec2-user — Amazon hasn’t yet updated the command to match its new AMI setups that don’t allow connecting with the root user.
    If you are on Windows, or prefer to use a more graphical SSH application, simply copy the name of the machine and open it in your SSH application of choice. Remember to use the user name ec2-user and the related key pair file (wordpress_demo.pem).
    Once logged in, you can use the virtual machine just as you would use any Linux machine.
    To install software, we will use an easy to use installer called Yum. First, to install the Apache web server, type the following command:
    1. sudo yum install httpd   
    The installer asks you to confirm the package to install and possibly some other steps as well. Reply with “Y” to every question, and, in no time at all, you will have Apache installed.
    Next up, PHP:
    1. sudo yum install php   
    And the same for the MySQL extensions:
    1. sudo yum install php-mysql   
    That’s it: the setup is done and we are ready to test Apache. Start it with this command (you can later replace “start” with “stop” or “restart” depending on what you want the web server to do):
    1. sudo /etc/init.d/httpd start   
    The web server is up, and you can test it by copying the URL of the machine to your browser:

    Step 4 - Create the Database Machine

    Before installing WordPress, we still need a database. Setting up a database using RDS is as easy as entering some values to a couple of settings screens and pressing a few buttons, so let’s get started.
    In your AWS console, switch to the RDS tab. As you have never used the service before, you will be asked to enable it. This time, the process is much faster and simpler than when you enabled EC2. Click on “Sign Up For Amazon RDS” to get started.
    Follow the process through and, when you are finished, head back to the RDS tab in the AWS console. The screen you will see looks a lot like the EC2 console we used in the previous step. Click “Launch DB Instance.”
    In the popup that opens, you can choose the properties for the database server to start, such as the server instance and storage space to allocate. Let’s pick the smallest values as they are more than enough for our simple test.
    Pick a name for you database server and the root user on it. I used “wordpress-db” as the database server name and “root” / “rootpass” as master login. Not the most secure of options, mind you, but this machine is not intended to last for long, so it’s okay in this instance.
    After setting up the database server information, you will be asked to define a default database to be created on the server. We will call it “wordpress” to follow the WordPress conventions. The rest of the settings on this screen can be left untouched.
    On the next screen, you can define settings for database backups. For now, leave the values to their defaults and click “Continue.”
    After verifying the settings, click “Launch DB Instance.”
    Creating a database instance takes quite a bit longer than creating the EC2 instance, but after about five minutes of waiting, you should see the database instance as “available” on the instances tab.
    Click on the instance to see more data about it, then copy the “endpoint” value. This is the URL of the database server that we will use in the WordPress setup.
    There is one more step before we can move on to installing WordPress: we need to configure the firewall for this database so that the application server can access it.
    Still on the RDS tab in the AWS console, click on “DB Security Groups.” You will see that there are no authorizations yet:
    To give our application server access to the database, we need to authorize its security group to this database security group. Select “EC2 Instance” from the drop down list below “No Authorizations” and enter the name of the security group (“wordpress_app_server“) and your Amazon Account ID to the two text fields that appear next to the drop down list.
    The account ID is hidden quite deep in the AWS settings: you’ll find it by first clicking the “Account” link at the top of the page, and then the “Security Credentials” link on the page that opens.
    After you’ve entered the required information and clicked “Add,” you will see the security group as authorized. We are ready to install WordPress.

    Step 5 - Install WordPress

    If you are not connected to the application server anymore, open an SSH connection to it again. We’ll start by downloading WordPress on the machine. By default, Apache is configured to look for HTML files from/var/www/html/, so let’s download WordPress to that directory.
    1. cd /var/www/html   
    To make things easier in the future, we will change ec2-user to be the owner of that directory:
    1. sudo chown ec2-user .   
    You can download WordPress to your own computer and upload it from there using SCP, but I find it easier to download it directly to the Amazon machine using wget:
    1. wget http://wordpress.org/latest.zip   
    This way, the WordPress package goes straight to its final location and all you need to do is to unzip it:
    1. unzip latest.zip   
    We are about ready to start configuring the blog. As our last step in the command line, change permissions of the wordpress directory that was just created so that the installation script can create the wp-config.php file:
    1. chmod o+w wordpress   
    Now, open the WordPress URL in your browser (The URL of the EC2 instance followed by /wordpress). Click on “Create a Configuration File” and then on the next page “Let’s go!”
    You will end up on a page for entering the database settings. Enter the database name, user name and password, and the URL of the RDS instance. Then click “Submit.”
    You are almost set. Configure the rest of the blog details to finish the installation and enjoy your newly created blog.
    You have successfully installed WordPress in the cloud!

    Step 6 Stop Everything

    As I mentioned at the beginning of this tutorial, AWS billing is based on the hours of usage, so to make sure you are not billed for time that you are not really using, make sure to stop everything once you are done testing.
    First, stop the EC2 instance by selecting it from the admin, right clicking on it and choosing “Stop.” If you later decide to start the machine again, all you need is to click on “Start” in the same drop down list and the machine will resume from where you left it.
    Stopping the database is just as easy, with the exception that you can’t resume the database again. That’s not so nice, but luckily you have the choice to save a snapshot of the data while stopping the database. You can later use this data to recreate the database setup whenever you need it again.
    On the RDS tab, right click on your database instance and select “Delete.”
    Verify that everything is stopped and log out of the AWS console. Your blog is gone, just as fast as it started.