How to install and configure LAMP ( Linux, Apache, MySQL and PHP ) in Ubuntu 14.04

1. Introduction

Short for Linux, Apache, MySQL and PHP, an open-source Web development platform, also called a Web stack, that uses Linux as the operating system, Apache as the Web server, MySQL as the RDBMS and PHP as the object-oriented scripting language. Perl or Python is often substituted for PHP.

2. Requirements

An ordinary server with Ubuntu 14.04 LTS installed. You can install it on any platform, but here the documentations are based on the Ubuntu / debian server.

3. Installation

Please proceed with the following steps:

3.1 Apache2 Installation

Type the below command for installing apache 2 package from repository.

#  apt-get install apache2

Restart apache service

# /etc/init.d/apache2 restart

Apache will start working with minimal configuration. Now Open your server-ip in the browser and You can see the apache ubuntu test page.( Default document root is located in this path /var/www/html )

apache3 ubuntu default

3.2 MySQL installation

Issue the below command to install mysql server with dependencies.

# apt-get install mysql-server

During installation it will prompt for a new password for mysql root . Give some strong password and retype the same password for confirmation.

After installation check the Database connection.

Check the service status.

# /etc/init.d/mysql status

Service is already up , Now Type this command to login

# mysql -u root -p

Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 42
Server version: 5.5.35-1ubuntu1 (Ubuntu)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

Mysql server installation is over.

3.3 PHP installation

Type the below command to install php5.

# apt-get install php5 php5-mysql

Create a php file and paste the below code into the file to show the php configuration . Goto the default apache root path /var/www/html . create a new file info.php and paste the below code and save it.

# vi /var/www/html/info.php

Please use the following codes to create a phpinfo page.


Now restart the apache2 service.

# /etc/init.d/apache2 restart

