LEMP Stack on Ubuntu 18.04: A Complete Installation Guide


5 min read 14-11-2024
LEMP Stack on Ubuntu 18.04: A Complete Installation Guide

In the world of web development and server management, the LEMP stack stands out as a robust and efficient combination of technologies. LEMP, which stands for Linux, Nginx (pronounced "engine-x"), MySQL (or MariaDB), and PHP, provides a powerful infrastructure for building dynamic web applications. This article serves as a comprehensive guide to installing the LEMP stack on Ubuntu 18.04, ensuring that both beginners and seasoned developers can set up their web servers with ease.

What is the LEMP Stack?

Before diving into the installation process, it’s vital to understand what each component of the LEMP stack is:

  • Linux: The operating system that serves as the foundation for the stack. In this guide, we will use Ubuntu 18.04, a popular choice due to its user-friendly interface and extensive community support.
  • Nginx: A high-performance web server known for its speed and efficiency in handling numerous connections simultaneously, making it an excellent choice for high-traffic websites.
  • MySQL/MariaDB: A relational database management system used to store and retrieve application data. MariaDB is a fork of MySQL and is often preferred due to its enhanced performance and features.
  • PHP: A server-side scripting language that is widely used for web development. It enables developers to create dynamic content and interact with databases.

Prerequisites

Before we begin the installation, ensure that you have:

  1. A server running Ubuntu 18.04 (you can use a local machine, a VPS, or a cloud provider).
  2. Root or sudo access to your server.
  3. Basic knowledge of using the command line.

Step 1: Update Your System

First and foremost, it’s crucial to update your system packages. Open your terminal and execute the following commands:

sudo apt update
sudo apt upgrade

Running these commands will ensure that you are working with the latest package versions, minimizing potential issues during the installation process.

Step 2: Install Nginx

Now, let’s install Nginx, the cornerstone of our LEMP stack. Use the following command:

sudo apt install nginx

After installation, Nginx should start automatically. You can check its status by running:

sudo systemctl status nginx

If Nginx is running, you should see an active status. To verify that Nginx is correctly installed and operational, open your web browser and type in your server's IP address. You should see the default Nginx welcome page.

Configuring Nginx

To make sure Nginx starts automatically on boot, use the command:

sudo systemctl enable nginx

Nginx’s default configuration file is located at /etc/nginx/nginx.conf. However, site-specific configurations are stored in /etc/nginx/sites-available/ and linked to /etc/nginx/sites-enabled/.

To create a new configuration file for your website, navigate to the sites-available directory:

cd /etc/nginx/sites-available/

Create a new file for your website:

sudo nano example.com

Add the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

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

Make sure to replace example.com with your actual domain name. Save and exit the file, then create a symlink to enable the site:

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

Finally, test your Nginx configuration for syntax errors and reload the service:

sudo nginx -t
sudo systemctl reload nginx

Step 3: Install MySQL/MariaDB

Next up, we will install the database management system. Execute the following command to install MariaDB:

sudo apt install mariadb-server

Once installed, run the security script to enhance the security of your MariaDB installation:

sudo mysql_secure_installation

You will be prompted to configure security settings such as setting a root password, removing anonymous users, and disabling remote root login. It is highly recommended to answer "Y" (yes) to all prompts for a more secure installation.

Accessing the MySQL/MariaDB Console

To access the MariaDB console, run:

sudo mysql -u root -p

You will be prompted to enter the root password you just set. Once logged in, you can create databases and manage users. For instance, to create a new database, run:

CREATE DATABASE example_database;

Replace example_database with your desired database name. To create a new user and grant them privileges, use:

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

Step 4: Install PHP

With Nginx and MariaDB ready, we can now install PHP. Execute the following command:

sudo apt install php-fpm php-mysql

After installation, you will need to configure PHP to work with Nginx. Open the PHP configuration file:

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

Search for the line cgi.fix_pathinfo=1 and change it to:

cgi.fix_pathinfo=0

This configuration helps prevent potential security issues with PHP files. Save the file and restart PHP-FPM to apply the changes:

sudo systemctl restart php7.2-fpm

Step 5: Testing the LEMP Stack

To ensure that everything is functioning correctly, let’s create a simple PHP file to test PHP processing. Create a new directory for your website:

sudo mkdir -p /var/www/example.com/html

Then create an index.php file:

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

Add the following content to the file:

<?php
phpinfo();
?>

Make sure to set the correct permissions for the directory:

sudo chown -R www-data:www-data /var/www/example.com/html

Now, open your browser and navigate to http://example.com/index.php. You should see a page displaying PHP configuration details, confirming that PHP is working as expected.

Step 6: Firewall Configuration

If you are using the UFW firewall, you’ll need to allow HTTP and HTTPS traffic. Run the following commands:

sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

This will ensure that your server can communicate over the web.

Step 7: Conclusion

Congratulations! You’ve successfully installed a LEMP stack on Ubuntu 18.04. With Nginx serving as your web server, MariaDB as your database, and PHP for dynamic content generation, you now have a powerful environment for developing and hosting web applications.

In summary, we covered the installation and configuration of each component of the LEMP stack:

  • Nginx for efficient web serving.
  • MariaDB for secure and reliable data management.
  • PHP for dynamic web content.

As you move forward, consider exploring additional features and optimizations for Nginx and PHP, such as setting up SSL for secure connections or utilizing caching mechanisms to enhance performance.

FAQs

1. What is the difference between LAMP and LEMP? The main difference is the web server used; LAMP uses Apache, while LEMP uses Nginx. Nginx is known for better performance under high load.

2. Can I use MySQL instead of MariaDB? Yes, you can use MySQL, but MariaDB is often recommended due to its improved performance and compatibility.

3. How do I secure my LEMP stack? Implement HTTPS using Let’s Encrypt, regularly update your software, and restrict access to sensitive areas on your server.

4. What are some common issues during installation? Common issues include incorrect permissions, firewall settings blocking traffic, and syntax errors in configuration files.

5. How do I update my LEMP stack? Run sudo apt update followed by sudo apt upgrade to update the packages. Always backup your data before major updates.

By following this guide, you will be well on your way to leveraging the power of the LEMP stack for your web applications on Ubuntu 18.04! Happy coding!