Home » Cloud Technologies » Amazon Web Services ( AWS ) » Install a Apache web server on Amazon EC2 ( Linux 2 Instance ) ?

Install a Apache web server on Amazon EC2 ( Linux 2 Instance ) ?

In this post, we will describe how you can install Apache web server ( LAMP Stack ) on your Amazon EC2 instance, so you can proceed to run your PHP or WordPress based website on Amazon EC2.

1. SSH to your EC2 Instance as mentioned in “How to access your AWS Linux instance using an SSH client ?” , if you already have EC2 keys then just ssh using below command,

ssh -i /your_local_path/my-key-pair.pem ec2-user@my-instance-public-ip

2. Install LAMP Stack

make sure, you are on Amazon Linux 2 (EC2) Instance, otherwise below steps will not work

$ cat /etc/system-release
Amazon Linux release 2 (Karoo)
$ sudo yum update -y
$ sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
$ sudo yum install -y httpd mariadb-server
$ sudo systemctl start httpd
$ sudo systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
$ sudo systemctl is-enabled httpd

Add a security rule to allow inbound HTTP (port 80) connections to your instance if you have not already done so. By default, a launch-wizard-N security group was set up for your instance during initialization. This group contains a single rule to allow SSH connections.

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. Choose Instances and select your instance.
  3. On the Security tab, view the inbound rules. You should see the following rule:Port range Protocol Source 22 tcp
  4. Choose the link for the security group. Using the procedures in Adding rules to a security group, add a new inbound security rule with the following values:
    • Type: HTTP
    • Protocol: TCP
    • Port Range: 80
    • Source: Custom

Test your web server. In a web browser, type the public DNS address (or the public IP address) of your instance. 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.

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.

$ sudo usermod -a -G apache ec2-user
$ exit
Connection to XXX.XXX.XXX.XXX closed.

Now, again SSH back to same console,

verify your user “ec2-user” membership in the apache group by using below command,

$ groups
ec2-user adm wheel apache systemd-journal

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

$ ls -alh /var/www/
drwxr-xr-x  2 root root   6 Aug 24 18:55 html
$ sudo chown -R ec2-user:apache /var/www
$ ls -alh /var/www/
drwxr-xr-x  2 ec2-user apache   6 Aug 24 18:55 html

As you can see above, the directory /var/www/html permissions got changed from “root:root” to “ec2-user:apache”

To add group write permissions and to set the group ID on future subdirectories, change the directory permissions of /var/www and its subdirectories.

$ 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

$ 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.

Now, you can test your Apache sever / LAMP Stack setup as,

If your server is installed and running, and your file permissions are set correctly, your ec2-user account should be able to create a PHP file in the /var/www/html directory that is available from the internet.

$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

Now, you can visit, this page in your browser using public dns url or public ip of your AWS instance and you should see the page as,

Reference : https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment