22 C
Texas
Melhttp://www.osradar.com
Guru Unix /Linux and GNU supporter

Install Nginx with PHP 7.2.10 and PHP-FPM on Fedora 29 and RHEL/Centos 7.6

In this  tutorial we will show you how to install Nginx in Combination with last php stable version and included  PHP-FPM (FastCGI Process Manager)

i will not explain about nginx, i assume that everyone knows that very perfect web server and even better than apache

My Servers  is  Centos 7  with   IP : 192.168.2.98

Client is  Debian 9    with IP  : 192.168.2.98

- Advertisement -

Lets  start :

A.1 Change to root user.

su -

A.2 Install Needed repositories

Fedora 28/29

##Fedora 29 ##
rpm -Uvh http://rpms.famillecollet.com/fedora/remi-release-29.rpm

## Fedora 28 ##
rpm -Uvh http://rpms.famillecollet.com/fedora/remi-release-28.rpm

CentOS 7.5/6.10 and Red Hat (RHEL) 7.5/6.10 Remi repository

## Remi Dependency on CentOS 7 and Red Hat (RHEL) 7 ##
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

## CentOS 7 and Red Hat (RHEL) 7 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

## Remi Dependency on CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

## CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

CentOS 7.6/6.10 and Red Hat (RHEL) 7.6/6.10  add Nginx repository

Create repo  file /etc/yum.repos.d/nginx.repo :
For CentOS

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Red Hat (RHEL)

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1

A.3 Install Nginx, PHP 7.2.11 and PHP-FPM
Fedora 28

dnf install nginx php-fpm php-common

===============================================================================
Package Arch Version Repository Size
===============================================================================
Installing:
php-fpm x86_64 7.2.11-1.fc29 updates 1.5 M
nginx x86_64 1:1.12.1-14.fc29 fedora 518 k
Installing dependencies:
httpd-filesystem noarch 2.4.37-2.fc29 updates 15 k
php-common x86_64 7.2.11-1.fc29 updates 1.0 M
gperftools-libs x86_64 2.7-3.fc29 fedora 304 k
libunwind x86_64 1.2.1-6.fc29 fedora 61 k
nginx-filesystem noarch 1:1.12.1-14.fc29 fedora 9.6 k
nginx-mimetypes noarch 2.1.48-4.fc29 fedora 20 k

Transaction Summary

Fedora 27/26

dnf --enablerepo=remi --enablerepo=remi-php72 install nginx php-fpm php-common

CentOS 7.6/6.10 and Red Hat (RHEL) 7.6/6.10

yum --enablerepo=remi,remi-php72 install nginx php-fpm php-common

1.4 Install PHP 7.2.11 modules

OPcache (php-opcache) – The Zend OPcache provides faster PHP execution through opcode caching and optimization.
APCu (php-pecl-apcu) – APCu userland caching
CLI (php-cli) – Command-line interface for PHP
PEAR (php-pear) – PHP Extension and Application Repository framework
PDO (php-pdo) – A database access abstraction module for PHP applications
MySQL (php-mysqlnd) – A module for PHP applications that use MySQL databases
PostgreSQL (php-pgsql) – A PostgreSQL database module for PHP
MongoDB (php-pecl-mongodb) – PHP MongoDB database driver
Redis (php-pecl-redis) – Extension for communicating with the Redis key-value store
Memcache (php-pecl-memcache) – Extension to work with the Memcached caching daemon
Memcached (php-pecl-memcached) – Extension to work with the Memcached caching daemon
GD (php-gd) – A module for PHP applications for using the gd graphics library
XML (php-xml) – A module for PHP applications which use XML
MBString (php-mbstring) – A module for PHP applications which need multi-byte string handling
MCrypt (php-mcrypt) – Standard PHP module provides mcrypt library support

Select what you need: OPcache, APCu, CLI, PEAR, PDO, MySQL, PostgreSQL, MongoDB, Memcache, Memcached, GD, MBString, MCrypt, XML

Install  PHP with all Included  Modules
Fedora 29

dnf install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd
php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached 
php-gd php-mbstring php-mcrypt php-xml

Fedora 28

dnf --enablerepo=remi --enablerepo=remi-php72 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

 

CentOS 7.5/6.10 and Red Hat (RHEL) 7.5/6.10

yum --enablerepo=remi,remi-php72 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

 

Check PHP version Installed :

