This post shows how to migrate a php website from 1and1 shared hosting to Amazon AWS. I mainly cover the Linux command lines and various configurations since they are often the difficult part. I will cover the steps for creating instances of AWS because they can be done by following the guide provided by Amazon and just clicking.
Technique/Tools Used:
1) Ubuntu 14.04
2) Windows 7
3) LAMP
4) Firezilla
5) Puttty or cygwin
1. Create an EC2 Instance
The first question is what kind of instance to create and what operating system to use. Here I select Ubuntu when choosing an Amazon Machine Image.
For EC2 instance I use the free tier instance – t2.micro. It has 1 vCPU and 1GB memory. If your website’s Alexa rank is after top 10,000 and the site mainly read the website without logins, then this should be good enough for your website. Anyway, if this is not good enough, you can take a snapshot and move it to a better instance.
In this step, it’s worth to mention that you should create a key pair and store the private properly as the private key will be used very often later. The key creation is in the last step during instance creation.
2. Set up LAMP
Installing Apache, MySQL and PHP can be done by running the following command one by one:
sudo apt-get install apache2 sudo apt-get update sudo apt-get install mysql-server php5-mysql sudo apt-get install php5 libapache2-mod-php5
After installation, if you type in the IP of the instance, you can view a default web page now.
3. Create a User Which Enables SSH Password
If is not convenient to use a private key to access the server, especially when we want to use some GUI tools such as Filezilla, Putty, etc. We can create a new user and then use a regular password to access our EC2 instance.
1) create a new user
# sudo adduser programcreek
# sudo usermod -a -G sudo programcreek
Following the prompted message to fill password and other information.
2) Edit /etc/ssh/sshd_config file change the setting of PasswordAuthentication to be Yes
PasswordAuthentication yes
3) Restart the ssh
# sudo service ssh restart
Now you can use a password to ssh to the instance. However, the newly created user, programcreek, doesn’t have any permissions right now. I mainly want to use this user to access the /var/www/html directory. So the next step is to assign permissions to the directory for programcreek.
4. Set Permissions for Web Directory
To best share with multiple users who should be able to write in /var/www/html, the directory should be assigned to a common group. For example, the default group for web content on Ubuntu and Debian is “www-data”. Make sure all the users who need write access to /var/www/html are in this group. Here I want to create a new group, it is the same idea.
Assign programcreek to the group web:
sudo usermod -a -G web-data programcreek
Set write permission on /var/www/html.
sudo chgrp -R www-data /var/www/html sudo chmod -R g+w /var/www/html
Now you can use your ftp client, e.g., Firezilla, to connect to the EC2 instance and start moving files for your website.
We just changed the group owner of existing files, but if programcreek create a new file, the new file’s group owner will be “programcreek” like the following:
Additionally, we should make the directory and all directories below it “set GID”, so that all new files and directories created under /var/www/html are owned by the web group.
sudo find /var/www/html -type d -exec chmod 2775 {} \;
Find all files in /var/www/html and add read and write permission for owner and group:
sudo find /var/www/html -type f -exec chmod ug+rw {} \;
You might have to log out and log back in to be able to make changes if you’re editing permission for your own account.
6. Configuring a Basic Firewall
Since I’m setting a LAMP server, HTTP 80 and ssh are allowed.
# sudo ufw allow ssh
# sudo ufw allow 80/tcp
In addition, you can allow email.
# sudo ufw allow 25/tcp
6. Moving Files to the Server
You will use the following command to extract file to /var/www/html.
$ tar -xzf programcreek.tar.gz
Then move all files under programcreek directory to the /var/www/html directory.
$ mv programcreek/* ./$ mv programcreek/* ./
Remove the default index.html file. Now if you visit the site by using the IP address, you will get: Error establishing a database connection
, because the database hasn’t been set up.
If you are going to set up a database on this instance, you can install PHPMyAdmin and then import data.
sudo apt-get install phpmyadmin
Then do the following:
vim /etc/apache2/apache2.conf
Add the following to the bottom of the file:
# phpMyAdmin Configuration Include /etc/phpmyadmin/apache.conf
When you import your SQL file by using PHPMyAdmin, you will get an error like: upload file size too big. Do the following to solve the problem:
sudo vim /etc/php5/apache2/php.ini
Change the following values:
post_max_size = 500M upload_max_filesize = 500M
Restart Apache:
sudo service apache2 restart
Now import your .sql file again, and the database will be imported.
That would be all!
However, if you want to use cloud database instance, which is recommended, you can create an RDS instance shown in the next step.
7. Create RDS and Move Database
Creating an RDS instance is very easy to do. After an instance is created, go back to PHPMyAdmin and WordPress to config the database. The host name should be something like this: programcreek-wordpress-db.9jfkder.us-east-1.rds.amazonaws.com
, and it can be found in the page of RDS instance. In addition, you will also need to change the database name, user name and password.
Reference:
1. http://superuser.com/questions/19318/how-can-i-give-write-access-of-a-folder-to-all-users-in-linux
2. http://stackoverflow.com/questions/12760394/1146-table-phpmyadmin-pma-recent-doesnt-exist
3. a href=”http://aws.amazon.com/free/”>Available Resources for Amazon Free Tier
4. https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-12-04
You can use Cloudways to easily migrate website to AWS EC2. There won’t be any need to hire a sysadmin or use command line. They also provide first time migration free.