How to Install LEMP on Fedora Linux

LEMP is an acronym for a popular web server software stack consisting of Linux (the operating system), Nginx (the web server), MariaDB (the database server), and PHP (the programming language). LEMP has gained popularity in web development due to its high performance, stability, and flexibility. This stack is widely used for hosting websites and web applications, ranging from simple blogs to complex e-commerce platforms and content management systems.

There are several benefits to using LEMP on Fedora servers, including:

  • Cutting-edge technology: Fedora is known for incorporating the latest technologies and providing frequent updates, ensuring that your LEMP stack is up-to-date and equipped with the latest features.
  • Performance: Nginx is an event-driven, asynchronous web server designed for high concurrency, resulting in better response times and scalability than traditional web servers like Apache.
  • Security: Fedora Server is built with security in mind and offers a solid foundation for a secure LEMP stack. Additionally, by following best practices, you can further enhance the security of your web applications.
  • Flexibility: LEMP on Fedora allows you to customize your stack according to your needs. You can easily add or remove components and configure your server to suit your requirements.

This guide will demonstrate installing LEMP on Fedora Linux using the command line terminal. The guide will also cover configuring Nginx with PHP-FPM and creating a test PHP page to ensure proper functionality. Following these instructions, you can set up a robust and efficient server environment for hosting your web applications on Fedora Linux.

Step 1: Update Fedora Linux

Before we begin, ensuring your system is up-to-date with the latest software packages is important. 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:

Example output:

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

Configure Nginx Firewall Rules

When you install Nginx, it 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

Example of Nginx test page on Fedora:

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

Example output:

MariaDB Security Configuration

The default settings for a new MariaDB installation are considered weak by industry standards, posing a security risk. It’s recommended to run the mysql_secure_installation script during the installation process to secure the installation and prevent potential intrusions or 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

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

Example output:

If no errors have occurred, move on to step 5.

Step 5: Configure Nginx Server Block

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

Step 6: Configure Nginx & PHP-FPM

On distributions like Debian/Ubuntu, the “www-data” user is often used for PHP-FPM. However, this is not the default for installations on the Fedora family. By default, the PHP-FPM service is configured to run under the “Apache” user, which is unsuitable for Nginx use. Therefore, adjustments need to be made.

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

Step 7: Create a Test PHP File

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();
?>

Step 8: Test the Installation

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 may vary depending on the installed version of PHP. For instance, if you have a PHP page for testing purposes, the output could differ based on the PHP version that is currently installed.

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.

Additional Resources and Relevant Links

To further enhance your knowledge and understanding of the LEMP stack on Fedora Linux, consider exploring the following resources and relevant links:

Share to...