We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsHosting ServicesCloud Servers

How to Install LAMP on Linux Operating Systems?

Follow These Steps to Install LAMP on Linux Operating Systems

LAMP (short for Linux + Apache + MySQL + PHP) is the term used for the popular open-source web platform stack used to run dynamic websites and web applications, and it runs on most of the servers in the world.

In this tutorial you will learn how to setup your first LAMP server on CentOS, Ubuntu or Debian.

Before you get started installing the programs needed for your LAMP server, make sure you have the system updated with all the latest packages. To do so, simply run the following commands on the terminal:

For CentOS use:

$ sudo yum clean all
$ sudo yum -y update

For Debian or Ubuntu use:

$ sudo apt-get update
$ sudo apt-get upgrade

You will notice that you have to add the “sudo” command in front of “apt-get” (for Debian) or “yum” (for CentOS). It means that you are running the command as root user (Super Admin). You only need to add “sudo” if you are not logged as root.

First, you need to install the Apache web server, as it will be used to serve all the content from your server to the users.

After your system is updated, install Apache using the package manager yum, which allows you to easily install programs without the need to manually download anything from the Internet.

For Debian or Ubuntu simply use:

$ sudo apt-get install apache2

For CentOS follow these steps:

$ sudo yum -y install httpd

After you have Apache installed on your machine, register it as a service and make sure it runs on boot. First use:

$ sudo systemctl start httpd.service
$ sudo systemctl enable httpd.service

Then to enable the server to be accessed via browser, you need install firewalld and open port 80 and 443 by typing the following commands:

$ sudo yum install -y firewalld
$ sudo systemctl start firewalld
$ sudo systemctl enable firewalld
$ sudo firewall-cmd –permanent –add-port=80/tcp
$ sudo firewall-cmd –permanent –add-port=443/tcp

Afterwards, reload the firewall:

$ sudo firewall-cmd –reload

After having apache installed on your machine for either Linux distribution, make sure it’s working by opening your browser and going to http://server-ip

Note: You will find your server’s IP address on the server console from Host1Plus.

If you are using CentOS, you will see this window:

If you are using Debian, you will see this window:

If you are using Ubuntu, you will see this window:

Install MySQL by runing the following commands. For CentOS use:

$ sudo yum -y install mariadb-server mariadb

For Debian or Ubuntu use:

$ sudo apt-get install mysql-server php5-mysql

If you are performing the installation for Debian or Ubuntu, during the installation you will be required to insert the password for the “root” user, which is the master user to the MySQL server. The user will be able to view and manage all other databases accounts.

Enter a password and make sure you write it down somewhere.

Also, after you install MySQL on CentOS, initiate it and make sure it runs on boot:

$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb.service

Now run an installation script on Debian or Ubuntu to secure your database:

$ sudo mysql_secure_installation
$ sudo mysql_install_db

With the second command you will be required to enter the root password again, and then just follow the directions on screen (P.S.: you don’t need to change the root password again as you have just created it), you can press ENTER on all the other questions.

For CentOS, first you will be required to enter the current ‘root’ password, which is blank, so just press Enter. Second, you will enter a new password. Don’t forget to write it down.

Set root password? [Y/n]
New password: (enter password)
Re-enter new password: (enter password)
Password updated successfully!
Reloading privilege tables...
... Success!

You now have a database server up and running on your machine!

To install PHP on your server, use yum for CentOS or apt-get for Debian or Ubuntu with the following commands. For Debian or Ubuntu type:

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

For CentOS type:

$ sudo yum -y install php php-mysql

Once the installation is complete, you should have PHP installed and working perfectly on your system. For CentOS, all we need to do now is restart Apache:

$ sudo systemctl restart httpd.service

Now you have to change the default folder where your web files will be served, start by installing Nano, which is a text editor.

$ sudo yum -y install nano

Create your first configuration file:

$ sudo nano /etc/httpd/conf.d/example.conf

Write the following information into the file (you can copy and paste the code below). If you have a domain pointing to the server’s IP, you can change the example.org text and uncomment the two lines below.

# file: /etc/http/conf.d/example.conf
# vhost: example.org *.example.org#ServerName example.org
#ServerAlias *.example.org
ServerAdmin webmaster@example.org
ErrorLog /var/log/httpd/example.err
CustomLog /var/log/httpd/example.log combined
DocumentRoot /var/www/html
<Directory “/var/www/html”>
Order allow,deny
Allow from all

On CentOS, to save the file, you have to press Ctrl+X, Y and ENTER to save it. Now if you go to the browser and reload the page that has your IP address, you will see your message on the screen.

To make sure that PHP is working, go to the folder where you serving the web files ‘/var/www/html’. To do so, type on the terminal:

$ cd /var/www/html
$ ls

Once you type the second command, you will see that in the folder you have a file called index.html. Delete this file and create a new one.

$ rm index.html

Now that you have deleted the index.html file, install an easy to use text editor called Nano.

$ sudo apt-get install nano

One last thing you can do on either of the Linux distributions, is to create a info.php file that you would be able to see all our system configurations. To do so, follow a similar process:

$ nano /var/www/html/info.php
And type inside the file:

&lt;? phpinfo(); ?&gt;

Now go to http://you_server_ip/info.php on your browser and you will be able to see all the configurations.
If you are using Ubuntu, you should see this:

As for Debian, you should see the window below:

And if you are a CentOS user, you will see this window:

Rate this Tutorial:
No Comments