Updating APT Package Repository Cache:
First, update the APT package repository cache with the following command:
The APT package repository cache should be updated.
Installing and Configuring MySQL/MariaDB:
Now, install MariaDB server and client packages from the official package repository of Debian 10 with the following command:
To confirm the installation, press Y and then press <Enter>.
The APT package manager will download and install all the required packages.
At this point, MariaDB server and client packages will be installed.
Now, check whether mariadb service is running with the following command:
As you can see, the mariadb service is running. It’s also enabled to automatically start on system boot.
If in any case, mariadb service is not running, then start the service with the following command:
Now, run the following command to set a root password for MariaDB:
Press <Enter>.
Now, press Y and then press <Enter>.
Now, type in your root password and press <Enter>.
Type in your root password again and press <Enter>.
Press Y and then press <Enter> to remove anonymous users.
If you don’t want to allow root login remotely, press Y. Otherwise, press N. Then, press <Enter>.
Now, press Y and press <Enter> to remove test database.
Now, press Y and then press <Enter> to reload the privilege table.
MariaDB should be configured.
Creating New MySQL/MariaDB Users and Databases:
Now, you have to create a new user and a database for your web application.
Login to MariDB shell with the following command:
Now, type in the MariaDB root password you’ve already set and press <Enter>.
You should be logged in.
Now, create a database app1 with the following SQL statement:
Now, create a new user (let’s say, shovon), set a password for the user (let’s say 123) and grant the user permission to use the database app1 with the following SQL statement:
Now, flush the MariaDB privileges for the changes to take effect as follows:
Now, exit out of the MariaDB shell as follows:
Installing Apache Web Server and PHP:
Now, install Apache 2 web server and PHP with the following command:
Now, press Y and then press <Enter> to confirm the installation.
Apache 2 web server and PHP should be installed.
Now, check whether apache2 service is running with the following command:
As you can see, apache2 service is running. It’s also enabled to start automatically on system boot.
If in any case, apache2 service is not running, then you can start it manually as follows:
Installing PHP Extensions:
Debian 10 official package repository has a lot of PHP extensions pre-packaged.
You can list all the available PHP extensions/libraries with the following command:
The package name of all the PHP extensions including their version number and short description should be listed. It’s a very long list. So, it may take a while to find what you’re looking for this way.
To install the most common PHP extensions/libraries, run the following command:
php-json php-xml
Now, press Y and then press <Enter> to confirm the installation.
The common PHP extensions should be installed.
Once the PHP extensions are installed, restart the Apache web server as follows:
Enabling/Disabling Apache Modules:
Apache web server comes with a lot of modules. You can enable or disable them as you need.
To list all the available Apache 2 modules, run the following command
As you can see, all the available Apache 2 modules are listed.
To enable a module (let’s say, rewrite), run the following command:
Don’t forget to restart the Apache 2 web server if you enable/disable Apache 2 modules.
To restart the Apache 2 web server, run the following command:
To list all the enabled/active Apache 2 modules, run the following command:
All the enabled/active Apache 2 modules should be listed.
If you decide to disable an enabled module, use the a2dismod command as follows:
Changing Apache Run User:
The default Apache run user on Debian 10 is www-data and the default web root directory is /var/www/html. So, as an ordinary user, you won’t be able to create files/directories, or modify existing files/directories in the web root directory. As you’re setting up a development LAMP server, this is not what you want. To solve this problem, you should change the Apache run user to your login user and change the owner and group of the webroot /var/www/html to your login user.
To change the Apache run user, edit /etc/apache2/envvars configuration file with the following command:
You have to modify the APACHE_RUN_USER and APACHE_RUN_GROUP environment variables.
Now, set APACHE_RUN_USER and APACHE_RUN_GROUP environment variables to your login user’s username (the output of the whoami command). Once you’re done, save the file by pressing <Ctrl> + X followed by Y and <Enter>.
Now, change the owner and group of the /var/www/html directory to the username of your login user with the following command:
Now, restart the Apache 2 web server with the following command:
Testing LAMP Server:
I have created 2 PHP scripts index.php and phpinfo.php in the webroot /var/www/html.
Contents of index.php:
$host = "localhost";
$user = "shovon";
$pass = "123";
$db = "app1";
try {
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Contents of phpinfo.php:
<?php
phpinfo();
?>
Now, you should be able to access the PHP scripts from your browser as you can see in the screenshot below.
So, that’s how you setup a Debian 10 LAMP server for PHP web development. Thanks for reading this article.