Everything About Deploying a PHP + MySQL Web Application to AWS EC2

Photo by Matt Brett on Unsplash

Introduction

I recently wrote a PHP + Vue.js application as part of the coding assessment from a company, as a simple CRUD app it’s honestly not that hard, but when it comes to deploying it on AWS EC2, it did take some time for me to figure it out, and that’s the reason I’m writing this blog to help anyone might looking for a tutorial about deploying a PHP application to EC2 with MySQL database.

The App and Database

  1. Create an EC2 instance with Elastic IP and HTTP access
  2. Set up a LAMP environment on your EC2 instance
  3. Move your source code to EC2
  4. Export your db and import it to your new database on EC2
  5. Profit

Start & Configure an EC2 Instance

  1. Launch an Ubuntu instance first
chmod 400 yourKey.pem

Set up LAMP on your EC2 instance

LAMP is short for Linux + Apache + MySQL + PHP, basically everything you need to run a php application. What I am using on windows is XAMPP, for the same purpose.

sudo apt-get update
sudo su
apt-get install apache2
systemctl status apache2
sudo apt-get install php php-mysql mysql-server
systemctl status mysql
mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password'mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yue'exit
sudo apt-get install phpmyadmin
/etc/init.d/apache2 restart
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.confsudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadminsudo service apache2 restart

Migrate source code to EC2

If you take a look at /var/www/html, you will find the html file used to display the Apache testing page:

  1. After connecting to the same instance, you can simply drag-n-drop your project folder to /var/www/html
root@ip-xxx-xx-xx-xx:/var/www/html# chmod 777 /var/www/html
root@ip-xxx-xx-xx-xx:/var/www/html# ls
covid-vic index.html phpmyadmin
root@ip-xxx-xx-xx-xx:/var/www/html# cd covid-vic/
root@ip-xxx-xx-xx-xx:/var/www/html/covid-vic# cp -r * /var/www/html
root@ip-xxx-xx-xx-xx:/var/www/html/covid-vic# cd ..

Migrating your data

At this stage, you will only need to migrate your database to cloud.

  1. Export your local MySQL database: