Useful Command Line for Working on Amazon EC2 Ubuntu for Apache, PHP and MySQL

On September 24, 2013, in Other Online Technology, by James Liu

In the past few weeks, I am working to migrate our WordPress website to Amazon EC2 Ubuntu system. Even though I have some experience on Linux System, but it is long time ago. I just forget lots of Linux command and it is time to remember. In this tutorial, I will not teach you how to setup the ubuntu in Amazon EC2, or how to set up Apache and PHP5 because there are too many tutorials on internet about that and they are good enough to guide you and me to get our job done.

In this post, I am going to write some basic Linux commands which are quite useful when we are working on Amazon EC2 Ubuntu, especially for Apache, PHP and MySQL. All these commands are used when I am build my Amazon EC2 WordPress website.

Start Service Linux Command

We can also use “sudo service servicename start/restart/stop” to start, restart or stop a service, such as apache, nginx, mysql, or varnish (a cache web server service).

sudo service apache2 start
sudo service apache2 restart
sudo service apache2 stop

Check Process and CPU Memory Usage

ps -aux | grep nginx                            #check if there are nginx applications running

top                            #check the current system resource usage
In top screen, type “O” (uppercase) to set sorting type. For example, typing “On”, then enter, the process list will sorted by memory usage.

apt-get to Install and Uninstall Applications in Ubuntu

sudo apt-get install nginx                 #install nginx

Here is the command line to fully uninstall the Nginx in Ubuntu.
dpkg -l | grep nginx                            #check if there are nginx related applications

sudo apt-get remove nginx            #Remove all but config files.
sudo apt-get purge nginx                #Remove everything.
sudo apt-get autoremove               #After using any of the above commands, use this command to remove dependencies used by nginx which are no longer required.

Compress and Extract Zip File and Folder

When I am working in Ubuntu, backup and restore files are common jobs. Tar can help us to package a folder or files easily.
tar -zcvf wordpress             #create from wordpress folder
tar -xvf                        #extract the

VI Editor Command

VI is a text editor in Linux command line. There are some useful commands to remember.

dd               //delete current line
:set number      //show row number in left side
:set number!     //hide row number
/this            //search word “this”

MySQL Management Command

For MySQL management, backup and restore are the daily jobs. The following command will show us how to backup, restore, check datable size, and check row size in a table.

Backup Full MySQL Database
sudo mysqldump -u root -ppassword database_name > backup.sql

Restore Full MySQL Database
mysql -u root -ppassword database_name < backup.sql

Backup a MySQL Database One Table
sudo mysqldump -u root -ppassword database_name wp_options –where=”option_name=’cron’” > cron.sql

Check MySQL Table Size
SELECT table_name, round(((data_length + index_length) / 1024 / 1024), 2) “Size in MB”
FROM information_schema.TABLES
WHERE table_schema = “database_name”;

Check Row Size in MySQL Table
SELECT option_name, length(option_value) as size
FROM wp_options
WHERE autoload=”yes” ORDER BY size;

Reset Root Password:

  • Stop MySQL service
  • Create a text file mysql-init
  • Add following two lines, each on a single line
  • UPDATE mysql.user SET Password=PASSWORD(‘MyNewPass’) WHERE User=’root’;

  • Run following command
  • mysqld_safe –init-file=/home/me/mysql-init &


2 Responses to “Useful Command Line for Working on Amazon EC2 Ubuntu for Apache, PHP and MySQL”

  1. Just curious, are you using a single instance for WordPress in EC2 or a tiered architecture? If tiered, how did you structure it?

    • James says:

      What do you mean “a single instance in EC2”? My Amazon EC2 instance is “m1.medium”, and I install all the Apache2, PHP and MySQL on one instance.

      What do you suggest? Actually, I meet some performance problem with WordPress with Amazon EC2

Leave a Reply

WordPress Themes