End to End Project Deployment-LAMP Stack on AWS

WEB STACK IMPLEMENTATION (LAMP STACK) IN AWS
Introduction
LAMP (Linux, Apache, MySQL, PHP or Python, or Perl) LAMP is a popular open-source software stack used for web development. It stands for Linux (operating system), Apache (web server), MySQL (database), and PHP, Python, or Perl (programming languages), enabling dynamic website and application development. The stack provides a reliable and flexible foundation for hosting websites and applications.
Step 0 - Prerequisites:
launch a EC2 instance of t2.micro family with Ubuntu Server 24.04 LTS (HVM) in Ohio Region using AWS Console.
Make sure Port 22 is open in the security group to allow connections from the SSH client.
Connect the SSH client via this command:
ssh -i "your-key.pem" ubuntu@your-public-ip
Step 1 — Installing Apache and Updating the Firewall
update a list of packages in package manager
sudo apt update -y
run apache2 package installation
sudo apt install apache2 -y
To verify that apache2 is running as a Service in our OS, use following command
sudo systemctl status apache2
Note: In this project, TCP port 22 is open by default on EC2 instances for SSH access. To allow inbound traffic for HTTP, a security group rule must be added to open port 80.
Testing Local Web Server on Port 80 with cURL Command
curl http://localhost:80
Accessing the Web Server via Public IP on Port 80
http://public-IP-Address:80
Step 2 — Installing MySQL
MySQL is a popular relational database management system used within PHP environments, so we will use it in our project.
sudo apt install mysql-server
Login into mysql
sudo mysql
Securing MySQL: Setting Root Password and Running Security Script
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';
Exit the mhysql shell
Start the interactive script by running:
sudo mysql_secure_installation
login mysql
sudo mysql -p
exit the mysql
Note : MySQL server is now installed and secured.
Step 3 - Install PHP
You have Apache installed to serve your content and MySQL installed to store and manage your data. PHP is the component of our setup that will process code to display dynamic content to the end user. In addition to the php package, you’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. You’ll also need libapache2-mod-php to enable Apache to handle PHP files. Core PHP packages will automatically be installed as dependencies.
To install these 3 packages at once, run:
1- php: The PHP interpreter.
2- libapache2-mod-php: Integrates PHP with the Apache web server.
3- php-mysql: Allows PHP to communicate with MySQL databases.
sudo apt install php libapache2-mod-php php-mysql
check php version
php -v
Note: At this point, your LAMP stack is completely installed and fully operational.
Step 4 — Creating a Virtual Host for your Website using Apache
The default directory serving the apache default page is /var/www/html. Create your document directory next to the default one.
Create the directory for projectlamp using 'mkdir' command as follows:
sudo mkdir /var/www/projectlamp
Assign the directory ownership with $USER environment variable which references the current system user.
sudo chown -R $USER:$USER /var/www/projectlamp
Create and open a new configuration file in apache’s “sites-available” directory using vi.
sudo vim /etc/apache2/sites-available/projectlamp.conf
<VirtualHost *:80>
ServerName projectlamp
ServerAlias www.projectlamp
ServerAdmin webmaster@localhost
DocumentRoot /var/www/projectlamp
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the new virtual host
sudo a2ensite projectlamp
Disable apache’s default website.
sudo a2dissite 000-default
Ensure the configuration does not contain syntax error
sudo apache2ctl configtest
Reload apache for changes to take effect.
sudo systemctl reload apache2
The new website is now active but the web root /var/www/projectlamp is still empty. Create an index.html file in this location so to test the virtual host work as expected.
sudo echo 'Hello LAMP from hostname' \
$(TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/public-hostname) \
'with public IP 18.118.158.106' \
> /var/www/projectlamp/index.html
Open the website on a browser using the public IP address.
http://public-ip-Address:80
Open the website with public dns name (port is optional)
http://<public-DNS-name>:80
Step 5 - Enable PHP on the website
Open the dir.conf file with vim to change the behaviour
sudo vim /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
# Change this:
# DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
# To this:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Reload Apache
Apache is reloaded so the changes takes effect.
sudo systemctl reload apache2
Create a php test script to confirm that Apache is able to handle and process requests for PHP files.
A new index.php file was created inside the custom web root folder.
vim /var/www/projectlamp/index.php
Add the text below in the index.php file
<?php
phpinfo();
Now refresh the page
This page provides information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly. If we can see this page in your browser, then your PHP installation is working as expected.After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment -and your Ubuntu server. You can use rm to do so:
sudo rm /var/www/projectlamp/index.php
Conclusion:
The LAMP stack is a solid foundation for building and deploying web applications. By following this guide, you can easily set up, configure, and maintain a LAMP environment, providing a stable platform for developing scalable web solutions.
Subscribe to my newsletter
Read articles from Rizwan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
