Distributed Systems Practice Notes

Cloud Web Apps - Install a LAMP Web Server on Amazon Linux 2

October 11, 2018

This tutorial introduces how to set up LAMP stack on Amazon Linux 2 instance via SSH.

Prerequisites

  • Amazon Linux 2 AMI (HVM) instance (ami-0922553b7b0369273)
  • Security group to allow inbound SSH (port 22), HTTP (port 80), and HTTPS (port 443) connections.

Official Links

Operations

1. Create Linux 2 instance and connect through SSH

A previous blog about how to set up EC2 virtual machine can be found here.

2. Enable inbound HTTP and HTTPS connections in security group

http

http

3. Update packages through yum

This process may take a few minutes, but it is important to make sure that you have the latest security updates and bug fixes.

[ec2-user ~]$ sudo yum update -y

The -y option installs the updates without asking for confirmation.

4. Install LAMP components from Amazon Linux Extras repositories or yum

  • lamp-mariadb10.2-php7.2 and php7.2
[ec2-user ~]$ sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
  • Apache web server and MariaDB
[ec2-user ~]$ sudo yum install -y httpd mariadb-server

5. Apache web server configurations

  • Start Apache
[ec2-user ~]$ sudo systemctl start httpd
  • Configure to start at each system boot
[ec2-user ~]$ sudo systemctl enable httpd
  • Verify that httpd is on
[ec2-user ~]$ sudo systemctl is-enabled httpd
  • Access Apache test page

Simply type in the public DNS address (or the public IP address) of your instance in browser, you should see

apache test page

If not, go back to step 2 to check security group settings.

  • Set file permissions

Apache httpd serves files that are kept in a directory called the Apache document root. The Amazon Linux Apache document root is /var/www/html, which by default is owned by root.

To allow the ec2-user account to manipulate files in this directory, you must modify the ownership and permissions of the directory. There are many ways to accomplish this task. In this tutorial, you add ec2-user to the apache group, to give the apache group ownership of the /var/www directory and assign write permissions to the group.

  • Add your user (in this case, ec2-user) to the apache group.

    [ec2-user ~]$ sudo usermod -a -G apache ec2-user
  • Log out and then log back in again to pick up the new group, and then verify your membership.

    [ec2-user ~]$ groups
    ec2-user adm wheel apache systemd-journal
  • Change the group ownership of /var/www and its contents to the apache group.

    [ec2-user ~]$ sudo chown -R ec2-user:apache /var/www
  • To add group write permissions and to set the group ID on future subdirectories, change the directory permissions of /var/www and its subdirectories.

    [ec2-user ~]$ sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
  • To add group write permissions, recursively change the file permissions of /var/www and its subdirectories:

    [ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;

Now, ec2-user (and any future members of the apache group) can add, delete, and edit files in the Apache document root, enabling you to add content, such as a static website or a PHP application.

6. Test PHP

php

  • Delete the phpinfo.php file for security concerns

    [ec2-user ~]$ rm /var/www/html/phpinfo.php

7. Database Server Configurations

  • Start MariaDB server

    [ec2-user ~]$ sudo systemctl start mariadb
  • Run mysqlsecureinstallation, set password for root, provide answer Y to all questions

    [ec2-user ~]$ sudo mysql_secure_installation
  • Stop MariaDB server

    [ec2-user ~]$ sudo systemctl stop mariadb
  • Configure MariaDB to start at every system boot

    [ec2-user ~]$ sudo systemctl enable mariadb

8. Install phpMyAdmin

It is strongly recommended that you have enabled SSL/TLS in Apache, otherwise your database administrator password and other data are transmitted insecurely across the internet.

  • Install the required dependencies

    [ec2-user ~]$ sudo yum install php-mbstring -y
  • Restart Apache

    [ec2-user ~]$ sudo systemctl restart httpd
  • Restart php-fpm

    [ec2-user ~]$ sudo systemctl restart php-fpm
  • Download, unzip PhpAdmin tarball to the Apache document root at /var/www/html

    [ec2-user ~]$ cd /var/www/html
[ec2-user html]$ wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
[ec2-user html]$ mkdir phpMyAdmin && tar -xvzf phpMyAdmin-latest-all-languages.tar.gz -C phpMyAdmin --strip-components 1
[ec2-user html]$ rm phpMyAdmin-latest-all-languages.tar.gz
  • If the MariaDB server is not running, start it now
[ec2-user ~]$ sudo systemctl start mariadb
  • Login PhpAdmin

Simply type url http://my.public.dns.amazonaws.com/phpMyAdmin in browser, you should see

php admin

Login as root and use the root password of MariaDB, you should see the console,

php admin console


Warren

Written by Warren who studies distributed systems at George Washington University. You might wanna follow him on Github