---
slug: Deploying a LAMP Environment on Debian 9 Installation Guide
title: Deploying a LAMP Environment on Debian 9 Installation Guide
authors: [slorber]
tags: [hola, docusaurus]
---

<!-- truncate -->

import CodeBlock from '@site/src/components/CodeBloack';
import debian from '@site/static/img/debian.png';
import php from '@site/static/img/php7.0.png';


<div className="head">Introduction</div>

<div className="text">Have you ever wondered how websites work? Well, they rely on a special combination of software called a "LAMP" stack. It's like a toolbox for building websites and apps. Let's break it down:</div>

<div className="text"><ol><li>"L" stands for Linux, which is the operating system that runs the server.</li><li>"A" stands for Apache, which is the software that serves up web pages to visitors.</li><li>"M" stands for MariaDB, which is where all the data for the website is stored.</li><li>"P" stands for PHP, which is a programming language used to create dynamic content on the website.</li></ol></div>

<div className="text">In this guide, we'll walk you through setting up a LAMP stack on a Debian 9 server. It's easier than it sounds, and by the end, you'll have your own server ready to host websites and apps!</div><br/>


<div className="head">Preparation for Debian 9 LAMP Setup</div>
<div className="text">Before we begin, ensure you have the following:</div>
<div className="text"><ol><li>A Debian 9 server: This will be your website's home.</li><li>A user account with special permissions: This allows you to make important changes.</li><li>Basic security measures: Protect your server from unauthorized access.</li></ol></div>

<div className="text">If you're unsure how to set these up, don't worry. We'll guide you through it.</div><br/>


<div className="head">Setting Up Apache and Security</div>
<div className="text">Apache is a crucial component for hosting websites. It helps to serve web pages to people when they visit your site. Here's how to get it running on your server:</div><br/>

<div className="text">First, we need to install Apache. Don't worry, it's easy! We'll use something called "apt" to do this, which is a tool that helps install software on your server. Just follow along with these steps:</div>

<CodeBlock code={`sudo apt update
sudo apt install apache2
`} /><br/>

<div className="text">Since this is a special command, you'll need to confirm with your regular password to make sure it's you making the changes.</div><br/>

<div className="text">Once you've entered your password, it will show you what it's going to do and how much space it will need. Simply type "Y" and then press ENTER to proceed. That's all there is to it!</div><br/>

<div className="text">Next, if you've already set up the UFW firewall by following the initial server setup instructions, ensure that your firewall lets through traffic for both HTTP and HTTPS.</div><br/>

<div className="text">When you install UFW on Debian 9, it already has some pre-made settings that you can use to adjust your firewall. You can see all of these settings by running:</div>

<CodeBlock code={`sudo ufw app list`} /><br/>

<div className="text">The WWW profiles help control the ports that web servers use.</div>

<CodeBlock code={`Output
Available applications:
. . .
  WWW
  WWW Cache
  WWW Full
  WWW Secure
. . .
`} /><br/>

<div className="text">If you look at the WWW Full profile, you'll see that it allows traffic to go through ports 80 and 443.</div>

<CodeBlock code={`sudo ufw app info "WWW Full"`} /><br/>

<CodeBlock code={`Output

Profile: WWW Full
Title: Web Server (HTTP,HTTPS)
Description: Web Server (HTTP,HTTPS)

Ports:
  80,443/tcp
`} /><br/>

<div className="text">Enable incoming traffic for websites using HTTP and HTTPS on this profile.</div>

<CodeBlock code={`sudo ufw allow in "WWW Full"`} /><br/>

<div className="text">You can quickly check if everything worked by opening your web browser and entering your server's public IP address.</div>

<CodeBlock code={`http://your_server_ip`} /><br/>

<div className="text">You'll see a page with some information and a test message. This page is provided by Apache on Debian 9 to help you check if everything is set up correctly.</div><br/>

<img src={debian} alt="Debain" />

<div className="text">If you can see this page, it means your web server is set up correctly and can be accessed through your firewall.</div><br/>

<div className="text">If you're not sure what your server's public IP address is, there are a few ways to find it. Usually, it's the same address you use to connect to your server through SSH.</div><br/>

