Search for:
Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9

Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9 &-8211; this Article or News was published on this date:2019-05-28 16:51:26 kindly share it with friends if you find it helpful

Early this week (June 17, 2017), the Debian project announced the availability of the new stable version (Debian 9) codenamed Stretch.

With this release, the well-known and widely-used Debian 8 Jessie gained old-stable status, which designates the previous stable repository. As it always happens with the release of a new stable version, Stretch includes hundreds of new packages, and updates to thousands of others.

Since Debian powers a large percentage of web servers all over the world, in this article we will explain how to install the LAMP stack in Debian 9 Stretch.

This will allow system administrators to set up brand new web servers on top of Stretch using the recent updates to the distribution’s official repositories. It is assumed that you have installed Debian 9 on a bare metal, virtual machine, or VPS, or have upgraded from Jessie.

Installing LAMP in Debian 9 Stretch

The “M” in LAMP stands for MariaDB or MySQL, the database server for the stack. Depending on your choice, you can install the database server and the other components (the Apache web server and PHP) as follows.

Install LAMP with MariaDB on Debian 9

- aptitude update && aptitude install apache2 mariadb-server mariadb-client mariadb-common php php-mysqli

Install LAMP with MySQL on Debian 9

- aptitude update && aptitude install apache2 mysql-server mysql-client mysql-common php php-mysqli

As a personal preference, I will use MariaDB in the rest of this article.

Once the installation is complete, let us make sure all the services are running. If so, the following commands.

- systemctl is-active apache2
- systemctl is-active mariadb

should return active for both. Otherwise, start both services manually:

- systemctl start {apache2,mariadb}

Finally, before proceeding let’s use mysql_secure_installation to set up the password for the database root account. If you don’t know how to do this, you can refer to step -4 in How to Install MariaDB 10 on Debian and Ubuntu.

Testing LAMP on Debian 9 Stretch

To begin, we are going to create and populate a sample database. Next, we will use a basic PHP script to retrieve a set of records from the database in JSON format.

Finally, we will use Firefox’s developer tools to verify the version of Apache being used. Although we could find out this same information with.

- apache2 -v

the reason why we’re using a script is to make sure all the components of the stack are working properly when bundled together.

Creating and Populating a Database

Let’s enter the MariaDB prompt with the following command.

- mysql -u root -p

and entering the password that was chosen in the previous section.

Now we will create a database named LibraryDB as follows:

MariaDB [(none)]> CREATE DATABASE LibraryDB;

and add two tables named AuthorsTBL and BooksTBL:

MariaDB [(none)]> USE LibraryDB;
CREATE TABLE AuthorsTBL (
AuthorID INT NOT NULL AUTO_INCREMENT,
FullName VARCHAR(100) NOT NULL,
PRIMARY KEY(AuthorID)
);

MariaDB [(none)]> CREATE TABLE BooksTBL (
BookID INT NOT NULL AUTO_INCREMENT,
AuthorID INT NOT NULL,
ISBN VARCHAR(100) NOT NULL,
Title VARCHAR(100) NOT NULL,
Year VARCHAR(4),
PRIMARY KEY(BookID),
FOREIGN KEY(AuthorID) REFERENCES AuthorsTBL(AuthorID)
);

For security reasons, we will create a special account to access our database:

MariaDB [(none)]> CREATE USER 'librarian'@'localhost' IDENTIFIED BY 'Today123';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON LibraryDB.* TO 'librarian'@'localhost';

The last step now consists of populating the tables with Authors and Books:

MariaDB [(none)]> INSERT INTO AuthorsTBL (FullName) VALUES ('Paulo Coelho'), ('Isabel Allende'), ('Jorge Luis Borges');
MariaDB [(none)]> INSERT INTO BooksTBL (AuthorID, ISBN, Title, Year) VALUES
(1, '9788576653721', 'El alquimista', '1988'),
(1, '9780061194740', 'El peregrino', '1987'),
(2, '9789500720380', 'La casa de los espiritus', '1982'),
(3, '9789875666481', 'El Aleph', '1945');

Testing Database Connection with PHP Script

The following PHP script will first connect to the database and retrieve the records that match the query. If an error occurs, a descriptive message will be displayed so as to give us a hint on what’s wrong.

Save the following script as booksandauthors.php under /var/www/html:

?php
	// Show PHP version
	echo "Current PHP version: " . phpversion() .  "rn";

	// Connect to database
	$connection = mysqli_connect("localhost","librarian","Today123","LibraryDB") or die("Error " . mysqli_error($connection));

	// SQL query
	$sql = "SELECT A.ISBN, A.Title,
        	A.Year, B.FullName
        	FROM BooksTBL A JOIN AuthorsTBL B
        	ON A.AuthorID = B.AuthorID;";
	$result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));

	// Populate an array with the query results
	$libraryarray = array();
	while($row = mysqli_fetch_assoc($result))
	{
    	$libraryarray[] = $row;
	}

	// Convert to JSON
	echo json_encode($libraryarray);
?>

Change the ownership to www-data and add the appropriate permissions:

- chown www-data:www-data /var/www/html/booksandauthors.php
- chmod 600 /var/www/html/booksandauthors.php

Finally, open a web browser and point it to the URL where the script resides.

http://192.168.0.35/booksandauthors.php
Verify LAMP on Debian 9Verify LAMP on Debian 9

Verify LAMP on Debian 9

That’s it! In this article we have explained how to install and test the LAMP stack on Debian 9. If you have questions or comments about this article, don’t hesitate to let us know by using the form below.

How to Install LEMP (Linux, Nginx, MariaDB, PHP-FPM) on Debian 9 Stretch

How to Install LEMP (Linux, Nginx, MariaDB, PHP-FPM) on Debian 9 Stretch &-8211; this Article or News was published on this date:2019-05-28 16:51:03 kindly share it with friends if you find it helpful

Since Debian powers a large percentage of web servers all over the world, in this article we will explain how to install the LEMP (Linux + Nginx + MariaDB + PHP-FPM) stack on Debian 9 Stretch as an alternative to LAMP (use this guide to install LAMP on Debian 9).

Additionally, we will show how to perform a minimal Nginx / PHP-FPM configuration so that even new system administrators can set up brand new web servers to set up dynamic pages.

To do this, we will leverage the recent updates to the distribution’s official repositories. It is assumed that you have installed Debian 9 on a bare metal, virtual machine, or VPS, or have upgraded from Jessie.

Installing LEMP in Debian 9 Stretch

You may be wondering why we mentioned PHP-FPM instead of PHP as part of the LEMP stack. As opposed to other web servers, Nginx does not provide native support for PHP.

For that reason, PHP-FPM (Fast Process Manager) is used to handle requests for PHP pages. You can learn more about PHP-FPM in the PHP official site.

The default version provided in the Debian repositories php7.0-fpm. As you can probably guess based on the package’s name, this version can even handle requests to pages including PHP 7 code.

NOTE: If Apache has been installed in the same box previously, make sure it is stopped and disabled before proceeding.

With that said, let’s install the components of the LEMP stack as follows:

- aptitude update 
- aptitude install nginx mariadb-server mariadb-client php-mysqli php7.0-fpm

When the installation is complete, let’s first make sure that Nginx and PHP-FPM are running and enabled to start on boot:

- systemctl status nginx php7.0-fpm

If indicates that one or both services are not running, then do.

- systemctl start nginx php7.0-fpm
- systemctl enable nginx php7.0-fpm

As it is the case with every MariaDB or MySQL installation, it is important to run mysql_secure_installation to perform a minimal security configuration and set the password for the database root account.

- mysql_secure_installation

If you don’t know how to do this, you can refer to step -4 in How to Install MariaDB 10 on Debian and Ubuntu.

Configuring Nginx to Use PHP-FPM on Debian 9

The Nginx main configuration file is /etc/nginx/sites-available/default, where we will need to make the following changes inside the server block:

  • Ensure the location block that handles PHP requests is enabled, with the exception of the one where the fastcgi_pass directive points to the loopback NIC.
  • Add index.php after the index directive to indicate that if found, it should be served by default before index.html or other files.
  • Add the server_name directive pointing to the IP address or hostname of your server. This will be 192.168.0.35 in our case.
  • Additionally, make sure the root directive points to the location where your .php files will be stored (/var/www/html by default).

When you’re done, you can use the following command to test the configuration file for errors.

- nginx -t 

At this point, your /etc/nginx/sites-available/default should look as follows where the numbers refer to the configuration represent the above list:

- grep -Ev '-' /etc/nginx/sites-available/default
Configure Nginx for PHP-FPMConfigure Nginx for PHP-FPM

Configure Nginx for PHP-FPM

Testing Nginx and PHP-FPM on Debian 9

To make sure we are now using Nginx as our web server, let’s create a file named info.php inside /var/www/html with the following contents:

?php
	phpinfo();
?>

Then go to http://192.168.0.35/info.php and check the top of the page where you should see this:

Configure Nginx for PHP-FPMTest Nginx and PHP-FPM

Test Nginx and PHP-FPM

Finally, let’s point our browser to the booksandauthors.php file that we created in Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9.

