FedoraRHEL Based

How To Install LEMP Stack on Fedora 41

Install LEMP Stack Fedora 41

The LEMP stack is a powerful and efficient web development environment that combines Linux, Nginx (pronounced “Engine-X”), MariaDB, and PHP. This popular stack is known for its high performance, stability, and flexibility, making it an excellent choice for hosting websites and web applications. In this comprehensive guide, we’ll walk you through the process of installing LEMP with PHP 8.3 on Fedora 41, one of the latest releases of the Fedora Linux distribution.

Fedora 41, known for its cutting-edge features and robust security, provides an ideal platform for deploying a LEMP stack. By following this tutorial, you’ll be able to set up a fully functional web server environment that can handle everything from simple static websites to complex, dynamic web applications.

We’ll cover each component of the LEMP stack in detail, providing step-by-step instructions, troubleshooting tips, and optimization advice. Whether you’re a seasoned system administrator or a newcomer to web server setups, this guide will equip you with the knowledge to successfully install and configure LEMP on Fedora 41.

Prerequisites

Before we begin the installation process, ensure that you have the following:

  • A Fedora 41 system with root or sudo access
  • A stable internet connection for downloading packages
  • Basic familiarity with the Linux command line
  • At least 2GB of RAM and 20GB of free disk space

It’s also recommended to have a fresh installation of Fedora 41 to avoid conflicts with existing software. If you’re using a virtual private server (VPS), make sure it meets these minimum requirements.

Updating Fedora 41

Before installing any new software, it’s crucial to ensure your system is up to date. This helps prevent compatibility issues and ensures you have the latest security patches. To update your Fedora 41 system, follow these steps:

  1. Open a terminal window.
  2. Run the following command:
    sudo dnf upgrade --refresh
  3. When prompted, enter your password and press ‘Y’ to confirm the update.
  4. Wait for the update process to complete. This may take several minutes depending on your internet speed and the number of updates available.

Once the update is finished, it’s a good practice to reboot your system to ensure all changes take effect:

sudo reboot

Installing Nginx

Nginx is a high-performance web server that excels at serving static content and handling multiple concurrent connections. Let’s install and configure Nginx on your Fedora 41 system:

  1. Install Nginx using DNF:
    sudo dnf install nginx
  2. Once installed, start the Nginx service and enable it to start on boot:
    sudo systemctl start nginx
    sudo systemctl enable nginx
  3. Configure the firewall to allow HTTP and HTTPS traffic:
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
  4. Verify that Nginx is running by accessing your server’s IP address or domain name in a web browser. You should see the default Nginx welcome page.

Install LEMP Stack on Fedora 41

If you encounter any issues, check the Nginx error logs:

sudo tail -f /var/log/nginx/error.log

Installing MariaDB

MariaDB is a powerful, open-source relational database management system that’s fully compatible with MySQL. Here’s how to install and secure MariaDB on Fedora 41:

  1. Install MariaDB server:
    sudo dnf install mariadb-server
  2. Start the MariaDB service and enable it to start on boot:
    sudo systemctl start mariadb
    sudo systemctl enable mariadb
  3. Secure your MariaDB installation:
    sudo mysql_secure_installation

    Follow the prompts to set a root password, remove anonymous users, disallow root login remotely, and remove the test database.

  4. Create a new database and user for your web applications:
    sudo mysql -u root -p
    CREATE DATABASE mywebsite;
    CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
    GRANT ALL PRIVILEGES ON mywebsite.* TO 'myuser'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;

    Replace ‘mywebsite’, ‘myuser’, and ‘mypassword’ with your preferred names and a strong password.

To test your MariaDB connection, try logging in with the new user:

mysql -u myuser -p

Installing PHP 8.3

PHP 8.3 brings significant performance improvements and new features. Let’s install PHP 8.3 and configure it to work with Nginx:

  1. Add the Remi repository, which provides the latest PHP versions:
    sudo dnf install https://rpms.remirepo.net/fedora/remi-release-41.rpm
  2. Enable the PHP 8.3 module:
    sudo dnf module reset php
    sudo dnf module enable php:remi-8.3
  3. Install PHP 8.3 and necessary modules:
    sudo dnf install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring
  4. Start and enable PHP-FPM service:
    sudo systemctl start php-fpm
    sudo systemctl enable php-fpm

To verify the PHP installation, create a test file:

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

Add the following content to the file:

<?php
phpinfo();
?>

Save the file and exit the editor. We’ll test this file after configuring Nginx to work with PHP.

Configuring Nginx to Work with PHP

Now that we have Nginx and PHP installed, we need to configure Nginx to use PHP-FPM to process PHP files:

  1. Open the default Nginx server block configuration:
    sudo nano /etc/nginx/nginx.conf
  2. Find the server block and modify it to look like this:
    server {
        listen       80;
        server_name  localhost;
        root         /var/www/html;
        index        index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/www.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }
  3. Save the file and exit the editor.
  4. Test the Nginx configuration for syntax errors:
    sudo nginx -t
  5. If there are no errors, reload Nginx to apply the changes:
    sudo systemctl reload nginx

Testing the LEMP Stack

Now that we have all components installed and configured, let’s test our LEMP stack:

  1. Open a web browser and navigate to http://your_server_ip/info.php. You should see the PHP info page, which confirms that Nginx is correctly processing PHP files.
  2. If you don’t see the PHP info page, check the Nginx error logs for any issues:
    sudo tail -f /var/log/nginx/error.log
  3. After confirming that PHP is working, it’s recommended to remove the info.php file for security reasons:
    sudo rm /var/www/html/info.php

Optimizing LEMP Stack Performance

To get the best performance out of your LEMP stack, consider the following optimizations:

Nginx Optimization

  • Enable Gzip compression in your Nginx configuration to reduce bandwidth usage.
  • Implement browser caching for static assets to reduce server load.
  • Use Nginx’s FastCGI cache to cache dynamic content and reduce PHP processing overhead.

MariaDB Performance Tuning

  • Adjust the innodb_buffer_pool_size based on your server’s available memory.
  • Enable query caching for frequently accessed data.
  • Optimize your database schema and indexes for your specific use case.

PHP-FPM Configuration

  • Adjust the number of PHP-FPM workers based on your server’s resources and traffic patterns.
  • Enable OpCache to improve PHP performance by caching compiled bytecode.

Security Considerations

Securing your LEMP stack is crucial to protect your web applications and data. Here are some important security measures to implement:

Implementing SSL/TLS

Use Let’s Encrypt to obtain free SSL certificates and configure Nginx to use HTTPS:

sudo dnf install certbot python3-certbot-nginx
sudo certbot --nginx

Configuring Firewall Rules

Ensure that your firewall only allows necessary incoming connections:

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Regular System Updates and Backups

  • Set up automatic system updates to ensure you have the latest security patches.
  • Implement a regular backup strategy for your databases and web files.
  • Monitor your server logs for any suspicious activity.

Congratulations! You have successfully installed LEMP. Thanks for using this tutorial for installing the LEMP Stack on Fedora 41 system. For additional help or useful information, we recommend you check the Fedora website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button