LEMP (Linux, Nginx, MariaDB/MySQL, PHP) is a powerful and efficient stack for hosting dynamic websites and applications. This guide will walk you through installing and configuring a LEMP web server on CentOS 8.
Prerequisites
Before we begin, ensure you have the following:
- A CentOS 8 server with root or sudo user privileges.
- Access to the command line.
- Basic knowledge of terminal commands.
Step 1: Update Your System
It’s a good practice to start by updating your system to ensure all packages are up-to-date. Run the following commands to update your CentOS server:
sudo dnf update -y
Step 2: Install Nginx
Nginx is the web server component of the LEMP stack. To install Nginx on CentOS 8, follow these steps:
- First, add the EPEL (Extra Packages for Enterprise Linux) repository, which provides the latest versions of many packages, including Nginx:
sudo dnf install epel-release -y
- Now install Nginx:
sudo dnf install nginx -y
- Start and enable Nginx to ensure it runs on startup:
sudo systemctl start nginx
sudo systemctl enable nginx
- To confirm that Nginx is running, check the status:
sudo systemctl status nginx
You can also verify Nginx by accessing your server’s IP address in a web browser. If Nginx is installed correctly, you’ll see the default Nginx welcome page.
Step 3: Install MariaDB (MySQL alternative)
MariaDB is a drop-in replacement for MySQL, providing the database layer for your web applications. To install MariaDB, use the following command:
sudo dnf install mariadb-server mariadb -y
After installation, start and enable MariaDB:
sudo systemctl start mariadb
sudo systemctl enable mariadb
To secure your database installation, run the following command:
sudo mysql_secure_installation
You will be prompted to set a root password, remove anonymous users, disallow remote root login, and remove the test database. Answer each question appropriately based on your needs.
Step 4: Install PHP
PHP is the server-side scripting language that processes dynamic content. CentOS 8 comes with multiple versions of PHP, and we will install PHP along with the necessary extensions for Nginx.
- First, enable the Remi repository, which provides PHP packages:
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
- Next, enable the module for PHP 7.4, which is a stable version for most web applications:
sudo dnf module reset php
sudo dnf module enable php:remi-7.4 -y
- Install PHP along with some commonly used extensions:
sudo dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y
- Once installed, start and enable PHP-FPM (FastCGI Process Manager) for handling PHP requests through Nginx:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Step 5: Configure Nginx to Use PHP
Next, we need to configure Nginx to process PHP files. To do this, modify the default server block file in Nginx.
- Open the Nginx configuration file for the default server block:
sudo nano /etc/nginx/conf.d/default.conf
- Add the following configuration within the
server {}
block to enable PHP processing:
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
location ~ /\.ht {
deny all;
}
- Save and close the file, then test the Nginx configuration for any syntax errors:
sudo nginx -t
- If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 6: Test PHP Processing
To confirm that PHP is working correctly, create a PHP info file in the web root directory.
- Create the file:
sudo nano /usr/share/nginx/html/info.php
- Add the following content to the file:
<?php
phpinfo();
?>
- Save the file and exit. Now, open your browser and visit
http://your_server_ip/info.php
. You should see a page displaying PHP information, confirming that PHP is successfully configured.
Step 7: Secure Your Installation
- Firewall Configuration: Ensure that your firewall is configured to allow traffic on HTTP (port 80) and HTTPS (port 443) for web access.
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
- Remove the PHP Info File: For security reasons, it’s recommended to delete the
info.php
file after testing.
sudo rm /usr/share/nginx/html/info.php
Conclusion
You have successfully installed and configured a LEMP web server on CentOS 8. Your server is now ready to host dynamic web applications using Nginx, MariaDB (or MySQL), and PHP.