Skip to content

titusnangitech/LAMP-web-stack-implementation-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 

Repository files navigation

Deployed a LAMP (Linux, Apache, MySQL, PHP) stack website on AWS

STEP 01 - Created and connected to an EC2 instance in the us-east-1 region

ec2 pic

  • Selected the us-east-1 region and launched a new EC2 instance of t2.micro family with Ubuntu Server 20.04 LTS (HVM) launch EC2

created an ec2 with an ami of ubuntu

  • created an ec2 key pair and choose the t2 micro

choose the t2 micro type

  • created a security group and made changes to the inbound rules

new sg with new inboud rules

  • security group has been created successfully

selected the sg that i created above

  • the ec2 instance been successifully created and running

ec2 is a success

  • populated Mobaxterm with ec2 public IP address, username and key pair details to connect to the EC2 instance

mobaxterm populated with details

  • connected successfully into the EC2 instance using Mobaxterm

connected into ubuntu

  • Switch from ubuntu user to root user

    sudo -i
    

switch to root with thesudo command

STEP 02 - Installing Apache web server and Updating the firewall

  • Install Apache using Ubuntu’s package manager ‘apt’. First update a list of packages in the apt package manager
apt update

run apt update

  • run apache2 package installation
apt install apache2

install apache

  • To verify that apache2 is running as a Service in our OS, enter the following command
systemctl status apache2

verify that apche is running

  • server is running and can be accessed locally using both localhost and IP address
curl http://localhost:80
or
 curl http://127.0.0.1:80

test using curl local host


curl using ip address

  • testing how Apache HTTP server can respond to requests from the Internet. Opening a web browser of my choice and to access following url
http://<Public-IP-Address>:80
  • I can see the Apache Ubuntu Default Page displaying which means my web server is now correctly installed and accessible through the firewall.

apache displayed

STEP 03 - Installing MySql

#I used ‘apt’ to acquire and install mysql software. When prompted, I confirmed installation by typing Y, and then ENTER
apt install mysql-server

install mysql server

  • When installation finished, i logged in to the MySQL console by typing:
mysql

connecting to mysql

  • This will connect to the MySQL server as the administrative database user root, which is inferred by the use of sudo when running this command. You should see output like this:

connecting to mysql

  • set a password for the root user, using mysql_native_password as default authentication method. We’re defining this user’s password as PassWord.1
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';

password lock in

  • Exit the MySQL shell with:
mysql> exit

exit mysql

  • Start the interactive script by running:
 mysql_secure_installation

mysecure installation

STEP 04 - Installing PHP

  • I have Apache installed to serve my content and MySQL installed to store and manage my data. PHP is the component of my setup that will process code to display dynamic content to the end user. In addition to the php package, i’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. I’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, will run:

apt install php libapache2-mod-php php-mysql

install PHP component

  • Once the installation is finished, i ran the following command to confirm my PHP version:
php -v

php version

STEP 05 - Creating a Virtual Hosts for my website using Apache

-**Apache on Ubuntu 20.04 has one server block enabled by default that is configured to serve documents from the /var/www/html directory. We will leave this configuration as is and will add our own directory next next to the default one.

Created the directory for projectlamp using ‘mkdir’ command as follows:**

mkdir /var/www/projectlamp
  • Next, i assignwd ownership of the directory with my current system user:
chown -R $USER:$USER /var/www/projectlamp
  • Then, created and opened a new configuration file in Apache’s sites-available directory using my preferred command-line editor. Here, i’ll be using vi:
vi /etc/apache2/sites-available/projectlamp.conf
  • This will create a new blank file and i pasted the following:
<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>
  • I used the ls command to show the new file in the sites-available directory
ls /etc/apache2/sites-available
  • Something like this was shown;
000-default.conf  default-ssl.conf  projectlamp.conf
  • I used a2ensite command to enable the new virtual host:
a2ensite projectlamp
  • I disabled the default website that comes installed with Apache. This is required if you’re not using a custom domain name, because in this case Apache’s default configuration would overwrite my virtual host. To disable Apache’s default website I used a2dissite command:
a2dissite 000-default
  • To make sure my configuration file doesn’t contain syntax errors, I ran:
apache2ctl configtest
  • Finally, I reloaded Apache so these changes could take effect:
systemctl reload apache2
  • My new website is now active, but the web root /var/www/projectlamp is still empty. Created an index.html file in that location so that I can test that the virtual host works as expected
echo 'Hello LAMP from hostname' $(curl -s http://169.254.169.254/latest/meta-data/public-hostname) '54.88.16.160' $(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) > /var/www/projectlamp/index.html
  • Now I went to my browser and tried to open my website URL using IP address. I could see the text from ‘echo’ command I wrote to index.html file, then it means my Apache virtual host is working as expected.
http://54.88.16.160:80

step 5

STEP 06 Enabled a PHP on website

  • With the default DirectoryIndex settings on Apache, a file named index.html will always take precedence over an index.php file. This is useful for setting up maintenance pages in PHP applications, by creating a temporary index.html file containing an informative message to visitors. Because this page will take precedence over the index.php page, it will then become the landing page for the application. Once maintenance is over, the index.html is renamed or removed from the document root, bringing back the regular application page. To change this behavior, edited the /etc/apache2/mods-enabled/dir.conf file and changed the order in which the index.php file is listed within the DirectoryIndex directive:
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>
  • After saving and closing the file, I reloaded Apache so the changes take effect:
systemctl reload apache2
  • Finally, I created a PHP script to test that PHP is correctly installed and configured on my server. Created a new file named index.php inside my custom web root folder:
vim /var/www/projectlamp/index.php
  • This opened a blank file and added the following text, which is a valid PHP code, inside the file:
<?php
phpinfo();

last commands

  • I saved and closed the file, refreshed the page and saw a page similar to the one below. This page provides information about my server from the perspective of PHP. It is useful for debugging and to ensure that my settings are being applied correctly. By seeing this page in my browser, then my PHP installation is working as expected

correct php server display

  • After checking the relevant information about my PHP server through that page displayed, it’s best to remove the file I have created as it contains sensitive information about my PHP environment and my Ubuntu server. I used the following command to do so.
rm /var/www/projectlamp/index.php

The End, Lamp stack website successfully deployed.

Releases

No releases published

Packages

No packages published