Build and Deploy LAMP Server on AWS

Jacqueline Mkindi
5 min readJun 17, 2023

--

First let’s understand what is a LAMP stack? (Linux, Apache, MySQL and PHP). So, we use Linux as an operating system, Apache server, MySQL Database and PHP as a Programming language for our application.

Implementation:

  1. Go to AWS console https://aws.amazon.com/aws management/console and login.
  2. Search for EC2 to create your EC2. Select Amazon Linux AMI 2023, t2.micro for instance type. Create a new key pair to be able to access your SSH. Create a security groups for your firewalls. Since you want to allow access to the internet select allow on port 22, 80 and 443 this will allow connection to your instance. Create a key pair and download it so you can be able to access your instance through SSH once finished click launch instance and your instance will be launched.
  3. We are going to download putty for windows so we can connect to our instance. Go here to download putty Download PuTTY — a free SSH and telnet client for Windows . Select highlighted to download.

4. Setup putty for Windows. Select PuTTYgen to setup as below. Then load the key pair you downloaded and load it to Putty gen and save it to your desktop.

5. Open Putty and start configuring it with your aws credentials. Go to Data and enter ec2-user. Go to SSH- Auth and click browse to enter your private key that you downloaded. Then go to session enter you public IP/ Host name and enter your private key and load and save.

6. Click open on Putty to be able to connect to your EC2 instance.

7. Install PHP 8.1

Check if PHP is installed php -v

8. Let us install the php-mysqlnd driver so that your PHP code can connect to MySQL database.

9. Start PHP-FPM process

10. Install Apache

When you install PHP as you know by default it will also install HTTPD/Apache (since there is a dependency on HTTPD). Let us check whether HTTPD is installed and confirm the version

11. Start Apache

12. Install MySQL 8.0

First use wget to get the package

13. Let us install MySQL and confirm the version/installation as shown below.

14. Let’s start MySQL service as shown below.

15. Connect to MySQL and Change temporary password.

You need to find the temporary password generated by MYSQL to connect to the service. Check the file named mysqld.log, under /var/log (Default log location).

This temporary password needs to be modified first before any database can be created within MySQL. Once we create a new password the next step is to create a sample database named registration.

Show the database you created.

16. Check if PHP is working. First we need to create an index.php file with content as phpinfo() and deploy it under /var/www/html as shown

For phpinfo()

Here we will display phpinfo page from the public IP as shown below

17. I create a simple php login application that you need to register and then be able to login and it’s connected to the Database when the user register it stores it to the database. When you user login it will check the credentials in the database and verify if it’s valid and allow the user to login. To get the application please download it using this command.

wget https://codewithawa.com/download-source/1

Once you get your code working you should be able to see the registration screen below.

18. Try to register a user as below.

19. Once registered you should see the homepage screen below.

19. Check the database if the user has been registered in your putty as below. As you can see below user sisi was registered successfully and stored in the database.

20. If you click logout from the home page you should see the Login page as below.

This is how you can build and Deploy LAMP stack applications on AWS EC2 and access it through aws public IP as below.

20. Clean up by deleting EC2 and you should be done.

Thank you for reading and happy learning!! Please clap, comment if you enjoy and follow for more when I publish new articles!

--

--

Jacqueline Mkindi

AWS Solutions Architect | Senior Full Stack Developer | Cloud Architect