How to setup LAMP server on Amazon Linux 2 AMI

Category: Tech Stuff | Last updated: November 11, 2020


The following steps will help you to install an Apache web server with PHP and MySQL on your Amazon Linux 2 instance.

I took help of following links to create this guide:


  1. Launch EC2 Instance:
    • Sign into your AWS account and go to "EC2" service section.
    • In "EC2 Dashboard" click "Launch Instance" button.
    • Select "Amazon Linux 2 AMI (HVM)".
    • In next screen, select appropriate instance type. You can choose the one which is marked "Free tier eligible" to get started. This can be changed later.
    • Click "Review and Launch" button.
    • During this process, you will asked to select a "Key Pair" to use with this instance. You can either use an existing key pair or create a new one.
    • Download the key pair that you selected. Let's say you saved it with name myserver.pem
    • Launch the instance.
  2. Install Apache/PHP/MySQL:
    • On your local system, using the terminal, navigate to the directory where you stored the myserver.pem file.
    • Update permissions for this file - chmod 400 myserver.pem
    • SSH into your instance using following command:
      • ssh -i "myserver.pem" ec2-user@{your public dns}
        • You can get the public DNS by selecting your instance in the "Instances" page
      • Example:
        • ssh -i "myserver.pem" ec2-user@ec2-xx-xx-xx-xxx.compute-1.amazonaws.com
    • Update your instance:
      •  sudo yum update -y
    • Install MySQL:
      • Add MySQL Yum Repository to Amazon Linux 2
        • MySQL 8:
          • sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
        • MySQL 5.7:
          • sudo yum install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
      • Press the y key when prompted to start the installation.
        • A new repository file has been created inside the /etc/yum.repos.d directory.
      • Install MySQL 8 on Amazon Linux 2
        • sudo yum install mysql-community-server
      • Start and Configure MySQL 8 on Amazon Linux 2
        • Start MySQL server - sudo systemctl enable --now mysqld
        • Confirm if MySQL server service is started and running - systemctl status mysqld
      • A superuser account ‘root’@’localhost is created with initial password set and stored in the error log file. To reveal it, use the following command:
        • sudo grep 'temporary password' /var/log/mysqld.log
          • It will show output such as:
            • A temporary password is generated for root@localhost: ABCD1234
        • Use this password to setup a new password and other MySQL security settings:
          • sudo mysql_secure_installation -p'ABCD1234'
    • Install Apache:
      • sudo yum install -y httpd
      • Start apache web server - sudo systemctl start httpd
      • Setup apache server to start on each system startup - sudo systemctl enable httpd
      • You can verify that httpd is ON - sudo systemctl is-enabled httpd
    • Install PHP:
      • sudo amazon-linux-extras install -y php7.2
      • Install other PHP packages:
        • sudo yum install php-cli php-xml php-json php-mbstring php-process php-common php-fpm php-zip php-mysqlnd git -y
      • Check installed PHP version - php --version
    • Make sure that the security group used by the instance allows HTTP traffic (port 80)
    • Go to your Public DNS, it shall show Apache test page similar to following:
  3. Add/update files to document root:
    • By default Apache document root directory is /var/www/html and ec2-user doesn't have permissions to update files in this directory.
    • To allow ec2-user to make changes to this directory, we need to change the ownership and permissions of this directory.
    • We will add ec2-user to apache group and then give apache group ownership of the /var/www directory
      • sudo usermod -a -G apache ec2-user
    • Logout and SSH to your instance again 
      • exit
      • ssh -i "myserver.pem" ec2-user@ec2-xx-xx-xx-xxx.compute-1.amazonaws.com
    • Verify that you have been added to apache group:
      • groups
        • It shall list apache in the groups list, such as:
        • ec2-user wheel apache
    • Change ownership of /var/www directory
      • sudo chown -R ec2-user:apache /var/www
    • Provide write permissions to the group
      • sudo chmod 2775 /var/www
      • find /var/www -type d -exec sudo chmod 2775 {} \;
      • find /var/www -type f -exec sudo chmod 0664 {} \;
  4. Test your LAMP server:
    • Create a PHP file named phpinfo.php in your server's document root /var/www/html/phpinfo.php
    • It shall just have following content:
      • <?php
        phpinfo();
        ?>
    • In web browser, visit above file using the public DNS of your instance such as:
      • http://ec2-xx-xx-xx-xxx.compute-1.amazonaws.com/phpinfo.php
        • You should see PHP info page similar to following

Get In Touch

Dropping a line to say g’day, ask for my resume or see if we can build something amazing together? I’d love to hear from you!

Email me at

Follow me: