Install LAMP Apache, MySQL, PHP on Ubuntu

Introduction

A “LAMP”  is a group of open source software that is typically installed together to configure a server with multiple host websites and web pages. This term is used to represents the Linux ,Apache,Mysql,PHP.
Lamp Represents Linux operating system with the Apache web server and hosted site data is stored in a MySQL database and dynamic content is processed by PHP.

In this guide, we’ll get a LAMP stack installed on an Ubuntu 12.04 and above. Where Ubuntu is an Linux Operating System which cover first requirement of LAMP Stack. There are different ways of installing packages/software in Linux Operating System from package manager, from Source, using binary files. Here we will cover of Install Apache, MySQL, PHP (LAMP) steps using Ubuntu Package Manager,apt. A package manager allows us to install most software pain-free from a repository maintained by Ubuntu.

Follow these steps to Install Apache, MySQL, PHP (LAMP) on Ubuntu

How to install

Apache, MySQL, PHP

Install Apache

Currently there are two type of web server are commonly uses which is Nginx and Apache. The Apache web server is currently the most popular web server in the world, which makes it a great default choice for hosting a website.Where as Nginx use like a proxy server in front of Apache Web Server.

First update ubuntu repositories by giving the below command which will update ubuntu repos if there would be any new repo added in it.

sudo apt-get update

Apache is an open-source multi-platform web server. It provides a full range of web server features including CGI, SSL and virtual domains.

So next step is to install Apache, enter the following command from your terminal. This command shows the packages which will going to install on the server with their dependencies. Press ‘Y’ for Continue –

sudo apt-get install apache2

Now, apache2 server has been installed, verify your apache2 server status by running the following command.

sudo service apache2 status

Output as follow :

Apache2 is running (pid xxxx).

You can check right away to verify that everything in working condition by visiting your Lan Ip or server’s public IP address in your web browser.

Open your web browser and navigate to http://localhost/or http://server-ip-address/.

aapache-home-page

If you see this page, then your web server is now correctly installed.

Install MySQL

We have our web server up and running now it’s time to install MySQL for organizing our website information and provide access to database with their stored information.

Install Mysql server  by typing the below command in terminal . This command will install mysql-server and mysql-client along with dependencies .

sudo apt-get -y install mysql-server mysql-client

During the installation, your server will ask you to enter password for the MySQL “root” user and confirm it. This is an administrative account in MySQL that has full privileges. This is similar as root user account of Linux Operating System Server itself.

When the installation is complete, We can verify our mysql server Status by following command.

sudo service mysql status

if output as follows that means mysql server is in working situation.

mysql start/running, process xxx

Our next task is to setup database directory strucuture and mysql environment setup securely.

Following command will create its database directory structure where it will store its information –

sudo mysql_install_db

Our Mysql server is installed and running but our major concern should be to secure also so we have to check the security of mysql server and set it up.This script remove default access permission and checking for root password security.

Note:- Default setup will disable mysql root logins from remote location.

sudo mysql_secure_installation

For the rest of the questions, you should simply hit the “ENTER” key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

The prompt will ask you for your current root password.

Type it in.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.

It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                                            
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

Now your Database Server is setup and running. Now we can move to our dynamic content access for our web stores.

Install PHP

PHP process process code to display dynamic content on our web store. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display.

sudo apt-get -y install php5 php5-mysql libapache2-mod-php5

It will Install PHP in our server. Now we need to restart our web server for enable it with live server.

sudo /etc/init.d/apache2 restart or sudo service apache2 restart

And create a sample file in your apache2 root directory as follow.

sudo vim /var/www/html/phpinfo.php

 And type the following code.

<?php
phpinfo();
?>

And navigate to http://localhost/phpinfo.php or http://server-ip-address/phpinfo.php

This will display all the details about php version and so on.

apache-phpinfo(1)

We can check if our server is configured default with php script. Create a new file index.php under web directory (/var/www/html)

sudo vim /var/www/html/index.php

 And type the following code.

<?php
echo "Index.php is default access";
?>

and navigate http://localhost/  or http://server-ip-address/ .  If it shows a default apache Installation page that means index.php is not set as default directory index.

In that cases, we’ll want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server,Apache first look for a file called index.html and if it's not there then next it goes to index.php file. We want to tell our web server to prefer PHP files, so we’ll make Apache look for an index.php file first.

To do this, type this command to open the dir.conf file in a text editor with root privileges:

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

It will look like this:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

We want to move the PHP index file highlighted above to the first position after the DirectoryIndex specification, like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

When you are finished, save and close the file by pressing “:x”

After this, we need to restart the Apache web server in order for our changes to be recognized. You can do this by typing this:

sudo /etc/init.d/apache2 restart or sudo service apache2 restart

Conclusion

Now all are set with LAMP stack installed and setup, you have many choices for what to do next. Basically, you have to install some CMS like Joomla,Wordpress, Magento and their required modules for PHP and Apache.

Some popular options are:

  • Install Magento the most popular E-commerce content management system on the internet
  • Install WordPress the most popular content management system on the internet

 

LinuxTweaks

Linuxtweaks Blog helping Server Admin to Manage their servers, Desktop users for making more friendly with linux. Tutorials , guides and tips for linux server maintenance. Here you can learn how to tweak linux servers with code and how to manage it properly.

You may also like...

Leave a Reply

Your email address will not be published.