Web Stack Implementation (lamp Stack) In Aws

Web Stack Implementation (lamp Stack) In Aws

LAMP (Linux, Apache, MySQL, PHP or Python, or Perl)

Step0: Preparing Prerequisites

Launching an EC2 instance

  • First, you need to log into your AWS account, if you don't have one, you can sign up for a free tier account.

  • Sign in to your AWS account and go to the search bar. Search for EC2 and click on it.

  • It would lead you to the EC2 dashboard page, and when there, click on launch instance.

  • At the "Launch An Instance" page, set a name for your EC2 instance.

  • Next, select an AMI for the instance.
    An AMI is a template that contains the software configuration (operating system, application server, and applications) required to launch your instance.

  • You can choose from already existing AMIs or buy from the AWS marketplace.
    For this task, we will use the Ubuntu AMI.

  • Select an instance type.
    We selected the t2.micro because it's covered by the AWS free tier plan, and we do not require much computing power for what we're about to do. You can choose your instance type depending on the computing power you need.

  • In the "Key Pair login" session, Create a key pair.
    A key pair can be used to securely connect to your instance. If you already have an existing key pair, select it from the drop-down menu.

  • select the .pem private key format for open SSH, for Linux users. And the .ppk for use with putty. I would be using putty to connect to my server.

  • Click on create key pair, and the key would be created.

  • Ensure to keep your private key safe, or else you won't be able to connect to your server again, once lost.


Next is the Network settings.

  • Leave the VPC and subnet on the default setting, and scroll down to the Firewall(Security Group) line.

  • You could use an already existing Security Group(SG), or create a new one, depending on the needs of the server you're creating.

    Screenshot (172)

For this repo, I'd be creating a new SG with an inbound rule allowing SSH, HTTP and HTTPS access from anywhere...

Screenshot (173)

Note: Rules with the source of 0.0.0.0/0 allow all IP addresses to access your instance. And if you take note of the CIDR of our security group rules, they all have the 0.0.0.0/0 source, allowing access from all IPs.

  • Click on the "Launch Instance" button, and your instance is created.

  • Wait until it passes all checks, then your instance is ready for use.

  • Select the instance and you will see a dropdown menu with information, your public IP required to SSH is also included among the info.

Screenshot (175)



Step1

Installing Apache Server

Using the public IP address from our previously created EC2 instance, we are going to use putty, to connect to the instance and install an Apache server on it.

  • Open the Putty application

  • Input your public IP address in the Host Name(or IP name) box.

Screenshot (176)

  • Click on "connection", and change the section between keepalives to 30

Screenshot (177)

  • Click the + sign beside the SSH button, a drop-down menu would appear.

  • Click Auth, click the "browse" button below, besides "Private key file for authentication"

  • Select your previously created .ppk file, and click on the open button below.
    Putty would show a dialog box asking for permission, select yes and you would be taken to a login page asking for an authentication password.

  • Type "ubuntu", this is the password for the ubuntu AMI selected during the creation of the EC2 instance.

  • You have been successfully connected to your EC2 instance.

Screenshot (179)


Now we are going to install an Apache server on our EC2 instance. We would be installing the Apache server using Ubuntu’s package manager ‘apt’.

  • Update a list of packages in the package manager, using sudo apt update

  • Run the apache2 package installation with sudo apt install apache2

  • When prompted, confirm installation by typing Y, and then ENTER

Screenshot (180)

  • After installation, to check if the Apache server is running as a service in our OS, we use the sudo systemctl status apache2 You should see a message similar to the one in the image below, telling you the Apache server is active and running.

Screenshot (182)

  • Use ctrl + c to exit the process

Now it is time to test how our Apache HTTP server can respond to requests from the Internet.

  • Open a web browser of your choice(i used chrome) and type in your EC2 public IP address, it should take you to the Apache server landing page.

Screenshot (183)

Congratulations!!! Your Apache server is accessible from the internet.



Step2

Installing MYSQL

MySQL is a popular relational database management system used within PHP environments, and we shall be using it in our project. Once again, we would be using the ubuntu package manager 'apt' to make the installation.

  • Install MySQL server using the command sudo apt install mysql-server

  • When prompted, confirm installation by typing Y, and then ENTER.

  • When the installation is finished, log in to the MySQL console by typing sudo mysql, this would connect to the MySQL server as the root user

Screenshot (185)

It’s recommended that you run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system. Before running the script, you will set a password for the root user, using mysql_native_password as the default authentication method. We’re defining this user’s password as mysqltrial

  • Exit the MySQL shell with "exit"

