Install LAMP Stack

What is LAMP stack?

LAMP stack is commonly known as combination of Linux, Apache, MySql and PHP. LAMP stack is available freely and can be hosted easily.

Using a LAMP stack you can create new dynamic websites or apps. In this tutorial we will learn more about installation process for lamp stack.

What is Linux?

Linux is a popular computer operating system. You can easily install linux on your laptop or desktop. To install linux you have to download the iso file and install via USB or Disk Drive.

To create bootable CD or USB download following iso file:

Download Ubuntu Desktop

How to install apache on ubuntu?

Apache is an open source web server used to host dynamic website. To install apache webserver you must have a computer with operating system. For our tutorial we assume that you have linux based server already.

Log on to your remote linux box and perform following commands to install apache webserver:

# install apache and test config
$ sudo apt install apache2
$ sudo apache2ctl configtest

# enable rewrite module
$ sudo a2enmod rewrite

# restart your apache
$ service apache2 restart​

Once apache is installed on your linux machine and you restart the apache server you sometime get following known error:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, 
using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK​

How to fix "AH00558: apache2: Could not reliably" error

To fix error with apache run following commands on your linux terminal window:

# open apache config file
$ sudo nano /etc/apache2/apache2.conf

# add following line to above file and save your changes
ServerName DOMAIN_NAME OR DOMAIN_IP_ADDRESS

# restart apache server
$ sudo systemctl restart apache2​

You should not get the previous error anymore. You will now be able to see apache index page when you go to your browser window and access http://SEVER_PUBLIC_IP or http://DOMAIN_NAME.

How to install Mysql on Ubuntu?

MySQL is an open source database management tool. To install mysql on ubuntu follow the commands below:

# install default mysql server
sudo apt-get install mysql-server

# check if mysql installed 
mysql -v

# to start/stop/restart mysql server
sudo service mysql start
sudo service mysql stop
sudo service mysql restart

# to secure your mysql server
sudo mysql_secure_installation​

If you are looking for specific version of mysql try following article:

Install MySQL on Ubuntu

How to install PHP on Ubuntu?

PHP is a programming language used for creating dynamic websites or apps. PHP7 is a default version available to install on Ubuntu 16.04.

Run following commands to install php on ubuntu terminal window:

# update repository
$ sudo apt-get update
$ sudo apt-get install python-software-properties
$ sudo apt-get install -y php

# verify php version
$ php -v

# install important php dependecies
$ sudo apt-get install libapache2-mod-php
$ sudo apt-get install php7.0-mysql php7.0-curl php7.0-json php7.0-mcrypt php7.0-gd

# restart apache and mysql
$ sudo service apache2 restart
$ sudo service mysql restart​

Sometime depending on your project requirements you might need to enable some of the features of php. To enable certain php extensions or to install them follow the commands below:

# install php command line tool
$ sudo apt-get install php-cli

# install php xml
$ sudo apt-get install php7.0-xml

# install php mbstring
$ sudo apt-get install php7.0-mbstring

# install imagick
$ sudo apt-get install php-imagick

# install zip/unzip utility
$ apt-get install zip unzip​

Once you have lamp stack installed and you want to create a new website you need to create a virtual host. Virutal host allows you to tie your domain with an ip address.

For example, when you a server using hosting services you get the ip address and login credentials for your linux server. You installed apache, mysql and php on ubuntu server.

Next, you want to create a website called www.learn2torials.com. You go to domain name provider and buy name of your new website.

Now, we have a domain name and an ip adress where our server resides. Using your hosting provider you point your domain name to ip address.

When you access your ip adress or domain name it now points to your ubuntu server however it still does not know where to look for your website code.

We have to tell our apache server that if someone coming from www.learn2torials.com look into perticular directory for our php code.

How to enable php support in apache?

First of all we have a lamp stack server now however our apache server does not know anything about php language. We have to tell our apache server that we will use .php extension for our php scripts.

Open /etc/apache2/mods-enabled/dir.conf and add following line:

# open apache config file
$ sudo nano /etc/apache2/mods-enabled/dir.conf

# update your file to reflect following new changes
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>​

That is it we have successfully configured our lamp stack environment so that it can handle php websites.