<div className="text">You can find your IP address using a command in the terminal. One way is to use a tool called "iproute2." Just type this command:</div>

<CodeBlock code={`ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'`} /><br/>

<div className="text">When you run the command, you'll get two or three lines back, each showing a different address. They're all correct, but your computer might only be able to use one of them. So, you can try each one to see which works.</div><br/>

<div className="text">Another way to find your IP address is by using a tool called "curl." This tool contacts another server to ask what your IP address is. Here's how to do it:</div>

<CodeBlock code={`sudo apt install curl
curl http://icanhazip.com
`} /><br/>

<div className="text">No matter how you found your IP address, just type it into the address bar of your web browser to see the default Apache page.</div><br/>


<div className="head">Step 2: Configure MariaDB</div>
<div className="text">Now that your web server is set up, let's install MariaDB. MariaDB is like a filing cabinet for your website. It stores and manages all the information your site needs.</div><br/>

<div className="text">In Debian 9, MariaDB is the default database system instead of MySQL. To install it, we'll use a special package called mariadb-server. It's the best way to get MariaDB up and running smoothly.</div><br/>

<div className="text">Once more, use apt to get and install this software:</div>

<CodeBlock code={`sudo apt install mariadb-server`} /><br/>

<div className="note"><strong>Note</strong>:  You don't need to run sudo apt update again before this command. You already did it earlier when installing Apache, so your computer's list of available software should be current.</div><br/>

<div className="text">This command will also show you a list of packages that will be installed and how much space they'll take up. Just type "Y" to continue.</div><br/>

<div className="text">After the installation is finished, you'll want to run a security script that's already included with MariaDB. This script helps make your database more secure by fixing some settings that might not be safe and restricting access to it. To start the script, just run:</div>

<CodeBlock code={`sudo mysql_secure_installation`} /><br/>

<div className="text">This will guide you through a series of questions where you can adjust the security settings for your MariaDB installation.The first question will ask you to enter the current database root password. This is like having a master key to access all parts of MariaDB, similar to the administrator account on your computer. Since you've just installed MariaDB and haven't set a password yet, simply press ENTER when prompted.</div><br/>

<div className="text">The next question will ask if you want to set up a password for the database root. Type "N" for "No" and then press ENTER. In Debian, the root account for MariaDB is closely tied to automated system maintenance. It's important not to change the authentication methods for this account. If you do, it could cause problems with future updates and possibly break the database system. We'll show you later how to set up another administrative account if you need password access for your specific situation.</div><br/>

<div className="text">After that, you can type "Y" and then press ENTER to stick with the default settings for the rest of the questions. This will remove some anonymous users and a test database, stop remote root logins, and apply these changes right away so MariaDB follows your instructions.</div><br/>

<div className="text">On new installs of Debian systems, the root MariaDB user is set to authenticate using the unix_socket plugin instead of a password by default. This adds some security and convenience in many situations, but it might complicate things if you need to give external programs (like phpMyAdmin) administrative rights.</div><br/>

<div className="text">Because the root account on the server handles important tasks like managing logs and starting and stopping services, it's not a good idea to change its authentication settings. If you modify the root account credentials in the /etc/mysql/debian.cnf file, it might work at first, but updates could overwrite those changes in the future. Instead, it's recommended to create a new administrative account if you need to use a password for access.</div><br/>

<div className="text">To do this, we'll create a new account called "admin" with the same privileges as the root account, but set up for password authentication. Here's how: Open the MariaDB prompt in your terminal:</div>

<CodeBlock code={`sudo mariadb`} /><br/>

<div className="text">Now, let's make a new user with special powers like the root user, but this time with a password. You can choose any username and password you like: <br/>
Just follow these steps to create the new user:</div>

<CodeBlock code={`GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;`} /><br/>

<div className="text">After creating the new user, we need to make sure the changes are applied right away. To do this, just follow this step:</div>

<CodeBlock code={`FLUSH PRIVILEGES;`} /><br/>

<div className="text">Then, exit the MariaDB shell as follows:</div>
<CodeBlock code={`exit`} /><br/>

