How to Install LEMP on Fedora 39, 38 Linux

This guide will demonstrate how to install LEMP on Fedora Linux, a powerful combination of software that enhances server capabilities for web hosting and development. LEMP stands for Linux operating system, with Nginx (pronounced as ‘Engine-X’) web server, MySQL database management system, and PHP programming language. This stack is highly favored for its performance, flexibility, and efficiency, making it an ideal choice for beginners and experienced developers.

Key Features of LEMP:

  • Nginx: Renowned for its high performance, Nginx serves as a more resource-efficient and scalable alternative to traditional servers like Apache.
  • MySQL: A robust database management system, MySQL offers reliable data storage and retrieval, essential for dynamic websites.
  • PHP: Widely used for web development, PHP ensures seamless processing and execution of server-side scripts.
  • Linux (Fedora): Fedora, known for its cutting-edge features and robust security, forms the stable and powerful foundation of the LEMP stack.

Setting up LEMP on Fedora involves steps that ensure each component works harmoniously. This setup will enhance your server’s capability to host dynamic websites and applications efficiently. By following this guide, you’ll establish a solid foundation for web hosting and application development, leveraging the strengths of each LEMP component.

Install LEMP on Fedora Linux via DNF

Step 1: Update Fedora Linux Before LEMP Installation

Before we begin, ensuring your system is up-to-date with the latest software packages is essential. To update Fedora Linux, open a terminal window and run the following command:

sudo dnf upgrade --refresh

This will check for available updates and prompt you to confirm the installation of any updates found. Follow the on-screen prompts to complete the update process.

Step 2: Install Nginx

To install Nginx on Fedora Linux, open a terminal window and run the following command:

sudo dnf install nginx

This will install the latest version of Nginx and its dependencies. Once the installation is complete, you can start the Nginx service by running the following command:

sudo systemctl enable nginx --now

This command will enable the Nginx service to start automatically on system boot and start the service immediately.

To verify that Nginx is running correctly, you can check its status by running the following command:

systemctl status nginx

If Nginx is running without any errors, you should see a message indicating that the service is active and running, similar to the following output:

Screenshot of systemctl status command for nginx during LEMP installation on Fedora Linux.
Screenshot showing systemctl status command output for nginx during LEMP installation on Fedora Linux.

If no errors have occurred, move on to the next step.

Configure Nginx Firewall Rules on Fedora

Installing Nginx does not automatically configure firewall rules for standard ports 80 (HTTP) and 443 (HTTPS). You need to set appropriate rules for the ports you plan to use. Use the following commands for reference:

To open port 80 or HTTP, run the following command:

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

To open port 443 or HTTPS, run the following command:

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

After adding the rules, reload the firewall with the following command:

sudo firewall-cmd --reload

Before proceeding, open your browser and ensure you can access the Nginx access page:

http://localhost

Or

http://server-ip
Screenshot of the default nginx test page during LEMP installation on Fedora Linux.
Users might see an example of the nginx test page during LEMP installation on Fedora Linux.

Step 3: Install MariaDB

MariaDB is a popular open-source relational database management system. To install MariaDB on Fedora Linux, use the following command:

sudo dnf install mariadb-server mariadb

After installation, start the MariaDB service by running the following command:

sudo systemctl enable mariadb --now

To check the status of the MariaDB service, run the following command:

systemctl status mariadb
Screenshot of systemctl status command for MariaDB during LEMP installation on Fedora Linux.
Screenshot showing systemctl status command output for MariaDB during LEMP installation on Fedora Linux.

Run MariaDB Security Script on Fedora

Industry standards deem the default settings for a new MariaDB installation weak, presenting a security risk. Experts advise running the mysql_secure_installation script during installation to enhance security, thwart potential intrusions, and prevent exploits.

Execute the script with the following command:

sudo mariadb-secure-installation

Follow the prompts to configure the root password, disallow remote access from external sources, remove anonymous user accounts, and delete the test database.

As you go through the mysql_secure_installation settings, you’ll see prompts similar to the example below:

For example, the script will prompt you with various questions like:

Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y

To secure your installation, answer “Y” to all the above questions.

If no errors have occurred, proceed to the next step.

Step 4: Install PHP and PHP-FPM

To install PHP on Fedora Linux, use the following command:

sudo dnf install php php-fpm php-cli php-mysqlnd

Once the installation is complete, start the PHP-FPM service with this command:

sudo systemctl enable php-fpm --now

To check the status of the PHP-FPM service, use this command:

systemctl status php-fpm
Screenshot of systemctl status command for PHP during LEMP installation on Fedora Linux.
Screenshot showing systemctl status command output for PHP during LEMP installation on Fedora Linux.

Configure Nginx Server Block LEMP on Fedora Linux

To configure Nginx to work with PHP, you must make changes to the configuration file located at /etc/nginx/nginx.conf. Add the following lines to the server block:

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

Once you have made the changes, test to ensure no errors occur in your syntax:

sudo nginx -t

Now, you will need to restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

Configure Apache to Nginx on Fedora Linux

On Debian/Ubuntu distributions, the “www-data” user typically handles PHP-FPM tasks. However, on the Fedora family, installations configure the PHP-FPM service to run under the “Apache” user by default, which doesn’t suit Nginx use. Therefore, you must make adjustments.

First, open the configuration file (www.conf) with the following command:

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

Substitute the (Apache) user and group with the (Nginx) user and group.

Change from:

user = apache
group = apache

Change to:

user = nginx
group = nginx

Save the file by pressing (CTRL+O), then exit (CTRL+X).

Now, you can restart your PHP-FPM service:

sudo systemctl restart php-fpm

Create a Test PHP File on Fedora Linux

Create a test PHP file to verify that PHP works correctly with Nginx. Place this file in the Nginx web root directory, typically located at /usr/share/nginx/html/.

sudo nano /usr/share/nginx/html/info.php

Create a file called info.php and paste the following code:

<?php
phpinfo();
?>

Test LEMP Installation on Fedora

After completing the installation, you can test that everything works correctly by visiting the test PHP file in a web browser. Open a web browser and visit the following URL:

http://your-server-ip/info.php

The output of your PHP test page might vary depending on your installed PHP version. For example, a PHP test page will display different outputs with different installed PHP versions.

Screenshot of PHP info page after LEMP installation on Fedora Linux.
After completing the LEMP installation on Fedora Linux, users will see a screenshot of the PHP info page.

As a final step, it’s a good idea to remove the `info.php` file you created earlier, as it can expose sensitive information about your server:

sudo rm /usr/share/nginx/html/info.php

Conclusion

In conclusion, this guide provided a comprehensive, step-by-step approach to setting up a LEMP stack on Fedora Linux. By following these instructions, even novice to intermediate users can install and configure Nginx, MariaDB, and PHP to create a solid foundation for hosting web applications. It’s crucial to adhere to security best practices and maintain your server regularly to ensure optimal performance and reliability. With this LEMP stack setup, you can confidently deploy your web projects and achieve higher visibility on Google search rankings.

1 thought on “How to Install LEMP on Fedora 39, 38 Linux”

  1. This was excellent! Thank you. I’m switching from macOS to Fedora, and need to setup my testing environment. This was flawless in comparison to all the hoops I had to jump through (and frustrations) with the other machine.

    Reply

Leave a Comment


Your Mastodon Instance
Share to...