Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Last active May 6, 2020 15:42
Show Gist options
  • Save webmechanicx/209115a85a1a084840456cfaba8447fa to your computer and use it in GitHub Desktop.
Save webmechanicx/209115a85a1a084840456cfaba8447fa to your computer and use it in GitHub Desktop.
LAMP (Linux, Apache, MySQL, PHP) web server on an Amazon EC2 Linux instance

Install and start the LAMP web server

Step1:

First thing you have to connect to ssh using macos or putty for windows by added the downloaded key file. Run the following command:

sudo yum update -y

The above command ensure that all of your software packages are up to date.

Step2: Now install packages in your current instance.

you can install the Apache web server, MySQL, and PHP software packages.

sudo yum install -y httpd24 php72 mysql57-server php72-mysqlnd

Step3: Start the Apache web server.

sudo service httpd start

Test your web server. In a web browser, type the public DNS address. If there is no content in /var/www/html, you should see the Apache test page.

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.

ls -l /var/www

Step4: Setting file permissions

To allow the ec2-user account to manipulate files in this directory, you must modify the ownership and permissions of the directory.

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

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.

Log out (use the exit command or close the terminal window):

exit

To verify your membership in the apache group, reconnect to your instance, and then run the following command:

groups

Change the group ownership of /var/www and its contents to the apache group.

sudo chown -R ec2-user:apache /var/www

To add group write permissions and to set the group ID on future subdirectories

sudo chmod 2775 /var/www

Then:

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:

find /var/www -type f -exec sudo chmod 0664 {} \\;

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

Test your LAMP server

Create a PHP file in the Apache document root.

echo "" > /var/www/html/phpinfo.php

If you get a “Permission denied” error when trying to run this command, try logging out and logging back in again to pick up the proper group permissions that you configured in Set File Permissions.

http://my.public.dns.amazonaws.com/phpinfo.php

Step5: Enabling .htaccess for url rewrite

sudo nano /etc/httpd/conf/httpd.conf

Save the file ctrl + ^O and restart the apache:

sudo service httpd restart

Step6: Secure the Database Server

sudo mysql_secure_installation

When prompted, type a password for the root account and then set Y to all question being asked.

(Optional) If you want the MySQL server to start at every boot, type the following command.

sudo chkconfig mysqld on

Step 7: (Optional) Install phpMyAdmin

  1. Navigate to the Apache document root at /var/www/html
cd /var/www/html
  1. Create a phpMyAdmin folder and extract the package into it using the following command
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
  1. Delete the phpMyAdmin-latest-all-languages.tar.gz tarball.
rm phpMyAdmin-latest-all-languages.tar.gz
sudo service mysqld start

phpmyadmin url should something like here: http://my.public.dns.amazonaws.com/phpMyAdmin

Follow this link other configuration e.g. phpmyadmin

AWS LAMP PHPMYADMIN

AWS LAMP FROM Amazon Doc

AWS LAMP YOUTUBE

Command to install additional php72 extensions:

# Install PHP 7.2 
# automatically includes php72-cli php72-common php72-json php72-process php72-xml
sudo yum install -y httpd24 php72 mysql57-server php72-mysqlnd

# Install additional commonly used php packages
sudo yum install php72-gd
sudo yum install php72-imap
sudo yum install php72-mbstring
sudo yum install php72-mysqlnd
sudo yum install php72-opcache
sudo yum install php72-pdo
sudo yum install php72-pecl-apcu

sudo service httpd restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment