If you believe my posts will help someone please feel free to share.
Just some notes as I had to do this a number of times today. There are likely still some efficiencies to be gained but this is pretty quick.
I am running Apache 2.4 with per user web directories for this post. One of the reason’s this works os well with per user web directories is that I can visibly see progress without having to change my local machine’s host files, and I can name my EC2 servers user accounts the same as my cpanel server’s accounts, and it makes the commands a bit less confusing.
Adding a user to your EC2 Apache server configured with per user web directories means if I add a user I can serve files from whatever directory you configure in your .conf files
UserDir directive specifies a directory out of which per-user content is loaded.
I have my server configured read files in public_html inside of each user home directory.
UserDir public_html Say I have a user on my EC machine rbowen
If I visit the URL
I will see any files i have put into /Home/rbown/public_html on my new server. This is similar to how my old cpanel server is configured.
Let’s start, and again there are a plethora of ways one could do this. This is just how I did it today.
sudo adduser butlerraines
This command will create a new home directory and user on my EC2 instance.
sudo su butlerraines
Here I switch user (su) and impersonate butlerraines. The command sudo will allow me to switch users easily and without a password (note I didn’t set one when I added the user)
The command cd ~ puts me in the home directory of the current user so now my current path is /home/butlerraines
Let’s make our public_html directory.
Now let’s check your permissions.
Ok! So let’s navigate to public_html
We will use an absolute path just to make sure we know where we are.
Now we have files over on the old Hostgator cpanel server we want to move. And granted you could pull all these files down by FTP, either zipped or unzipped and push the back up. Or really a ton of different ways to do this.
In this post I simply use rsync.
As always I like to make sure programs and utilities are on my machine so I run the which command.
and it tells me it is in fact on my machine, and I can tell it is in path. If “path” is a foreign concept to you just know if the command or executable is in the path you can run with from just about anywhere.
[butlerraines@ip-xxx-xx-x-xxx public_html]$ which rsync
Cool. Ok here we go. I am in /home/butlerraines/public_html and about to sync the files from my old Hostgator Cpanel VPS to my EC instance.
rsync -azvv email@example.com:/home/butlerraines/public_html/ /home/butlerraines/public_html/
Let’s go through this because it could be confusing, and I purposely did it this way for simplicity. Yeah I know. Sounds backwards.
Using the rsync command I am going to attempt to log into my old server, firstname.lastname@example.org and go to the path holding the files I want to copy email@example.com:/home/butlerraines/public_html/
The trailing slash / tells rsync to only get the contents of /home/butlerraines/public_html/ and not the directory public_html itself.
Then once I have logged into this directory successfully, make a copy of those files on my EC2 instance which I have purposely set up with the same username and directory structure.
When I click enter it will ask for the password to user butlerraines on my old Hostgator VPS, and if successful start making copies of the files.
Step 2. Move the database
Exporting the database tables from the old VPS
Since my old VPS is utilizing cpanel, I’m going to make an assumption here and say anyone reading this is probably used to managing their database in a GUI interface like phpMyAdmin. We could do all this from the command line on the old VPS server but here we do it from cpanel and phpMyAdmin.
On my old VPS I log into my account via cpanel and navigator to phpMyAdmin
I select the database I need and export the tables from that database. Why is that underlined, well you can export the database itself but I just want the tables for this post.
I exported the tables from the database to my local machine. It is not pictured here but I selected the option to gzip my .sql file. You don’t have to depending on the size of you database. You probably don’t have to at all, but I did. Ok moving on.
This exported to my Downloads folder on my local machine.
Copying the exported .sql file to a EC2 instance
Now that I have my exported database on my local machine I need to move it to my remote EC2 instance.
Since I am already able to SSH into my EC2 machine with my SSH keys (if I couldn’t I wouldn’t have even been able to do the first part of this post), I can easily apply that to a similar protocol, scp.
SSH into an EC2 instance looks like this
ssh -i /Users/username/.ssh/filename.pem firstname.lastname@example.org</code>
The first part is an example path on a local machine to a private ssh key.
The second part is the standard way to connect to a remote machine username@ipaddress.
scp looks very similar
scp -pr -i /Users/username/filename.pem /Users/username/Downloads/database_1.sql.gz email@example.com:/home/ec2-user
Basically we are copying the database file from the Downloads folder on a local machine to the ec2-user home directory of the EC2 instance. Don’t be confused by the fact am putting in /home/ec2-user instead of the specific user directory where we placed our original files. MySql is a bit agnostic to where the .sql files is stored. We just need to know where to find the sql quickly, and it will be in /home/ec2-user
Importing the tables to the new database
We have exported the database (well the tables from the database) from the old VPS and now have pushed the .sql file into home directory of the ec2-user on the new EC2 instance.
Now let’s log back into our EC2 instacne
ssh -i /Users/username/.ssh/filesname.pem firstname.lastname@example.org
This should put us in the home directory of ec2-user. If you want to be sure you can always
I always like to take a peek by listing all the files if the directory. You should see your database file in there ie database_1.sql.gz
Now you need to login to mysql from the command line on the EC2 instance.
mysql -u username -ppassword
- I set up mysql on this machine long before I wrote this post and setting up mysql is out of scope of this post.
- You mysql username and password is mostly likely not nor it should be the same password as your Linux users on your EC2 machine.
- That is not a typo above. The flag is -p for accepting a password and then there is no space after the -p. Alternatively you can simply put ‘mysql -u username -p’ and upon enter it will ask for a password.
- I’ve already created the database I want to import so yes I cheated a bit.
Now that your end your command prompt will change a bit
mysql> show databases;
mysql> show databases will show you all the databases you have created.
mysql> use database_1;
use database_1 selects the database you will be using. Put if your database name of course.
mysql> source database_1.sql;
source database_1.sql puts the tables in the export into the database_1 (or whatever you named it).
mysql> exit gets you back to the normal command line.
Step 3. Hook up the files and the database.
Today I was moving Drupal and WordPress sites from my old Hostgator VPS to a new Amazon EC2 instance.
Inevitably you will have to make some adjustments to each web applications configuration files.
This particular example was a WordPress site. You can tell by the database table names. So I will need to edit the wp-config.php files in /home/butlerraines/public_html and add in the new database name, database username and database password.
I find it easier to switch users than to worry with file permissions. Currently I am logged in as ec2-user but I want to edit the wp-config files as butlerraines.
Earlier I showed you how to switch users.
sudo su butlerraines
now let’s go back to /home/butlerraines/public_html
If you look in this directory you will see wp-config.php.
Most Linux machines will have a variety of text editors you can use. The one I like is called nano
You can open and edit the wp-config.php file with nano and make the changes you need to connect your database.
Now go visit visit the URL
and you will see things starting to happen.