Install Nginx/PHP-FPM on Fedora 24/23, CentOS/RHEL 7.2/6.8/5.11

nginx-logoThis is guide howto install Nginx with PHP 7.0.8 and PHP-FPM on Fedora 24/23/22/21/20, CentOS 7.2/6.8/5.11 and Red Hat (RHEL) 7.2/6.8/5.11. PHP-FPM is easier to use and configure than FastCGI and some tests says that PHP-FPM is even much more efficient than FastCGI.

Nginx (engine x) is a robust, small and high performance http server, reverse proxy server and also mail proxy server.

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

1. Install Nginx, PHP 7.0.8 and PHP-FPM on Fedora 24/23/22/21/20, CentOS 7.2/6.8/5.11, Red Hat (RHEL) 7.2/6.8/5.11

1.1 Change to root user.

1.2 Install needed repositories

Fedora 24/23/22/21/20 Remi repository

CentOS 7.2/6.8/5.11 and Red Hat (RHEL) 7.2/6.8/5.11 Remi repository

CentOS 7.2/6.8/5.11 and Red Hat (RHEL) 7.2/6.8/5.11 Nginx repository

Create file /etc/yum.repos.d/nginx.repo and add following content to repo file:
CentOS

Red Hat (RHEL)

1.3 Install Nginx, PHP 7.0.8 and PHP-FPM

Fedora 24/23/22

Fedora 21

Fedora 20

CentOS 7.2/6.8/5.11 and Red Hat (RHEL) 7.2/6.8/5.11

1.4 Install PHP 7.0.8 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

More info about PHP APC from PHP APC Configuration and Usage Tips and Tricks

Fedora 24/23/22

Fedora 21

Fedora 20

CentOS 7.2/6.8/5.11 and Red Hat (RHEL) 7.2/6.8/5.11

1.5 Stop httpd (Apache) server, Start Nginx HTTP server and PHP-FPM (FastCGI Process Manager)

Stop httpd (Apache)

Start Nginx

Start PHP-FPM

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

Autostart PHP-FPM on boot

1.7 Configure Nginx and PHP-FPM

Create directory layout for your site

I use here testsite.local site, but this could of course be your real site, like www.if-not-true-then-false.com.

Alternative setup to add logs under /var/log directory.

Note: I use apache user and group here, because PHP-FPM runs as apache default (apache choosed to be able to access some dir as httpd). If you use some other user on your php-fpm conf then change this accordingly.

Create and configure virtual host directories under /etc/nginx

Add following lines to /etc/nginx/nginx.conf file, after “include /etc/nginx/conf.d/*.conf” line (inside http block).

Create testsite.local virtual host file

Add following content to /etc/nginx/sites-available/testsite.local file. This is very basic virtual host config.

Link your virtual host to /etc/nginx/sites-enabled

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

/etc/hosts file Nginx on same machine
127.0.0.1 … row should look like example following:

And if you use another machine where you are running your Nginx server, then add it’s public IP as following:

Note: This is just very simple basic configuration, but if you want configure and optimize Nginx and PHP-FPM more then check following guide, Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks

1.8 Test your Nginx and PHP-FPM setup

Create /srv/www/testsite.local/public_html/index.php file with following content:

Access following address, with your browser. http://testsite.local/
CentOS 7 Running Nginx and PHP-FPM 5.6.0

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

2. Enable Remote Connection to Nginx Web Server (Open Port 80 on Iptables Firewall)

2.1. CentOS/Red Hat (RHEL) 6.8/5.11

2.1.1 Edit /etc/sysconfig/iptables file:

2.1.2 Add following INPUT rule:

2.1.3 Restart Iptables Firewall:

2.2. Fedora 24/23/22/21/20 and CentOS/Red Hat (RHEL) 7.2

2.2.1 List Your Active Firewalld Zones

Example output:

2.2.2 Add New Rule to Firewalld

You might have active zone like public, FedoraWorkstation, FedoraServer.

2.2.3 Restart firewalld.service

3. Test remote connection

Access following address, with your browser. http://your.domain/

Share this post

195 Comments

    • Hi bob,

      There is not nginx own repo for Fedora (as this guide says, it’s only for CentOS and Red Hat), please remove that repo file and use just yum.

      Reply
  1. Dear fried,
    I have follow your instructions in CENTOS 6.5 and i have install nginx ok but the php it was 5.3.3 ?!?
    Is there something wrong with the remi repo ??

    Reply
    • Hi Dimitrios,

      Did you enabled remi and remi-php55 repos? Like:

      Reply
  2. Hi JR,

    Thanks for the post. I am new to the web server world, may I raise a question?
    Does php-fpm works with apache? What did you choose nginx?

    Reply
    • Hi dpdh,

      You are welcome.

      PHP-FPM works with Apache 2.4.x using mod_proxy_fcgi module. Then you don’t need suphp, suexec or mod_php.

      If you want run PHP with nginx, then PHP-FPM is only reasonable option. Nginx is much lighter and powerful web server than Apache so that’s why this guide exists.

      Reply
  3. Since you explicitly set the document root in the server clause, you can simplify the following line in the testsite.local virtual host file:

    with

    Reply
  4. Hi JR,

    I need help when i goto install php-fpm it give me

    gpg key retrieval failed errno 14 could not open/read file ///etc/pki/rpm-gpg/rpm-gpg-key-remi

    How to install it. It Fresh Server.

    Reply
    • my mistake, i dropped repo files in new server. That is why it giving that error

      Reply
  5. thanks a lot

    Reply
  6. Thanks for the Share . Good Work

    Reply
  7. I am unable to understand that testsite.local. i am using a vps from ovh. where i need to use my server ipv4 ? and if my domain is xyz.com and ip is xxx.xxx.xxx.xxx then how i can config my virtual host file? where is virtual host located?

    Reply
    • Hi bdspice,

      This testsite.local is just an example which works on localhost if you don’t have real domain. Same setup works for real domain, but you don’t have to do any changes on /etc/hosts file.

      If you have domain xyz.com, then setup is almost same, you have to of course replace directory names and change dns records to point right place and open port 80 on firewall.

      Please read guide, but here is quick steps:

      1. Point your domain xyz.com dns records to your ip.

      2. Create directories like
      /srv/www/xyz.com/public_html
      /srv/www/xyz.com/logs

      3. Create Virtualhost /etc/nginx/sites-available/xyz.com:

      4. “Enable” your virtualhost

      5. Restart (or reload) your nginx service

      6. Open firewall port

      Reply
      • Its working but i have face another big problem.i install nginx,mariadb,php-fpm,phpmyadmin.i config my domain (faceur.com) successfully. i try to install a laraval based script (ninja media script).but its show storage/session folder permission problem.then i changed whole www folder group and permission.but then i am not able to get it work and i quit.then i try to work a simple php based script.But its showing all php code as text on browser.Do i need to install apache also to run a php based script???

        Reply
        • No, you don’t need Apache. PHP-FPM is enough to run php scripts.

          This might be some configuration error. Could you post full code of your simple script which is not working?

          Reply
  8. https://www.mirrorcreator.com/files/8VRQRYVC/vs.zip_links
    here is the link of my script.i think there is some problem in configuration.Now in server,the ninja media script is up.but i cant run it.while its a working script. i try the same script on same server on webmin.but webmin use more ram that i cant effort.if you want config files.just tell me which config files you need.i will provide you soon.

    Reply
  9. Thanks for the post!!!

    Reply
  10. How to make it work with PHP 7

    Reply
  11. Hai JR
    Great tutorial. You just might want to add, that it might be necessary to edit [i]/etc/php-fpm.d/www.conf[/i] in case of a 502 Bad Gateway error.

    It should listen to the same values as you give for fastcgi_pass in the available site config :)

    – UltraG33k

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *