How to Install LEMP (Linux, Nginx, MySQL, PHP) on Ubuntu for High-Performance Websites?

Are you looking to set up a robust web server that can handle high-traffic websites with lightning-fast performance? Look no further! In this tutorial, we’ll guide you through the process of installing LEMP (Linux, Nginx, MySQL, PHP) stack on Ubuntu. LEMP is a powerful combination of open-source software that ensures exceptional performance, stability, and security for your website. Let’s dive into the step-by-step installation process.

Step 1: Preparing Your Ubuntu Server

Before installing the LEMP stack, make sure your Ubuntu server is up-to-date by running the following commands:

sudo apt update
sudo apt upgrade

Step 2: Installing Nginx

Nginx is a high-performance web server that will serve as the backbone of your LEMP stack. Execute the following commands to install Nginx:

sudo apt install nginx

After the installation is complete, start the Nginx service:

sudo systemctl start nginx

–or–

sudo service nginx start

Allow nginx in firewall using these commands:

sudo ufw allow 'Nginx HTTP'
sudo ufw enable

To verify that Nginx is running, open your web browser and enter your server’s IP address (e.g. http://127.0.0.1/). If everything is set up correctly, you should see the default Nginx welcome page.

Step 3: Installing MySQL

MySQL is a widely used relational database management system. Let’s install MySQL using the following command:

sudo apt install mysql-server

During the installation process, you’ll be prompted to set a password for the MySQL root user. Make sure to choose a strong and secure password. Once the installation is complete, start the MySQL service:

sudo systemctl start mysql

To enhance the security of your MySQL installation, run the MySQL secure installation script:

sudo mysql_secure_installation

Follow the prompts to remove the anonymous user, disallow remote root login, remove test databases, and reload the privilege tables.

Note:

If you are not prompted to set password in the above method, you can use these steps to set password for your user (e.g. root):

sudo mysql

You will be logged into MySQL server, now run this query to set the password:

ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password by 'password';

Verify your MySQL installation and password with this command, you will be asked to enter your password afterwards.

mysql -h 127.0.0.1 -u root -p

Step 4: Installing PHP

PHP is a popular server-side scripting language that enables dynamic content generation. Install PHP and its required dependencies using the following commands:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php php-fpm

If you need to install any specific version of PHP, you can use this command instead:

sudo apt install php8.1 php8.1-fpm

If you need to install additional PHP extensions for your framework, like Magento, you can install them like this:

sudo apt install php8.1 php8.1-fpm php8.1-common php8.1-mysql php8.1-gmp php8.1-curl php8.1-intl php8.1-mbstring php8.1-xmlrpc php8.1-gd php8.1-xml php8.1-cli php8.1-zip php8.1-soap php8.1-bcmath

Once installation is done, you can start it like this:

sudo systemctl start php8.1-fpm

Step 5: Configuring Nginx to Use PHP

To configure Nginx to work with PHP, open the default Nginx configuration file using your preferred text editor:

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

Inside the server block, locate the location ~ \.php$ line and uncomment it by removing the ‘#’ character. It should look like this:

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

Save the file and exit the text editor.

Step 6: Testing the LEMP Setup and Verifying PHP Configuration

To ensure that Nginx can handle PHP files properly, create a new PHP test file:

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

Add the following code to the file:

<?php
phpinfo();
?>

Save the file and exit the text editor. Open your web browser and navigate to http://your_server_ip/info.php. You should see the PHP information page, which confirms that PHP is working correctly with Nginx.

Step 7: Restarting Services

After making configuration changes, you may restart the Nginx, MySQL and PHP-FPM services:

sudo systemctl restart nginx
sudo systemctl restart mysql-server
sudo systemctl restart php8.1-fpm

Conclusion

Congratulations! You’ve successfully installed the LEMP stack on your Ubuntu server. By combining Linux, Nginx, MySQL, and PHP, you have set up a powerful web server that can handle high-performance websites with ease. Take your time to explore the various configuration options for each component, and enjoy the benefits of a robust LEMP stack for your website.

Remember, maintaining the security of your server and keeping your software up-to-date are essential practices for a safe and efficient web hosting environment. Happy coding!

Updated: