Linux VPSServer ManagementUbuntu

Step By Step Guide To: How Installing The LEMP Stack On Ubuntu

The LEMP stack, consisting of Linux, Nginx, MySQL (or MariaDB), and PHP, is a powerful combination of open-source software that forms the foundation of countless web applications and websites. If you’re looking to set up a robust web server environment on your Ubuntu system, this guide will walk you through the step-by-step process of installing the LEMP stack.

Prerequisites

Before we dive into the installation process, make sure you have the following prerequisites:

  • An Ubuntu-based server or desktop system (we’ll use Ubuntu 20.04 LTS for this guide).
  • A user account with sudo privileges.
  • Let’s get started.

Step 1: Update and Upgrade Your System

Begin by ensuring your system is up-to-date. Open a terminal and run the following commands:

sudo apt update

The sudo apt update command is used to refresh the package information on a Debian-based Linux system. It ensures that your system’s package manager (APT, which stands for Advanced Package Tool) has the latest information about available packages and their versions from the configured software repositories.

Then Upgrade:

sudo apt upgrade

The sudo apt upgrade command is used to upgrade the installed packages on a Debian-based Linux system, such as Ubuntu. This command will search for updates to your installed packages and, if any updates are available, it will prompt you to confirm the upgrade. It is a good practice to regularly run this command to keep your system up to date with the latest software updates and security patches.

Step 2: Install Nginx

Nginx is a powerful and efficient web server that will serve as the first layer in your LEMP stack. To install Nginx, use the following command:

sudo apt install nginx

Once the installation is complete, start Nginx and enable it to start on boot:

sudo systemctl start nginx

To enable the Nginx web server service to start automatically on system boot run this command:

sudo systemctl enable nginx

You can check Nginx’s status to ensure it’s running:

sudo systemctl status nginx

You should see an “active (running)” status if Nginx is correctly installed and running.

Step 3: Install MySQL (or MariaDB)

The “M” in LEMP can refer to either MySQL or MariaDB. Both are powerful relational database management systems. To install MySQL, run:

sudo apt install mysql-server

During the installation, you will be prompted to set a root password for MySQL. Follow the instructions and choose a strong, secure password. After installation, you can run the following command to secure your MySQL installation:

sudo mysql_secure_installation

Follow the on-screen prompts to configure security options.

If you prefer MariaDB over MySQL, you can substitute “mysql-server” with “mariadb-server” in the installation command.

Step 4: Install PHP

PHP is the scripting language that will enable you to develop dynamic web applications. To install PHP, run:

sudo apt install php-fpm php-mysql

This command installs PHP and its MySQL extension, which is commonly used with databases.

Step 5: Configure Nginx to Use PHP

You’ll need to configure Nginx to process PHP files. Create a configuration file for your site. For example:

sudo nano /etc/nginx/sites-available/example.com

Inside the configuration file, add the following lines:

server {
     listen 80;
      server_name example.com www.example.com;
      root /var/www/example.com;
      index index.php;

      location / {
            try_files $uri $uri/ =404;
      }

      location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
      }
}

Make sure to replace “example.com” with your own domain or server’s IP address. Save the file and create a symbolic link to enable the configuration:

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

Now, test your Nginx configuration for any syntax errors:

sudo nginx -t

If everything is in order, restart Nginx:

sudo systemctl restart nginx

Step 6: Create a PHP Info File (Optional)

To verify that PHP is working correctly, you can create a PHP info file. Create a file called “info.php” in your web root directory:

sudo nano /var/www/example.com/info.php

Add the following content to the file:

<?php
     phpinfo();
?>

Save the file and access it in your web browser at http://example.com/info.php. You should see a page displaying PHP information.

Step 7: Testing Your LEMP Stack

With the LEMP stack fully installed and configured, you can now deploy your web applications or websites. Don’t forget to secure your server and keep your software up-to-date to maintain a safe and efficient web server environment.

Conclusion

Congratulations! You’ve successfully installed the LEMP stack on your Ubuntu system. This robust combination of open-source software will serve as the foundation for your web applications and websites. With Nginx, MySQL (or MariaDB), and PHP working together, you’re ready to start developing, hosting, and serving your web content efficiently.

IQBAL HASAN

I love to talk on trending ⚡ technology ⚡, I have huge attraction on 😁 Linux 😅 and I am a bit 😄 mad 😄 when I code.

Related Articles

Leave a Reply

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

Back to top button