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 backup.zip wordpress #create backup.zip from wordpress folder
tar -xvf backup.zip #extract the backup.zip
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”
WHERE table_schema = “database_name”;
Check Row Size in MySQL Table
SELECT option_name, length(option_value) as size
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
- Run following command
UPDATE mysql.user SET Password=PASSWORD(‘MyNewPass’) WHERE User=’root’;
mysqld_safe –init-file=/home/me/mysql-init &