Install LAMP with PHP 7.4 on FastCGI/CGI configuration on CentOS 7

Step 1 – Setup Yum Repository

In the first step install all the required yum repositories in your system used in the remaining tutorial for various installations. You are adding REMI, EPEL, Webtatic & MySQL community server repositories in your system.

CentOS / RHEL 7

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm -Uvh http://repo.mysql.com/mysql-community-release-el7-7.noarch.rpm

Step 2 – Install PHP 7.4

Now install php 7 packages from webtatic rpm repository using following command.

yum --enablerepo=remi-php74 install php

Now install required php modules. Use following command to list available modules in yum repositories.

yum --enablerepo=remi-php74 search php

Now check all listed modules in above command and install required modules like below.

yum --enablerepo=remi-php74 install php-mysql php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt

For additonal php function to install use the command syntax below-

yum --enablerepo=remi-php74 install <your-php-funciton>

Step 3 – Install Apache 2.4

Apache (HTTPD) is the most popular web server used on Linux systems. Let’s install Apache web server using following command with enabling epel and remi yum repositories.

yum --enablerepo=epel,remi install httpd

Now start httpd service and enable to start on boot using below commands.

systemctl start httpd.service
systemctl enable httpd.service

Step 4 – Install MySQL 5.6

In step 1 we already have installed required yum repository in your system. Let’s use the following command to install MySQL server on your system. If you want to install MySQL 5.7 visit this tutorial.

yum install mysql-server

Apply security on mysql and also set root user password.

systemctl start mysqld.service
mysql_secure_installation

Now restart MySQL service and enable to start on system boot.

systemctl restart mysqld.service
systemctl enable mysqld.service

Step 5 – Open Port in Firewall

Finally open firewall ports for HTTP (80) and https (443) services using the following command.

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

Step 6 – Check Installed Version

Let’s check the installed versions of packages on system using following commands one by one.

php -v
PHP 7.2.0 (cli) (built: Nov 28 2017 20:22:21) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Nov 19 2015 21:43:13
mysql -V
mysql Ver 14.14 Distrib 5.6.28, for Linux (x86_64) using EditLine wrapper

Now you have successfully configured LAMP setup on your CentOS / RHEL 7.4 & 6.9 systems.

Step 7 – Install PHP and FastCGI

After installing the Apache web server, let’s install PHP and FastCGI Apache module on your system. You can install any version of the required PHP or simply use the following command to install available PHP packages. This tutorial doesn’t include installing PHP modules, So you can also install required PHP modules.

yum --enablerepo=remi-php74 install php php-cli mod_fcgid

Step 8 – Disable Default PHP Handler

Before using PHP/FastCGI handler, you have to disable the default PHP handler on your system. Edit PHP configuration file for Apache (/etc/httpd/conf.d/php.conf) in your favorite text editor and comment following lines showing in below screenshot by adding the hash (#) sign at the start of the lines.

Step 9 – Setup FastCGI Handler

At this point we have successfully installed Apache FastCGI Module. Now nagigate to /var/www/cgi-bin directory, If not exists create directory. Then create a php.fastcgi file and add the following content to this file. Also make sure the php.ini file and php-cgi exist on your system.

vim /var/www/cgi-bin/php.fastcgi
#!/bin/bash

PHPRC="/etc/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php-cgi

Change permissions of php.fastcgi script to make it executable by Apache server.

chown apache:apache /var/www/cgi-bin/php.fastcgi
chmod +x /var/www/cgi-bin/php.fastcgi

Step 10 – Setup VirtualHost with FastCGI

Finally, create a VirtualHost in our Apache configuration file with FastCGI support. VirtualHosts are used to configure multiple sites with a single IP. Below configuration will allow siting svr1.tecadmin.net with any system IP on port 80.

<VirtualHost *:80>
ServerName svr1.tecadmin.net
ServerAdmin admin@tecadmin.net
DocumentRoot /var/www/html
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/html">
Options +Indexes +FollowSymLinks +ExecCGI
AddHandler php-fastcgi .php
Action php-fastcgi /cgi-bin/php.fastcgi
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>

Step 11 – Restart Apache and Test Setup

At this point, you have completed the Apache configuration with FastCGI support. Let’s restart the Apache server using the following command.

service httpd restart

Now create a file in your document root /var/www/html/info.php and add following content to check detailed php information.

<?php
  phpinfo();
?>

Access your Apache server using an IP address for domain name followed by php.info file in your web browser like below. This will show the current configuration of PHP in your system. Look the value of Server API option, if you get this value CGI/FastCGI, it means server is properly configured to use FastCGI.

Src:

https://tecadmin.net/setup-httpd-with-fastcgi-and-php-on-centos-redhat/
https://tecadmin.net/install-php-7-apache-2-4-mysql-on-centos-rhel/

Share

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.