How to Install LAMP Stack on Ubuntu 20.04 Server (Apache, MariaDB)

How to Install LAMP Stack on Ubuntu 20.04 Server (Apache, MariaDB, PHP). In this tutorial we shall introduce what a LAMP Stack is and then navigate to installations steps. Let’s start!

What is LAMP Stack

LAMP is a set of open source software components that provides a complete environment to deploy web application. LAMP uses a Linux as the operating system, Apache as the webserver, MySQL/MariaDB as a database server and PHP/Perl/Python as the scripting language. Each component has its own capability to deploy a dynamic and high performance web applications. It is customizable and allows users to replace every component with another open-source solution as per application’s requirement.

LAMP stack server is chosen by many developers due to its ease of deployment, customization and large community support. Most widely used Content Management CMS like, Joomla, Drupal and WordPress run on the LAMP architecture. Compared to other commercial products, LAMP simplifies the software development process and provides flexibility.

LAMP Stack Alternatives

A): Some of the Open source alternatives 

 

 

B): Non-open source alternatives

  • WAMP (Windows, Apache, MySQL/MariaDB, PHP/Perl/Python).

Follow this post below to learn how to install LAMP Stack on Ubuntu 20.04 server.

Install LAMP Stack on Ubuntu 20.04 Server (Apache, MariaDB, PHP)

Install Apache Web Server

Apache web server is one of the most popular and used web servers around the world. More than half of the websites on the Internet used the Apache webserver to serve the web content. By default, the Apache webserver is included in the Ubuntu default repository. You can install it using the following command:

				
					apt-get install apache2 -y
				
			

After installing the Apache webserver, start and enable the Apache service using the following command:

				
					systemctl start apache2
systemctl enable apache2

				
			

To check the Apache service status, run the following command:

				
					systemctl status apache2
				
			

You will get the Apache running status in the following output:

				
					● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-04-01 12:01:25 UTC; 8s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1347 (apache2)
      Tasks: 55 (limit: 2348)
     Memory: 5.3M
     CGroup: /system.slice/apache2.service
             ├─1347 /usr/sbin/apache2 -k start
             ├─1349 /usr/sbin/apache2 -k start
             └─1350 /usr/sbin/apache2 -k start

Apr 01 12:01:24 ubuntu2004 systemd[1]: Starting The Apache HTTP Server...

				
			

To verify the Apache version, use the following command:

				
					apache2 -v
				
			

You will get the Apache version in the following output:

				
					Server version: Apache/2.4.41 (Ubuntu)
Server built: 2022-03-16T16:52:53

				
			

Access Ubuntu

You can now open your web browser and access the Apache test page using the URL http://your-server-ip. You should see the Apache test page on the following screen:

Install MariaDB Database Server

MariaDB is an open source relational database management system used to store application data. By default, the MariaDB server package is included in the Ubuntu default repository. You can install it with the following command:

				
					apt-get install mariadb-server -y
				
			

Once the MariaDB is installed, start the MariaDB service and enable it to start after the system reboot.

				
					systemctl start mariadb
systemctl enable mariadb
				
			

To check the MariaDB status, run the following command:

				
					systemctl status mariadb
				
			

You will get the MariaDB status in the following output:

				
					
● mariadb.service - MariaDB Database Server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-04-01 12:02:28 UTC; 15s ago
   Main PID: 2365 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 2348)
     Memory: 357.5M
     CGroup: /system.slice/mariadb.service
             └─2365 /usr/sbin/mysqld

Apr 01 12:02:27 ubuntu2004 systemd[1]: Starting MariaDB Database Server...
Apr 01 12:02:28 ubuntu2004 systemd[1]: Started MariaDB Database Server.

				
			

Secure MariaDB

Next, you will need to secure the MariaDB installation and set the root password. You can do it with the following command:

				
					mysql_secure_installation
				
			

You will be asked to set the root password as shown below:

				
					Press y|Y for Yes, any other key for No:
Please set the password for root here.

New password:

Re-enter new password:

				
			

