Install LAMP (Apache, PHP, MySQL) stack on Ubuntu

In today tutorial, we will learn how to install Apache2, PHP5 and Mysql on Linux Ubuntu. LAMP Stands for Linux, Apache, MySQL, PHP which is taking first letter of each of the packages. LAMP is combination of operating system and bunch of open-source software which is developed by different organizations.

Step One — Updating System

Updating your Ubuntu installation

sudo apt-get update && sudo apt-get upgrade

Step Two — Installing Apache

Apache is a free open source software which runs over 50% of the world’s web servers. To install apache, open terminal and type in these commands:

sudo apt-get install apache2

That’s it. To check if Apache is installed, direct your browser to your server’s IP address (eg. http://12.34.56.789). The page should display the words “It works!” or something like this

apache-installed

How to Find your Server’s IP address

You can run the following command to reveal your server’s IP address.

ifconfig eth0 | grep inet | awk '{ print $2 }'

If the apache is not started by default, try this

sudo service apache2 start

Step Three — Installing MySQL

MySQL is a powerful database management system used for organizing and retrieving data. To install MySQL, in terminal type these commands:

sudo apt-get install mysql-server libapache2-mod-auth-mysql

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.

Maybe required:

For some server, you’ll need additional steps once you have installed MySQL for it to be activated with this command:

sudo mysql_install_db
sudo /usr/bin/mysql_secure_installation

Once MySQL installed successfully, Log into MySQL by typing this code to your console:

mysql -u root -p

Now try to create a new User and Database for testing:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

At this point newuser has no permissions to create database, etc. So we will need to grant permission to the newuser.

GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

The asterisks in this command refer to the database and table (respectively) that they can access—this specific command allows to the user to read, edit, execute and perform all tasks across all the databases and tables.

Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.

FLUSH PRIVILEGES;

Your changes will now be in effect.

How To Grant Different User Permissions

Here is a short list of other common possible permissions that users can enjoy.

  • ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system)
  • CREATE- allows them to create new tables or databases
  • DROP- allows them to them to delete tables or databases
  • DELETE- allows them to delete rows from tables
  • INSERT- allows them to insert rows into tables
  • SELECT- allows them to use the Select command to read through databases
  • UPDATE- allow them to update table rows
  • GRANT OPTION- allows them to grant or remove other users’ privileges

To provide a specific user with a permission, you can use this framework:

 GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;

If you want to give them access to any database or to any table, make sure to put an asterisk (*) in the place of the database name or table name.

Each time you update or change a permission be sure to use the Flush Privileges command.

If you need to revoke a permission, the structure is almost identical to granting it:

 REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;

Just as you can delete databases with DROP, you can use DROP to delete a user altogether:

 DROP USER ‘demo’@‘localhost’;

To test out your new user, log out by typing

quit

Then login with the new user:

mysql -u newuser -p

Try creating a database with this command:

create database newdb;

If you want to grant permission for other non root user to access your database you can use this command (include quote):

grant all on newdb.* to 'username' identified by 'password';

Now we’re ready for installing PHP server side scripting engine. Quit your MySQL.

Step Four — Installing PHP

Install PHP, and the PHP Extension and Application Repository:

sudo apt-get install php5 php5-mysql libapache2-mod-php5 php5-mcrypt php-pear

php5-mcrypt and php-pear is optional but you may need this later.

Once PHP5 is installed, try it out by creating a Hello World code:

First check out your PHP root directory:

ls -alt /var/www

If you see html directory then it means the default php root (public_html) will be in /var/www/html

Now go to the root directory

cd /var/www/html

Then add our new hello world app here:

sudo nano helloworld.php

It will open the nano editor, in nano editor type

<?php echo 'Hello World'; ?>

Save the file by hitting CTRL + X then Y. Next visit your site IP Address plus the trailing slash helloworld.php

http://192.168.0.0/helloworld.php

You’ll see Hello World text.

PHP Modules

PHP also has a variety of useful libraries and modules that you can add onto your virtual server. You can see the libraries that are available.

apt-cache search php5-

Once you decide to install the module, type:

sudo apt-get install name of the module

You can install multiple libraries at once by separating the name of each module with a space.

Congratulations! You have now set up and configured a LAMP stack.

Leave a Reply

Your email address will not be published. Required fields are marked *