How To Install Linux, Nginx, MySQL, and PHP (LEMP stack) on Ubuntu 22.04?

Introduction:

The LEMP stack, comprising Linux, Nginx, MySQL, and PHP, forms the backbone of many modern web applications. This comprehensive guide will walk through the step-by-step installation and configuring each component on Ubuntu 22.04 LTS.

Prerequisites:

Before diving into the installation, ensure you have:

  • A fresh Ubuntu 22.04 server instance.
  • SSH access to your server with sudo privileges.

Step 1: Update System Packages

First, update your system packages to the latest versions:

sudo apt update
sudo apt upgrade -y

Step 2: Install Nginx

  • Install Nginx using the following command:
sudo apt install nginx
  • Start Nginx and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
  • Verify Nginx installation by accessing your server’s IP address in a web browser.

If you receive this page, it means you have successfully installed Nginx on your server.

Step 3: Install MySQL

MySQL is a popular relational database management system.

Install MySQL Server and secure the installation:

sudo apt install mysql-server
sudo mysql_secure_installation

Follow the prompts to set a root password, remove anonymous users, disallow remote root login, and remove test databases.

When you’re finished, test if you are able to log in to the MySQL console:

sudo mysql

To exit the MySQL console, write the following:

exit

Step 4: Install PHP and Required Extensions

  • Install PHP and necessary extensions for WordPress and other PHP-based applications:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
  • Verify PHP installation and version:
php -v

Step 5: Configure Nginx for PHP

  • Create a new Nginx server block configuration for your website:
sudo nano /etc/nginx/sites-available/linuxchamp.com
mkdir -p /var/www/linuxchamp.com/
  • Sample Nginx configuration for PHP:
server {
    listen 80;
    server_name linuxchamp.com www.linuxchamp.com;
    root /var/www/linuxchamp.com;

    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/php8.1-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

}
  • Enable the Nginx configuration and test for syntax errors:

Unlink the default configuration file from the /sites-enabled/ Directory:

sudo unlink /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/linuxchamp.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 6: Test Your LEMP Stack

echo "<?php phpinfo(); ?>" | sudo tee /var/www/linuxchamp.com/info.php

Access http://your_server_ip/info.php in a web browser to view PHP information.

Conclusion

Congratulations! You’ve installed and configured the LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu 22.04. You’re now ready to deploy and host dynamic web applications with ease.

More from this stream

Recomended