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.
Apache is one of the most widely used web servers in the world. It's our first component.
Install Apache using Ubuntu's package manager, apt:
sudo apt install apache2
Type Y and press Enter when prompted to confirm the installation.
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).
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"
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.
Now that our web server is running, we need to install the database system to store and manage data.
Install the MySQL server package:
sudo apt install mysql-server
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.
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.
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.
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:
phpMyAdmin provides a convenient GUI for MySQL. However, because it's a well-known target for attackers, securing it is crucial.
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.
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:
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.
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;
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.