Installing and configuring LAMP stack on a cloud Amazon Linux 2 server is very easy, you can install and configure it on your cloud server in just a few minutes, for you just need to connect to your Amazon Linux 2 server with an ssh terminal and then run some yum
commands, with the help of which you can easily install and set up Linux, Apache 2, PHP, and MySQL on your cloud server.
How to Install LAMP Stack on Amazon Linux 2 Server
Here are steps to install lamp Stack (Linux, Apache, MySql & PHP) on amazon aws Linux 2
Step 1 – Login to Amazon AWS Console
Log into the AWS EC2 Management Console.
Step 2 – Connect to Amazon AWS Linux 2 Server via SSH
To connect your Amazon AWS linux 2 server from the SSH terminal, Open your putty or ssh and connect to from server.
And type the sudo yum update
command on ssh and press enter to update your linux 2 server:
sudo yum update
If you are new to connect aws ec2 instance with Windows, ubuntu, and mac systems. A step-by-step guide on How to Connect to ec2 Instance From Putty and SSH Terminal.
Step 3 – Install Apache
To run sudo yum install httpd
command on aws ssh terminal to install apache 2 web server:
sudo yum install httpd
By default Apache 2 web server remains disabled and stopped in Linux 2 server, to start and enable it, you need to run sudo systemctl start httpd && sudo systemctl enable httpd
command on AWS terminal window:
sudo systemctl start httpd && sudo systemctl enable httpd
Now, verify that Apache is running, simply use the sudo systemctl status httpd
on aws linux 2 server terminal window:
sudo systemctl status httpd
Step 4 – Install MySQL
To run sudo yum install mariadb-server
command on aws ssh terminal to install MariaDB or mysql server:
sudo yum install mariadb-server
By default MariaDB or MySQL server remains disabled and stopped in Linux 2 server, to start and enable it, you need to run sudo systemctl start mariadb && sudo systemctl enable mariadb
command on AWS terminal window:
sudo systemctl start mariadb && sudo systemctl enable mariadb
Now, verify that mariadb or Mysql is running, simply use the sudo systemctl status mariadb
on aws linux 2 server terminal window:
sudo systemctl status mariadb
One more thing, default MariaDB or MySQL is not secure after installation, it needs to be secured, then you can use sudo mysql_secure_installation
command to secure it:
sudo mysql_secure_installation
A prompts will open on aws terminal window asking you to set root password to secure your mysql and Mariadb, and also to do some configuration settings then it will be as per your requirement.
Now that you have secured your MySQL or MariaDB server with password, you can access it on an AWS SSH terminal window by using sudo mysql -u root -p
command:
sudo mysql -u root -p
Step 5 – Install PHP 8.x and PHP Extensions
To install EPEL and REMI repositories, with the help of which you can enable and use PHP 8.x version in your Amazon AWS Linux 2 server, here are the commands for that:
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
Now you need to check which versions of PHP you have in your amazon-linux-extras repository, which you can do using the sudo amazon-linux-extras | grep php
command:
sudo amazon-linux-extras | grep php
Now your terminal window will show php 8 versions like 8.1, 8.2, 8.3, and 8.4, you can use any version by enabling it on your Amazon Linux 2 server.
Enable and start the version of php 8.3
, for this, you can use sudo amazon-linux-extras enable php8.3
command:
sudo amazon-linux-extras enable php8.3
To install it’s php 8.3 extension, by running the sudo yum install php-{curl,pear,gd,mbstring,bcmath,pdo,mbstring,mysqlnd,json,xml,zip,common,cgi,intl}
command aws terminal window:
sudo yum install php-{curl,pear,gd,mbstring,bcmath,pdo,mbstring,mysqlnd,json,xml,zip,common,cgi,intl}
Now confirm the version of PHP installed, run php -v
command on aws ssh terminal window:
php –v
Step 7 – Test of the LAMP server
Run the cd /var/www/html/
command on ssh terminal window and create a PHP file named phpinfo.php by using touch phpinfo.php
:
cd /var/www/html/
touch phpinfo.php
After that, open phpinfo.php by using sudo nano /var/www/html/phpinfo.php
command on terminal window:
sudo nano /var/www/html/phpinfo.php
To update the line of code in phpinfo.php file:
<?php phpinfo(); ?>
Save the changes. and restart the Apache 2 web server By typing
command command on the ssh terminal window, press Enter:sudo systemcl restart httpd
sudo systemcl restart httpd
To test lamp server installation, Simply open your browser and type http://server-ip/phpinfo.php:
http://server-ip/phpinfo.php
Now you will see the version of web browser php and information related to it.
Conclusion
That’s it; you have learned how to install lamp stack (Linux, Apache, MySql & PHP) on amazon aws linux 2 server.