Deploy LEMP Stack with Let’s Encrypt on Red Hat

This guide will walk you through the steps to deploy a LEMP stack with Let’s Encrypt on a Red Hat server.

Step 1: Update System Packages

sudo yum update -y

Step 2: Install Nginx

sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

Verify Nginx is running:

sudo systemctl status nginx

Step 3: Install MySQL

sudo yum install @mysql -y
sudo systemctl start mysqld
sudo systemctl enable mysqld

Secure MySQL installation:

sudo mysql_secure_installation

Step 4: Install PHP

sudo yum install php php-fpm php-mysqlnd -y
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Edit PHP-FPM configuration to work with Nginx:

sudo vim /etc/php-fpm.d/www.conf

Set the following:

user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx

Restart PHP-FPM:

sudo systemctl restart php-fpm

Step 5: Configure Nginx

Create a new Nginx server block:

sudo vim /etc/nginx/conf.d/example.com.conf

Add the following configuration:

server {
    listen       80;
    server_name  example.com www.example.com;
    
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Test Nginx configuration:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx

Step 6: Install Certbot

sudo yum install certbot python3-certbot-nginx -y

Step 7: Obtain SSL Certificate

sudo certbot --nginx -d example.com -d www.example.com

Follow the prompts to complete the certificate installation.

Step 8: Configure Firewall

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Step 9: Verify HTTPS

Open your browser and visit https://example.com to ensure everything is working correctly.

Conclusion

You have successfully deployed a LEMP stack with Let’s Encrypt on your Red Hat server.

Other Recent Posts