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/phpmyadminshould show the phpMyAdmin login screen.
๐ Summary
| Component | Installed? | 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 statusto inspect running services.