How to Install LAMP on Debian Server

by Ahmed Khan  August 9, 2016

A LAMP stack is a group of open source softwares: Linux, Apache, MySQL and PHP. Through this stack, the developers run their websites. Most VPS or Cloud servers come pre-installed with Linux. They need to install LAMP stack themselves in order to run their website.

Install LAMP on Debian Server

Most developers who are not SysAdmins don’t know how to create a LAMP stack on their servers. If you are one of those developers, this blog is for you. In this blog, we will learn how to install LAMP on Debian server along with PHPMyAdmin to manage your database.

Before getting started, make sure that all the packages on your server are updated. To do that, open your terminal and run the following command:

Now your packages are updated. Now type the following command to know your server IP which will also be useful in the coming tutorial. Type the following command to know print your server IP.

Let’s start creating a Stack now:

Step 1: Installing Apache on Debian

Web Server are the main components that run your site. Apache is an open source software which runs 50% of the Web Servers around the world. To install apache on your server, run the following command:

When it prompts, that extra space would be filled; type “y” and press enter

Installing Apache on Debian

Once the download is finished, head to your IP address and you will see this page:

Installing Apache 2 on Debian

By default, you can access your server scripts by adding it inside /var/www/html and creating your script there. In order to configure it to run your scripts inside public_html, we need to configure 000-default.conf file which is inside apache2/sites-enabled. To reach that folder type the following command:

Now use the following command to view and edit that file.

Now head to DocumentRoot, press “i” to edit the file and then remove html from it and add public_html. Now save the file by pressing Esc and type “:wq”.

editing apache root folder

Now head to the terminal and type the following command to reach public_html folder and change the html folder name to public_html

Now restart your web server by typing the following command:

Now your settings are changed, and you can check them by going to your IP address and you will find the html directory root of your server. Now that we have configured Apache, let’s install MySQL.

Step 2: Installing MySQL on Debian

On your terminal, type the following command:

Once installation is finished, it will ask you to set your root password.

installing mysql on debian

Set your password and press enter. Now complete your MySQL installation by running the following command:

Once you hit enter, the prompt will ask you to enter the root password, then hit enter.

  • Type n in order to not change root password and hit enter,
  • then type y to remove anonymous users and hit enter.
  • Type n if you want to disallow root login remotely and hit enter.
  • Now type y to remove test tables and databases and hit enter, then type y again and hit enter.

installing mysql on debian 2

MySql is now successfully installed on your server.

Step 3: Installing PHP on Debian

We have successfully setup Apache and MySql on our web server till now. One thing that’s remaining is server side language. We will be installing PHP on our server, so type the following command to install it:

Once it’s complete, run the following command to restart your Apache:

Now head to your public_html folder of Apache by typing the following command

Let’s create a simple PHPinfo file in it now. Type the following command to create a file:

Press i to start writing and type the following code in it:

Save it by pressing Esc and then typing :wq. Now head to browser and open [server ip]/index.php. Type the following command to find out your IP address again:

php info

If you find your PHPinfo file rendered then you have successfully configured your LAMP stack.

Step 4: Installing PHPMyAdmin on Debian

Every developer wants a UI to manage their MySQL server and they mostly prefer PHPMyAdmin which is an open source GUI which helps in Managing MySQL databases. Let’s first setup a SSL with Apache so that our password isn’t sent in plain text form. Type the following to install a SSL with Apache:

Now restart your Apache with the following command:

Now install PHPMyAdmin by typing the following command:

During the installation a prompt will open which will ask you about the Web Server you are using select Apache and press enter, after that it will ask about the root user password. Enter the password and press enter.

PHPMyAdmin will be installed in /usr/share/phpmyadmin. We will create it’s symlink inside the public_html folder. Type the following commands to route to public_html folder and create a symlink:

Now type ls to verify whether the symlink has been created or not. Now open this in browser through [server ip address]/phpmyadmin. You will see that it is installed. Now let’s secure it create by creating a .htaccess file using vim inside PHPMyAdmin folder and allowing only your server ip to access it.

Step 5: Installing Git and Composer

Every developer nowadays uses Git to manage their version controller and Composer to manage and install packages. These 2 softwares have become essential for every server, so let’s install them by typing the following command to install git:

Now type the following commands, as discussed on the official website, to install composer and move it to the bin so we can use it when we call composer on command line.

Conclusion:

In this tutorial you have learned how to install LAMP on Debian server. As you can see, creating a LAMP stack on an empty server is simple but it takes a lot of time and effort. When a client gives you a project, you will need to complete it and test it on a web server. But if you are going to build your own server, it will take a lot of time and the client might  get upset. That’s where Cloudways comes handy.

On Cloudways, you just need to create your account, run your server and everything will be installed and the UI environment will be set up for you within minutes. You just need to upload your work on your server and start testing your project on an online server. Sign Up now on Cloudways to enjoy 3 days of free trial on DigitalOcean server.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways – A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...