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 7/RHEL 7 servers and configure it to maximize its performance of website serving.
# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
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
1. SSH into your CentOS 7 Linux server.
2. Run the following commands at once to install wget, download the script, make it executable then execute it.
sudo yum install -y wget
sudo wget "https://filedn.com/lOX1R8Sv7vhpEG9Q77kMbn0/scripts/redhat/install_lemp_centos7.sh"
sudo chmod +x "install_lemp_centos7.sh"
sudo sed -i 's/\r//' "install_lemp_centos7.sh"
sudo /bin/bash "install_lemp_centos7.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 7 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_centos7.sh
drwx------ 3 root root 4096 Oct 25 13:01 snap
rm -f "install_lemp_centos7.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 7 System
yum update -y
yum install wget vim epel-release -y
yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
###Configure SELinux and Firewall
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 yum -y install yum-utils
sudo yum-config-manager --enable remi-php74
yum update -y
yum install nginx php php-cli -y
yum install php-fpm php-gd php-json php-mbstring php-mysqlnd php-xml php-xmlrpc php-opcache -y
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 yum clean all
sudo yum install MariaDB-server MariaDB-client MariaDB-backup -y
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