Setting Up LEMP server on Ubuntu 22.04 LTS

Introduction

LEMP is an acronym that stands for LinuxNginx (pronounced “engine-x”), MySQL, and PHP. It is a popular open-source technology stack that is similar to LAMP, but instead of using the Apache web server, it uses Nginx (pronounced “engine-x”).

Prerequisites


You must following things to complete this tutorial.

  • Ubuntu 22.04 LTS up and running.
  • Root permission.
  • Basic knowledge of linux commands.
  • Internet connectivity.


In this post, We will show you how to install LEMP server on ubuntu 22.04 LTS.

Step 1: Update System

We need to update the ubuntu’s repository and their packages by executing given command.

sudo apt-get update
sudo apt-get upgrade

Step 2: Installing Nginx Web Server

To install nginx web server we just need to execute the given command that command install and setup the nginx web server and service for us.

sudo apt-get install nginx -y

Step 3: Verify Nginx Service

After installation of nginx web server we need to make sure nginx’s service should up and running, Use the given command for the same.

sudo systemctl status nginx.service

Step 4: Installing MySQL Database Server

To install MySQL on ubuntu machine, We need to use the given command by default repository available in ubuntu 22.04 LTS version.

sudo apt-get install mysql-server -y

Step 5: Secure MySQL

After installation of MySQL server on ubuntu machine its having some test and user data, To secure MySQL we need to execute the secure script, Use the given command for the same.

Run the MySQL security script

sudo mysql_secure_installation

Follow the on-screen instructions to set a root password, remove anonymous users, disable remote root login, and remove test databases.

Step 6: Installing PHP & Modules

PHP package is required to run the PHP based project and PHP module, Execute the given command for the same.

sudo apt-get install php-fpm php-mysql php-curl php-gd php-json php-zip php-xml -y

Step 7: Configure Nginx to use PHP-FPM

We need to configure PHP with nginx web server in order to run the PHP project perfectly, Make the required changes for the same.

To open the nginx’s default virtualhost config.

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

Find the following lines:

# pass PHP scripts to FastCGI server
#
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# # fastcgi_pass 127.0.0.1:9000;
# }

Uncomment the lines and make sure the path to the PHP-FPM socket is correct:

# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # With php-fpm (or other unix sockets):
    fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    # With php-cgi (or other tcp sockets):
    # fastcgi_pass 127.0.0.1:9000;
}

Save and close the file.

Step 8: Test the Nginx Configuration

To make sure our recently changes are not having any conflict and syntax error, Use the given command for the same.

sudo nginx -t

Step 9: Restart Nginx and PHP-FPM

Now we need to restart the nginx and php8.0-ftm service, Use the given command for the same.

sudo systemctl restart nginx
sudo systemctl restart php8.0-fpm

Step 10: Verify PHP Installation

Now we need to verify the nginx is working with PHP or not to make sure, We need to use the given steps.

Create a PHP info file in the web root directory:

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

Add the following lines to the file:

<?php
phpinfo();
?>

Save and close the file.

Access the PHP info page using your web browser:

http://<server-ip-address>/info.php

You should see the PHP version and configuration details.

Step 11: Remove PHP Info Page

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

Conclusion

We have successfully installed and setup the OpenCart service using the LAMP stack on Ubuntu. If you have any questions or issues, please leave a comment below.

Setting Up LEMP server on Ubuntu 22.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top