KodeInfo | Learning resources for web and mobile development

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 14.04 in AWS EC2 instance

November 28th, 2014 07:19:36 by Imran Iqbal Comments(0) - Views(16321)

Introduction

This turorial will help people who wants to own their own web server to experiment and also to host their own website without cost on AWS. This will also show how to setup LAMP stack environment on AWS EC2 instance and how to deploy web applications on AWS.

A LAMP stack is group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. This term actually is an acronym which represents Linux operating system, with Apache Web server. The site data will be store on MySQL database, and dynamic content is processed by PHP.

Prerequisites

Before you begin with this tutorial, you should sign up for an new AWS account with verification of your credit card information and billing information. You won't be charged anything for usage but for verification you will be charged $1 and it will be refunded back again. 

Step One -- Launching AWS EC2 Instance

Once you signed as a new AWS user you have to sign in and access aws EC2 console. You have to launch a new EC2 instance. So follow up the steps provided in the video or follow below steps.

  • Click on launc instance
  • Select Ubuntu 14.04 64bit OS
  • Select t2.micro instrance which is only available for free tier
  • Just leave the next step same
  • Add storage step same
  • No need to any tags
  • Configure Security groups SSH, HTTP to access instance later
  • Click on launc it will ask to create new key pair which will be used later to access using SSH
  • Save the key in a known place
  • It will take some time to initialize EC2 instance

Step Two -- Accessing EC2 Instance using SSH

Now once we launched a enw EC2 instance we have to access it using SSH. To access using SSH in Windows and MAC.

In Windows we need Putty and Putty Gen tools. Putty gen will be used to convert pem public key which we downloaded to a private key as a ppk file. Putty will be used to access instance using ppk file.

Get IP address and Public DNS of your EC2 instance. We need both to access using SSH.

Open Putty tool give IP address, port 22 and also load ppk file using SSH->Auth in the left side. Then click on connect you will be asked to connect in a popup if you are connecting first time say Yes.

Username: ubuntu

You are succesfully accessed your EC2 instance.

In MAC you can connect using terminal or tool name VSSH. VSSH is a tool same like Putty but it's for MAC.

ssh -p 22 username@hostname or ssh -p 22 username@ipaddress

It will ask for password if requires. In our case it won't ask.

Step Three -- Installing LAMP stack on Ubuntu 14.04

After accessing EC2 instance using SSH. We will start LAMP stack setup on Ubuntu 14.04 step by step.

Installing Apache is the first step. The Apache web server is currently the most popular web server in the world. Which makes it a great choice for hosting a website. 

We can install Apache easily using Ubuntu package mangaer, apt. A package manager allows us to install most software pain-free from a repository maintained by Ubuntu.

For our purposes first we have to login as a root user. After that we have to update all packages up to date then install Apache package.

sudo su

sudo apt-get update

sudo apt-get install apache2

Afterwards your webserver installed. To verify whether it was installed or not access your server using Public DNS url in browser. You will get Apache configuration like below.

To access your instance in browser check below.

http://your_server_IP_address

Now that we have webserver up and running, it is time to install MySQL. MySQL is a database management system. Basically it will organize  and provide access to database where our site can store information. 

Again we can use apt command to install MySQL from package manager using below command.

sudo apt-get install mysql-server php5-mysql

It will ask for password, give a secure password and remember it. We have to give later to access MySQL.

PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connects to our MySQL database to get information, and hand the processed content over to our web server to display.

We can again take the leverage from apt system to install our components. We're going to include some helper packages as well for PHP.  Before installing PHP we have to restart Apache for thatyou have to use below command.

sudo service apache2 restart
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

After to verify PHP installation go to your server and follow the path using below command.

cd /var/www/html

Here create a new php extension file using Vi editor to show PHP configuration using phpinfo function. To know how to create follow below steps.

vi index.php

To insert some code in Vi editor you have to know how to use it. So some basic commands to work on Vi editor in terminal.

To insert some content click i character

Once you wrote your content and wants to save first you have to comet out from insert environment. For that use click on escape button

Now to save use :w

If you don't want to save and wants to quit :q

if you want to save and quit same time :wq

 

In index.php file we have to use phpinfo function to show php configuration of our server in browser.  So use below code for that.

<?php  phpinfo(); ?>

To see PHP configuration in browser just access your server in the same way how you saw Apache configuration.

http://your_server_IP_address/index.php

Now it's better to install some php helper packages like mod_rewrite, Curl etc.

php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-gd - GD module for php5

Now we are done with LAMP satck environment setup. Now to deploy a dynamic web application go to below folder and upload your code.

There are different ways to upload your application either using SVN, Git, FTP or via Termincal.

For now i will show how to use Git to deply your application. To use git first we have to install git using Ubuntu package manager. 

sudo apt-get install git

After this we have to clone the enitre repository using git clone command like below.

git clone http://git repository url

To upload database right now we have access using only terminal for GUI we have to install PhpMyAdmin. We can install PHPMyAdmin using Ubuntu package manager.

sudo apt-get install phpmyadmin apache2-utils

It will ask for Webserver choose Apache and in the next step choose No option.

To access PhpMyAdmin in browser to update our information as a database checl below. It will ask for user name and password to access. 

Username: root
http://your_server_IP_address/phpmyadmin

Step Four -- Setting up Apache configuration for multiple websites hosting

If we want to host multiple website then we have to create different Apache configurations for that you have to create Apache configuratiosn files like below.

cd /etc/apache2/sites-available/

vi sitename.conf

<VirtualHost *:80>
    ServerAdmin user@hostname.com
    ServerName websitename
    ServerAlias www.websitename
    DocumentRoot /var/www/html/foldername
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After this we have to enable the newly created Apache configuration file for new website using below command.

sudo a2ensite sitename.conf

Once it's done we have to restart Apache so that changes will be reflected, for that use below command.

sudo service apache2 restart

 

Now we are successfully setup LAMP satck environment on Ubuntu 14.04 in AWS EC2 instance and also deployed web applications.

For video tutorial check below.

 

Thanks 

 KodeInfo

Author

  • Imran Iqbal
    Imran Iqbal

    Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus