RHEL7: How to install a Nginx & PHP service.

Share this link

Presentation

Nginx is one of the best webserver available today.
It consumes less resources (CPU, memory) but processes more requests simultaneously than most of  its competitors.
Because of a completely different mindset, you will have to learn the Nginx syntax from scratch except if you love regular expressions.

As Nginx doesn’t provide any PHP features, you will have to use the PHP-FPM solution, an alternative PHP FastCGI implementation.

Configuration Procedure

To install a Nginx & PHP webserver, execute the following steps:

Install Remi’s repository.

Enable the remi (if it’s not already done) and remi-php56 repositories:

# yum-config-manager --enable remi --enable remi-php56

Install the nginx and php-fpm packages:

# yum install -y nginx php-fpm

Install the php-opcache package to get good performances (mandatory):

# yum install -y php-opcache

Activate at boot time and start the nginx and php-fpm services:

# systemctl enable nginx && systemctl start nginx 
# systemctl enable php-fpm && systemctl start php-fpm

Add the HTTP service to the firewall configuration and reload it:

# firewall-cmd --permanent --add-service=http
Success
# firewall-cmd --reload
Success

The PHP configuration file is /etc/php.ini.
You should define cgi.fix_pathinfo=0 for security reasons and date.timezone to your default timezone (for example date.timezone=”America/New_York”).

The php-fpm configuration files are /etc/php-fpm.conf (for things which shouldn’t need to change) and /etc/php-fpm.d/www.conf (for things which need adjustment).

The Nginx configuration file is /etc/nginx/nginx.conf.

Here is a basic example of Nginx configuration file:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /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;
   keepalive_timeout  65;

   server {
      listen       80 default_server;
      server_name  localhost;
      root         /var/www/html/mywebsite;
      index       index.html index.htm index.php

      location / {
           try_files $uri $uri/ /index.php?$args;
      }
      error_page  404              /404.html;
      location = /40x.html {
      }

      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
      }

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      location ~ \.php$ {
         root           /var/www/html/mywebsite;
         try_files $uri =404;
         fastcgi_pass    127.0.0.1:9000;
         fastcgi_index    index.php;
         include              fastcgi_params;
         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
   }
}

Note: Don’t forget to set the SELinux context:
# mkdir /var/www/html/mywebsite; restorecon -R /var/www/html/mywebsite

If you are migrating from RHEL 6 to RHEL 7, you could need to enable the httpd_unified boolean:

# setsebool -P httpd_unified 1

Also, if you use NFS, remember that:
In RHEL 7, the same SELinux policies that apply to Apache also apply to Nginx. So you can use the same booleans:

httpd_use_nfs (off , off) Allow httpd to use nfs

Set the correct boolean to allow the web server to use NFS.

# setsebool -P httpd_use_nfs 1

Source: serverfault thread about Nginx and NFS.

Additional Resources

Karl Johnson provides a Nginx full-featured solution called Nginx-more.
DigitalOcean offers a tutorial about the Nginx log module.
The Hostinger website provides a tutorial on How to Install Nginx, MySQL, PHP v7 (LEMP) stack on CentOS 7.
If you want to enable http/2 available with RHEL 7.4/CentOS 7.4, follow the tutorial about restoring http/2 fonctionality on Nginx.

(No Ratings Yet)
Loading...

Leave a Reply

Upcoming Events (Local Time)

There are no events.

Follow me on Twitter

Archives

vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |