How to install a LAMP web server on Amazon Linux 2?

Share

The following procedures help you set up an Apache web server with support from PHP and MariaDB (a branch of MySQL developed by the community) on your Amazon Linux2 Instance.

Source : AWS Documents

Steps 1: Prepare LAMP server

1. If you have not launched any Linux instance till now, please click on below link.

2. Connect your instance using below link.

3. To make sure all your software packages are up to date, quickly update the software in your instance. This process may take a couple of minutes, but it is important to ensure that you have the latest security updates and bug fixes.
-y at the end installs the updates without prompting for confirmation. This is optional. If you want to do the confirmation manually, then you can skip -y.

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

4. Install the lamp-mariadb10.2-php7.2 and php7.2 Amazon Linux Extras repositories to get the latest versions of the LAMP MariaDB and PHP packages for Amazon Linux 2.

[ec2-user ~]$ sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2

5. Now you can install the Apache web server, MariaDB, and PHP software packages.

[ec2-user ~]$ sudo yum install -y httpd mariadb-server

6. Start the Apache web server.

[ec2-user ~]$ sudo systemctl start httpd

7. Use the systemctl command to configure the Apache web server to start at each device boot.

[ec2-user ~]$ sudo systemctl enable httpd

Optional : Verify that httpd is on by running the following command:

[ec2-user ~]$ sudo systemctl is-enabled httpd

8. If you have not already done so, add a security rule to allow inbound HTTP (port 80) connections from the Internet. The rules were created for you automatically in a launch-wizard-N security group during initialization.

* Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

* Choose Instances and select your instance.

* On the Security tab, view the inbound rules. You should see the following rule:

Port range : 22
Protocol : tcp
Source : 0.0.0.0/0


* Choose the link for the security group. Using the procedures in Add rules to a security group, add a new inbound security rule with the following values:

  • Type: HTTP
  • Protocol: TCP
  • Port Range: 80
  • Source: Custom

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

10. To give ec2-user permission to manipulate files in the /var/www directory, run the following commands:

  • Add your user (in this case, ec2-user) to the apache group.Protocol: TCP
[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.

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

[ec2-user ~]$ exit

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

[ec2-user ~]$ groups
Output : 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 {} \;

Users can now add, delete, and edit files in the Apache document root, adding useful resources. For instance, your company may use ec2-user (or other members of the apache group), to add, manage, and display website content.

Step 2: Test LAMP server

1. Create a PHP file in the Apache document root.

[ec2-user ~]$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

2. In a web browser, type the URL of the file that you just created.
For Example :  

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

You will se something similar to this below image.

3. After testing Lamp, Delete the phpinfo.php file. Although this can be useful information for security reasons, we should not put it out to the world.

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

Step 3: Secure database server

1. Start the MariaDB server.

[ec2-user ~]$ sudo systemctl start mariadb

2. Run mysql_secure_installation.

[ec2-user ~]$ sudo mysql_secure_installation
  • When prompted, type the current root password. By default, the root account does not have a password set. Press Enter.
  • Type Y to set a password, and type a secure password twice.
  • Type Y to remove the anonymous user accounts.
  • Type Y to disable the remote root login.
  • Type Y to remove the test database.
  • Type Y to reload the privilege tables and save your changes.

Commands to remember for start and stop maria db

* Stop : [ec2-user ~]$ sudo systemctl stop mariadb
* Start : [ec2-user ~]$ sudo systemctl enable mariadb

Step 4: Install phpMyAdmin (Optional)

1. Install the required dependencies.

[ec2-user ~]$ sudo yum install php-mbstring php-xml -y

2. Restart Apache.

[ec2-user ~]$ sudo systemctl restart httpd

3. Restart php-fpm.

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

4. Navigate to the Apache document root at /var/www/html.

[ec2-user ~]$ cd /var/www/html

5. Select a source package for the latest phpMyAdmin release from https://www.phpmyadmin.net/downloads. To download the file directly to your instance, copy the link and paste it into a wget command, as in this example:

[ec2-user html]$ wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz

6. Create a phpMyAdmin folder and extract the package into it with the following command.

[ec2-user html]$ mkdir phpMyAdmin && tar -xvzf phpMyAdmin-latest-all-languages.tar.gz -C phpMyAdmin --strip-components 1

7. Delete the phpMyAdmin-latest-all-languages.tar.gz tarball.

[ec2-user html]$ rm phpMyAdmin-latest-all-languages.tar.gz

8. (Optional) If the MySQL server is not running, start it now.

[ec2-user ~]$ sudo systemctl start mariadb

9. In a web browser, type the URL of your phpMyAdmin installation. This URL is the public DNS address (or the public IP address) of your instance followed by a forward slash and the name of your installation directory. For example:

http://my.public.dns.amazonaws.com/phpMyAdmin

10. Log in to your phpMyAdmin installation with the root user name and the MySQL root password you created earlier.

Leave a Comment

Your email address will not be published. Required fields are marked *

 - 
Afrikaans
 - 
af
Albanian
 - 
sq
Amharic
 - 
am
Arabic
 - 
ar
Armenian
 - 
hy
Azerbaijani
 - 
az
Basque
 - 
eu
Belarusian
 - 
be
Bengali
 - 
bn
Bosnian
 - 
bs
Bulgarian
 - 
bg
Catalan
 - 
ca
Cebuano
 - 
ceb
Chichewa
 - 
ny
Chinese (Simplified)
 - 
zh-CN
Chinese (Traditional)
 - 
zh-TW
Corsican
 - 
co
Croatian
 - 
hr
Czech
 - 
cs
Danish
 - 
da
Dutch
 - 
nl
English
 - 
en
Esperanto
 - 
eo
Estonian
 - 
et
Filipino
 - 
tl
Finnish
 - 
fi
French
 - 
fr
Frisian
 - 
fy
Galician
 - 
gl
Georgian
 - 
ka
German
 - 
de
Greek
 - 
el
Gujarati
 - 
gu
Haitian Creole
 - 
ht
Hausa
 - 
ha
Hawaiian
 - 
haw
Hebrew
 - 
iw
Hindi
 - 
hi
Hmong
 - 
hmn
Hungarian
 - 
hu
Icelandic
 - 
is
Igbo
 - 
ig
Indonesian
 - 
id
Irish
 - 
ga
Italian
 - 
it
Japanese
 - 
ja
Javanese
 - 
jw
Kannada
 - 
kn
Kazakh
 - 
kk
Khmer
 - 
km
Korean
 - 
ko
Kurdish (Kurmanji)
 - 
ku
Kyrgyz
 - 
ky
Lao
 - 
lo
Latin
 - 
la
Latvian
 - 
lv
Lithuanian
 - 
lt
Luxembourgish
 - 
lb
Macedonian
 - 
mk
Malagasy
 - 
mg
Malay
 - 
ms
Malayalam
 - 
ml
Maltese
 - 
mt
Maori
 - 
mi
Marathi
 - 
mr
Mongolian
 - 
mn
Myanmar (Burmese)
 - 
my
Nepali
 - 
ne
Norwegian
 - 
no
Pashto
 - 
ps
Persian
 - 
fa
Polish
 - 
pl
Portuguese
 - 
pt
Punjabi
 - 
pa
Romanian
 - 
ro
Russian
 - 
ru
Samoan
 - 
sm
Scots Gaelic
 - 
gd
Serbian
 - 
sr
Sesotho
 - 
st
Shona
 - 
sn
Sindhi
 - 
sd
Sinhala
 - 
si
Slovak
 - 
sk
Slovenian
 - 
sl
Somali
 - 
so
Spanish
 - 
es
Sundanese
 - 
su
Swahili
 - 
sw
Swedish
 - 
sv
Tajik
 - 
tg
Tamil
 - 
ta
Telugu
 - 
te
Thai
 - 
th
Turkish
 - 
tr
Ukrainian
 - 
uk
Urdu
 - 
ur
Uzbek
 - 
uz
Vietnamese
 - 
vi
Welsh
 - 
cy
Xhosa
 - 
xh
Yiddish
 - 
yi
Yoruba
 - 
yo
Zulu
 - 
zu