Install LEMP (Linux, Nginx, MySQL And PHP) Stack On Red Hat Linux

Exported on 28-Aug-2021 13:35:06

Install LEMP (Linux, Nginx, MySQL and PHP) On Red Hat Server With Attune Automation

This Blueprint Install LEMP Stack Components On A Red Hat Server And Creates A PHP Info File To Verify The Installation
Pre-Blueprint Attune setup
  1. On the Inputs tab, create a Linux node for the host you wish to install the stack on.
  2. On the Inputs tab, create Linux credentials to connect to the host you wish to install the stack on.

Parameters

Name Type Script Reference Default Value Comment
server_credential Linux OS Credential server_credential
server_host Linux / Unix Server server_host

1 - Install Prerequisite For Rhel

We will being by installing epel-release respository for this automation task. This makes Nginx package available to be installed by Yum.

The connection details have changed from the last step.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
# Update yum repository
sudo yum -y update

# Install epel repository
sudo yum -y install epel-release

2 - Install Nginx Web Server

This step installs Nginx web server and configures it to launch at system reboot. Nginx is a light weight web server used in production by Enterprises.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
# Install dependencies for Rhel
sudo yum -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/centos-indexhtml-7-9.el7.centos.noarch.rpm
sudo yum -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/gperftools-libs-2.6.1-1.el7.x86_64.rpm

#Install nginx
sudo yum -y install nginx

# Start nginx server
sudo systemctl start nginx 

# Enable nginx server
sudo systemctl enable nginx

3 - Install MySQL Server

Installs MySQL database server

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
# Download MySQL server rpm
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

# Install MySQL Server rpm
sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm

# Download and start mysql server
sudo yum -y install mysql-server

4 - Install PHP v7.4 and FPM

Installs PHP version 7.4 and PHP-FPM package which is used as the php interpreter for Nginx.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
# Install PHP remi repository
sudo yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

# Enable remi repository
sudo yum-config-manager --enable remi-php74

#Install dependencies
sudo yum -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/apr-1.4.8-7.el7.x86_64.rpm
sudo yum -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/apr-util-1.5.2-6.el7.x86_64.rpm
sudo yum -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/mailcap-2.1.41-2.el7.noarch.rpm
sudo yum -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/httpd-tools-2.4.6-95.el7.centos.x86_64.rpm
sudo yum -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/httpd-2.4.6-95.el7.centos.x86_64.rpm

# Install requiered php plugins 
sudo yum -y install php php-mysqlnd php-fpm

# Starts php-fpm service
sudo systemctl start php-fpm 

# Configures php-fpm to launch at reboot
sudo systemctl enable php-fpm

# Start nginx
sudo systemctl start nginx

5 - Upload nginx config file

Uploads PHP configuration file, this enable nginx to support php execution with the help of php-fpm socket.

Login as user on node

Connect via SSH
ssh user@hostname
Deploy archive nginx-php.conf.zip to remote path uploads
  1. Locate Files archive "nginx-php.conf.zip", This can be downloaded from Attune
  2. Copy the Files archive to the server
  3. Extract the root of the Files archive to uploads, relative to the home directory
  4. Check that the files are in the correct location

6 - Uploads php-fpm configuration file

Uploads php-fpm configuration files, this sets correct FPM parameters for the LEMP stack environment.

Login as user on node

Connect via SSH
ssh user@hostname
Deploy archive php-fpm-www.conf.zip to remote path uploads
  1. Locate Files archive "php-fpm-www.conf.zip", This can be downloaded from Attune
  2. Copy the Files archive to the server
  3. Extract the root of the Files archive to uploads, relative to the home directory
  4. Check that the files are in the correct location

7 - Configure Firewall To Allow HTTP Traffic

Allows port 80 for HTTP traffic on the target machine

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
# Add firewall rule
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent

# Reload firewall
sudo firewall-cmd --reload

8 - Restart Nginx & Verify PHP installation

Finally, move the configuration files to their respective directories and restart the servers. To test that PHP is working fine with Nginx, a info.php file is created in the root directory of the server.

Visit http://YOUR_IP/info.php , once this step has been completed to verify the installation.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
# Replace nginx default config file
sudo cp ~/uploads/nginx.conf /etc/nginx/nginx.conf

# Replace fpm configuration file
sudo cp ~/uploads/www.conf /etc/php-fpm.d/www.conf

# Restart nginx and fpm
sudo service nginx restart
sudo service php-fpm restart

# Creates info.php file 
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php