Screenshot (187)

  • Start the interactive script by running: sudo mysql_secure_installation

  • When prompted for the root password, input the previously created password, which in this case is mysqltrial

  • You would receive a message asking you to validate the password component
    You can choose to validate or not, depending on how secure you want your password to be. It checks the strength of a password and allows users to set only passwords that are strong enough.
    If enabled, passwords that don’t match the specified criteria will be rejected by MySQL with an error. It is safe to leave validation disabled, but you should always use strong, unique passwords for database credentials.

Screenshot (188)

I am choosing to continue without enabling. I chose No, and continued the other processes.

  • For the rest of the questions, press Y and hit the ENTER key at each prompt. This will prompt you to change the root password, remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

Screenshot (189)

  • When you’re finished, test if you’re able to log in to the MySQL console by typing: sudo mysql -p

    Screenshot (190)

Notice the -p flag in this command, which will prompt you for the password used after changing the root user password. To exit the MySQL console, type: mysql> exit



Step3

Installing PHP

You have Apache installed to serve our content, and MySQL installed to store and manage your data. PHP is the component of your 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: sudo apt install php libapache2-mod-php php-mysql

Screenshot (191)

Once installation is finished, type php -v to confirm your PHP version.

At this point, your LAMP stack is completely installed and fully operational.

  • Linux (Ubuntu)

  • Apache HTTP Server

  • MySQL

  • PHP



Step4

CREATING A VIRTUAL HOST FOR YOUR WEBSITE USING APACHE

In this project, you will set up a domain called projectlamp, but you can replace this with any domain of your choice. 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 directory next to the default one.

  • Create the directory for projectlamp using ‘mkdir’ command as follows: sudo mkdir /var/www/projectlamp

  • Assign ownership of the directory with your 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 your preferred command-line editor. Here, we’ll be using nano. sudo nano /etc/apache2/sites-available/projectlamp.conf

Screenshot (194)

This will create a new blank file. Paste in the following bare-bones configuration

<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

Screenshot (195)

  • To save the file, press ctrl + x, and you would be asked if you want to save, type Y for yes.

Screenshot (196)

  • Click enter at the "name to write" section to save as is.

Screenshot (197)

You can use the ls command to show the new file in the "sites-available" directory

sudo ls /etc/apache2/sites-available

You will see something like this;

Screenshot (198)

With this VirtualHost configuration, we’re telling Apache to serve projectlamp using /var/www/projectlamp as its web root directory. If you would like to test Apache without a domain name, you can remove or comment out the options ServerName and ServerAlias by adding a # character at the beginning of each option’s lines. Adding the # character there will tell the program to skip processing the instructions on those lines. You can now use a2ensite command to enable the new virtual host:

sudo a2ensite projectlamp

You might want to disable 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 your virtual host. To disable Apache’s default website use a2dissite command, type:

sudo a2dissite 000-default

To make sure your configuration file doesn’t contain syntax errors, run:

sudo apache2ctl configtest

Finally, reload Apache so these changes take effect:

sudo systemctl reload apache2

Screenshot (200)

Your new website is now active, but the web root /var/www/projectlamp is still empty. Create an index.html file in that location so that we can test that the virtual host works as expected:

sudo echo 'Hello LAMP from hostname' $(curl -s http://169.254.169.254/latest/meta-data/public-hostname) 'with public IP' $(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) > /var/www/projectlamp/index.html

Now go to your browser and try to open your website URL using your public IP address.

If you see the text from the ‘echo’ command you wrote to the index.html file, then it means your Apache virtual host is working as expected. In the output, you will see your server’s public hostname (DNS name) and public IP address.

You can leave this file in place as a temporary landing page for your application until you set up an index.php file to replace it. Once you do that, remember to remove or rename the index.html file from your document root, as it would take precedence over an index.php file by default.



Step5

ENABLE PHP ON THE 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. In case you want to change this behavior, you’ll need to edit the /etc/apache2/mods-enabled/dir.conf file and change the order in which the index.php file is listed within the DirectoryIndex directive:

sudo nano /etc/apache2/mods-enabled/dir.conf

    #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

Screenshot (202)

After saving and closing the file, you will need to reload Apache so the changes take effect:

sudo systemctl reload apache2

Finally, we will create a PHP script to test that PHP is correctly installed and configured on your server. Now that you have a custom location to host your website’s files and folders, we’ll create a PHP test script to confirm that Apache can handle and process requests for PHP files. Create a new file named index.php inside your custom web root folder:

nano /var/www/projectlamp/index.php

This will open a blank file. Add the following text, which is valid PHP code, inside the file:

Screenshot (203)

When you are finished, save and close the file, refresh the page and you will see a page similar to this:

This page provides information about your server from the perspective of PHP. It is useful for debugging and ensuring that your settings are being applied correctly. If you 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

THE END!!!