How to Install MySQL, phpMyAdmin, and Nginx on Ubuntu

MySQL, phpMyAdmin, and Nginx

Setting up a LEMP stack (Linux, Nginx, MySQL, PHP) with phpMyAdmin on Ubuntu allows you to manage databases via a web interface while using the powerful and fast Nginx web server. This guide walks you through installing and configuring each component step-by-step.


๐Ÿ“ฆ Prerequisites

  • Ubuntu 20.04 or later (root or sudo access)
  • A terminal or SSH connection
  • Basic knowledge of Linux command line

๐Ÿ”ง Step 1: Update Your System

sudo apt update && sudo apt upgrade -y

๐Ÿ“” Step 2: Install MySQL

sudo apt install mysql-server -y

Secure MySQL Installation

sudo mysql_secure_installation
  • Youโ€™ll be asked to configure the VALIDATE PASSWORD PLUGIN.
  • Choose a password strength level and set the root password.
  • Answer Y to all prompts (removing anonymous users, disallowing root remote login, etc.).

Verify MySQL is Running

sudo systemctl status mysql

๐ŸŒ Step 3: Install Nginx

sudo apt install nginx -y

Allow Nginx Through Firewall

sudo ufw allow 'Nginx Full'

๐Ÿง  Step 4: Install PHP

sudo apt install php-fpm php-mysql -y

Check PHP version:

php -v

๐Ÿ—… Step 5: Configure Nginx to Use PHP

Create a server block file:

sudo nano /etc/nginx/sites-available/example.com

Example Config:

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust if using a different PHP version
    }

    location ~ /\.ht {
        deny all;
    }
}

Enable the config and restart Nginx:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

๐Ÿงฐ Step 6: Install phpMyAdmin

sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y
  • Select nginx: Press TAB to skip web server config if nginx isn’t listed.
  • Choose Yes to configure the database and set an admin password.

Enable PHP extensions:

sudo phpenmod mbstring
sudo systemctl restart php7.4-fpm

(Replace php7.4-fpm with your PHP version.)


๐ŸŒ Step 7: Configure Nginx for phpMyAdmin

Create a symlink for phpMyAdmin:

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

Now, phpMyAdmin is accessible at:

http://your_server_ip/phpmyadmin

๐Ÿ›ก Optional: Secure phpMyAdmin with a Password

sudo apt install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd your_user

Edit your Nginx block and add inside /phpmyadmin:

location /phpmyadmin {
    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Restart Nginx:

sudo systemctl restart nginx

โœ… Final Checks

  • http://your_server_ip/ should show your Nginx welcome page.
  • http://your_server_ip/phpmyadmin should show the phpMyAdmin login screen.

๐Ÿ“ Summary

ComponentInstalled?Notes
Nginxโœ…Web server
MySQLโœ…Database server
PHP + FPMโœ…Backend language + processor
phpMyAdminโœ…Web interface for MySQL

๐Ÿ’Š Troubleshooting Tips

  • Ensure Nginx and PHP are linked correctly via fastcgi_pass.
  • If phpMyAdmin returns a 403, check folder permissions.
  • Use systemctl status to inspect running services.

Leave a Reply

Your email address will not be published. Required fields are marked *