How to Install LAMP (Linux, Apache, MySQL, PHP) + phpMyAdmin on Ubuntu

How to Install LAMP (Linux, Apache, MySQL, PHP) + phpMyAdmin on Ubuntu

How to Install LAMP (Linux, Apache, MySQL, PHP) + phpMyAdmin on Ubuntu: A Detailed Step-by-Step Guide

Installing a LAMP stack, which stands for Linux, Apache, MySQL, and PHP, is a fundamental requirement for many web developers. A LAMP stack allows you to serve dynamic websites and web applications. Adding phpMyAdmin, a web-based interface for managing MySQL databases, enhances your setup by providing a graphical user interface (GUI) for database management. This article provides a detailed, step-by-step guide on installing LAMP and phpMyAdmin on an Ubuntu system.

Prerequisites

Before beginning the installation, ensure you have the following:

  1. Ubuntu Server or Desktop: This guide assumes you are using Ubuntu 22.04 LTS or a similar version.
  2. Root or Sudo Access: You need administrative privileges to install and configure software on your Ubuntu system.

Step 1: Update and Upgrade the System

Before installing any new software, update the package list to ensure you get the latest versions.

sudo apt update
sudo apt upgrade -y

 

This command updates the list of available packages and their versions, then upgrades any installed packages to their latest versions.

Step 2: Install Apache Web Server

Apache is a robust and widely-used web server. To install it, run:

sudo apt install apache2 -y

 

Once installed, Apache should start automatically. To verify it’s running, use:

sudo systemctl status apache2

 

You should see a green light indicating that Apache is active and running. To test Apache, open a web browser and navigate to http://your_server_ip. You should see the default Apache2 Ubuntu Default Page.

Step 3: Install MySQL Database Server

MySQL is a powerful relational database management system. To install MySQL, execute:

sudo apt install mysql-server -y

 

Once installed, the MySQL server should start automatically. You can verify this by checking its status:

sudo systemctl status mysql

 

Next, secure your MySQL installation by running the security script:

sudo mysql_secure_installation

 

This script will prompt you to configure several security options, including setting a root password and removing anonymous users.

Step 4: Install PHP

PHP is the scripting language that runs server-side code for your web applications. To install PHP, run:

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

 

This command installs PHP and the necessary modules to integrate with Apache and MySQL. To check the installed PHP version, use:

php -v

 

You should see the PHP version displayed, confirming the installation.

Step 5: Test PHP with Apache

To test the PHP installation, create a phpinfo() file:

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

 

Add the following PHP code to the file:

<?php
phpinfo();
?>

 

Save and exit the file by pressing CTRL+X, then Y, and Enter. Now, open a web browser and navigate to http://your_server_ip/info.php. You should see the PHP information page, which confirms PHP is working correctly with Apache.

Step 6: Install phpMyAdmin

phpMyAdmin simplifies MySQL database management through a web interface. To install phpMyAdmin, execute:

sudo apt install phpmyadmin -y

 

During the installation, you will be prompted to select the web server. Choose Apache by pressing Space and then Enter. Next, you will be asked if you want to configure dbconfig-common for phpMyAdmin. Select Yes and enter the MySQL root password when prompted.

phpMyAdmin is now installed, but you need to enable its configuration in Apache. To do this, run:

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin
sudo systemctl reload apache2

 

Step 7: Configure MySQL User for phpMyAdmin

For phpMyAdmin to manage databases, you need to create a new MySQL user with sufficient privileges. Log in to the MySQL shell:

sudo mysql

 

Create a new user and grant all privileges:

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

 

Replace your_password with a strong password. This user can now access all databases through phpMyAdmin.

Step 8: Secure phpMyAdmin

To prevent unauthorized access to phpMyAdmin, you should restrict access and use strong passwords. You can secure phpMyAdmin with an Apache basic authentication prompt:

First, create a password file:

sudo htpasswd -c /etc/phpmyadmin/.htpasswd admin

 

You’ll be prompted to create a password for the admin user. Next, edit the Apache configuration for phpMyAdmin:

sudo nano /etc/apache2/conf-available/phpmyadmin.conf

 

Add the following lines within the <Directory /usr/share/phpmyadmin> directive:

AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user

 

Save and exit the file, then restart Apache to apply the changes:

sudo systemctl restart apache2

 

Now, when you access phpMyAdmin, you’ll be prompted for the additional username and password you just set up.

Step 9: Test and Access phpMyAdmin

To access phpMyAdmin, open a web browser and navigate to http://your_server_ip/phpmyadmin. You should see the phpMyAdmin login page. Log in using the MySQL user you created earlier.

Step 10: Enable and Test LAMP Stack Security

Finally, securing your LAMP stack is crucial. Here are some additional steps:

  1. Disable Directory Listing: Prevent Apache from listing files in directories that do not have an index file.Edit the Apache configuration file:sudo nano /etc/apache2/apache2.conf Find the <Directory /var/www/> section and set Options -Indexes. Save and exit the file, then reload Apache:sudo systemctl reload apache2
  2. Enable UFW Firewall: Ubuntu’s Uncomplicated Firewall (UFW) can block unauthorized access.Enable UFW and allow SSH, HTTP, and HTTPS:sudo ufw allow OpenSSH sudo ufw allow 'Apache Full' sudo ufw enable
  3. Secure MySQL: Ensure that MySQL root access is only allowed from localhost and avoid using root for phpMyAdmin. Always use strong, unique passwords.
  4. Use SSL/TLS: Encrypt traffic to your server with SSL/TLS. You can obtain a free SSL certificate from Let’s Encrypt:sudo apt install certbot python3-certbot-apache -y sudo certbot --apache Follow the prompts to configure SSL.

Conclusion

Installing a LAMP stack and phpMyAdmin on Ubuntu provides a powerful foundation for web development and database management. This step-by-step guide has walked you through installing and configuring Apache, MySQL, PHP, and phpMyAdmin, ensuring a secure and functional environment for your web projects. Regularly update your server and components to maintain security and performance. With this setup, you’re ready to develop and manage dynamic web applications with ease.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: