Setting Up a LEMP Stack: A Step-by-Step Guide for Installing Linux, Nginx, MySQL, and PHP on Ubuntu 22.04

How to Install LEMP Stack on Ubuntu 22.04

Welcome to LEMP Stack Installation on Ubuntu. Follow these steps to install the LEMP stack (Linux, Nginx, MySQL, PHP) on your Ubuntu 22.04 server:

Step 1: Update System Packages

Update and upgrade system packages for the latest versions and security patches.

sudo apt update
sudo apt upgrade

Step 2: Install Nginx

Install Nginx, a high-performance web server that will serve as the foundation for your LEMP stack.

sudo apt install nginx

Step 3: Enable Firewall

Allow incoming HTTP and HTTPS traffic to Nginx through the system firewall.

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'

Open a web browser and visit http://domain_or_ip to verify the installation, where you should see a page with Welcome to nginx!

Step 4: Install MySQL

Install MySQL, a relational database management system. mysql-server is the server component, and mysql-client is the client component for interacting with the server.

sudo apt install mysql-server mysql-client

After the installation, it is recommended to run a security script that comes with MySQL. It will remove insecure default settings and lock down access to your database.

sudo mysql_secure_installation

Once finished, test if you can log in to the MySQL console.

sudo mysql -p -u root

Step 5: Install PHP

Install PHP, a server-side scripting language. php8.2-fpm is the FastCGI Process Manager, and php-mysql is a PHP extension for MySQL support.

sudo apt install php8.2-fpm php-mysql

Step 6: Configure PHP

Open the PHP configuration file, set cgi.fix_pathinfo=0, save, and restart the PHP-FPM service.

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

Ensure the following configuration is set:

cgi.fix_pathinfo=0

Restart the PHP-FPM service.


sudo systemctl restart php8.2-fpm

Once done, you can test if PHP is set up correctly by creating a new PHP file.

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

Add the following code to the file:

<?php
	phpinfo();
?>

Open a web browser and visit http://domain_or_ip/info.php, and you should see a PHP page.

Step 7: Configure Nginx to use PHP-FPM

Create a web directory for yourdomain.com.

sudo mkdir /var/www/yourdomain.com

Create an index.html file.

sudo nano /var/www/yourdomain.com/index.html

With the following content.

<html>
  <head>
    <title>{Your Domain Name}</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    <p>Welcome to <strong>{Your Domain Name}</strong>.</p>
  </body>
</html>

Create a server block configuration file for your website in the /etc/nginx/sites-available/ directory:

sudo nano /etc/nginx/sites-available/yourdomain.com.conf

Add the following configuration to the file:

server {
	listen 80;
	server_name yourdomain.com;

	root /var/www/yourdomain.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.2-fpm.sock;
	}

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

Save and exit the file.

Create a symbolic link to enable the Nginx configuration:

sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/

Check if the Nginx configuration is correct:

sudo nginx -t

If the configuration test is successful, restart Nginx:

sudo service nginx restart

Step 8: Test the LEMP Stack

That’s it! You have completed the LEMP Stack Installation on Ubuntu guide. You have successfully installed the LEMP stack on Ubuntu 22.04.

Thank you for exploring FastDT. Explore our range of services to enhance your business.

Learn more about our services