SSD Grow


Learn to Set Up LAMP on Ubuntu 22.04

A LAMP stack in Linux is a bundle of software that includes Linux as the operating system, Apache as the web server, MySQL (or MariaDB) as the database system, and PHP (or Perl/Python) as the scripting language. It is used for building and deploying dynamic websites and web applications by combining these components. LAMP stands for “Linux, Apache, MySQL, and PHP.” Together, these technologies can be used to create a fully-functional web server. Linux is the most popular, secure and open source operating system used in web servers. Apache HTTP Server is a free and open-source web server that delivers web content through the internet. MySQL is a relational database engine that allows you to store and manage data. PHP is a widely used open source and general-purpose server-side scripting language used mainly in web development to create dynamic websites and applications. In this tutorial, we’ll be using MariaDB, which is a drop-in replacement of the MySQL Database Server. It includes all major open-source storage engines, and allows you to manage relational databases for storing and organizing data.
Before proceeding with installing LAMP on Ubuntu 22.04, make sure you have the following prerequisites:
Root access to your server or a sudo user.

How to Install LAMP on Ubuntu 22.04

To install the LAMP stack on Ubuntu 22.04, you can use the apt package manager to install Apache, MySQL, and PHP. Once these components are installed, you need to ensure that they are properly configured and integrated for web development. Follow the steps below to accomplish this.

Step 1: Update the Package Cache Before installing LAMP

It is important to update the Ubuntu packages in the package manager cache to ensure that you have the latest available versions. To do this, use the following command:

sudo apt update

Step 2: Install the MariaDB Database Server

After updating our Ubuntu package cache, we will now install the MariaDB database server, the M in the LAMP acronym. We’ll use MariaDB instead of MySQL because it includes more features and supports new storage engines, in addition to its high performance.
To install MariaDB, execute the following command:

sudo apt install mariadb-server mariadb-client

Tap the y key then Enter to continue the installation.
In the preceding command, you install two packages:
  • mariadb-server: The MariaDB database server which stores data.
  • mariadb-client: The MariaDB database client which allows you to interact with and manage the database server via the command line.
  • Once the installation is finished, verify that the MariaDB database server is running properly by executing the following command to check out the MariaDB service status:

    sudo service mariadb status

    The output should show that the service is enabled and running:
    Here, you can see that the service is active and running in the line

    "Active: active (running) ..."

    Ensure That MariaDB Starts at Boot
    To make sure that the MariaDB database server starts with the system at boot, use the enable subcommand of the systemctl command. To do so, execute the following command:

    sudo systemctl enable mariadb.service

    You should receive the following output:

    Synchronizing state of mariadb.service with SysV service script with /lib/systemd/systemd-sysv-install.
    Executing: /lib/systemd/systemd-sysv-install enable mariadb

    Securing The MariaDB Database Server
    MariaDB comes with some unsafe default settings which may expose your database server to dangerous security vulnerabilities. It’s recommended that you run a security script that comes with MariaDB to strengthen your database server and minimize the risk of database intrusions or breaches.
    To secure your MariaDB database server, execute the following command, where you will be presented with seven prompts:
    First, run the script:

    sudo mysql_secure_installation

    You’ll be asked for your current root password:

    To log into MariaDB to secure it, we'll need the current
    password for the root user. If you've just installed MariaDB, and
    haven't set the root password yet, you should just press enter here.

    Enter current password for root (enter for none):
    Tap Enter.
    Next, you’ll be asked whether you want to use the unix_socket authentication method:

    OK, successfully used the password, moving on...

    Setting the root password or using the unix_socket ensures that nobody
    can log into the MariaDB root user without the proper authorisation.

    Switch to unix_socket authentication [Y/n]

    Tap Y to enable unix_socket authentication for better security.
    The unix_socket authentication method allows users to connect to their MariaDB account without entering a password. Instead, it verifies the user’s Unix credentials. This makes it a passwordless security mechanism. However, keep in mind that it cannot be used to grant multiple Unix users access to a single MariaDB user account. Therefore, access to your MariaDB database server is restricted to the Unix user who has the corresponding MariaDB account.
    The unix_socket authentication method is particularly strong due to the default sturdiness of Unix user security in preventing remote access. However, unskilled administration of your Unix user system may expose dangerous vulnerabilities. So, keep a wide and open eye on potential Unix user security issues such as weak passwords or accidental password exposure, excessive sudo permissions that allow users to execute commands of a different Unix user, scripts of other users executed by your MariaDB Unix user, running unsafe scripts, or weak remote access security.
    Next, you’ll be asked to change the root password:

    Enabled successfully!
    Reloading privilege tables..
    ... Success!

    Change the root password? [Y/n]

    Tap Y to set a new password for root, and re-enter it for validation.
    Next, you’ll be asked to remove anonymous users:

    Change the 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]

    Tap Y to remove the anonymous users that come with your MariaDB installation.
    Next, you’ll be asked whether you want to disallow remote root logins:

    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]

    Tap Y to disable remote root login.
    Next, you’ll be asked whether you want to remove the test database that comes with your MariaDB installation:

    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]

    Tap Y to remove your MariaDB test database and disable access to it.
    For the changes you’ve made to take effect, you’ll be asked to reload your MariaDB privilege tables:

    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]

    Tap Y to reload your MariaDB privilege tables. This will ensure that your changes 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!

    With this, your MariaDB installation is more secure than before, provided that you follow security best practices.
    You now have the MariaDB database server installed, and you’re ready to store and manage your data. Next, you’ll need to serve the data you store using an HTTP server, which is the Apache HTTP server in the LAMP stack.

    Step 3: Install the Apache HTTP Server

    After installing and securing the MariaDB database server, the next step in our LAMP stack installation is installing the Apache HTTP server, which serves web content.
    Use apt to install Apache using the following command:

    sudo apt install apache2

    You’ll be asked to confirm the installation. Tap the y key then Enter to continue.
    Once the installation is finished, check Apache’s version to confirm that it was properly installed using the following command:

    sudo apachectl -v

    You should receive an output that looks as follows:

    Server version: Apache/2.4.52 (Ubuntu)
    Server built: 2022-06-14T12:30:21

    Then check Apache’s status:

    sudo systemctl status apache2

    The output should show that the Apache service is enabled and running:

    ● apache2.service - The Apache HTTP Server
    Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
    Active: active (running) since Tue 2022-08-02 19:00:13 UTC; 2min 31s ago
    Main PID: 52442 (apache2)
    Tasks: 55 (limit: 19072)
    Memory: 5.2M
    CPU: 44ms
    CGroup: /system.slice/apache2.service
    ├─52442 /usr/sbin/apache2 -k start
    ├─52444 /usr/sbin/apache2 -k start
    └─52445 /usr/sbin/apache2 -k start

    You now have Apache installed. Next, you’ll install the PHP language.

    Step 4: Install PHP

    With MariaDB you can store and manage data, and the Apache HTTP server allows you to serve it. You now need to install PHP to dynamically display data, and allow the final user to interact with your web service via easy-to-use web forms or API calls.
    Use the following command to install the main PHP package, along with basic PHP packages that allow you to interact with your database and HTTP server:

    sudo apt install php php-mysql php-xml php-mbstring libapache2-mod-php

    When prompted, tap the y key then Enter to continue the installation.
    In the preceding command, you install the following packages:
  • php: The main PHP language package.
  • php-mysql: A package that allows PHP to communicate with MySQL-based databases such as MariaDB.
  • php-xml: A package that provides a DOM, SimpleXML, WDDX, XML, and XSL module for PHP.
  • php-mbstring: A package that provides the MBSTRING module for PHP, which is used to manage non-ASCII strings.
  • libapache2-mod-php: A package that allows Apache to handle PHP files.
  • To ensure PHP was successfully installed, check its version using the following command:

    php -v

    You should receive an output similar to the following:

    PHP 8.1.2 (cli) (built: Jul 21 2022 12:10:37) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

    This means that you’ve successfully installed PHP.

    Step 5: Testing with a Custom-Made Web Page

    After we’ve finished installing all the components of the LAMP stack, we will now test our Apache server by creating a small HTML file in an Apache Document Root folder –a folder that contains your website files.
    To create a document root folder to serve our test file, create a new directory called mysite inside the /var/www directory, which is the default document root for Apache. Note that you can use your site’s name instead of mysite:

    sudo mkdir /var/www/mysite

    Next, create a file called index.html inside your mysite directory:

    sudo nano /var/www/mysite/index.html
    Paste the following into the file:

    My website

    Hello World!

    This is the landing page of My Site

    As you can see, this is straightforward HTML code.
    Save and close the file.
    For Apache to know about your mysite document root directory and serve its index.html on your server’s IP address or domain, you will need to modify the default Apache configuration file. To do this, first open it using the following command:

    sudo nano /etc/apache2/sites-enabled/000-default.conf

    Then make sure to set the correct server name and document root path:

    ServerName your_server_domain_or_IP
    DocumentRoot /var/www/mysite

    Run the following command to ensure that your new configuration file does not contain any errors:

    sudo apache2ctl configtest

    You might receive a AH00558: apache2: Could not reliably determine the server’s fully qualified domain name message. You can ignore this message because it’s just an informational note that does not affect the purposes of this tutorial, and Apache will run as expected.
    You should receive an output that ends with the following:

    Syntax OK

    This means you can safely reload Apache. Otherwise, you will get a specific description pointing out the error you have to fix.
    Restart Apache for these modifications to take effect:

    sudo systemctl restart apache2

    After you do so, use your browser and navigate to your server’s domain name or IP address:


    You should see that the index.html file you created earlier is properly served.

    Related Articles