
Set Up a Powerful Web Server: A Step-by-Step Guide to Installing a LEMP Stack on Fedora
Building a fast, reliable web server is the foundation of any successful online project. For those seeking performance and efficiency, the LEMP stack is a premier choice. This guide will walk you through setting up a complete LEMP environment on a Fedora system, empowering you to host dynamic websites and applications with confidence.
LEMP is an acronym that stands for Linux, Eginx (pronounced “Engine-X”), MySQL, and PHP. This combination of open-source software provides a robust platform for modern web development. Fedora, with its cutting-edge packages and strong security focus, makes an excellent base for this powerful stack.
Prerequisites
Before we begin, ensure you have the following:
- A running instance of a recent Fedora release.
- Access to a user account with
sudo
or root privileges.
Step 1: Update Your System
First things first, it’s crucial to ensure your system is up-to-date. This fetches the latest security patches and package versions from the Fedora repositories. Open your terminal and run the following command:
sudo dnf update -y
Step 2: Install and Configure the Nginx Web Server
Nginx is a high-performance web server known for its stability and low resource consumption. To install it, use the dnf
package manager:
sudo dnf install nginx -y
Once the installation is complete, you need to start the Nginx service and enable it to launch automatically on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Next, you must configure the system firewall to allow web traffic. Without this step, your server will be unreachable from the outside world. We will add rules for both standard HTTP (port 80) and HTTPS (port 443) traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
To verify that Nginx is running correctly, open a web browser and navigate to your server’s IP address (http://your_server_ip
). You should see the default Nginx welcome page.
Step 3: Install and Secure the MySQL Database Server
MySQL is the world’s most popular open-source relational database. It will store your application’s data. Install the MySQL server package with this command:
sudo dnf install mysql-server -y
After installation, start and enable the MySQL service, which is called mysqld
:
sudo systemctl start mysqld
sudo systemctl enable mysqld
Now for a crucial step: securing your MySQL installation. The default setup is not safe for a production environment. MySQL provides a helpful script to lock it down.
Run the security script by typing:
sudo mysql_secure_installation
This interactive script will guide you through several important security measures, including:
- Setting a strong root password.
- Removing anonymous users.
- Disallowing remote root login.
- Removing the test database.
We highly recommend completing all steps suggested by the script to harden your database server against common threats.
Step 4: Install PHP and PHP-FPM
PHP is the server-side scripting language that will process your application’s logic. We will also install PHP-FPM (FastCGI Process Manager), which is the standard way for Nginx to handle PHP requests efficiently.
Install PHP, PHP-FPM, and a few common extensions that are necessary for most modern applications, like connecting to a MySQL database:
sudo dnf install php php-fpm php-mysqlnd php-cli php-json php-mbstring -y
The php-mysqlnd
package is essential for allowing PHP to communicate with your MySQL database. Once installed, start and enable the PHP-FPM service:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Step 5: Configure Nginx to Process PHP Files
You have installed all the components, but Nginx doesn’t yet know how to handle PHP files. We need to edit the Nginx configuration to pass PHP requests to the PHP-FPM service.
Open the default Nginx server block configuration file in a text editor like nano
:
sudo nano /etc/nginx/nginx.conf
Inside the server
block (within the http
block), you need to modify the location
block and add a new section to process PHP files. Look for the location /
section and add index.php
to the index
directive. Then, uncomment and modify the location ~ \.php$
block to look like this:
# ... inside the http block
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Add index.php to the list of default file types
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# Pass PHP scripts to FastCGI server
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# ... other directives
}
Save the file and exit the editor. To apply the changes, you must test the configuration for syntax errors and then restart Nginx:
sudo nginx -t
sudo systemctl restart nginx
If nginx -t
reports the syntax is okay, you’re ready for the final test.
Step 6: Create a Test PHP File to Verify the Setup
The final step is to create a simple PHP file to confirm that the entire LEMP stack is working together correctly. Create a file named info.php
in the Nginx web root directory:
sudo nano /usr/share/nginx/html/info.php
Add the following single line of PHP code to the file:
<?php phpinfo(); ?>
Save and close the file. Now, open your web browser and navigate to http://your_server_ip/info.php
. You should see a detailed page with information about your PHP installation, including version numbers and configured extensions.
Important Security Note: This info.php
file reveals sensitive information about your server’s configuration. For security reasons, it is critically important to delete this file after you have confirmed your setup is working.
sudo rm /usr/share/nginx/html/info.php
Your Fedora LEMP Stack is Ready
Congratulations! You have successfully deployed a powerful and efficient LEMP stack on your Fedora server. You now have a robust platform ready for hosting websites, launching web applications, or installing a CMS like WordPress or Drupal. Your next steps might include setting up Nginx server blocks (virtual hosts) for multiple sites or securing your server with a free Let’s Encrypt SSL/TLS certificate.
Source: https://kifarunix.com/install-lemp-stack-with-mysql-8-on-fedora-30-fedora-29/