Setting up a Lamp Stack on Debian Server
Tags: lamp, php, webdev, devtips
Published: 2017-08-17 09:11:41

---
title: Setting up a Lamp Stack on Debian Server
published: true
description: Learn How you can create the complete LAMP Stack on a debian server.
cover_image: https://www.cloudways.com/blog/wp-content/uploads/Installing-LAMP-Stack-On-Debian-Banner.jpg
tags: LAMP,PHP,webdev,devtips
---

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. 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 with root access.

Note: If you do not know about sysadmin process and creating the LAMP stack, you can use Cloudways [**Managed PHP Hosting**](https://www.cloudways.com/en/php-cloud-hosting.php). The complete server containing Lamp stack and PHP app will be built in few min and you don’t need to worry about it.  

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

But first, you need to update all the packages present on your server by running the following command in 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 terminal to find out your server IP address

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

Yay! You can see your IP address on screen. Note it down. You will need it in subsequent installation steps. So ninjas! We are ready to install 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 the 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.

![](https://www.cloudways.com/blog/wp-content/uploads/image04-32-768x160.png)

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

![](https://www.cloudways.com/blog/wp-content/uploads/image00-49-768x577.png)


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

![](https://www.cloudways.com/blog/wp-content/uploads/image03-37-768x383.png)

We also need to change the folder name from html to public_html. For this, move 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 in 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 MySQL relational database. To install MySQL, run the following command in terminal

```
$ apt-get install mysql-server
```

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

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

```
$ mysql_secure_installation
```

Press enter. You will be asked to enter root password, which you just setup. Type password and hit enter. 

-Type â€˜n’ in order to not change 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

![](https://www.cloudways.com/blog/wp-content/uploads/image05-24.png)

You may also need to install your favourite MySQL GUI client 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 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. As we have installed Apache, so select Apache and press enter. After that, it will ask for the root user password. 

![](https://www.cloudways.com/blog/wp-content/uploads/image01-46-768x274.png)

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 Linux. 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 line in it

```
<?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.

![](https://www.cloudways.com/blog/wp-content/uploads/image02-39-768x443.png)

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