<div className="text">From now on, whenever you want to use your database with your new administrative user, you'll need to log in as that user using the password you just created. Here's how:</div>

<CodeBlock code={`mariadb -u admin -p`} /><br/>


<div className="text">Now your database is ready, and you can proceed to install PHP, which is the last part of the LAMP stack.</div><br/>


<div className="head">Step 3: Getting PHP up and running</div>

<div className="text">PHP is what makes your website dynamic, allowing it to process code and display content based on your needs. It can run scripts, fetch data from your MariaDB databases, and deliver the final content to your web server for display.</div><br/>

<div className="text">To install PHP, we'll once again use the apt system. This time, we'll also include some helper packages so that PHP can work seamlessly with Apache and communicate with your MariaDB database.</div>

<CodeBlock code={`sudo apt install php libapache2-mod-php php-mysql`} /><br/>

<div className="text">PHP installation should go smoothly without any issues. We'll check this soon.</div><br/>

<div className="text">Usually, you'll want to adjust how Apache serves files when someone asks for a directory on your website. Right now, if someone requests a directory, Apache will look for a file named index.html first. Instead, we want Apache to look for an index.php file first.</div><br/>

<div className="text">To do this, follow these steps to open the dir.conf file using a text editor with special permissions:</div>

<CodeBlock code={`sudo nano /etc/apache2/mods-enabled/dir.conf`} /><br/>

<div className="text">Here's what it will look like:</div>

<CodeBlock code={`<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
`} /><br/>

<div className="text">Let's move the PHP index file (highlighted earlier) to the top position after the DirectoryIndex specification, like this:</div>

<CodeBlock code={`<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
`} /><br/>

<div className="text">Once you're done, save and close the file by pressing CTRL+X. Confirm the save by typing "Y" and then press ENTER.</div><br/>

<div className="text">After that, you'll need to restart the Apache web server to apply your changes. Just type this:</div>

<CodeBlock code={`sudo systemctl restart apache2`} /><br/>

<div className="text">You can also see if the Apache web server is running properly by using this command:</div>
<CodeBlock code={`sudo systemctl status apache2`} /><br/>

<CodeBlock code={`Sample Output
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-09-04 18:23:03 UTC; 9s ago
  Process: 22209 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 22216 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 22221 (apache2)
    Tasks: 6 (limit: 4915)
   CGroup: /system.slice/apache2.service
           ├─22221 /usr/sbin/apache2 -k start
           ├─22222 /usr/sbin/apache2 -k start
           ├─22223 /usr/sbin/apache2 -k start
           ├─22224 /usr/sbin/apache2 -k start
           ├─22225 /usr/sbin/apache2 -k start
           └─22226 /usr/sbin/apache2 -k start
`} /><br/>

<div className="text">To add more features to PHP, you can install extra modules. To see what modules and libraries are available, you can use a command called "apt search" along with a tool called "less" that lets you scroll through the list easily. Here's how:</div>

<CodeBlock code={`apt search php- | less`} /><br/>

<div className="text">You can use the arrow keys to move up and down the list, and press the "Q" key when you're done looking.</div><br/>

<div classname="text">These results show extra things you can add to PHP if you want. Each one comes with a brief description to help you understand what it does.</div>

<CodeBlock code={`Output

Sorting...
Full Text Search...
bandwidthd-pgsql/stable 2.0.1+cvs20090917-10 amd64
  Tracks usage of TCP/IP and builds html files with graphs

bluefish/stable 2.2.9-1+b1 amd64
  advanced Gtk+ text editor for web and software development

cacti/stable 0.8.8h+ds1-10 all
  web interface for graphing of monitoring systems

cakephp-scripts/stable 2.8.5-1 all
  rapid application development framework for PHP (scripts)

ganglia-webfrontend/stable 3.6.1-3 all
  cluster monitoring toolkit - web front-end

haserl/stable 0.9.35-2+b1 amd64
  CGI scripting program for embedded environments

kdevelop-php-docs/stable 5.0.3-1 all
  transitional package for kdevelop-php

kdevelop-php-docs-l10n/stable 5.0.3-1 all
  transitional package for kdevelop-php-l10n
…
:
`} /><br/>

