Table of Contents
The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications written in PHP. This bash script will install LEMP stack on your CentOS Stream 9 and configure it to maximize its performance of website serving.
# cat /etc/os-release
NAME="CentOS Stream"
VERSION="9"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="9"
Before you begin, let’s take a look this bash script. This script will install and configure the following packages:
- PHP 7.4 and Nginx
- MariaDB 10.8
Usage
Note
This script utilises root user privileges. If you run it from another user you need to add this user to sudoers group and prepend sudo to all commands in the script.
1. SSH into your CentOS Stream 9 Linux server.
2. Run the following commands at once to install wget, download the script, make it executable then execute it.
sudo dnf install -y wget
sudo wget "https://filedn.com/lOX1R8Sv7vhpEG9Q77kMbn0/scripts/redhat/install_lemp_centos9.sh"
sudo chmod +x "install_lemp_centos9.sh"
sudo sed -i 's/\r//' "install_lemp_centos9.sh"
sudo /bin/bash "install_lemp_centos9.sh"
Verification
Once done, you can verify the installation with the info.php file or using the following commands to check the version of PHP, Nginx and MariaDB.
# nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
# php -v
PHP 7.4.3 (cli) (built: Aug 17 2022 13:29:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
# mariadb -V
mariadb Ver 15.1 Distrib 10.3.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Alternatively, you can type the IP address of your CentOS Stream 9 server in a browser address bar to access the default welcome page.

You can remove the downloaded script from your Linux server when the LEMP is installed.
# ls -l
-rwxr-xr-x 1 root root 1987 Oct 27 01:32 install_lemp_centos9.sh
drwx------ 3 root root 4096 Oct 25 13:01 snap
rm -f "install_lemp_centos9.sh"
The Nginx server block needs the following example below for Nginx to process the PHP files. Below is an example of all server {} blocks that process PHP files that need the location ~ .php$ added.
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;
}
Manually
If you don’t want to download the script from the internet, you can create a script then run it manually on your server.
sudo vi installer.sh
chmod +x installer.sh
./installer.sh
Copy then paste all below lines into the installer.sh file that you have created.
#/bin/sh
###Update the CentOS 9 System
sudo sed -i 's/best=True/best=False/g' /etc/yum.conf
sudo sed -i 's/best=True/best=False/g' /etc/dnf/dnf.conf
sudo dnf erase subscription-manager -y
sudo dnf -y update
sudo dnf config-manager --set-enabled crb
###Configure SELinux and Firewall
sudo sed -i 's/enforcing/disabled/g' /etc/selinux/config
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
####Install PHP7.4 and Nginx
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
sudo dnf -y install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm
sudo dnf -y update --refresh
sudo dnf module enable php:remi-7.4 -y
###Install PHP 7.4 Extensions
sudo dnf update -y
sudo dnf -y install nginx php php-cli
sudo dnf -y install php-fpm php-gd php-json php-mbstring php-mysqlnd php-xml php-xmlrpc php-opcache
sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 64M/g' /etc/php.ini
sudo sed -i 's/post_max_size = 8M/post_max_size = 64M/g' /etc/php.ini
systemctl restart php-fpm && systemctl enable php-fpm
systemctl restart nginx && systemctl enable nginx
###Configure php-fpm
sudo sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf
sudo sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf
sudo sed -i 's/listen = 127.0.0.1:9000/listen =/run/php-fpm/www.sock/g' /etc/php-fpm.d/www.conf
sudo sed -i 's/;listen.owner = nobody/listen.owner = nginx/g' /etc/php-fpm.d/www.conf
sudo sed -i 's/;listen.group = nobody/listen.group = nginx/g' /etc/php-fpm.d/www.conf
#### Install Packages for MariaDB
sudo curl -LsS -O https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
sudo bash mariadb_repo_setup --mariadb-server-version=10.8
sudo dnf clean all
sudo dnf -y install MariaDB-server MariaDB-client MariaDB-backup
sudo systemctl enable mariadb && sudo systemctl start mariadb
sudo mariadb-secure-installation
#### Restart all services
systemctl restart nginx && systemctl restart php-fpm && systemctl restart mariadb.service



