Home » How to Set Up a LEMP Server on Ubuntu: A Step-by-Step Guide

How to Set Up a LEMP Server on Ubuntu: A Step-by-Step Guide

by Shubham Verma
Published: Last Updated on 0 comment 1.2K views 6 minutes read

Introduction:

Setting up a LEMP (Linux, Nginx, MySQL, PHP) server on Ubuntu is a fundamental task for hosting web applications and websites. In this comprehensive guide, we’ll walk you through each step, ensuring that your server is secure, optimized, and ready to handle your web traffic. So, let’s get started!

Step 1: Log in via SSH

Before we begin, make sure you have SSH access to your Ubuntu server. This allows you to manage your server remotely and execute commands from your terminal.

Step 2: Update the Server’s Package Index

The first step is to update the package index on your Ubuntu server. This ensures that you have the latest information about available packages and their versions.

sudo apt update

Step 3: Install Nginx

Nginx is a powerful and efficient web server that will serve as the backbone of our LEMP stack.

sudo apt install nginx

Step 4: Allow Nginx Through the Firewall

To enable Nginx to accept incoming connections, we need to allow it through the firewall.

sudo ufw app list
sudo ufw allow 'Nginx Full'

Now, if you enter your server’s IP address in a web browser, you should see the default Nginx landing page, confirming that Nginx is successfully installed.

Step 5: Install MySQL

MySQL is a popular and robust database management system that will store and manage your application’s data.

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 and secure password.

sudo mysql

Step 6: Installing PHP

PHP is a server-side scripting language used for dynamic web content and is essential for running PHP-based applications.

sudo apt install php-fpm php-mysql

If you need a particular PHP version, like php8.2-fpm, you can mention it in the package name when you’re installing it.

Step 7: Configure Nginx for PHP

To make Nginx work seamlessly with PHP, we need to make some configurations. Open the Nginx configuration file:

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

Within the file, locate the index directive and make sure to include index.php in the list. After that, insert the following lines inside the location ~ .php$ block. You can also remove the ‘#’ symbol in front of ‘fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;‘ and make sure to update the PHP version accordingly.

include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Save and close the file by pressing Ctrl+X, Y, and Enter.

Now restart your server using this command:

systemctl restart nginx

Step 8: Test PHP Installation

To verify that PHP is working correctly, create a test PHP file in your web server’s root directory. For example:

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

Add the following PHP code to the file:

<?php
	phpinfo();
?>

Save and close the file.

Step 9: Access the PHP Info Page

Now, open your web browser and navigate to http://your_server_ip. You should see a comprehensive page detailing PHP information, confirming that PHP is correctly installed and operational.

Conclusion:

Congratulations! You’ve successfully set up a powerful LEMP server on your Ubuntu system. From this point, you can begin deploying and hosting your web applications with ease. Remember to follow security best practices and keep your server updated to ensure optimal performance and safety. Happy hosting!

You may also like

Leave a Comment

ShubhamVermma.com: Your one-stop destination for live technical tutorials. Master PHP, Laravel, WordPress, Node.js, and more with our expert instructors.

Subscribe

Subscribe my Newsletter for new Articles. Let's stay updated!

A Technology Media Company – All Right Reserved.