How install LEMP on Ubuntu?

Setting up a LEMP stack (Linux, Nginx, MySQL, PHP) on an Ubuntu server is a great way to run your websites or web applications efficiently. In this guide, we’ll walk you through the installation process, step-by-step.

Update System Packages

Before installing any new software, it’s always a good practice to update your system to ensure you have the latest security patches and software updates.

sudo apt update
sudo apt upgrade

Install Nginx

Nginx is a powerful and efficient web server. To install it, run the following command:

sudo apt install nginx

Once the installation is complete, you can start and enable Nginx to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Install MySQL

Next, we need to install MySQL, which will serve as our database management system.

sudo apt install mysql-server

After installation, it’s important to run the security script to configure your MySQL installation:

sudo mysql_secure_installation

Follow the prompts to set a root password and secure your installation.

Install PHP

Now, let’s install PHP along with the necessary extensions for working with Nginx and MySQL.

sudo apt install php-fpm php-mysql

To check if PHP is installed correctly, use the following command:

php -v

Configure Nginx to Use PHP

Once PHP is installed, we need to configure Nginx to process PHP files. Open the default Nginx configuration file:

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

Look for the following section and update it to look like this:

server {
    listen 80;
    server_name your_domain_or_IP;

    root /var/www/html;
    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/php7.x-fpm.sock;  # Make sure to replace 7.x with your PHP version
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save the file and exit. Then, test the Nginx configuration for syntax errors:

sudo nginx -t

If there are no errors, reload Nginx:

sudo systemctl reload nginx

Start and Enable Services

Finally, start all the services (Nginx, MySQL, PHP) and enable them to run on system boot:

sudo systemctl start nginx
sudo systemctl start mysql
sudo systemctl start php7.x-fpm  # Replace with your PHP version
sudo systemctl enable nginx
sudo systemctl enable mysql
sudo systemctl enable php7.x-fpm

Test Your LEMP Stack

To test your LEMP stack, create a simple PHP file in the web root directory:

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

Add the following content:

<?php
phpinfo();
?>

Now, open your web browser and navigate to your server’s IP address or domain name followed by /info.php (e.g., http://your_domain_or_IP/info.php). If everything is set up correctly, you should see the PHP information page. That’s it! You’ve successfully set up a LEMP stack on your Ubuntu server. Enjoy hosting your websites or web applications!

Share:

More Posts

How to Install Maven on macOS 2025

What you will read?1 Check for Java Installation2 Install Homebrew (If Not Installed)3 Install Maven via Homebrew4 Verify Environment Variables (Optional)5 Create a Sample Maven

How to Use Google SMTP Server

What you will read?1 Step 1: Enable SMTP Access in Gmail2 Step 2: Secure Connection with TLS3 Step 3: Send an Email via SMTP4 Step

how to install wine on RHEL

What you will read?1 Check RHEL version and CPU architecture2 Update system and install base tools3 Verify subscription and enable CodeReady Builder4 Enable EPEL repository5

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments