Introduction

LEMP stack consists of Linux, Nginx, MySQL/MariaDB, and PHP, providing a powerful framework for web hosting. This guide will walk you through the installation of Nginx, PHP, and MariaDB on Ubuntu, along with Opcache for caching, Redis for data storage, and Let's Encrypt for SSL certificates. This setup is ideal for deployment on a Windows VPS UK for improved performance and security.

Prerequisites

  • An Ubuntu server (20.04 or later) with root access
  • Basic knowledge of Linux commands
  • A domain name pointed to your server (for Let's Encrypt)

Step 1: Update Your System

Begin by updating your package index and upgrading any installed packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Install Nginx using the following command:

sudo apt install nginx -y

Start and enable Nginx to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Install MariaDB

Install MariaDB by executing the following command:

sudo apt install mariadb-server -y

Secure the MariaDB installation:

sudo mysql_secure_installation

Follow the prompts to set a root password and secure your installation.

Step 4: Install PHP and Extensions

Install PHP along with the necessary extensions:

sudo apt install php-fpm php-mysql php-opcache php-redis -y

To install additional PHP extensions, you can run:

sudo apt install php-xml php-mbstring php-curl php-zip php-gd -y

Step 5: Configure PHP Opcache

Edit the PHP configuration file to enable Opcache:

sudo nano /etc/php/8.1/fpm/conf.d/10-opcache.ini

Add or uncomment the following lines:

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.fast_shutdown=1

Save the file and restart PHP:

sudo systemctl restart php8.1-fpm

Step 6: Configure Nginx to Use PHP

Edit the default Nginx configuration file:

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

Update the server block to include the following configuration:

server {
    listen 80;
    server_name your_domain.com; # Replace with your domain

    root /var/www/html; # Path to your web files
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Adjust PHP version if necessary
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(jpg|jpeg|gif|png|css|js|ico|svg)$ {
        expires 30d;
        access_log off;
    }
}

Test the Nginx configuration for syntax errors:

sudo nginx -t

If everything is correct, restart Nginx:

sudo systemctl restart nginx

Step 7: Install and Configure Redis

Install Redis server:

sudo apt install redis-server -y

Start and enable the Redis service:

sudo systemctl start redis-server
sudo systemctl enable redis-server

Step 8: Secure Your Site with Let's Encrypt

Install Certbot and the Nginx plugin:

sudo apt install certbot python3-certbot-nginx -y

Obtain an SSL certificate:

sudo certbot --nginx -d your_domain.com

Follow the prompts to set up your SSL certificate.

Step 9: Test Your Setup

To test if everything is working correctly, navigate to http://your_domain.com in your web browser. You should see the Nginx default page or your application if you have uploaded files to /var/www/html.

Step 10: Conclusion

You have successfully installed Nginx with PHP and MariaDB (LEMP stack) on Ubuntu, including Opcache, Redis, and Let's Encrypt for secure hosting. This setup provides a robust platform for web applications, especially when using a Windows VPS. For further options, consider exploring VPS UK Windows, including Windows Virtual Private Server Hosting and Windows VPS Hosting UK for optimal performance.

© 2024 LEMP Stack Installation Tutorial. All rights reserved.

Cette réponse était-elle pertinente? 0 Utilisateurs l'ont trouvée utile (0 Votes)