Jun 10, 2015

Set Up Linux, Apache, MySQL, PHP (LAMP) Stack on Debian 8

The LAMP stack of software, consisting of the Linux operating system, Apache web server, MySQL database, and PHP scripting language, is a great foundation for web or application development. When you installed it together, this software stack enables your server to host dynamic websites and web applications.

This article will guide you through the steps to install and configure LAMP Stack on a Debian 8.


To follow the steps mentioned in this guide, please complete the following:

  • One Debian 8 (virtual or physical) machine with a sudo user
  • Install basic security packages such as IPTables, Fail2Ban, and others

If you have not yet finished your basic Debian 8 setup, go ahead and finish that up and then come back to this guide. We'll be using a sudo user to complete this article. 

Update the System

Before installing any software, it’s important to make sure your system is up to date. Log in to your system via SSH.

To update your package lists, type:
sudo aptitude update

This above command compare the software packages currently installed with any new versions that might have been updated recently in the Debian online repositories, where base software packages are stored.
Let’s go ahead and update our system. You can do this by executing this command:
sudo aptitude safe-upgrade

Once you’ve determined that these updated software components are relevant for your needs, go ahead and update your machine. This may take a while, depending on the current version of the operating system you have installed, software packages, and network conditions. On a fresh Debian 8, it will take a couple of seconds.

Once done however, the Debian 8 machine is fully patched, updated, and ready for our LAMP installation.

Install Apache

The next step in our LAMP installation is installing the Apache web server. This is a popular web server that allows your server to display web content. To install Apache, type the following:
sudo aptitude install apache2 apache2-doc

This installs the basic Apache web server package as well as the documentation that goes along with it. This may take a few seconds as Apache and its required packages are installed. Once done, Aptitude will exit; Apache is now installed.

Let's test that the web server will respond with a sample web page. You will need the IP address of your Debian 8 machine.

sudo ifconfig eth0

On your screen, you will see a few lines of output, including your server's IP address. You'll want the four-part number shown after inet addr::

inet addr:

Note the IP address listed and type it into your favorite web browser like this:

Once done, you will see the default Apache 2 web page, similar to this:

If you see this page, then you have successfully installed Apache on your Debian 8 machine!

You can upload your website content to the /var/www/html directory. If you want to set up multiple websites, please see this article on setting up Apache virtual hosts.

Install and Secure MySQL

The next component of the LAMP server is MySQL. This relational database software is an essential backend component for other software packages such as WordPress, Joomla, Drupal, and many others.

To install MySQL and PHP support for it, type the following:

sudo aptitude install mysql-server php5-mysql

This will install MySQL and other required packages. Note that the installation routine will ask you to enter a new password for the root MySQL user:

This is a separate account used specifically for MySQL for administrative functions. The username is root and the password is whatever you set here. Be sure to set a good password with various combinations of letters and numbers.

After this, the MySQL installation is finished.

To keep your new database server safe, there is an additional script you need to run. Type the following to get started:

sudo mysql_secure_installation

At this point, the script will now ask you a few questions. When prompted, go ahead and enter the password for the root MySQL account. The system will then ask you:

Change the root password? [Y/n] n

Since we already set the root MySQL password at our installation, you can say no at this point. The script will then ask:

Remove anonymous users? [Y/n] y

Go ahead and answer yes to remove the anonymous users option for safety. You can answer yes to the rest of the questions as well by entering y.

Next, the script will ask you to either allow or disallow remote logins for the root account. For safety, disallow remote logins for root unless your environment requires this.

Finally, the script will ask you to remove the test database and then reload the privilege tables. Answer yes to both of these. This will remove the test database and process the security changes.

If everything is correct, once done, the script will return with:

All done!  If you have completed all of the above steps, your MySQL installation should now be secure.

Let's double-check that our new MySQL server is running. Type this command:

mysql -u root -p

Enter the root password you set up for MySQL when you installed the software package. Remember, this is not the root account used for your Debian administration. Once in, now type the following to get the server status, version information and more:

mysql> status

This is a good way to ensure that you’ve installed MySQL and are ready for further configuration. When you are finished examining the output, exit the application by typing this:

mysql> exit

Install PHP

For our last component, we will set up and install PHP, known as PHP: Hypertext Preprocessor. This widely-used server-side scripting language is used far and wide for dynamic web content, making it essential to many web and application developers.

To install PHP, simply type the following:

sudo aptitude install php5-common libapache2-mod-php5 php5-cli

Agree to the installation and PHP will be installed on your Droplet. You will see many packages being installed beyond just PHP; don’t worry, as this is integrating the software with your existing Apache2 installation and other programs.

Restart Apache on your Droplet to make sure all of the changes with the PHP installation take effect. To do this, type the following:

sudo service apache2 restart

Now, let’s take a moment to test the PHP software that you just installed. Move into your public web directory:

cd /var/www/html

Once there, use your favorite console text editor to create a file named info.php. Here’s one method of doing this:

sudo vi info.php

This command will use the command line editor vi to open a new blank file with this name. Inside this file, type the following:

Save your changes. Open your web browser and type the following URL:

If you’ve done everything correctly, you will see the default PHP information page, like the one shown below:

When you are done looking at this test PHP page, please remove it for security. To do that, run this command:

sudo rm -i /var/www/html/info.php

The system will then ask you if you wish to remove the test file that you've created. Answer yes and you're finished; you have completed the basic PHP installation.

You have now successfully installed the basic LAMP stack on your Debian 8 machine.

Post a Comment

TECH SUPPORT © 2012-2017