Now open the file in the browser ( Eg: http://yourip/info.php ) you can see the php information page as below.
Make sure you can see the mysql details in the php information page.

ubuntu phpinfo

We have successfully configured Apache2 , mysql and php.

How to install and configure Linux,Apache,Mysql(Mariadb) and PHP (a.k.a LAMP) in Centos 7 / RHEL7


LAMP (Linux, Apache, Mysql & PHP ) is an open source web development platform that uses Linux as the operating system, Apache as the Web server, MySQL as the relational database management system and PHP as the object-oriented scripting language. It is powerful and also comparatively simple and easy to use. This article will help you configure LAMP in Centos7.


Operating System: Centos 7 or RHEL 7

Before you begin LAMP INSTALLATION , you should have a server with root user privilege. By completing steps mentioned below you can configure LAMP in your server.

3. Install Apache

We can install Apache easily using CentOS’s package manager, yum.

# yum install httpd

Afterwards, your web server is installed , you can start Apache on your sever:

# systemctl start httpd.service

Now you can check webserver by using server IP address in your browser:

http://your_server_IP_address/    >>>>> here it is

Now you can see the default CentOS 7 Apache web page. Please see some sample screen shorts below,


4. Install MySQL (MariaDB)

MariaDB is one of the most popular database servers in the world. It’s made by the original developers of MySQL and guaranteed to stay open source.

# yum install mariadb-server mariadb

When the installation is complete, we need to start MariaDB with the following command:

# systemctl start mariadb

Now your mysql database is running.You can access database without any password its not secure so we need to provide password protection.

# mysql_secure_installation

The prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter. Then the prompt will ask you if you want to set a root password. Go ahead and enter Y, and follow the instructions:

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

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.

New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

After this when you try to access database with out password it may show an error like this:

[root@arunima-centos7 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

You set a mysql root password for your database server so from now on you can’t access it without password.

“root” session do not know password to mysql root user.

If you want to make easier to access your mysql create a .my.cnf in /root/ and add the following content

# touch /root/.my.cnf
# vim /root/.my.cnf

user = root
password = mysqlrootpassword

where of-course “mysqlrootpassword” is your password for mysql’s root user so when you execute mysql command it uses this password.

Install PHP

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

# yum install php php-common php-gd php-mysql

After installing PHP, you should restart apache.

# systemctl restart httpd.service

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root” or “document root”.

In CentOS 7, this directory is located at /var/www/html/. We can create the file at that location as shown below:


Now we can test whether our web server can correctly display content generated by a PHP script.


Please see a sample screen short of phpinfo below,


How to Safely Change the Location of MySQL Data Directory on cPanel/WHM Servers

I had seen many cPanel servers running out of disk space due to MySQL data directory on “/var” partition.To solve this issue you need to move your MySQL data directory to a new location.There are also other situations like moving your MySQL data’s to a new standalone database server or moving it to a separate solid-state-drive partition for increasing MySQL server performance.Whatever the reason, moving MySQL data directory is simple and has no impact on cPanel functionality.

In this article I am moving MySQL data directory to “/home” partition.You can proceed with the following steps for moving MySQL data directory:

1.Create a backup

Please make full database backup(including system tables) before moving your data directory. This action will prevent data losing in case if something goes wrong.

# tar -cvf mysql.tar /var/lib/mysql

2.Edit the my.cnf file

# vi /etc/my.cnf

Now in the mysqld section add the following. Don’t restart MySQL after adding new entry.


3.Create the new MySQL data directory

# mkdir /home/mysql

4.Now migrate the data to the new location using rsync command.

# nohup rsync -avp /var/lib/mysql/ /home/mysql

The nohup will keep rsync running even when your session with the server end, the other part “

# tail -f nohup.out

Notice that you have to do the syncing process twice , because when moving large size of data can take some time to complete and the tables may have changed in between. When we run it the second time we hopefully get it so that when the switch over happens there is very little, if any, lost data. If you can afford the downtime simply shut down MySQL before running this command.If you cannot though running it twice then quickly copy/pasting the other commands is a valid substitute.

5.Typically you want to stop MySQL for syncing data completely.

# /etc/init.d/mysqld stop

6.Start the re-sync process once again to copy data’s completely.

# rsync -avp --delete /var/lib/mysql/ /home/mysql/

7. Change ownership of new created MySQL data directory to MySQL.

# chown -R mysql:mysql /home/mysql/

8. Now, re-link the socket file to /tmp:

# rm -rf /tmp/mysql.sock
# ln -sf /home/mysql/mysql.sock /tmp/mysql.sock

9. Since you already added the data directory entry to my.cnf , all you need to do is restart again and everything should be working.

# /etc/init.d/mysqld start

Check whether your MySQL logs are written at the new location (Eg: /home/mysql/hostname.err)

10. Create a sample database named “foo” for checking.

# mysqladmin create foo

11. Check whether new database is created at new data directory.

# ls -d /home/mysql/foo

12. After confirming every thing works properly.You can remove the old data directory.

# rm -rf  /var/lib/mysql

That’s it !

How to install Percona Server in cPanel/WHM servers

What is Percona Server ?

Percona Server is based on the original MySQL code, but functions as a drop-in that replaces MySQL .Like MySQL, Percona Server is open-source, so it is free and it benefits from additions from the community. Percona Inc. takes these and other improvements and makes them available as a package to the general public.

What are the advantages of using Percona Server ?
  •  Percona Server will be faster and more consistent for your database queries.
  • Percona Server is backward-compatible with MySQL, So your developers don’t need to change code or rewrite queries.
  • Percona Server takes advantage of the advances in multi-core CPUs, multithreading and solid-state drives.
  • Percona Server contains optimized algorithms for background processing, and enhancements to buffers and caches.
  • Percona Server need only less time in tuning and administering.
  • Percona Server can be easily troubleshoot without guesswork.There are more status counters available for monitoring than in MySQL, So you can get a clear picture of what is happening with your database server.
  • Percona Server will save money on hardware upgrades.
How can you replace MySQL with Percona Server in cPanel installed systems ?

Moving from MySQL to Percona Server is relatively straightforward.But this setup is considered to be unsupported by the cPanel/WHM team.So you may need to manage and maintain any updates and maintenance on the database software by your self.

You can do it by following the steps provided here:

Step 1:

Please make full database backup(including system tables) before switching to Percona. This action will prevent data losing in case if something goes wrong.This is the most time consuming section of this process.

# mysqldump --all-databases --routines --triggers > /home/db_dump/alldb.sql
# service mysql stop
# cp -r /var/lib/mysql/mysql /home/db_dump/
# service mysql start
Step 2:

Move your existing mysql configuration file ( /etc/my.cnf ).Your original MySQL my.cnf file should work fine for Percona but to reduce risk factor,I suggest you to move your existing mysql configuration file:

# mv /etc/my.cnf /etc/my.cnf.old
Step 3:

Disable the targets so cPanel no longer handles MySQL updates:

The following will mark the versions of MySQL distribute as uninstalled so they are no longer maintained by cPanel/WHM.

# /scripts/update_local_rpm_versions --edit target_settings.MySQL50 uninstalled
# /scripts/update_local_rpm_versions --edit target_settings.MySQL51 uninstalled
# /scripts/update_local_rpm_versions --edit target_settings.MySQL55 uninstalled
Step 4:

Remove existing MySQL RPMs so there’s a clean slate for Percona.

# /scripts/check_cpanel_rpms --fix --targets=MySQL50,MySQL51,MySQL55
Step 5:

Create a yum repository for Percona:

nano /etc/yum.repos.d/percona.repo

name = CentOS $releasever - Percona
enabled = 1
gpgkey =
gpgcheck = 1
Step 6:

Remove php from the exclude section of /etc/yum.conf file then run the following commands to install Percona Server.

# yum install Percona-Server-client-55 Percona-Server-server-55 Percona-Server-devel-55
# /etc/init.d/mysql start
# mysql_upgrade
# /etc/init.d/mysql restart

Please make sure to add PHP back to exclude section of /etc/yum.conf file.

Step 7:

Compile PHP with newly installed MySQL libraries using :

# /scripts/easyapache --build

Make sure every websites work fine with new Percona Server.


Got error: 130: Incorrect file format ‘some_table’ – mysql issue fix


There is  a solution for this type mysql error. This error appear due to a corrupted table format in a myisam table. The myISAM folder will have  an frm file which contains the schema  of that table. You can use that schema to rebuild the  table  from the mysql command prompt .

You need to execute the query as follows,

#  repair table table_name use_frm;

How to install DBD::mysql in directadmin

To install DBI and DBD:mysql from source, run the following:

tar xvzf DBI-1.608.tar.gz
cd DBI-1.608
unset LANG;
perl Makefile.PL
make install

cd ..
tar xvzf DBD-mysql-3.0002.tar.gz
cd DBD-mysql-3.0002
unset LANG;
perl Makefile.PL
make install

You can also install it using the cpan mirrors as follows,

perl -MCPAN -e 'install Bundle::DBD::mysql'

How to rest mysql root password in directadmin

When connecting to the MySQL screens in DirectAdmin, if this error appears, that would indicated that the “da_admin” user has not been setup correctly.  Also you are reset the mysql root password too. To resolve this do the following.

/etc/init.d/mysqld stop
mysqld_safe --skip-grant-tables &

That should start up mysql without the need for a root password.  Once in, type

use mysql;
update user set Password=PASSWORD('newpass') where user='root';

Now we need to make sure it’s setup correctly for DA to use.   Edit /usr/local/directadmin/conf/mysql.conf and set


Now edit the file /root/.my.cnf  and add the following lines