Each letter in the acronym refers to a separate software package: That being said, let’s see how we can install and setup LEMP Stack on Ubuntu.

Pre-Requisities

Assuming that you have a running Ubuntu 22.04 Linux system with sudo (or root) privileged access. Access your system and open a terminal. It will be good to update the package manager cache and upgrade currently installed packages. To do this execute: Let’s begin the LEMP (Linux, Nginx, MySQL, and PHP) stack installation on Ubuntu 22.04 Jammy Jellyfish Linux system.

Step 1 – Installing NGINX

First, we will install the Latest Nginx web server on our system. Use the following commands to add PPA for installing the latest Nginx version on your Ubuntu 22.04 Linux. Use the following commands to install Nginx web server. This will install the Nginx web server and start the service. Now, you need to allow webserver ports in the firewall. To allow ports 80 and 443 in the UFW firewall, execute the following commands. Open a web browser on your system and type the server’s IP in the address bar. You will get the default Nginx server page

Step 2 – Installing PHP

First, you need to decide on the PHP version to install on your system. You can also install multiple PHP versions on a single system. Currently the repository contains PHP 5.6, PHP 7.1, 7.2, 7.3, 7.4 and PHP 8.0, 8.1. The below instruction will install PHP 8.1. Please change the version as per your requirements. The ondrej/php ppa contains all PHP version’s for Ubuntu systems. So add this repository in your system with command below: Now update the apt cache and install PHP 8.1. This will install PHP on your Ubuntu system along with some useful PHP extensions.

Step 3 — Install and Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features. Nginx web server required php-fpm for processing the PHP scripts. To install PHP-FPM, run the following command based on the installed PHP version: Once the installation finished, check the service status: In Step 6, we will configure the Nginx virtual host with PHP-FPM to serve PHP applications.

Step 4 – Installing MySQL

The default Ubuntu repositories contain MySQL 8.0. Which can be directly installed using the package manager. To install the available MySQL server version, execute the following command. Once the installation is finished, you can secure the MySQL server by executing the following command. This will ask for a few questions to secure the MySQL server. You have secured the MySQL server in the LAMP stack on Ubuntu 22.04 Linux system. Remember that the above password set for the root accounts is used for remote users only. To log in from the same system, just type mysql on terminal. Type ‘quit’ to exit from the MySQL shell and return to the system terminal.

Step 5 – Installing Additional Packages

You may also need to install modules like MySQL and other extensions for PHP based on the application requirements. Use the following command to find our available PHP extensions. The above command will list all available PHP7 modules for installation, Let’s begin the installation of modules. You may also need to install other required PHP extensions on your system.

Step 6 — Configure Nginx VirtualHost

Finally, do the configuration of the Nginx virtual host. For this example, we are editing the default configuration file. and make changes as below. In the above configuration file, look for location ~ .php$ section. Which is required to service PHP scripts via Nginx server. You have to do the same changes in all VirtualHosts configured.

Step 7 – Verify Setup

You have successfully completed the installation of Nginx, MySQL, and PHP on the Ubuntu 22.04 Linux system. To verify the PHP integration with Nginx, create a PHP script (example: info.php) on the website document root and write the below content.

Now access this file in the web browser. It will so all the details about versions and installation.

Conclusion

This tutorial helped you to set up the LEMP (Linux, Nginx, MySQL, and PHP) stack on Ubuntu 22.04 LTS system. Now, you can host PHP-based web applications on your server.

How To Install LEMP Stack on Ubuntu 22 04 - 18How To Install LEMP Stack on Ubuntu 22 04 - 90