How 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 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:
For CentOS follow these steps:
After you have Apache installed on your machine, register it as a service and make sure it runs on boot. First use:
$ 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 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:
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:
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_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...
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:
For CentOS type:
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:
Now you have to change the default folder where your web files will be served, start by installing Nano, which is a text editor.
Create your first configuration file:
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.
# vhost: example.org *.example.org#ServerName example.org
CustomLog /var/log/httpd/example.log combined
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:
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.
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:
<? phpinfo(); ?>
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: