Everything About Deploying a PHP + MySQL Web Application to AWS EC2
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
The web app is quite simple, I’m not using any framework such as Laravel, as you can see from above: An HTML file and Vue.js as front-end framework, insert.php to initiate the database with some data I fetched from https://www.dhhs.vic.gov.au/victorian-coronavirus-covid-19-data, and process.php to handle all the CRUD request, and that’s it.
To deploy this application to Amazon EC2, what we need to do is simply:
- Create an EC2 instance with Elastic IP and HTTP access
- Set up a LAMP environment on your EC2 instance
- Move your source code to EC2
- Export your db and import it to your new database on EC2
Now let’s start from the basic.
Start & Configure an EC2 Instance
- Launch an Ubuntu instance first
2. Associate an Elastic IP to the new instance
3. In security group setting, add HTTP as inbound rule
4. Now use anyway you prefer to connect to the EC2 instance
If you get a “Host key verification failed” error, try modify the access permission with chmod command:
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.
XAMPP is a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl. The XAMPP open…
You can choose to install XAMPP on you Ubuntu instance or either install them separately:
1. Update your instance:
sudo apt-get update
2. Switch to root:
3. Install Apache 2:
apt-get install apache2
4. Once it’s done, you can also use following command to check the status of your apache server
systemctl status apache2
5. At this point, you should be able to access the Apache testing page through your IP address, something like this:
6. Now, install other packages: php, php-mysql, and mysql-server
sudo apt-get install php php-mysql mysql-server
You will need to set up your database name and password during the installation. Write them down as you will need them later to login visa myphpadmin
7. Use the same command to test your mysql server
systemctl status mysql
8. Enter mysql comman line and set up the username and pwd:
mysql -u root -p
Once the mysql console shows up, you will need to configure a little bit like this:
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
9. Now install phpmyadmin and restart apache server:
sudo apt-get install phpmyadmin
10. Try access myphpadmin through http://your-ip-address/myphpadmin/
If you got an error of
“The requested URL /phpmyadmin was not found on this server”,
try use the following command:
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
which I shamelessly stole from here:
The requested URL /phpmyadmin was not found on this server
I believe this is because You haven't configure your phpmyadmin with the apache server well. If you installed the…
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:
What we need to do is quite simple: move your source code to the same location and replace index.html with your own web page.
I used Winscp to transfer my source code folder via SMTP, you can download it here:
WinSCP is a popular free SFTP and FTP client for Windows, a powerful file manager that will improve your productivity…
- After connecting to the same instance, you can simply drag-n-drop your project folder to /var/www/html
2. Now simply move all the files outside of the folder:
root@ip-xxx-xx-xx-xx:/var/www/html# chmod 777 /var/www/html
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 ..
3. Now you should be able to access your web application through the same IP address used before
Migrating your data
At this stage, you will only need to migrate your database to cloud.
- Export your local MySQL database:
2. Log in your phpadmin on EC2, simply import the sql file you just exported.
3. Now as MySQL is up and running with your data on EC2, try access your app and see if it’s working.
If you get error similar to this “CORS: PHP: Response to preflight request doesn’t pass.”, I would suggest check your front end API request, because its very likely that you are still calling from localhost, which is not accessible in the new EC2 environment.
What I did to resolve this problem is to change all the URL to the IP address of the new instance:
Before (When run locally):