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 withsudo
LAMP is an archetypal model of web service stacks, named as an acronym of the names of its original four open-source components: the Linux operating system, the Apache HTTP Server, the MariaDB database Server, and the PHP programming language.
This tutorial will guide how to install LAMP stack (Linux, Apache Server, MariaDB database and PHP) on Debian buster 10. You can follow the steps below.
Steps To Install LAMP Stack
To install the LAMP stack, you first need to have Debian 10 Buster running on your system with a non-root sudo
-enabled user account and a basic firewall.
Updating Debian 10 Buster
The first step to install the LAMP stack on your system is to update your Debian 10 Buster and it’s repositories to the latest version. You can run the following command
root@codesposts:~$ apt update && apt -y upgrade
Installing Apache Server
You can run the following commands to install the Apache
web server on your Debian 10.
root@codesposts:~$ apt install apache2
root@codesposts:~$ apt install apache2-utils
Verifying The Services
After installing Apache
web server, you can verify it’s process by running the command below:
root@codesposts:~$ systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset:
Active: active (running) since Tue 2019-08-06 11:58:27 UTC; 33s ago
Main PID: 14780 (apache2)
CGroup: /system.slice/apache2.service
├─14780 /usr/sbin/apache2 -k start
├─14782 /usr/sbin/apache2 -k start
└─14783 /usr/sbin/apache2 -k start
Aug 06 11:58:27 codesposts systemd[1]: Starting The Apache HTTP Server...
Aug 06 11:58:27 codesposts apachectl[14769]: AH00558: apache2: Could not reliabl
Aug 06 11:58:27 codesposts systemd[1]: Started The Apache HTTP Server.
lines 1-12/12 (END)
Verifying The Version
You can verify the version of Apache
web server by running the following command:
root@codesposts:~$ apache2 -v
Server version: Apache/2.4.25 (Debian)
Server built: 2019-04-02T19:05:13
Enabling Startup With Boot
If you want to enable the Apache
services to start with the boot, you can run the following command
root@codesposts:~$ systemctl enable apache2
Displaying Apache Full Status
If you want to display the full status of the Apache
services, you can install the elinks on your system.
root@codesposts:~$ apt install elinks
After the installation, run the following command to display the full status
root@codesposts:~$ apache2ctl fullstatus
Apache Server Status for localhost (via ::1)
Server Version: Apache/2.4.25 (Debian)
Server MPM: event
Server Built: 2019-04-02T19:05:13
--------------------------------------------------------------------------
Current Time: Tuesday, 06-Aug-2019 12:06:26 UTC
Restart Time: Tuesday, 06-Aug-2019 11:58:27 UTC
Parent Server Config. Generation: 1
Parent Server MPM Generation: 0
Server uptime: 7 minutes 59 seconds
Server load: 0.00 0.00 0.00
Total accesses: 0 - Total Traffic: 0 kB
CPU Usage: u0 s0 cu0 cs0
0 requests/sec - 0 B/second -
1 requests currently being processed, 49 idle workers
+--------------------------------------------------------------------------------------------+
| | | | Connections | Threads | Async connections |
| Slot | PID | Stopping |-------------------+-------------+--------------------------------|
| | | | total | accepting | busy | idle | writing | keep-alive | closing |
|------+-------+----------+-------+-----------+------+------+---------+------------+---------|
|0 |14782 |no |0 |yes |0 |25 |0 |0 |0 |
|------+-------+----------+-------+-----------+------+------+---------+------------+---------|
|1 |14783 |no |0 |yes |1 |24 |0 |0 |0 |
|------+-------+----------+-------+-----------+------+------+---------+------------+---------|
|Sum |2 |0 |0 | |1 |49 |0 |0 |0 |
+--------------------------------------------------------------------------------------------+
______________________________W___________________..............
................................................................
......................
Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process
Srv PID Acc M CPU SS Req Conn Child Slot Client Protocol VHost Request
GET
1-0 14783 0/0/0 W 0.00 0 0 0.0 0.00 0.00 ::1 http/1.1 ::1:80 /server-status
HTTP/1.1
--------------------------------------------------------------------------
Srv Child Server number - generation
PID OS process ID
Acc Number of accesses this connection / this child / this slot
M Mode of operation
CPU CPU usage, number of seconds
SS Seconds since beginning of most recent request
Req Milliseconds required to process most recent request
Conn Kilobytes transferred this connection
Child Megabytes transferred this child
Slot Total megabytes transferred this slot
--------------------------------------------------------------------------
Apache/2.4.25 (Debian) Server at localhost Port 80
Setting Up And Opening Port
If you are using UFW firewall, run the following command to open the TCP port 80 on your system
root@codespsots:~$ ufw allow http
Now we need to set www-data
(Apache user) as the owner of document root (otherwise known as web root). By default it’s owned by the root user.
root@codesposts:~$ chown www-data:www-data /var/www/html/ -R
Installing MariaDB Database Server
Run the following commands to install the MariaDB
Database Server ( drop-in replacement for MySQL-Server)
root@codesposts:~$ apt install -y mariadb-server
root@codesposts:~$ apt install -y mariadb-client
Verifying Installed Version
To verify the installed version, run the following command
root@codesposts:~$ apt policy mariadb-server
mariadb-server:
Installed: 10.1.38-0+deb9u1
Candidate: 10.1.38-0+deb9u1
Version table:
*** 10.1.38-0+deb9u1 500
500 http://http.us.debian.org/debian stretch/main amd64 Packages
100 /var/lib/dpkg/status
10.1.37-0+deb9u1 500
500 http://security.debian.org/debian-security stretch/updates/main amd64 Packages
Verifying Services
To check the status of the services of the MariaDB
, run the following command
root@codesposts:~$ systemctl status mariadb
● mariadb.service - MariaDB 10.1.38 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-08-06 12:16:38 UTC; 3min 53s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 17063 (mysqld)
Status: "Taking your SQL requests now..."
CGroup: /system.slice/mariadb.service
└─17063 /usr/sbin/mysqld
Aug 06 12:16:37 codesposts systemd[1]: Starting MariaDB 10.1.38 database server...
Aug 06 12:16:38 codesposts mysqld[17063]: 2019-08-06 12:16:38 140387851017600 [Note] /usr/sbin/mysqld (mysqld 10.1.38-MariaDB-0+deb9u1) starting as process 17063 ...
Aug 06 12:16:38 codesposts systemd[1]: Started MariaDB 10.1.38 database server.
Securing Database Server
Run the command below to secure your database server
root@codesposts:~$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
… Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
… Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
Dropping test database…
… Success!
Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
Testing MariaDB Installation
To test the installation of the MariaDB
Database Server, run the following command
root@codesposts:~$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.1.38-MariaDB-0+deb9u1 Debian 9.8
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Enabling Startup With Boot
If you want to enable the MariaDB
services to start with the boot, you can run the following command
root@codesposts:~$ systemctl enable mariadb
Installing PHP
To install the latest version of PHP on your system, you can follow the steps given below
root@codesposts:~$ apt install php libapache2-mod-php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
Enabling Apache PHP Module
To enable the Apache module, run the following command then restart the serrvice
root@codesposts:~$ a2enmod php7.3
root@codespsots:~$ systemctl restart apache2
Verifying The Version
To verify the version of the PHP installed, you can run the following command
root@codesposts:~$ php -v
PHP 7.3.4-2 (cli) (built: Apr 13 2019 19:05:48) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.4-2, Copyright (c) 1999-2018, by Zend Technologies
Testing PHP Scripts
To test the php scripts with Apache server, we need to create a codesposts.php
file in the Web root directory with a command line text editor.
root@codesposts:~$ nano /var/www/html/codesposts.php
Copy and paste the following code in the file
<?php phpinfo(); ?>
To save a file in Nano text editor, press Ctrl+O
, then press Enter to confirm. To exit, press Ctrl+X
.
Now open your browser and enter server-ip-address/codesposts.php
. Replace sever-ip-address
with your actual IP. If you follow this tutorial on your local computer, then type 127.0.0.1/codesposts.php
You should see a page like this:
You have successfully installed LAMP stack on your system.