As you can see in the following image, this file is now being served by Nginx:

Configure Nginx for PHP-FPMCheck PHP Pages Served by Nginx

Check PHP Pages Served by Nginx

NOTE: If you notice that Nginx serves the .php files as downloads instead of executing them, clear your browser cache or try a different browser. Particularly, if you are using Chrome you may want to test with incognito mode.

Summary

In this article we have explained how to install and configure Nginx to serve dynamic .php pages. It is important to note that after this initial setup there are settings that should be taken into account to secure the web server.

You may find a basic summary in The Ultimate Guide to Secure, Harden and Improve Performance of Nginx Web Server.

If you are looking for virtual hosting on Nginx, read How to Setup Name-based and IP-based Virtual Hosts on NGINX.

As always, don’t hesitate to let us know if you have questions or comments about this article.

Installation of Debian 9 (Stretch) Minimal Server

Installation of Debian 9 (Stretch) Minimal Server &-8211; this Article or News was published on this date:2019-05-28 16:50:44 kindly share it with friends if you find it helpful

In this guide, we will walk you through the installation of a Debian 9 (Stretch) Minimal Server, using the netinstall CD ISO image. This installation you will carry out is appropriate for building a future customizable server platform, without a GUI (Graphical User Interface).

You can use it to install only the necessary software packages that you need to work with, which we will show you in future guides. However, before you move further, read the system requirements, download the netinstall CD ISO image and then proceed to the Debian 9 installation instructions.

Requirements

Minimum Systems Requirements:

  • RAM 64 MB
  • Hard disk space: 1GB

Important: These are only values for a test scenario, in a production environment, you probably want to use suitable RAM and Hard disk size to meet your local environment needs.

Download Debian 9

Debian 9 server system network installation minimal CD image:

  1. For 32-bit: Debian-9.0.0-i386-netinst.iso (i386)
  2. For 64-bit: Debian-9.0.0-amd64-netinst.iso (x86_64 / amd64)

Installation of Debian 9 Minimal Server

1. After downloading the Debian 9 minimal CD image from the above links, burn it to a CD or create a bootable USB stick using LiveUSB Creator called Unetbootin or Etcher.

2. Once you have created the installer bootable media, place your CD/USB into your system appropriate drive.

Then start the computer, select your bootable device and the first Debian 9 installer boot menu should appear as shown below. Choose Graphical Install and press [Enter] key.

Debian 9 Boot MenuDebian 9 Boot Menu

Debian 9 Boot Menu

3. The system will start loading media installer and a page to select the installation language should appear as shown below. Select your installation process language and click on Continue.

Debian 9 Boot MenuSelect Debian 9 Installation Language

Select Debian 9 Installation Language

Configuring Location, Locales and Keyboard Settings

4. Now select your location used for setting the system timezone and locales, if not on the list go to Other and click on Continue. Find the region and then country. Once your done click Continue as shown below.

Debian 9 Boot MenuSelect Country Location

Select Country Location

Debian 9 Boot MenuSelect Location Region

Select Location Region

Debian 9 Boot MenuSelect Location Country

Select Location Country

5. Now configure the system locales (language and country combination) and click Continue.

Debian 9 Boot MenuConfigure Country Locales

Configure Country Locales

6. Next, choose your Keyboard Layout to use and click Continue.

Debian 9 Boot MenuSelect Keyboard Layout

Select Keyboard Layout

7. The installer will now load components from the CD shown below.

Debian 9 Boot MenuDebian 9 Installer Components

Debian 9 Installer Components

Configure Hostname and Domain Name

8. The next step is to set your system hostname and domain name and click Continue.

Debian 9 Boot MenuSet Debian 9 Hostname

Set Debian 9 Hostname

Debian 9 Boot MenuSet Debian 9 Domain Name

Set Debian 9 Domain Name

Configure Users and Password

9. Here, you will configure system users and their passwords. Start by setting the root user password as shown below and click Continue when your done.

Debian 9 Boot MenuSet Debian 9 Root Password

Set Debian 9 Root Password

10. Then create a user account for the system administrator. First set the user’s full name as shown below and click Continue when you are done.

Debian 9 Boot MenuSetup Debian 9 User Account

Setup Debian 9 User Account

11. In this step, set the user’s system name and click Continue.

Debian 9 Boot MenuSet Debian 9 Username

Set Debian 9 Username

12. Now set the above user’s password and click Continue.

Debian 9 Boot MenuSet Debian 9 User Password

Set Debian 9 User Password

Debian 9 Partitioning Disk

13. On the next screen, choose Manuel to perform disk partitioning.

