Set up LEMP (Linux, Nginx, MySQL, PHP) in Ubuntu Linux

LEMP Stack Overview

The LEMP stack is a popular open-source software stack for hosting dynamic web applications. It is an acronym that outlines the primary components that make up this powerful combination:

  • Linux: The operating system that serves as the foundation for the stack.

  • Nginx: Pronounced "Engine-X," it acts as the web server, efficiently handling requests and responses.

  • MySQL: The relational database management system used for storing and managing the application's data.

  • PHP: A widely-used scripting language that enables dynamic content by processing code on the server side.

These components provide a robust platform for developing and deploying web applications.

Prerequisite:

**It works on all Linux distributions.

Ubuntu 22.04

  • A server setup is required.

Update && Upgrade the server

$sudo apt-get update -y && apt-get upgrade -y

  • Install Nginx web server:

$sudo apt-get install nginx -y

Once the Nginx is installed, start the Nginx service and enable it to start at system reboot:

$sudo systemctl start nginx

$sudo systemctl enable nginx

To verify the current status of the service, follow these steps:

$sudo systemctl status nginx

You can also verify the installed version of Nginx with the following command:

$nginx -v

To verify that Nginx is running on its default port (80), execute the command below:

$ss -antpl | grep nginx

  • Adjusting The Firewall

To configure ufw to allow Nginx connections, it's important to find a balance between security and traffic requirements. If your server isn't set up with SSL, you'll need to allow HTTP traffic. To do this, enable connections on port

$sudo ufw allow 'Nginx HTTP'

$sudo ufw status

To verify your Nginx installation, navigate to http://your-server-ip your web browser. If the installation is successful, you will be greeted by the Nginx test page.

  • Install MySQL

Install MySQL by typing the following command:

$sudo apt-get install mysql-server -y

To secure the installation, MySQL comes with a script that will ask whether you want to modify some insecure defaults

$mysql_secure_installation

Answer Y for yes, or anything else to continue without enabling.

To change the root password, type Y. To keep the current password, type N

If you agree, please follow these steps:

If you’ve enabled validation, the script will also ask you to select a level of password validation.

Next, you’ll be asked to submit and confirm a root password:

  • Logging Into MySQL

To access your MySQL database, use the following command:

$sudo mysql

To follow a command as a user, please execute the following steps:

$mysql -u your_user -p

Replace your_username with your actual MySQL username. Upon entering this command, you will be prompted to input your password. After providing the correct password, you'll gain access to the MySQL shell, ready for you to execute SQL commands.

  • Install PHP and Configure Nginx to use the PHP

Nginx is now installed to serve your pages and MySQL is installed to store and manage your data. However, you still don’t have anything that can generate dynamic content. This is where PHP comes into play.

$sudo add-apt-repository universe

$sudo apt-get install php-fpm php-mysql

This is done on the server block level (server blocks are similar to Apache’s virtual hosts). To do this, create a new server block configuration file using your preferred text editor within the /etc/nginx/sites-available/ directory. In this example, we will be using nano/vi and the new server block configuration file will say, so you can replace it with your information:

sudo vi /etc/nginx/sites-available/your_domain_name

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.ngnix-debian.html;
        server_name <SERVER_IP>;
        
        location/ {
                try_files snippets/fastcgi-php.conf;
        }
        
        location ~ \.php$ {
                include snippents/fastcgi-oho.conf;
                fastcgi_pass unix:/var/run/php/php<PHP_VERSION_INSTALLED>-fpm.sock;
        }
        
        location ~ /\.ht {
                deny all;
        }
}

<SERVER_IP> : Server's public IP of the server

<PHP_VERSION_INSTALLED> : Please mention the PHP version as installed on the server , this can be checked by using the command $ php --version as used in the example below.

After adding the content, save and exit the file. If you're using nano/vi, save by pressing CTRL + X, then press Y followed by ENTER. Next, activate your new server block by creating a symbolic link from your server block configuration file in /etc/nginx/sites-available/ to the /etc/nginx/sites-enabled/ directory:

$sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf

Test your new configuration file for syntax errors:

$sudo nginx -t

If any errors are reported, go back and recheck your file before continuing.

When you are ready, reload Nginx to make the necessary changes:

$ sudo systemctl reload nginx

This concludes the installation and configuration of your LEMP stack. However, it’s prudent to confirm that all of the components can communicate with one another.

  • Creating PHP file to test configuration

To do this, use your preferred text editor to create a test PHP file called info.php in your document root:

$sudo vi /var/www/html/info.php

<? php
phpinfo();
?>

When you are finished, save and close the file.

To view this page, navigate to your server's domain name or public IP address in your web browser, and append /info.php to the URL.

http://your_Server or IP_address/info.php

Your browser will load a web page like the following that has been generated by PHP with information about your server:

Last updated