Are you using WHM / cPanel to manage your server or hosting? Is your site slow and optimizing the server is an impossible task? Are you considering changing your hosting provider? Then this tutorial about how to migrate WordPress to AWS is for you.
We know that cPanel is useful when it comes to managing your infrastructure and maybe it has been part of your basic toolkit since forever. But with no emotional feelings, it is time to move on to the next level of innovation for your environment. We present a better hosting to migrate WordPress; this is Amazon Web Services (AWS). In this blog, you are about to see the reasons to Migrate to AWS, its benefits and more importantly how to perform the migration.
Most people start using CPanel because it provides an effortless way to configure and access the server from an intuitive panel, at least more intuitive than a console. But everything has its pros and cons, for example, the server customization is too limited since only the functions provided by CPanel are available and, in case you try to configure it from the console, CPanel will ignore this configuration and the will replace by default.
CPanel is a control panel that manages your hosting and server, has a moderately intuitive design, it seizes the server and installs everything that is commonly necessary, such as MySQL, FTP, Cron Jobs, etc., but it is difficult to customize it since WHM / CPanel takes full control of it.
Amazon Web Services (AWS) is a cloud service provider that, among many other solutions, offers all the necessary services for a high performance hosting and it is adjustable to your needs and budget.
Among the services mentioned in this blog we can find:
An EC2 instance of AWS is a virtual server with the following characteristics:
NGINX is a faster web server than Apache, for this reason, we prefer the implementation of this one.
As a code language we will use PHP version 7.0 since it is the most recent and stable version of PHP; keep in mind that NGINX needs PHP-FPM to interpret this code.
Benefits of AWS:
Disadvantages of CPanel:
To migrate WordPress from CPanel to AWS you need:
The first step is to obtain the compressed directory and the database. The compressed directory, where you will find the WordPress to migrate, should be located as a “tar.gz” file, and the database will be in “sql.gz” format. To obtain the compressed directory of WordPress (document root) and the database, you can go to the CPanel section called “Backup” and select the database to be exported, just like the document root.
Go to your account in CPanel, entering in the browser http://111.111.111.111:2083 (change 111.111.111.111 by the IP of the server or domain). The number 2083 is the port through which the CPanel is accessed.
In the “Account Backups” section, click on the “Home directory” button and add the name of the database to start the download.
After having downloaded the WordPress files and database, you should start the creation of the EC2 instance and RDS that are going to be used to migrate WordPress on AWS.
Note: The database can be hosted on the same server as WordPress (instance EC2), but it is recommended to use an RDS since it is optimized.
Next, we will create instance EC2:
Login to the AWS account through the link https://xxxxxxx.signin.aws.amazon.com/console with username and password.
Before starting to create instances, we need to make sure to do it in the desired region. In this case, we will do it in US East (N. Virginia), since it is the most used and some services are cheaper than others.
In the next screen look for the EC2 service.
Click on “Instances” in the left menu.
Press the “Launch Instance” button to start the creation of the EC2 instance.
Choose the operating system of the instance, in this case it will be “Ubuntu Server 16.04 LTS”.
Choose the size of the instance based on the needs of your site. In this demonstrative case, we will choose an instance type t2.micro. (For more information regarding the types of instances, visit “https://aws.amazon.com/ec2/instance-types” and costs at “https://aws.amazon.com/ec2/pricing“)
Configure the network (VPC) and subnet as shown below.
Determine the size of the instance disk. To calculate it, it is recommended to add the size of the root document and the database (in case you want to host it in the same instance and not use AWS RDS) and multiply it by 3. Thus guaranteeing 60% of the free disk; in this demonstrative case, 8Gb were determined since it is the minimum required.
Optional. We recommend assigning a representative label to the instance for easy identification, as shown below.
When creating the security group (Security Group or SG), it is recommended to add your IP to the SSH rule, this in order that only you can connect via SSH to the instance; it also adds two rules to open ports 80 (HTTP) and 443 (HTTPS), as shown below.
Check the configurations and launch the instance.
Before AWS launches the instance; you must create or choose an SSH key because, for security, AWS by default allows only SSH connections with a key, not a password.
In this case, we create a new key and assign a representative name, download it and launch the instance.
Now it is necessary to create and configure the RDS for the database. Search and navigate to the RDS service, Click on “Instances” or “Get Started Now”. Start the creation of the RDS by clicking on “Launch DB instance”.
In this case, we will select MySQL as the database engine.
Choose the case in which the database will be used; in this case, it will be “Production – MySQL”. In case you want to use Amazon Aurora, you can do it too.
Note: Leave the remaining values by default.
The next thing is to test the connection to the RDS from the instance.
ssh-i/path/keySSH/created ubuntu@IP
Note: The IP of the instance can be observed in the details of the instance when selecting it in the AWS dashboard.
Install MySQL-client to establish the connection with the RDS.
sudo apt install mysql-client-core-5.7
Make sure that the Security Group allows the connection from the instance.
a) Navigate Services > RDS > Instances > Select Instance > Select Security Group
b) Select the entry rules (Inbound).
c) Edit the rules.
d) Add the necessary rules to allow access to the public and private IPs of the instance.
mysql -h ENDPOINT -u USER -p Enter password: **********
Note: The RDS endpoint can be observed in the details of the RDS endpoint by selecting it in the AWS dashboard.
Send the .tar.gz file to the instance.
Option A:
If you are using Linux on your computer, from console run the following command:
scp -i /route/keySSH/created /route/wordpress.tar.gz ubuntu@IP:/home/ubuntu/
scp -i /route/keySSH/created/route/db_name.sql.gz ubuntu@IP:/home/ubuntu/
Option B:
If you are using Windows on your computer, you can use the WinSCP tool by following this tutorial https://www.youtube.com/watch?v=58KmUBaEW34 .
Create the directory where the WordPress will be located.
sudo mkdir -p /var/www/domain.com/public_html/
Change the owner of the created directory.
sudo chown -R ubuntu:www-data /var/www/domain.com/
Unzip the .tar.gz file in/home/ubuntu/.
cd /home/ubuntu/ tar -xzvf /home/ubuntu/wordpress.tar.gz
Move the content to the directory created before.
mv /home/ubuntu/wordpress/* /var/www/domain.com/public_html/
Change permissions and owner to all WordPress.
a) Change the owner user to “ubuntu” and the group to “www-data”
sudo chown -R ubuntu:www-data /var/www/domain.com/
b) Change permissions to directories to allow reading, writing, and execution to owner and group, but only read and execute “all others”:
find /var/www/domain.com/public_html/ -type d -exec chmod 775 {} \;
find /var/www/domain.com/public_html/ -type d -exec chmod 775 {} \;
c) Change permissions to the files to allow the owner and group to read and write, but only read “everyone else”:
find /var/www/domain.com/public_html/ -type f -exec chmod 664 {} \;
Edit the wp-config.php file by changing the host to the RDS endpoint.
vim /var/www/domain.com/public_html/wp-config.php ... define('DB_HOST', 'ENDPOINT'); ...
Exit saving changes.
The first thing is to unzip the file “.sql.gz” to leave it in “.sql” format and facilitate its import.
cd /home/ubuntu/ tar -xzvf db_name.sql.gz
Connect to the RDS with the user and password of the administrator, and the corresponding host / endpoint.
mysql -h ENDPOINT -u USER -p Enter password: **********
Take note of the data in the WordPress wp-config.php file.
vim /var/www/domain.com/public_html/wp-config.php … define('DB_NAME', 'db_name’); define('DB_USER', 'db_user'); define('DB_PASSWORD', 'password'); ...
Create the WordPress database.
CREATE DATABASE db_name;
Create the user in the database ..
CREATE USER 'db_user'@'IP_EC2_instance' IDENTIFIED BY 'password';
Note: It is necessary to replace “IP_EC2_instance” with the IP of the EC2 instance (I recommend the private so that the traffic is minimal and it doesn’t need to leave the internal network), and thus increase the security in the connection to the RDS.
So, only the connection between the EC2 instance and the RDS through that user will be possible, from no other IP this connection will be possible.
Give user privileges on the database.
Use the newly created database.
use db_name;
Import database.
source /home/ubuntu/db_name.sql;
Update the packages.
sudo apt update
Install NGINX, PHP and PHP-FPM.
sudo apt install nginx php7.0 php7.0-common php7.0-curl php7.0-mysql php7.0-mcrypt php7.0-fpm
Configure the Virtual Host or VHOST in “/etc/nginx/sites-available/domain.com”
Before continuing, I want to explain what VHOST is; is a file in the configuration of the web server, in this case NGINX, to be able to host several sites on the same server; Its main function is to route each domain to its respective root directory.
Its most basic and functional configuration is the following:
We explain each directive in a general way:
listen 80: The port through which NGINX will “listen”, if you use SSL certificate it would listen by 443.
server_name domain.com www.dominio.com: The name or domain of the site to which it will respond.
root /var/www/domain.com/public_html/;: The path to the directory where the WordPress is located is specified.
index index.php index.html: The order in which you will search / read the index files.
access_log and error_log: Routes for log files.
location /: Settings that are within “{}” apply to the entire site (/).
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$: Settings that are inside “{}” apply to all files with jpg, jpeg, gif, css, png, js, ico and html.
location ~ \.php$: Settings that are inside “{}” are necessary for the server to interpret the PHP code.
fastcgi_pass unix:/run/php/php7.0-fpm.sock: Port or socket through which PHP-FPM listens.
Enable the VHOST.
sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/
Verify NGINX syntax.
sudo nginx -t
Restart NGINX.
sudo service nginx restart
Emulate the site; if you have doubts about how to do this, you can check it in How To Emulate Your Hosts File (DNS Trick)
Optionally, you can install the extension for Google Chrome called “Website IP” to check the IP of the server where you are loading a site.
And the IP will appear in the lower corners of the browser.
In this case, demonstratively, it will be done in AWS Route53. In the AWS console, navigate to the Route53 service.
As a result, the migrate WordPress (in this case) can be configured for its optimization based on the requirements of the site. But most importantly, with Nginx as Web Server, makes WordPress faster and it has full control of EC2 instances since it is not shared and neither has an invasive control panel installed.
As next steps I recommend the optimization of the EC2 and WordPress instance, hardening, better Google PageSpeed, automated backups, etc. You can find this information in our blogs and tutorials section.
At Clickittech, we are familiar with all these tools and trained to migrate WordPress, always looking for your complete satisfaction.
You may have considered hiring a nearshore software development company or services, but you still have doubts…
End-to-end project management goes as far back as you can remember. Every project in history, even…
AWS DevOps has recently become a trending topic in IT circles as it offers companies…
When understanding AI vs Machine Learning, it’s essential to grasp how these innovations shape the…
If you are involved in the IT industry by any means, it is your job…
A Fintech company was dealing with outdated infrastructure, incurring additional costs due to the deprecation…