Note: You can select Guided – use entire disk and setup LVM (Logical Volume Manager) as partition layout for efficient disk space management and follow the instructions.

Debian 9 Boot MenuSelect Manuel Disk Partitioning

Select Manuel Disk Partitioning

14. You will see an overview of your current system disks and mount points. Select the disk to be partitioned and click Continue.

After that, select Yes to create a new empty partition table on the disk.

Debian 9 Boot MenuOverview of Disks

Overview of Disks

Debian 9 Boot MenuCreate New Disk Partition

Create New Disk Partition

15. Next, select the free space on the disk to partition it and click Continue.

Debian 9 Boot MenuSelect Free Space to Partition

Select Free Space to Partition

Creating Swap Partition

16. Now create the Swap area by selecting Create a new partition and set appropriate size as shown in the screen shots below. Then click Continue.

Debian 9 Boot MenuCreate Swap Partition

Create Swap Partition

Debian 9 Boot MenuSet Swap Size

Set Swap Size

17. Set swap partition as Primary and choose the Beginning of the free space on disk and click Continue.

Debian 9 Boot MenuSet Swap Partition to Primary

Set Swap Partition to Primary

Debian 9 Boot MenuSet Swap Partition to Beginning

Set Swap Partition to Beginning

18. Now set partition as Swap area as shown in the following two screen shots.

Debian 9 Boot MenuSwap Partition Details

Swap Partition Details

Debian 9 Boot MenuSelect Swap Area

Select Swap Area

19. Now select Done setting up partition and click Continue.

Debian 9 Boot MenuDone Setting Swap Partition

Done Setting Swap Partition

Create Root Partition

20. In this step, you can now create the root partition by selecting the free space, then select Create new partition. Afterwards set the root partition size, make it Primary and set it at the Beginning of the free space.

Then use Ext4 file system on it and finally select Done setting up partition and click Continue as shown in the following screen shots.

Debian 9 Boot MenuSelect Free Space to Create Root Partition

Select Free Space to Create Root Partition

Debian 9 Boot MenuCreate Root Partition

Create Root Partition

Debian 9 Boot MenuSet Root Partition Size

Set Root Partition Size

Debian 9 Boot MenuSet Root Partition to Primary

Set Root Partition to Primary

Debian 9 Boot MenuSet Root Partition as Beginning

Set Root Partition as Beginning

Debian 9 Boot MenuSet Root Filesystem to Ext4

Set Root Filesystem to Ext4

Create Home Partition

21. Similarly to create a /home partition follow the same instruction as explained above using the remaining free space.

22. Once you have created all the necessary partitions, click on Finish partitioning and write changes to disk.

Debian 9 Boot MenuClick on Write Changes to Disk

Click on Write Changes to Disk

Finalizing Debian 9 Installation Process

23. At this point, installation of the base system should begin as shown below.

Debian 9 Boot MenuInstalling Debian 9 Base System

Installing Debian 9 Base System

24. Now configure the package manager as shown in the screen shot below. Select No and click Continue.

Debian 9 Boot MenuConfigure Debian 9 Package Manager

Configure Debian 9 Package Manager

25. Afterwards, press Esc to continue without a network mirror by selecting Yes. Then click Continue.

Debian 9 Boot MenuConfigure Without Network Mirror

Configure Without Network Mirror

26. Next, choose whether to participate in the package usage survey or not. Then click Continue.

Debian 9 Boot MenuConfigure Popularity Contest

Configure Popularity Contest

27. Now install standard system utilities and click Continue.

Debian 9 Boot MenuDebian 9 Software Selection

Debian 9 Software Selection

28. In this step, you will install the Grub boot loader by choosing Yes. After which you should choose the disk to install it.

Debian 9 Boot MenuInstall Debian 9 Grub Boot Loader

Install Debian 9 Grub Boot Loader

Debian 9 Boot MenuInstall Grub Boot Loader on Disk

Install Grub Boot Loader on Disk

29. Finally, the installation is done, click Continue to reboot the machine and remove the bootable media, then boot in your system and login.

Debian 9 Boot MenuDebian 9 Installation Completed

Debian 9 Installation Completed

Debian 9 Boot MenuDebian 9 Login

Debian 9 Login

That’s all. You now have a working a Debian 9 (Stretch) Minimal Server for developing a future customizable server platform. If you are looking to deploy a web server such as Apache or Nginx, go through these following articles.

  1. Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9
  2. How to Install LEMP (Linux, Nginx, MariaDB, PHP-FPM) on Debian 9
  3. The Ultimate Guide to Secure, Harden and Improve Performance of Nginx Web Server

To send us any queries or thoughts, use the comment section below.