Jamie Sefton | Web Developer

Installing Linux, Apache, MySQL & PHP (LAMP) on Centos

Posted: 17th Jan 2015

Installing Linux, Apache, MySQL and PHP also known as a LAMP stack will provide everything you need to get a web server up and running. Once you have this installed you might want to install cPanel or a free alternative such as ZPanel to help you manage your server through an interface rather than all command line.

First thing you need to do is connect in via command line to your server. If you don't know how to do this then please read my tutorial on Using Putty to access a server command line.

IMPORTANT: Before we start I would highly recommend running and update to make sure you have all the latest resources available.

To update run the command below:

sudo yum update

Press Y once executed to start the update. The total update might take a few minutes to process.

Quick Navigation

1) Installing Apache

Once you have the latest resource it is time to install Apache. Apache is the core foundations of a web server and is required.

Execute the command below to start the installation of Apache:

sudo yum install httpd

Once this has completed you can now use the below command to start Apache:

sudo service httpd start

You can now test to make sure that Apache is working correctly. Simply open a browser and navigate to your server IP address (Example: http://123.456.789.000). You should see a page load with a basic Apache / Centos page.

2) Installing MySQL

MySQL is what is used to power the databases on a web server. There are other versions, but MySQL is the most commonly used.

IMPORTANT:: Before we being it is important to know what version of Centos you are running. This is because in Centos 7 mysql-server isn't available by default and has been replaced with MariaDB. If you are using Centos 7 you need to skip the next step and jump to: Setting up MariaDB on Centos 7

To install MySQL onto the server enter the command below:

sudo yum install mysql-server

Once done start MySQL with this command:

sudo service mysqld start

IMPORTANT: If you are installing on Centos 7 you will get an error saying "No package mysql-server available". This is because Centos 7 comes with a MySQL package called MariaDB. You will need to enable and configure this.

Setting up MariaDB on Centos 7

To make sure you have the latest version and to automatically install it enter the command below:

yum -y install mariadb-server mariadb

Once this has processed you will need to start and enable MariaDB. This can be done by entering the 2 below commands:

systemctl start mariadb.service systemctl enable mariadb.service

Configuring MySQL

Once you have either setup mysql-server or MariaDB (Centos 7) then you can now configure MySQL.

Enter the command below to enter the setup of MySQL

mysql_secure_installation

This will then ask you for your root password. As this has only just been installed you won't have a password yet, so simply press enter.

Next you will be asked if you want to setup a root password. Press Y and then enter a password that will be used for root database access. IMPORTANT make sure that this password is very strong as this will have access to every database on your whole server.

Once this is done it will ask if you want to remove anonymous users? I would recommend enter Yes for this.

The next step asks if you want to disallow root login remotely? I would say No for this if you intend to manage all your databases through a program such as PHPMyAdmin or Adminer.

Next it will ask if you want to remove the test database. I would say you should just because it isn't really needed.

Finally, you will need to reload your privilege table.

3) Installing PHP

Now that we have Apache and MySQL working, it is time to get PHP installed so you can start building powerful applications.

To install PHP enter the command below:

sudo yum -y install php php-mysql

Now that that is installed you need to enter the below command so that PHP automatically starts:

sudo chkconfig httpd on

If you are using Centos 7 enter the following command to make MySQL start automatically:

sudo chkconfig mariadb on

If you are using Centos 6 or earlier then enter the following command to make MySQL start automatically:

sudo chkconfig mysqld on

Congratulations you now have a LAMP stack!

Now that you have finished installing all the elements of the LAMP stack you can do a few quick tests to make sure everything you need is in place. The quickest way to check your overall configuration is by creating a phpinfo file.

To create a new file in your home directory enter:

sudo nano /var/www/html/info.php

Note that on some versions of Centos the function nano is not installed. To install nano run the command below:

yum -y install nano

This will now create a new blank file. With this file enter the below code:

<?php phpinfo(); ?>

To save the file in putty press Ctrl + X, then press enter to save.

sudo service httpd restart

To view the file you just made you need to navigate to your server IP address /info.php. (Example: http://12.34.56.78/info.php).

What next?

Now that you have a LAMP stack it might be a good idea to install a server management program to help you manage things quicker through a browser interface.

I would recommend using ZPanel if you don't want to spend any money. To install ZPanel proceed to this tutorial: Installing ZPanel on Centos Server.