Installing the LEMP Stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04


5 min read 14-11-2024
Installing the LEMP Stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04

Installing a LEMP stack on Ubuntu 20.04 can seem daunting at first glance, especially for those who are just dipping their toes into web development. However, once you break down the process, it can become an exhilarating journey into the world of web servers and databases. The acronym “LEMP” stands for Linux, Nginx (pronounced as “engine-x”), MySQL, and PHP. Together, these four components create a powerful environment for hosting web applications and managing databases.

In this article, we will guide you through the step-by-step process of installing the LEMP stack on your Ubuntu 20.04 server. Along the way, we will share insights, tips, and best practices that will not only enhance your understanding but also ensure a smooth installation process. Let’s dive in!

What is the LEMP Stack?

Before we get our hands dirty, let’s clarify what each component of the LEMP stack encompasses:

  • Linux: The operating system that will serve as the foundation of your web server. Ubuntu is a popular choice for servers due to its stability and strong community support.

  • Nginx: An efficient web server that functions as a reverse proxy server and load balancer. Known for its speed and low resource consumption, Nginx has gained popularity over Apache in many setups.

  • MySQL: A relational database management system (RDBMS) that allows you to create and manage databases. MySQL is widely used for data storage in dynamic web applications.

  • PHP: A popular server-side scripting language that is especially suited for web development. PHP can be embedded into HTML and is widely used to create dynamic content on websites.

Prerequisites

Before we install the LEMP stack, it’s crucial to ensure that we have everything set up properly:

  • A VPS or Cloud Instance: You can use providers like DigitalOcean, AWS, or Google Cloud.

  • Ubuntu 20.04 Installed: Make sure you have an up-to-date installation of Ubuntu 20.04.

  • Sudo Privileges: Ensure that you have a user account with sudo privileges for executing administrative tasks.

  • Access via SSH: Use an SSH client like PuTTY or your terminal to access the server remotely.

Step 1: Update Your System

To ensure a smooth installation, we begin by updating the package lists and upgrading installed packages. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade

Updating your package lists allows your system to be aware of the latest versions of packages and their dependencies. Once the upgrade is complete, you're ready to proceed.

Step 2: Install Nginx

Nginx will be the first component we install. As a lightweight and efficient web server, it serves as the gateway to your application. To install Nginx, execute:

sudo apt install nginx

After installation, start the Nginx service and enable it to run at boot time:

sudo systemctl start nginx
sudo systemctl enable nginx

To verify that Nginx is running, open your web browser and navigate to your server's IP address (e.g., http://your_server_ip). You should see the default Nginx welcome page.

Step 3: Install MySQL

Now, we’ll proceed to install MySQL, the database management system. Use the following command:

sudo apt install mysql-server

Once the installation is completed, secure your MySQL installation with:

sudo mysql_secure_installation

This command will prompt you to set a password for the root user, remove anonymous users, disallow root login remotely, and remove test databases. Following these steps will enhance the security of your MySQL installation.

To log in to MySQL, use:

sudo mysql -u root -p

You’ll be prompted for the root password that you set earlier.

Step 4: Install PHP

PHP is essential for running dynamic web pages. To install PHP and the necessary extensions, execute:

sudo apt install php-fpm php-mysql

Once PHP is installed, configure PHP by editing its configuration file. Open the PHP configuration file:

sudo nano /etc/php/7.4/fpm/php.ini

Find the line that contains cgi.fix_pathinfo and set it to 0:

cgi.fix_pathinfo=0

After editing, save the file and exit. Next, restart the PHP processor to apply the changes:

sudo systemctl restart php7.4-fpm

Step 5: Configure Nginx to Use PHP

To allow Nginx to process PHP files, we need to modify its configuration files. Open the default Nginx configuration file:

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

Within the server block, look for the location block that handles PHP files. Ensure it resembles the following:

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

Also, ensure that Nginx serves index.php as a default file:

index index.php index.html index.htm;

After you make changes, save the file and exit the text editor. To test the configuration for syntax errors, run:

sudo nginx -t

If you receive a message saying “syntax is okay,” restart Nginx to apply your changes:

sudo systemctl restart nginx

Step 6: Test Your LEMP Stack

To check if everything is working seamlessly, create a PHP info file. This file will help you confirm that PHP is properly configured. Navigate to the web root directory:

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

Add the following PHP code to the file:

<?php
phpinfo();
?>

After saving the file, open your browser and navigate to http://your_server_ip/info.php. You should see a detailed page showing your PHP configuration. If you see this page, congratulations! You've successfully installed and configured the LEMP stack.

Step 7: Secure Your Installation

While your server is functional, it is crucial to ensure it is secure. Here are some best practices:

Use a Firewall

Utilize UFW (Uncomplicated Firewall) to create a basic firewall setup:

sudo ufw allow 'Nginx Full'
sudo ufw enable

Secure MySQL Further

Limit remote access to your MySQL server. If not needed, keep it restricted to local access.

Keep Everything Updated

Regularly check for updates and patch any vulnerabilities:

sudo apt update
sudo apt upgrade

Backup Data Regularly

Ensure that you have a robust backup solution in place to recover your data in case of an unforeseen incident.

Conclusion

In conclusion, installing the LEMP stack on Ubuntu 20.04 may seem like an intimidating task, but with a structured approach and guidance, it can be quite straightforward. This setup allows you to host dynamic web applications and manage databases efficiently. The LEMP stack is known for its performance and is widely adopted in the industry. Now that you have installed the LEMP stack, you are well on your way to building amazing web applications.

Whether you’re hosting a personal project or a business application, having a solid understanding of how to manage your web stack is crucial. By following the steps outlined in this guide, you’ve taken the first significant step towards your web development journey.

FAQs

1. What is the difference between LAMP and LEMP stacks?

LEMP uses Nginx as the web server, while LAMP uses Apache. Nginx is known for better performance and resource efficiency.

2. Can I use a different database instead of MySQL?

Yes, you can use other databases like MariaDB or PostgreSQL. The installation steps will vary slightly.

3. How do I remove the info.php file?

To remove the file, execute the command:

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

4. Is it safe to use PHP on a production server?

Yes, but you must ensure that you follow best security practices to minimize vulnerabilities.

5. How can I check if Nginx is running?

Use the command:

systemctl status nginx

This command provides information about the Nginx service status, confirming if it’s active and running.