Install LEMP Stack (Nginx + PHP + MySQL) on Ubuntu
Set up a production-ready LEMP stack — the foundation for WordPress, Laravel, and all PHP apps.
Table of Contents
What is the LEMP Stack?
Linux + EngineX (Nginx) + MySQL + PHP. It powers millions of websites including WordPress, WooCommerce, Laravel, and Drupal.
Step 1: Install Nginx
apt update
apt install nginx -y
systemctl enable nginx
systemctl start nginx
Open your server IP in a browser — you should see the Nginx welcome page. If not, check: ufw allow 80/tcp
Step 2: Install MySQL 8.0
apt install mysql-server -y
mysql_secure_installation
The secure installation wizard will ask you to: set a root password, remove anonymous users, disable remote root login, remove the test database, and reload privileges. Answer Yes to all.
Step 3: Install PHP 8.3
apt install software-properties-common -y
add-apt-repository ppa:ondrej/php -y
apt update
apt install php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip php8.3-bcmath php8.3-intl -y
Step 4: Configure Nginx to Use PHP
nano /etc/nginx/sites-available/yourdomain.com
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com;
index index.php index.html;
# Try files, then PHP
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP-FPM configuration
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
# Block .htaccess access
location ~ /\.ht {
deny all;
}
}
# Enable the site and test config
ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
Step 5: Create a Database and User
mysql -u root -p
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'StrongP@ssw0rd!';
GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 6: Test PHP is Working
echo "" > /var/www/yourdomain.com/info.php
Visit http://yourdomain.com/info.php — you should see the PHP info page. Delete it after testing: rm /var/www/yourdomain.com/info.php
Step 7: Add Free SSL
apt install certbot python3-certbot-nginx -y
certbot --nginx -d yourdomain.com -d www.yourdomain.com