dotlinux blog

How to Install LAMP Stack with PhpMyAdmin in Ubuntu 18.04

The LAMP stack is one of the most popular and foundational software bundles for web development and hosting. The acronym stands for:

  • Linux: The operating system (in our case, Ubuntu 18.04).
  • Apache: The HTTP web server that serves your web pages.
  • MySQL (or MariaDB): The database management system for storing your application's data.
  • PHP: The server-side scripting language that powers dynamic content.

Adding phpMyAdmin provides a user-friendly, web-based graphical interface to manage your MySQL databases and users, making it much easier than using the command line.

In this comprehensive, step-by-step guide, we will walk through the entire process of installing and configuring a fully functional LAMP stack with phpMyAdmin on an Ubuntu 18.04 server. Whether you're setting up a production server or a local development environment, this tutorial will get you up and running.


2026-05

Table of Contents#

  1. Prerequisites
  2. Step 1: Installing Apache Web Server
  3. Step 2: Installing MySQL Database Server
  4. Step 3: Installing PHP
  5. Step 4: Testing PHP Processing on Apache
  6. Step 5: Installing and Securing phpMyAdmin
  7. Step 6: Testing phpMyAdmin
  8. Troubleshooting Common Issues
  9. Conclusion
  10. References

Prerequisites#

Before you begin, you will need:

  • An Ubuntu 18.04 server or local machine.
  • A user account with sudo privileges.
  • Access to a terminal or command line.
  • An internet connection to download the necessary packages.

It's also a good practice to ensure your system is up to date. Run the following commands:

sudo apt update
sudo apt upgrade

Step 1: Installing Apache Web Server#

Apache is one of the most widely used web servers in the world. It's our first component.

  1. Install Apache using Ubuntu's package manager, apt:

    sudo apt install apache2

    Type Y and press Enter when prompted to confirm the installation.

  2. Once the installation is complete, Apache should start automatically. You can check its status with:

    sudo systemctl status apache2

    You should see an output indicating that the service is active (running).

  3. Adjust the Firewall (if enabled): To allow external access to the default web port (80), you need to adjust the firewall. Ubuntu 18.04 uses UFW. List the available application profiles:

    sudo ufw app list

    You should see "Apache Full," "Apache Secure," and others. To allow both HTTP (port 80) and HTTPS (port 443) traffic, enable the 'Apache Full' profile:

    sudo ufw allow in "Apache Full"
  4. Test Apache: Open your web browser and navigate to your server's IP address or http://localhost if you are on the same machine.

    http://your_server_ip
    

    You should see the default Ubuntu 18.04 Apache web page, confirming that the server is installed correctly.

Step 2: Installing MySQL Database Server#

Now that our web server is running, we need to install the database system to store and manage data.

  1. Install the MySQL server package:

    sudo apt install mysql-server
  2. After installation, it's highly recommended to run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system.

    sudo mysql_secure_installation

    This script will ask you to configure several options:

    • VALIDATE PASSWORD PLUGIN: It's a good idea to set this up to enforce password strength for database users. Choose Y if you want to enable it, then select a password strength level (0=Low, 1=Medium, 2=Strong).
    • Set a password for the root user: Enter a strong, secure password and confirm it.
    • Remove anonymous users? Type Y.
    • Disallow root login remotely? Type Y for better security.
    • Remove test database and access to it? Type Y.
    • Reload privilege tables now? Type Y to apply the changes immediately.

Your MySQL server is now secure and ready.

Step 3: Installing PHP#

PHP is the component that processes code to display dynamic content. We need to install PHP and the PHP module for Apache. We'll also install a package that allows PHP to communicate with MySQL.

  1. Install the necessary packages:

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

    This command installs:

    • php: The core PHP package.
    • libapache2-mod-php: The module that allows Apache to handle PHP files.
    • php-mysql: A PHP module for connecting to MySQL databases.
  2. In most cases, you'll want Apache to prioritize PHP files over HTML files. By default, if a directory contains a file named index.html and index.php, Apache will serve index.html. Let's change this. Open the dir.conf file:

    sudo nano /etc/apache2/mods-enabled/dir.conf

    You will see a line like this:

    <IfModule mod_dir.c>
        DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
    </IfModule>
    

    Move index.php to the first position after DirectoryIndex:

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
    </IfModule>
    

    Save and close the file (press Ctrl+X, then Y, then Enter).

  3. Restart the Apache web server to apply the changes:

    sudo systemctl restart apache2

Step 4: Testing PHP Processing on Apache#

