How to Install PHP MySQL in AWS EC2 Ubuntu 18.04|20.04|22.04

How to Install PHP MySQL in AWS EC2 Ubuntu 18.04|20.04|22.04

In this tutorial, we will show you step-by-step how to install PHP MySQL on AWS EC2 ubuntu 18.0|20.0|22.04.

How to Install PHP MySQL on Ubuntu 18.0|20.0|22.04 AWS EC2

Here are steps:

Step 1 – Connect Your AWS EC2 Instance from SSH

To connect your AWS EC2 instance from SSH. If you are new to connecting aws ec2 instance with Windows, ubuntu, and Mac systems, read this guide “How to Connect to ec2 Instance From Putty and SSH Terminal”.

Step 2 – Install MySQL on AWS EC2 Ubuntu 18.0|20.0|22.04

Open your ssh terminal and type the following command to install MySql Server:

sudo apt-get install mysql-server

Then, secure MySql server by using the following command. So, you will need to set the root password of the database and secure it using the following command :

sudo mysql_secure_installation

When you run this command, then the questions given below in the prompt will appear:

  • You will be presented with a screen where MySQL asks whether you would like to activate the VALIDATE PASSWORD PLUGIN. For now, keeping things simple, type no.
  • In the next type the root password of your choice. Confirm it again.
  • In the next screen MySql will ask whether to remove anonymous users. Type yes
  • Disallow root login remotely? Type No
  • Remove test database and access to it? Type Yes
  • Reload privilege tables now? Type Yes
  • After the password has been set you can check the whether MySQL is working correctly by logging into the database with the command :
sudo mysql -u root -p

The above command will ask for the password. Enter the password what you enter while the securing the MySQL and hit enter will login into MySQL:

Now, Type exit on ssh terminal to get out of MySql:


Step 3 – Install PHP Amazon EC2 Ubuntu 18.0|20.0|22.04

In this step, install PHP with it’s common extensions. So open your ssh terminal and type the following command:

sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring

After successfully installed PHP on ubuntu amazon ec2 instance. Now, you need to configure dir.conf file by using the following sudo nano command:

sudo nano /etc/apache2/mods-enabled/dir.conf

It will look like this:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.php index.xhtml index.htm

Move the PHP index file to the first position after the DirectoryIndex specification, like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

Step 4 – Restart Apache Web Server

Restart the Apache 2 server using the following command to make the changes effective:

sudo service apache2 restart

Recommended Tutorials


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *