|

How to Install Nginx, PHP, and MySQL on Ubuntu Server

Building a Real Web Server with Ubuntu Server

If you want to run a serious website especially WordPress sooner or later you’ll hear about something called the LEMP stack.

LEMP stands for:

  • Linux
  • Nginx
  • MySQL
  • PHP

This stack powers millions of websites around the world, from small personal blogs to large production platforms.

In this guide, we’ll walk through how to install Nginx, PHP, and MySQL on Ubuntu Server, step by step, in a relaxed and beginner-friendly way. No unnecessary theory, no copy-paste confusion just a clean, working setup you can actually use.

By the end of this article, you’ll have:

  • A running Nginx web server
  • PHP properly configured
  • MySQL database server installed
  • A system ready for WordPress or any PHP-based website

Why Ubuntu Server?

Ubuntu Server is one of the most popular Linux distributions for web hosting because it is:

  • Stable and reliable
  • Well-documented
  • Beginner-friendly
  • Widely supported by hosting providers
  • Frequently updated

If you’re learning Linux servers or building your own VPS, Ubuntu Server is an excellent choice.


What Is the LEMP Stack?

Before jumping in, let’s quickly clarify what each component does.

Linux

The operating system. In this case, Ubuntu Server.

Nginx

A fast and efficient web server that handles incoming web requests.

MySQL

A database server that stores website data (posts, users, settings).

PHP

The programming language used by WordPress and many web applications.

Together, they form the foundation of a modern Linux web server.


Prerequisites

Before starting, make sure you have:

  • Ubuntu Server installed (20.04, 22.04, or newer)
  • Root access or a sudo user
  • Internet connection
  • Basic familiarity with the terminal

All commands below are run in the terminal.


Step 1: Update Your Ubuntu Server

Always start by updating your system.

sudo apt update && sudo apt upgrade -y

Why this matters:

  • Fixes security issues
  • Prevents package conflicts
  • Ensures compatibility

Never skip this step.


Step 2: Install Nginx Web Server

Now let’s install Nginx, the web server.

sudo apt install nginx -y

Once installation finishes, start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Test Nginx Installation

Open your browser and visit your server’s IP address:

http://your-server-ip

If you see the “Welcome to nginx!” page, congratulations Nginx is working 🎉


Step 3: Adjust Firewall (If Enabled)

If you’re using UFW firewall:

sudo ufw allow 'Nginx Full'
sudo ufw reload

This allows:

  • HTTP (port 80)
  • HTTPS (port 443)

Step 4: Install MySQL Database Server

Now let’s install MySQL, which will store website data.

sudo apt install mysql-server -y

After installation, secure MySQL:

sudo mysql_secure_installation

You’ll be asked:

  • Set root password
  • Remove anonymous users
  • Disable remote root login
  • Remove test database

Answer YES to all recommended options.


Verify MySQL Status

sudo systemctl status mysql

If it’s running, you’re good to go.


Step 5: Install PHP and Required Extensions

Nginx does not process PHP by itself, so we need PHP-FPM.

Install PHP and common extensions:

sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y

These extensions are commonly required by WordPress and modern PHP apps.


Check PHP Version

php -v

You should see PHP version information.


Step 6: Configure Nginx to Use PHP

Now we connect Nginx and PHP.

Open the default server block:

sudo nano /etc/nginx/sites-available/default

Find the location ~ \.php$ block and make sure it looks like this (adjust PHP version if needed):

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}

Save and exit.


Test Nginx Configuration

sudo nginx -t

If everything is OK, reload Nginx:

sudo systemctl reload nginx

Step 7: Test PHP with Nginx

Create a test PHP file:

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

Add this content:

<?php
phpinfo();

Save and exit.

Now visit:

http://your-server-ip/info.php

If you see the PHP info page, PHP is working correctly with Nginx 🎉

⚠️ Important:
Delete this file afterward for security:

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

Step 8: Create a MySQL Database for WordPress

Login to MySQL:

sudo mysql

Create database and user:

CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace strongpassword with a secure password.


Step 9: Prepare Web Root for WordPress

Create a directory for your website:

sudo mkdir -p /var/www/wordpress
sudo chown -R www-data:www-data /var/www/wordpress
sudo chmod -R 755 /var/www/wordpress

Step 10: Create Nginx Server Block for WordPress

Create a new config file:

sudo nano /etc/nginx/sites-available/wordpress

Add:

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

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

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

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

Enable the site:

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 11: Download and Install WordPress

Move to web directory:

cd /var/www/wordpress
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo chown -R www-data:www-data wordpress

Now open your browser and follow the WordPress installation wizard.


Step 12: Basic Security Tips (Important)

Before going live, do these:

  • Keep Ubuntu updated
  • Use strong passwords
  • Disable root SSH login
  • Install HTTPS (Let’s Encrypt or Cloudflare)
  • Remove unused PHP files

Security is part of server ownership.


Common Beginner Mistakes

  • Forgetting to reload Nginx
  • Using wrong PHP socket path
  • Not securing MySQL
  • Leaving phpinfo files exposed
  • Skipping updates

Take your time servers reward patience.


Is This Setup Production-Ready?

For:

  • Personal blogs ✅
  • Small business sites ✅
  • Learning Linux servers ✅

For high-traffic enterprise sites:

  • Needs caching
  • Needs monitoring
  • Needs optimization

But this is a rock-solid foundation.


Why This Setup Is Great for Learning

You learn:

  • Linux basics
  • Web server concepts
  • PHP processing
  • Database management
  • Real-world hosting skills

These skills translate directly to VPS and cloud environments.


A Real Linux Web Server You Control

Installing Nginx, PHP, and MySQL on Ubuntu Server is a rite of passage for anyone serious about Linux and web hosting.

You now have:

  • A fast web server
  • A working PHP environment
  • A secure database backend
  • A system ready for WordPress

From here, you can:

  • Add HTTPS
  • Optimize performance
  • Deploy more sites
  • Explore Docker and automation

Your Linux web server journey has officially begun

Similar Posts

Leave a Reply

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