October 11, 2018
This tutorial introduces how to set up LAMP stack on Amazon Linux 2 instance via SSH.
A previous blog about how to set up EC2 virtual machine can be found here.
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.
[ec2-user ~]$ sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
[ec2-user ~]$ sudo yum install -y httpd mariadb-server
[ec2-user ~]$ sudo systemctl start httpd
[ec2-user ~]$ sudo systemctl enable httpd
[ec2-user ~]$ sudo systemctl is-enabled httpd
Simply type in the public DNS address (or the public IP address) of your instance in browser, you should see
If not, go back to step 2 to check security group settings.
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.
Create a PHP file in the Apache document root.
[ec2-user ~]$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
Simply type url http://my.public.dns.amazonaws.com/phpinfo.php in your browser, you should see
Delete the phpinfo.php file for security concerns
[ec2-user ~]$ rm /var/www/html/phpinfo.php
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
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
[ec2-user ~]$ sudo systemctl start mariadb
Simply type url http://my.public.dns.amazonaws.com/phpMyAdmin in browser, you should see
Login as root and use the root password of MariaDB, you should see the console,
Written by Warren who studies distributed systems at George Washington University. You might wanna follow him on Github