Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

"Skills"
01.11.2024

How to Install Linux, Nginx, MySQL, PHP (LEMP) on Ubuntu

The LEMP stack—which stands for Linux, , MySQL , and PHP—is a widely popular web server stack used to power dynamic websites and applications. By combining the stability of Linux, the performance of Nginx, the robust data management of MySQL, and the versatility of PHP, LEMP provides a powerful, high-performance environment ideal for hosting modern web applications.

For those interested in hosting with optimized support for PHP and MySQL, platforms like AlexHost’s PHP MySQL hosting are tailored to offer enhanced compatibility and stability for LEMP-based applications. Using a well-configured LEMP stack allows for better scalability, faster page load times, and efficient resource management, making it an excellent choice for high-traffic websites and complex applications.

In this article, we will walk you through the complete installation and configuration of the LEMP stack on an Ubuntu server. This guide will cover each component, ensuring that your server is optimized to run dynamic web applications with stability and efficiency. Whether you’re building a personal website or a large-scale web application, following these steps will set you up with a robust server environment tailored for modern web hosting needs. If you’re looking for a reliable platform to host this configuration, AlexHost’s  Ubuntu VPS hosting provides a high-performance solution designed specifically for Linux environments. With an Ubuntu VPS from AlexHost, you gain control over server resources and configurations, enabling you to optimize each component of the LEMP stack for better performance and security. This makes it an excellent choice for developers and businesses looking to host robust, scalable web applications with the flexibility to grow.

1. Prerequisites

Before you begin, ensure you have the following:

  • A server running Ubuntu 18.04 or 20.04.
  • Sudo privileges on the server.
  • A terminal or SSH access to your server.

2. Update Your System

Open your terminal and run the following command to update your package index:

sudo apt update

3. Install Nginx

Step 1: Install Nginx

Run the following command to install Nginx:

sudo apt install nginx -y

Step 2: Start and Enable Nginx

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

sudo systemctl start nginx sudo systemctl enable nginx

Step 3: Check Nginx Status

You can check the status of Nginx to ensure it’s running:

sudo systemctl status nginx

You can also open your web browser and navigate to your server’s IP address. You should see the Nginx welcome page.

4. Install MySQL

Step 1: Install MySQL Server

Run the following command to install MySQL:

sudo apt install mysql-server -y

Step 2: Secure MySQL Installation

After the installation, run the security script to enhance MySQL security:

sudo mysql_secure_installation

Follow the prompts to set a root password and configure other security settings.

5. Install PHP

Step 1: Install PHP and Required Extensions

To install PHP and the required extensions for Nginx and MySQL, run the following command:

sudo apt install php-fpm php-mysql -y

Step 2: Configure PHP Processor

Open the PHP configuration file for Nginx:

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

Ensure the following lines are set (uncomment if necessary):

cgi.fix_pathinfo=0

Step 3: Restart PHP-FPM Service

After making changes to the PHP configuration, restart the PHP-FPM service:

sudo systemctl restart php7.4-fpm

6. Configure Nginx to Use PHP

Step 1: Create a Server Block for Your Website

Create a new configuration file for your website:

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

Add the following configuration:

server {
listen 80;
server_name your_domain;
# Replace with your domain or server IP
root /var/www/your_domain;
# The root directory where your website files are stored
index index.php index.html index.htm;
# Default files to load when accessing the root directory
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# Adjust PHP version as necessary
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

Step 2: Enable the Server Block

Create a symbolic link to enable the server block configuration:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Step 3: Test Nginx Configuration

Test the Nginx configuration for any syntax errors:

sudo nginx -t

Step 4: Restart Nginx

Restart the Nginx service to apply the changes:

sudo systemctl restart nginx

7. Create a PHP Info File

To test the PHP installation, create a PHP info file in your document root:

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

Add the following content:

<?php phpinfo(); ?>

8. Accessing Your Application

Open your web browser and navigate to http://your_domain/info.php or your server’s IP address. You should see the PHP information page.

9. Conclusion

You have successfully installed the LEMP stack (Linux, Nginx, MySQL, PHP) on your Ubuntu server. This powerful combination provides a robust platform for hosting dynamic web applications. Be sure to secure your server, regularly update your software, and explore further configurations to optimize performance.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

"Skills"