{"id":2022,"date":"2019-01-12T11:12:29","date_gmt":"2019-01-12T08:12:29","guid":{"rendered":"http:\/\/kifarunix.com\/?p=2022"},"modified":"2019-01-12T11:13:50","modified_gmt":"2019-01-12T08:13:50","slug":"install-lamp-stack-on-debian-9","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-lamp-stack-on-debian-9\/","title":{"rendered":"Install LAMP Stack on Debian 9"},"content":{"rendered":"

Welcome to our guide on how to install LAMP Stack on Debian 9. LAMP Stack basically stands for Linux server, Apache web server, MariaDB\/MySQL relational database and PHP the server side scripting language. These all components are installed together on a server so that it can be able host websites and web applications.<\/p>\n

Install LAMP Stack on Debian 9<\/h2>\n

The first component of LAMP stack is the Linux server, which in this case is your Debian 9 server your are working on. Hence, run the command below to upgrade your system packages.<\/p>\n

apt update\r\napt upgrade<\/code><\/pre>\n

Once the upgrade is done, proceed as follows.<\/p>\n

Install Apache Web server<\/h3>\n

Apache web server packages are available on the default Debian 9 repositories. Hence, you can simply run the command below to install it.<\/p>\n

apt install apache2<\/code><\/pre>\n

By default, Apache is set to start after installation. It is also enabled to run in system boot. Verify by running the commands below;<\/p>\n

systemctl status apache2\r\n\u25cf<\/span> apache2.service - The Apache HTTP Server\r\n   Loaded: loaded (\/lib\/systemd\/system\/apache2.service; enabled; vendor preset: enabled)\r\n   Active: active (running)<\/span> since Sat 2019-01-12 01:36:47 EST; 2min 23s ago\r\n Main PID: 2915 (apache2)\r\n   CGroup: \/system.slice\/apache2.service\r\n           \u251c\u25002915 \/usr\/sbin\/apache2 -k start\r\n           \u251c\u25002918 \/usr\/sbin\/apache2 -k start\r\n           \u2514\u25002919 \/usr\/sbin\/apache2 -k start<\/code><\/pre>\n
systemctl list-unit-files --state=enabled | grep apache\r\napache2.service           enabled<\/strong><\/code><\/pre>\n

If UFW is running, allow Apache through it. To open both TCP ports 80 and 443, simply run the command;<\/p>\n

ufw allow in \u201cWWW Full\u201d<\/code><\/pre>\n

Navigate to your and enter the IP address of your server, http:\/\/server-IP<\/code> to verify that your web server is ready to server your web content. You should be welcomed by Apache2 Default Page.<\/p>\n

\"Install<\/a><\/p>\n

That is great. Apace web server is ready.<\/p>\n

Install MariaDB on Debian 9<\/h3>\n

MariaDB is a fork of the famous MySQL. As a result, MySQL defaults to MariaDB when installation is done. That is to say, even if you use the MySQL installation package, mysql-server<\/code>, it will install MariaDB. Hence, it is a good idea to install MariaDB using its installation package, mariadb-server<\/code>.<\/p>\n

apt install mariadb-server<\/code><\/pre>\n

Just like Apache, MariaDB is started and enabled to run on system boot after the installation is done.<\/p>\n

By default, MariaDB ships with a simple security script that can be run to remove anonymous database users, disallow remote root login, remove test databases in a move to perform preliminary database security. Hence run the script as shown below;<\/p>\n

mysql_secure_installation<\/code><\/pre>\n

When the script is run, it prompts you to enter the current root password. Since there is none yet, press Enter to set a new root password.<\/p>\n

...\r\nEnter current password for root (enter for none): Press Enter<\/strong>\r\nOK, successfully used password, moving on...\r\n\r\nSetting the root password ensures that nobody can log into the MariaDB\r\nroot user without the proper authorisation.\r\n\r\nSet root password? [Y\/n] y<\/strong>\r\nNew password: P@SSW0rd<\/strong>\r\nRe-enter new password: P@SSW0rd<\/strong>\r\nPassword updated successfully!\r\nReloading privilege tables..\r\n ... Success!<\/code><\/pre>\n

By default, MariaDB ships with anonymous users that can login to the database without requiring a user account. This poses a security risk. Hence, remove these users.<\/p>\n

Remove anonymous users? [Y\/n] y<\/strong>\r\n ... Success!<\/code><\/pre>\n

Disable remote root login such that root user can only login to database locally.<\/p>\n

Disallow root login remotely? [Y\/n] y<\/strong>\r\n ... Success!<\/code><\/pre>\n

Remove the test database<\/p>\n

Remove test database and access to it? [Y\/n] y<\/strong>\r\n - Dropping test database...\r\n ... Success!\r\n - Removing privileges on test database...\r\n ... Success!<\/code><\/pre>\n

Next, reload privilege tables to effect the changes we have made.<\/p>\n

Reload privilege tables now? [Y\/n] y<\/strong>\r\n ... Success!\r\n\r\nCleaning up...<\/code><\/pre>\n

The MariaDB is successfully set on Debian 9<\/p>\n

Install PHP on Debian 9<\/h3>\n

PHP being the server-side scripting language processes PHP code to generate dynamic web content. PHP and its extensions is available on the default Debian 9 repositories. Hence, run the command below to install PHP and other common PHP extensions.<\/p>\n

apt install php libapache2-mod-php php-mysql php-common php-gd php-mbstring php-curl php-xml<\/code><\/pre>\n

Now that the installation is done, we need to configure Apache to be able to process PHP content. This can be done by editing the file, \/etc\/apache2\/mods-available\/dir.conf<\/code><\/span> and adding index.php<\/code> as the first value of the DirectoryIndex<\/code> such that the configuration looks like;<\/p>\n

vim \/etc\/apache2\/mods-enabled\/dir.conf<\/code><\/pre>\n
<IfModule mod_dir.c>\r\n        DirectoryIndex index.php<\/strong> index.html index.cgi index.pl index.php index.xhtml index.htm\r\n<\/IfModule>\r\n\r\n# vim: syntax=apache ts=4 sw=4 sts=4 sr noet<\/code><\/pre>\n

Save the file and quit.<\/p>\n

Run PHP tests to confirm the web server can server PHP content correctly.<\/p>\n

Create a PHP test page with <?php phpinfo(); ?><\/code> content under the web server root directory.<\/p>\n

vim \/var\/www\/html\/test.php<\/code><\/pre>\n

Place the above content in this file. Save and quit.<\/p>\n

Restart Apache and navigate to the browser and enter the URL, http:\/\/server-IP\/test.php<\/code>. You should see a PHP info page.<\/p>\n

systemctl restart apache2<\/code><\/pre>\n

\"Install<\/a><\/p>\n

That is beautiful. Upto that far, you have learnt how to Install LAMP Stack on Debian 9. But before we call it a day, it is wise to remove the PHP test page to avoid exposing your environment to the internet script-kiddies.<\/p>\n

rm -rf \/var\/www\/html\/test.php<\/code><\/pre>\n

Enjoy.<\/p>\n

You can also check our previous articles on;<\/p>\n