The LEMP stack is a popular combination of software components used to host dynamic websites and web applications. It consists of Linux as the operating system, Nginx as the web server, MySQL as the relational database management system, and PHP as the scripting language. In this tutorial, we’ll guide you through the steps of installing and configuring the LEMP stack on Ubuntu 22.04.

    Step 1: Update and Upgrade

    Before we start installing any packages, let’s update the package index and upgrade existing packages to their latest versions:

    sudo apt update
    sudo apt upgrade

    Step 2: Install Nginx

    Nginx will serve as the web server for your applications. Install it using the following command:

    sudo apt install nginx

    Once the installation is complete, start the Nginx service:

    sudo systemctl start nginx

    To enable Nginx to start on boot, run:

    sudo systemctl enable nginx

    Step 3: Install MySQL

    Next, let’s install MySQL to manage our databases:

    sudo apt install mysql-server

    During the installation process, you will be prompted to set a password for the MySQL root user. Make sure to choose a strong password and remember it.

    To secure your MySQL installation, run the following command and follow the on-screen prompts:

    sudo mysql_secure_installation

    Step 4: Install PHP and Required Extensions

    Install PHP and some common PHP extensions required for most web applications:

    sudo apt install php php-fpm php-mysql

    Step 5: Configure Nginx to Use PHP

    Create a new Nginx server block configuration file for your website. Replace example.com with your domain in the following code:

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

    Add the following configuration, adjusting paths and domain as needed:

    server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/example.com;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    

    Create a symbolic link to enable the configuration:

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

    Check Nginx configuration for syntax errors:

    sudo nginx -t

    If the test is successful, reload Nginx to apply the new configuration:

    sudo systemctl reload nginx

    Step 6: Test PHP

    Create a PHP test file to ensure PHP is functioning correctly:

    echo "<?php phpinfo(); ?>" | sudo tee /var/www/example.com/info.php

    Access the PHP info page in your browser at http://example.com/info.php. If it displays PHP information, PHP is working properly.

    Step 7: Create and Configure MySQL Database

    Log in to MySQL using the root password you set earlier:

    mysql -u root -p

    Create a new database, user, and grant privileges:

    CREATE DATABASE dbname;
    CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON dbname.* TO 'username'@'localhost';
    FLUSH PRIVILEGES;

    Exit the MySQL prompt:

    exit

    Step 8: Deploy Your Application

    Upload your website or application files to the /var/www/example.com/ directory.

    Step 9: Configure DNS

    Ensure your domain’s DNS records point to your server’s IP address.

    Step 10: Test Your Website

    Open your browser and navigate to http://example.com. You should see your website or application live.

    Conclusion

    By following these steps, you’ve successfully installed the LEMP stack on Ubuntu 22.04, creating a solid foundation for hosting dynamic web applications. Remember to regularly update and maintain your server for security and performance.

    Share.

    Leave A Reply