This tutorial will walk you through the process of how to setup LAMP stack, which includes the set up of the components such as Apache 2, PHP , MySql 7 on Debian 9 Stretch.

This comprehensive linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

Install Apache

In this particular guide, we will show how Apache2 is configured. First Apache server will be setup and installed.

apt update
apt install apache2
systemctl start apache2 

/etc/apache2/apache2.conf 

This is the location of the apache file.

If the ufw firewall is running, we will need to allow the port 80 to take traffic. We can enable this port by following command.

ufw allow in “WWW Full”

first, quickly test that apache web server if it is setup and running fine by accessing http://localhost

Note: The local ip address of the ubuntu machine can replace localhost

Install MySQL or MariaDB

There is an option in between installing MySQL or MariaDB, which can be seen as a drop in replacement for MySQL. in MariaDB there all the enterprise features present but without all the corporate structuring.It is managed by the MariaDB Foundation as opposed to being managed by Oracle which manages MySQL. That said, they are both good choices.

Don’t install both, only one needs to be installed.

Install MySQL

After this, it is time to install MySQL. For this, both the server and client are needed. The database is run by the server while the interaction with the server is done by the client.

First get the official PPA for debian Linux. Then install packages.

wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
dpkg -i mysql-apt-config_0.8.9-1_all.deb
apt install -y mysql-server mysql-client php5-mysql

We will be asked to enter the password for the MySQL root account, since
we haven’t set this yet, so just hit ENTER . Then we will be asked to set that password. we should type Y to set a root password.

To rest of all the questions, the script asks, you should press Y, followed by the ENTER key at each prompt. During this process we will remove anonymous users and the test database, disable remote root logins, and load these new rules MySQL.

mysql_secure_installation

Install MariaDB

Since MariaDB is also an option, as mentioned earlier, and this means it can be installed just like MySql without any further steps. We will be installing MariaDB

apt install -y mariadb-server mariadb-client

Similar to MySQL server, a server needs to be setup as well.

mysql_secure_installation

Install PHP

Now to generate dynamic content, we need to install PHP ,
we will be installing the following modules.

 apt install php7 php-pear php7-mysql

we need to create a test PHP file in our document root. We will create a codesposts.php file in the document root to test our configuration

vi /var/www/html/codesposts.php
/var/www/html/codesposts.php
<?php phpinfo(); ?>      

After pasting the above code to our codesposts.php file, save and close the file.

Restart the httpd service and we can visit this web page in a web browser from the below shown URL.

systemctl restart apache2
http://<Your_serverIP>/codesposts.php

we should see a web page that has been generated by PHP with information about your server. We have successfully completed the installation process of LAMP stack on Debian 9.

Thanks for being with us, hope this help you in gaining knowledge and we are always ready to help you. For furthers queries or suggestions, kindly drop a comment below.