Setting up a Debian 10 LAMP Server for PHP Web Development

29/12/2020
php
In this article, I am going to show you how to setup a LAMP (Linux, Apache, MySQL/MariaDB, PHP) server for PHP web development. I am going to use the newly released Debian 10 Buster GNU/Linux distribution for the demonstration. So, let’s get started.

Updating APT Package Repository Cache:

First, update the APT package repository cache with the following command:

$ sudo apt update

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:

$ sudo apt install mariadb-server mariadb-client

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:

$ sudo systemctl status mariadb

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:

$ sudo systemctl start mariadb

Now, run the following command to set a root password for MariaDB:

$ sudo mysql_secure_installation

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:

$ sudo mysql -u root -p

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:

$ CREATE DATABASE app1;

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:

GRANT ALL ON app1.* TO ‘shovon’@‘localhost’ IDENTIFIED BY ‘123’;

Now, flush the MariaDB privileges for the changes to take effect as follows:

FLUSH PRIVILEGES;

Now, exit out of the MariaDB shell as follows:

q

Installing Apache Web Server and PHP:

Now, install Apache 2 web server and PHP with the following command:

$ sudo apt install apache2 php

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:

$ sudo systemctl status apache2

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:

$ sudo systemctl start apache2

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:

$ sudo apt search ^php-

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:

$ sudo apt install php-curl php-gd php-mbstring php-mysql php-zip
 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:

$ sudo systemctl restart apache2

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

$ ls /etc/apache2/mods-available

As you can see, all the available Apache 2 modules are listed.

To enable a module (let’s say, rewrite), run the following command:

$ sudo a2enmod rewrite

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:

$ sudo systemctl restart apache2

To list all the enabled/active Apache 2 modules, run the following command:

$ sudo a2query -m

All the enabled/active Apache 2 modules should be listed.

If you decide to disable an enabled module, use the a2dismod command as follows:

$ sudo a2dismod moduleName

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:

$ sudo nano /etc/apache2/envvars

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:

$ sudo chown -Rf $(whoami):$(whoami) /var/www/html

Now, restart the Apache 2 web server with the following command:

$ sudo systemctl restart apache2

Testing LAMP Server:

I have created 2 PHP scripts index.php and phpinfo.php in the webroot /var/www/html.

Contents of index.php:

<?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.

http://localhost

http://localhost/phpinfo.php

So, that’s how you setup a Debian 10 LAMP server for PHP web development. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Setting up a Debian 10 LAMP Server for PHP Web Development

In this article, I am going to show you how to setup a LAMP (Linux, Apache, MySQL/MariaDB, PHP) server for PHP web development....
29/12/2020

How to Install Jetbrains PHPStorm on Ubuntu

PHPStorm by JetBrains is one of the best PHP IDE. It has plenty of amazing features. It also has a good looking and user...
29/12/2020

Install CakePHP on Ubuntu

Many MVC based PHP frameworks are available now to make web developing tasks simpler and easier than before. In an MVC...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024