[root@osrdarFedora ]# php --version
PHP 7.2.11 (cli) (built: Oct 9 2018 15:09:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.11, Copyright (c) 1999-2018, by Zend Technologies
[root@osrdarFedora ]#

A.5 Disable and remove  httpd (Apache) server if  installed , Start Nginx HTTP server and PHP-FPM (FastCGI Process Manager)

Stop httpd (Apache)

## Fedora 28/29 and CentOS/RHEL 7.6 ##
systemctl stop httpd.service
systemctl disable httpd.service

## CentOS/RHEL 6.10 ##
service httpd stop
chkconfig httpd off

Start Nginx

## Fedora 28/29 and CentOS/RHEL 7.6 ##
systemctl start nginx.service

## CentOS/RHEL 6.10 ##
service nginx start

Start PHP-FPM

## Fedora 28/29 and CentOS/RHEL 7.5 ##
systemctl start php-fpm.service

## CentOS/RHEL 6.10 ##
/etc/init.d/php-fpm start ## use restart after update
## OR ##
service php-fpm start ## use restart after update

1.6 Autostart Nginx and PHP-FPM on boot, also prevent httpd (Apache) autostarting on boot
Prevent httpd (Apache) autostarting on boot

 

Autostart Nginx on boot

## Fedora 28/29 and CentOS/RHEL 7.6
systemctl enable nginx.service

## CentOS/RHEL 6.10 ##
chkconfig --add nginx
chkconfig --levels 235 nginx on

Autostart PHP-FPM on boot

## Fedora 28/9 and CentOS/RHEL 7.6
systemctl enable php-fpm.service

## CentOS/RHEL 6.10 ##
chkconfig --add php-fpm
chkconfig --levels 235 php-fpm on

A.7 Configure Nginx and PHP-FPM
Latest Fedora Nginx + PHP-FPM builds use custom config, first restore default config

Restore nginx  configs to default

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vi /etc/nginx/nginx.conf

This is how my nginx conf file look like

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;
#gzip on;

include /etc/nginx/conf.d/*.conf;
}

Modify PHP-FPM to listen ip instead of socket ##
vi /etc/php-fpm.d/www.conf

## Change following   Lines##
;listen = /run/php-fpm/www.sock
listen = 127.0.0.1:9000

public_html directory  for site Osradar.local

mkdir -p /var/www/osradar.local

chown -R nginx:nginx /var/www/osradar.local

add logs under /var/log directory.

## public_html directory and logs directory ##
mkdir -p /var/www/osradar.local/
chown -R nginx:nginx /var/www/osradar.local
chown -R nginx:nginx /var/log/nginx

my  Vhost Configuration File

cat  /etc/nginx/conf.d/osradar.local.conf

server {
    listen         80 default_server;
    listen         [::]:80 default_server;
    server_name     osradar.local;
    root           /var/www/osradar.local;
    index          index.html index.php;

    location / {
      try_files $uri $uri/ =404;
    }

    location ~* \.php$ {
      fastcgi_pass 127.0.0.1:9000;
      include         fastcgi_params;
      fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
      fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
}

Start  Nginx

Fedora 28/29 and CentOS/RHEL 7.6 ##
systemctl restart nginx.service 


## CentOS/RHEL 6.10 ##
service nginx restart

Add your testsite.local “domain” to /etc/hosts file

/etc/hosts file Nginx on same machine

127.0.0.1 localhost.localdomain localhost 
192.168.2.98 osradar.local

A.8 Test your Nginx and PHP-FPM setup

Create /var/www/osradar.local/test.php file with following content:

 <?php phpinfo(); ?>

Note:
If you get 403 forbidden error, then you probably have problem with SELinux, then run simply following command:

chcon -R -t httpd_sys_content_t /var/www/osradar.local/

## Or some apps might need httpd_sys_rw_content_t ##

chcon -R -t httpd_sys_rw_content_t /var/www/osradar.local/

B.0  (Open Port 80 on Iptables Firewall)

vi /etc/sysconfig/iptables

Add following INPUT rule:

-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

B.1 Restart Iptables Firewall:

service iptables restart

B.2. Fedora 28/29 and CentOS/Red Hat (RHEL) 7.6

B.2.1 Add New http Rule to Firewalld

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

B.2.2 Restart firewalld.service

systemctl restart firewalld.service

3. Test remote connection

Access following address, with your browser. http://osradar.local/

note: i have already crated index.html with the text showing bellow on /var/www/osradar.local/index.html

Test PHP   conf file http://osradar.local/test.php

Please  let us know if you still have any issues so that we can solve it together. Enjoy

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article