<div className="text">To find out more about what each module does, you can search online for more information. Alternatively, you can see a detailed description of the package by typing:</div>

<CodeBlock code={`apt show package_name`} /><br/>

<div className="text">You'll see a lot of information, including a field called "Description" that provides a detailed explanation of what each module does.</div><br/>

<div className="text">For instance, if you want to know what the "php-cli" module does, you can type:</div>

<CodeBlock code={`apt show php-cli`} /><br/>

<div className="text">In addition to a bunch of other details, you'll come across something like this:</div>

<CodeBlock code={`Output
…
Description: command-line interpreter for the PHP scripting language (default)
 This package provides the /usr/bin/php command interpreter, useful for
 testing PHP scripts from a shell or performing general shell scripting tasks.
 .
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
 .
 This package is a dependency package, which depends on Debian's default
 PHP version (currently 7.0).
…
`} /><br/>

<div className="text">If you find a program you want to add to your computer, you can use a command called 'apt install' to do it, just like you did before.</div><br/>

<div className="text">For example, if you think you need something called 'php-cli,' you can type:</div><br/>

<CodeBlock code={`sudo apt install php-cli`} /><br/>

<div classname="text">If you want to add multiple programs at the same time, you can do it by writing down the names of each program with a space between them. Just like before, use the 'apt install' command, and list the names of the programs you want to install, like this:</div>

<CodeBlock code={`sudo apt install package1 package2 …`} /><br/>

<div className="text">Now that you've set up your LAMP stack, it's a good idea to check if everything is working smoothly with PHP. This way, you can catch and fix any problems before you make more changes or try running an application. Let's test your PHP configuration to make sure everything is okay.</div><br/>

<div className="head">Step 4 — Running PHP on your web server and testing it</div>

<div className="text">Now, let's check if PHP is set up correctly on your system by creating a simple PHP script. We'll call it 'info.php.' To make sure Apache can find and show this file, we need to save it in a specific folder called the 'web root.'</div><br/>

<div className="text">Now, let's check if PHP is set up correctly on your system by creating a simple PHP script. We'll call it 'info.php.' To make sure Apache can find and show this file, we need to save it in a specific folder called the 'web root.'</div>

<CodeBlock code={`sudo nano /var/www/html/info.php`} /><br/>

<div className="text">Once the blank file opens, let's add some code to make it a valid PHP file. Copy and paste the following text into the file. This code is what PHP understands and can execute:</div>

<CodeBlock code={`<?php
phpinfo();
?>
`} /><br/>

<div className="text">After you've added the code, save the changes and close the file.</div><br/>

<div className="text">Now, let's see if your web server can show the result of the PHP script. To do this, open your web browser and go to a specific page. You'll need to use your server's public IP address again.</div><br/>

<div className="text">The page you want to visit is:</div>

<CodeBlock code={`http://your_server_ip/info.php`} /><br/>

<div className="text">When you go to the page in your web browser, it should look something like this:</div><br/>

<img src={php} alt="php7.0" />

<div className="text">This page shows basic information about your server using PHP. It's helpful for checking if everything is set up correctly.</div><br/>

<div className="text">If you can view this page in your browser, it means PHP is working as it should.</div><br/>
<div className="text">However, it's a good idea to delete this file after testing because it might share information about your server with unauthorized users. To remove it, just type the following command:</div>

<CodeBlock code={`sudo rm /var/www/html/info.php`} /><br/>

<div className="text">If you ever need the information on this page again, don't worry! You can always recreate it. For now, it's a good practice to remove the page to keep your server secure. If you ever need it in the future, you can easily make it again.</div><br/>


<div className="con">Conclusion:</div>

<div className="text">Now that you have your LAMP stack ready, you're like a superhero with the power to create all sorts of websites and cool things on your computer!</div><br/>

<div className="text" style={{ textAlign: 'center' }}><strong>Feeling excited? </strong></div><br/>

<div className="text" style={{ textAlign: 'center' }}><strong>How about checking out some fun websites or even trying to make your very own? </strong></div><br/>

<div className="text">There are so many cool things you can do! If you ever get stuck or need some help, just ask and we're here for you!</div>
