Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

Setup Nginx, MySQL, PHP (LEMP stack) on Ubuntu 24.04

Published

Making a web server is not hard. Many people think it’s difficult, but it’s just like following a recipe. The LEMP stack is what most websites use today.

LEMP means four things: Linux, Nginx, MySQL, and PHP. Linux runs everything. Nginx shows web pages to people. MySQL saves data. PHP makes websites work. These four work together like a team.

Why use LEMP? It’s faster than old ways of making websites. Nginx can handle lots of people visiting your site at the same time. It also uses less computer memory.

Ubuntu 24.04 is perfect for this. It’s easy to use and very stable. When we finish, you’ll have a server that can run many websites easily.

Prerequisites for Installing LEMP Stack

Before we start, you need a few things ready.

Get an Ubuntu 24.04 server. You can buy one from companies like DigitalOcean or AWS. You can also use your own computer with VirtualBox for practice.

Your server needs 1GB of memory at least. 2GB is better if you have it. You also need 20GB of free space for all the programs we’ll install.

Make sure you can type admin commands on your server. This means using “sudo” before commands. Most servers come with this already set up.

Write down your server’s IP address. You’ll need it to test things later. Also, make sure your internet connection is good because we’ll download programs.

Installing and Configuring Nginx Web Server

Let’s start with Nginx. This program will show your websites to visitors.

First, get the newest programs:

sudo apt update
sudo apt upgrade -y

Now install Nginx:

sudo apt install nginx -y

Start Nginx and make it start every time your server turns on:

sudo systemctl start nginx
sudo systemctl enable nginx

Open the firewall so people can see your website:

sudo ufw allow 'Nginx Full'
sudo ufw --force enable

Test it by typing your server’s IP address in a web browser. You should see a page that says “Welcome to nginx!”

Basic Nginx Configuration

Nginx keeps its settings in files. These files are in a folder called /etc/nginx/. The main file is nginx.conf. Other files for websites are in sites-available.

Right now, Nginx shows files from /var/www/html/. This is good for starting. Later, you can make more websites by adding new files.

Don’t worry about changing these files now. The basic setup works fine for learning.

Setting Up MySQL Database Server

MySQL stores information for your websites. Things like user names, passwords, and blog posts go here.

Install MySQL:

sudo apt install mysql-server -y

Start MySQL and make it run automatically:

sudo systemctl start mysql
sudo systemctl enable mysql

Run a script that makes MySQL more secure:

sudo mysql_secure_installation

MySQL Security Configuration

This script asks questions. Pick these answers for safety:

  • Make a strong password for the main user. Use letters, numbers, and symbols like !@#$. Don’t use easy passwords like “123456”.
  • Remove users without names by saying “Yes”. These let anyone get into your database without a password.
  • Stop people from logging in from other computers by saying “Yes”. This keeps hackers away.
  • Delete the test database by saying “Yes”. You don’t need it and it’s not safe.

Creating Database and User

Don’t use the main account for websites. Make a new user instead.

Log into MySQL:

sudo mysql -u root -p

Make a database and user:

CREATE DATABASE mywebsite;
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'MyPassword456!';
GRANT ALL PRIVILEGES ON mywebsite.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Change “MyPassword456!” to your own password. Your websites will use this user to save and get data.

Installing PHP and Required Extensions

PHP makes websites do things. Without PHP, websites just show text and pictures that never change.

Install PHP and extra parts that websites need:

sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-json php-common php-bcmath -y

Start PHP and make it run automatically:

sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm

PHP Configuration Optimization

Let’s change some PHP settings to make it work better.

Open the PHP settings file:

sudo nano /etc/php/8.3/fpm/php.ini

Find these lines and change them:

upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 300
date.timezone = <Your TimeZone or UTC>

These changes let you upload bigger files. They also give PHP more memory to work with. Change the timezone to where you live.

Restart PHP to use the new settings:

sudo systemctl restart php8.3-fpm

Integrating Nginx with PHP-FPM

Now we connect Nginx and PHP so they work together.

Open the Nginx settings file:

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

Delete everything in that file and put this instead:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

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

    server_name _;

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

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

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

Check if this is right and restart Nginx:

sudo nginx -t
sudo systemctl restart nginx

Testing PHP Processing

Let’s see if PHP works.

Make a test file:

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

Put this inside:

<?php phpinfo(); ?>

Go to http://your-server-ip/info.php in your browser. You should see lots of information about PHP.

Test if the database works too:

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

Put this code:

<?php
$connection = mysqli_connect('localhost', 'webuser', 'MyPassword456!', 'mywebsite');
if ($connection) {
    echo "Database works great!";
    mysqli_close($connection);
} else {
    echo "Database not working: " . mysqli_connect_error();
}
?>

Go to http://your-server-ip/dbtest.php to test it.

Delete these test files when done:

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

Verifying LEMP Stack Installation

Let’s check if everything works right.

See if all programs are running:

sudo systemctl status nginx
sudo systemctl status mysql
sudo systemctl status php8.3-fpm

All three should say “active (running)”. If not, something went wrong.

Make a simple web page to test:

sudo nano /var/www/html/test.html

Put some basic HTML in it and check if you can see it in your browser.

Make sure everything starts when the server turns on:

sudo systemctl is-enabled nginx mysql php8.3-fpm

All should say “enabled”.

Securing Your LEMP Stack

Keep your server safe from bad people on the internet.

Get certificates for HTTPS (secure websites):

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com

Install a program that blocks attackers:

sudo apt install fail2ban -y
sudo systemctl start fail2ban
sudo systemctl enable fail2ban

Fix file permissions so everything works right:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Turn off dangerous PHP functions that hackers like to use:

sudo nano /etc/php/8.3/fpm/php.ini

Find this line and add these function names:

disable_functions = exec,passthru,shell_exec,system,proc_open,popen

Keep your server updated with new security fixes:

sudo apt update && sudo apt upgrade -y

Common LEMP Stack Problems and Solutions

Here are fixes for things that might go wrong:

  • If Nginx won’t start, check your settings with sudo nginx -t. Look for typing mistakes in the config file.
  • If PHP files download instead of running, your Nginx settings are wrong. Make sure the PHP part is in the config file.
  • If the database won’t connect, check your username and password. Also make sure the user has permission to use the database.
  • If you get permission errors, the web server can’t read your files. Make sure www-data owns your web files.
  • If you get “port busy” errors, something else is using port 80. Use sudo netstat -tulpn | grep :80 to see what it is.

Conclusion

Good work! You now have a working web server on Ubuntu 24.04. Your server can run websites, handle PHP programs, and save data in databases.

What can you do next? Install WordPress to make a blog. Try learning PHP to build custom websites. Set up multiple websites on the same server.

Keep your server updated and make backups of important data. Check on it regularly to make sure everything runs smoothly.

This setup can handle small personal websites or bigger business sites. Start small and learn as you go. Don’t be scared to try new things – that’s how you learn best.

Account services are unavailable until the production backend is configured.