Set your root password and press the Enter key. You will be asked to remove the anonymous users:

				
					Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
				
			

Type Y and press the Enter key. You will be asked to disallow remote root login:

				
					Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y

				
			

Type Y and press the Enter key. You will be asked to remove the test database:

				
					Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
				
			

Type Y and press the Enter key. You will be asked to reload the privilege table;

				
					Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
				
			

Type Y and press the Enter key to finish applying all changes.

Install PHP Language

PHP is an open source programming language that combines all the elements of the LAMP stack. It works with Apache to create dynamic web pages. It is also used to retrieve the data from the database. In a simple terms, it allows web applications to run efficiently.

You can install PHP along with other extensions using the following command:

				
					apt-get install php libapache2-mod-php php-mysql -y
				
			

Once PHP is installed with other extensions, you can verify the PHP version using the following command:

				
					php -v
				
			

You will get the following output:

				
					PHP 7.4.3 (cli) (built: Mar  2 2022 15:36:52) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
				
			

Next, create a simple PHP page to test PHP processing on the Apache webserver.

				
					nano /var/www/html/info.php
				
			

Add the following code:

				
					<?php
phpinfo();
				
			

PHP Test Page

Save and close the file then open your web browser and access the PHP test page using the URL http://your-server-ip/info.php. You should see the PHP test page on the following screen:

PHP MySQL Connect to database

In this section, we will test whether the PHP can connect to the MariaDB database and run the database queries. First, connect to the MariaDB shell with the following command:

				
					mysql -u root -p
				
			

Once you are connected, create a database and user with the following command:

				
					CREATE DATABASE linuxdb;
CREATE USER 'linuxuser'@'%' BY 'password';
				
			

Next, grant all the privileges to the linuxdb database:

				
					GRANT ALL ON linuxdb.* TO 'linuxuser'@'%';
				
			

Next, flush the privileges with the following command:

				
					FLUSH PRIVILEGES;
				
			

Create a table with the following command:

				
					CREATE TABLE linuxdb.os_list (
	item_id INT AUTO_INCREMENT,
	content VARCHAR(255),
	PRIMARY KEY(item_id)
);
				
			

Insert some data into table with the following command:

				
					INSERT INTO linuxdb.os_list (content) VALUES ("Ubuntu 20.04");
INSERT INTO linuxdb.os_list (content) VALUES ("CentOS 8");
INSERT INTO linuxdb.os_list (content) VALUES ("Debian 11");
INSERT INTO linuxdb.os_list (content) VALUES ("Rocky Linux 8");

				
			

Please verify all inserted data with the following command:

				
					SELECT * FROM linuxdb.os_list;
				
			

You will get the following output:

				
					+---------+---------------+
| item_id | content |
+---------+---------------+
| 1 | Ubuntu 20.04 |
| 2 | CentOS 8 |
| 3 | Debian 11 |
| 4 | Rocky Linux 8 |
+---------+---------------+
				
			

Next, exit from the MariaDB with the following command:

				
					EXIT;
				
			

Then create a PHP page that connects to the MariaDB database and retrieves the table content:

				
					nano /var/www/html/list.php
				
			

Add the following code:

				
					<?php
$user = "linuxuser";
$password = "password";
$database = "linuxdb";
$table = "os_list";

try {
  $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
  echo "<h2>Types of Operating System</h2><ol>"; 
  foreach($db->query("SELECT content FROM $table") as $row) {
    echo "<li>" . $row['content'] . "</li>";
  }
  echo "</ol>";
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

				
			

Save and close the file when you are done.

Now, open your web browser and access the PHP page using the URL http://your-server-ip/list.php. If everything is fine, you will get the content you’ve inserted in your table:

How to Install LAMP Stack on Ubuntu 20.04 Server (Apache, MariaDB, PHP) Conclusion

In this post, we explained how to install LAMP Stack on Ubuntu 20.04 server. We also explained how to connect to the MariaDB database using PHP and execute queries. I hope you can now easily deploy your PHP application using the LAMP stack.

 

You can consider other stacks like XAMPP, LEMP or MEAN. 

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x