Project 1 : WEB STACK IMPLEMENTATION (LAMP STACK) IN AWS
The goal of this project is to describe the step-by-step implementation of a Lamp Stack on AWS. LAMP is an acronym for Linux, Apache, MySQL, PHP, or Python. Its one of the common web stacks that is used for software development. Prerequisites • An Aws account to run an EC2 instance or a virtual server with Ubuntu server. • Basic Linux command line skills. STEP 1: Establishing a LINUX Environment a) Provisioning an EC2 instance on AWS • Open a web browser and login to your AWS account as IAM user.
• From EC2 dashboard, click on launch instance.
• Give the instance a name of your choice.
• Select Ubuntu from the Application and OS Images section and select a t2. micro from the instance type or anyone that is free tier eligible.
• Select an existing key pair if you have one or create a new key pair on the fly. N. B if you are creating a new key pair, remember to save the downloaded private key (.pem file) in a safe place on your PC and do not share with anyone. I will be needed to gain access into the instance.
• Under network settings, select an existing security group if you have one or create a new security group on the fly.
Leave every other setting as default and click on launch instance, wait a few seconds as the new instance gets provisioned.
b) Let us now connect to our EC2 instance via SSH. • Select the instance, you want to connect to and copy the public IP address.
• Open your preferred console on your computer (e.g., visual studio, mobaxterm, termius, git bash) and run the following command. ssh -i . pem ubuntu@
Type yes when prompted.
Congratulations, you have successfully connected to your EC2 instance and completed the first step of the LAMP stack i.e., LINUX.
STEP 2: Installing Apache2 and Updating Firewall a) Installing Apache2 To install Apache, we will make use of Ubuntu’s package manager called ‘apt’
$ sudo apt update
$ sudo apt install apache2 -y
$ sudo systemctl start apache2
run the following command to enable apache2 (this will ensure that apache2 starts up automatically if the server reboots
$ sudo systemctl enable apache2
$ sudo systemctl status apache2
b) Updating Firewall Now that apache2 webserver is installed and running on our machine, we need to configure the firewall of our server to allow web clients communicate to our server. To do this, we need to update the security group of our AWS instance by adding port 80 to the inbound rules and allow connection from anywhere. By default, we already have port 22 opened on the security group of our AWS instance to allow connection via SSH
$ curl http:// localhost:80 OR $ curl http:// 127.0.0.1:80 To confirm that our server can respond to requests from the internet, open your web browser and type in the following $ http:// :80 You should see the image below
STEP 3: Install MySQL
$ sudo apt install mysql-server -y
$ sudo mysql
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;
mysql> exit
$ sudo mysql_secure_installation
$ sudo mysql -p
mysql> exit STEP 4: Install PHP #run this command to install PHP and it’s required dependencies $ sudo apt install php libapache2-mod-php php-mysql
$ php -v
STEP 5: Creating A VIRTUAL HOST FOR PROJECT LAMP USING APACHE Apache has a server block that is pre-configured, with a document root from /var/www/html/ directory. However, we will create our own project directory (projectlamp) and a new server block with a different document root within Apache’s virtual host so that the two websites can be served on a single server.
$ sudo mkdir /var/www projectlamp
$ sudo chown -R $USER:$USER /var/www/projectlamp
run the command below to create and open a new configuration file for projectlamp in Apache’s sites-available directory
$ sudo vi /etc/apache2/sites-available/projectlamp.conf • Press I on the keyboard to switch to edit mode, paste the command below into the blank page
<VirtualHost *:80>
ServerName projectlamp
ServerAlias www.projectlamp
ServerAdmin webmaster@localhost
DocumentRoot /var/www/projectlamp
ErrorLog
• Press Esc key on the keyboard to switch to normal mode • Type :wq followed by the enter key to write, quit and save the file. #run the following command to check the content of the sites- available directory $ sudo ls /etc/apache2/sites-available You should see the following files listed 000-default.conf default-ssl.conf projectlamp.conf
$ sudo a2ensite projectlamp
$ sudo a2dissite 000-default
$ sudo apache2ctl configtest
$ sudo systemctl reload apache2
$ 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 open the website from a web browser using the public IP address of the EC2 instance at port 80 http://:80
STEP 6: ENABLE PHP ON THE WEB SERVER By default, Apache gives precedence to an index.html file over an index.php file when serving a web client, To serve php files from our webserver, we need to change Apache’s default setting to give precedence to index.php files over index.html files.
$ sudo vim /etc/apache2/mods-enabled/dir.conf
$ sudo systemctl reload apache2 Now let’s create a php script to test that PHP is correctly configured and that our server can serve PHP files from the document root.
$ sudo vim /var/www/projectlamp/index.php • Copy and paste the following text into it, then save and exit. $ <?php phpinfo(); • Refresh the webserver and you should see the page below.
N.B remember to terminate your EC2 instance