Over a million developers have joined DZone.

Setting Up a Lamp Stack on Debian Server

DZone 's Guide to

Setting Up a Lamp Stack on Debian Server

How to set up a basic LAMP stack on the popular Linux distro, Debian.

· DevOps Zone ·
Free Resource

We now have many web stacks to develop web applications rapidly on different operating systems. For instance, we have wamp, xamp, and Lamp stacks. All these stacks consist of some technologies. Our focus in this article is Lamp stack, which is a popular open source web platform used and endorsed by millions of developers to develop dynamic web applications on Linux servers. Lamp is a group of four open source technologies which are:

1. Linux operating system

2. Apache web server

3. MySQL relational database

4. PHP scripting language

In this tutorial, I will be setting up the lamp stack on a Debian server using Cloudways hosting. If you are a SysAdmin, and you are confused about how to set up lamp stack, then you can follow the steps in this tutorial. I assume you have an empty Debian server on Cloudways  with root access.

Let's take a deep dive and start setting up our Lamp stack on a Debian server.

But first, you need to update all the packages present on your server by running the following command in the terminal:

$ apt-get update

This command will update all your existing packages. You also need to know the server’s IP address. Run the following command in the terminal to find out your server's IP address

$ ifconfig eth0 | grep inet | awk '{ print $2 }'

Yay! You can see your IP address on screen. Write it down. You will need it in subsequent steps. We are now ready to install our other grouped technologies, which are Apache, MySQL, and PHP. Let's start with Apache.

Install Apache on Debian Server

Apache is an open source HTTP server. Around 50% of web applications are running on Apache with an aim to provide high-quality configurations to web apps. You can install Apache on Debian by running the following terminal command

$ apt-get install apache2

When you run this, you will see a message for extra space. Just type "y" and hit enter.

After finishing installation, run Server IP address in a browser which you have noted before.

Our first installation is complete. Now you can access your server and add files in var/www/html folder, but many developers are familiar with public_html folder — so maybe you wish to change this folder to public_html. You need to edit 000-default.conf file exist in apache2/sites-enabled. Move to the folder by running the following command

$ cd /etc/apache2/sites-enabled

Next, run the vim command with file name to view this in the terminal

$ vim 000-default.conf

Find “Document root” and edit its path by pressing “i” key. Remove html and replace it with public_html. Press ESC key then type :wq to save the changes in terminal.

We also need to change the folder name from html to public_html. For this, move it to the folder and use move command to change the folder name.

$ cd /var/www
$ mv html public_html

Restart your web server by typing the following command:

$ service apache2 restart

So now we have installed Apache on our Debian server. Enter your IP address in the browser and check it.

Installing MySQL on Debian

The second main installation in this stack is the MySQL relational database. To install MySQL, run the following command in the terminal:

$ apt-get install mysql-server

After the installation finishes, a prompt will appear to set your root password:

Enter your password and select ok. Now complete the rest of the installation by typing the following command:  $ mysql_secure_installation.

Press Enter. You will be asked to enter the root password, which you just set up. Then hit Enter again.

  • Type ‘n’ in order to not change the root password and hit enter.

  • 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.

You may also need to install your favourite MySQL GUI client, like PHPMyAdmin.

PHPMyAdmin 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:

$ apt-get install mcrypt

Restart Apache server by using the service command and type the following command to install PHPMyAdmin.

$ apt-get install phpmyadmin

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 for 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:

$ cd /var/www/public_html
$ ln -s /usr/share/phpmyadmin

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

order allow,deny
allow from <your server ip>

Installing PHP on Debian

We have set up Apache and MySQL on Cloudways Linux Server. Now the next and last stack member is PHP language to write web applications. PHP can be installed by running the following command:

$ apt-get install php5 php-pear php5-mysql

After the installation of PHP, restart your Apache server by running:

$ service apache2 restart 

Move to your public_html folder by typing:

$ cd /var/www/public_html

Create a PHP file in this folder by typing the following command:

$ vim index.php

Add the following code:

<?php echo phpinfo();?>

Save the file by pressing ESC and then :wq. Run your IP address in the browser, you will see the PHP info page, which means PHP is installed on server.

Final Words

In this tutorial, we have successfully set up Lamp stack on our server and we are ready to develop applications. If you are SysAdmin or server side developer, you can easily set up LAMP stack by following the tutorial step by step. This is not the end. You can also do more configurations and installations according to requirement by following the same road map. 

lamp stack ,debian ,linux

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}