To confirm that PHP is configured correctly, let's create a basic PHP script.

  1. Create a new file named info.php in the web root directory. The default web root for Apache on Ubuntu is /var/www/html/.

    sudo nano /var/www/html/info.php
  2. Add the following line of PHP code into the new file. This code calls the phpinfo() function, which displays information about your PHP configuration.

    <?php phpinfo(); ?>
  3. Save and close the file.

  4. Now, open your web browser and visit the following page, replacing your_server_ip with your actual IP address:

    http://your_server_ip/info.php
    

    You should see a detailed web page with information about your PHP installation.

    Security Note: This file exposes sensitive information about your server. Delete it after testing to prevent unauthorized access.

    sudo rm /var/www/html/info.php

Step 5: Installing and Securing phpMyAdmin#

phpMyAdmin provides a convenient GUI for MySQL. However, because it's a well-known target for attackers, securing it is crucial.

  1. Install the phpMyAdmin package from the default repositories:

    sudo apt install phpmyadmin

    During the installation, you will be prompted with a few configuration screens:

    • Web server to configure automatically: Press Space to select apache2 (an asterisk * should appear next to it), then press Tab to highlight <Ok> and press Enter.
    • Configure database for phpmyadmin with dbconfig-common? Select Yes.
    • MySQL application password for phpmyadmin: You will be asked to set a password for phpMyAdmin's database user. You can choose a new password or let the system generate a random one.
  2. The Most Important Step: Secure phpMyAdmin. The best practice is to protect it with an additional layer of authentication using Apache's built-in .htaccess functionality.

    • A. Edit the Apache configuration file for phpMyAdmin:
      sudo nano /etc/apache2/conf-available/phpmyadmin.conf
      Add the following line within the <Directory /usr/share/phpmyadmin> section:
      AllowOverride All
      
      The section should look similar to this:
      <Directory /usr/share/phpmyadmin>
          Options FollowSymLinks
          DirectoryIndex index.php
          AllowOverride All
          ...
      
    • B. Enable the .htaccess override: We need to enable the mod_rewrite module if it's not already active, and then restart Apache.
      sudo a2enmod rewrite
      sudo systemctl restart apache2
    • C. Create a .htaccess file:
      sudo nano /usr/share/phpmyadmin/.htaccess
      Paste the following content into the file. Replace your_password_here with a very strong password of your choice.
      AuthType Basic
      AuthName "Restricted Files"
      AuthUserFile /etc/phpmyadmin/.htpasswd
      Require valid-user
      
    • D. Create the password file: We'll use the htpasswd utility to create the .htpasswd file and add a user. Replace username with a name you want to use.
      sudo htpasswd -c /etc/phpmyadmin/.htpasswd username
      You will be prompted to enter and confirm a password for this user. The -c flag creates the file. If you need to add another user later, omit the -c flag to avoid overwriting the file.

Step 6: Testing phpMyAdmin#

Your phpMyAdmin installation should now be secured. Let's test it.

  1. Open your web browser and navigate to:

    http://your_server_ip/phpmyadmin
    
  2. You will first be greeted by the HTTP authentication pop-up window. Enter the username and password you created with the htpasswd command.

  3. After passing the first authentication, you will see the standard phpMyAdmin login screen. Now, log in using:

    • Username: root (or another MySQL user you've created)
    • Password: The root MySQL password you set during the mysql_secure_installation step.

You should now have full access to manage your MySQL databases through the phpMyAdmin interface.

Troubleshooting Common Issues#

  • "404 Not Found" when accessing /phpmyadmin: Ensure the Apache configuration is correctly linked. You can try creating a symbolic link:
    sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
    sudo a2enconf phpmyadmin
    sudo systemctl reload apache2
  • MySQL root login issue within phpMyAdmin: By default, MySQL 5.7+ uses the auth_socket plugin for the root user, which can cause login failures in phpMyAdmin. To fix this, log into MySQL as root and change the authentication method:
    sudo mysql
    Then, in the MySQL prompt, run the following (replace 'password' with your strong password):
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
    FLUSH PRIVILEGES;
    EXIT;

Conclusion#

Congratulations! You have successfully installed and secured a complete LAMP stack (Linux, Apache, MySQL, PHP) along with phpMyAdmin on your Ubuntu 18.04 server. You now have a robust platform for deploying web applications, from simple websites to complex content management systems like WordPress, Joomla, or Drupal.

Remember to keep your server and all its software updated regularly for security and performance.


References#

  1. Ubuntu 18.04 Official Documentation
  2. Apache HTTP Server Documentation
  3. MySQL 5.7 Reference Manual
  4. PHP Official Documentation
  5. phpMyAdmin Official Documentation