nginx 1.4 on Red Hat Enterprise Linux 7

In this post I’m going to talk about using the nginx web server on Red Hat Enterprise Linux 7.  nginx 1.4 was added as a “Tech Preview” in the v1.1 release of Red Hat Software Collections.

nginx

Starting from a freshly kickstarted RHEL7 VM, here’s how to get going:

[root@virt-el7scratch ~]# subscription-manager repos --enable rhel-server-rhscl-7-rpms
Repo 'rhel-server-rhscl-7-rpms' is enabled for this system.

This enables the Red Hat Software Collections repository in the yum configuration, which is available with most RHEL entitlements.

[root@virt-el7scratch ~]# yum install nginx14
...
Installed:
  nginx14.x86_64 0:1.1-3.el7
Dependency Installed:
  gd.x86_64 0:2.0.35-26.el7 nginx14-nginx.x86_64 1:1.4.4-10.el7
  nginx14-runtime.x86_64 0:1.1-3.el7 scl-utils.x86_64 0:20130529-5.el7

Complete!

That has installed the nginx 1.4 software collection. Like all SCLs, nginx ends up in /opt/rh – specifically /opt/rh/nginx14, but we have a systemd unit file in the correct system location for integration:

[root@virt-el7scratch ~]# systemctl start nginx14-nginx
[root@virt-el7scratch ~]# systemctl status nginx14-nginx
nginx14-nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx14-nginx.service; disabled)
Active: active (running) since Thu 2014-10-09 12:28:39 BST; 47s ago
Process: 10346 ExecStart=/opt/rh/nginx14/root/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 10345 ExecStartPre=/opt/rh/nginx14/root/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 10348 (nginx)
CGroup: /system.slice/nginx14-nginx.service
        ├─10348 nginx: master process /opt/rh/nginx14/root/usr/sbin/nginx
        └─10350 nginx: worker process
...

The nginx daemon is running out of the SCL like any other systemd service. Now, I could demo a static page load or a proxy configuration here… but dynamic languages are more fun! Instead I’ll pull in another software collection and demo PHP:

To use PHP from nginx we use FastCGI. So, I’ll pull in the FastCGI Process Manager package, “FPM” from the PHP 5.5 software collection, and start that daemon:

[root@virt-el7scratch ~]# yum install php55-php-fpm
...
Installed:
  php55-php-fpm.x86_64 0:5.5.6-10.el7

Dependency Installed:
  libzip.x86_64 0:0.10.1-8.el7 php55-php-cli.x86_64 0:5.5.6-10.el7
  php55-php-common.x86_64 0:5.5.6-10.el7 php55-php-pear.noarch 1:1.9.4-10.el7
  php55-php-pecl-jsonc.x86_64 0:1.3.5-1.el7 php55-php-process.x86_64 0:5.5.6-10.el7
  php55-php-xml.x86_64 0:5.5.6-10.el7 php55-runtime.x86_64 0:1.1-7.el7

[root@virt-el7scratch ~]# systemctl start php55-php-fpm
[root@virt-el7scratch ~]# systemctl status php55-php-fpm
php55-php-fpm.service - The PHP FastCGI Process Manager
  Loaded: loaded (/usr/lib/systemd/system/php55-php-fpm.service; disabled)
  Active: active (running) since Thu 2014-10-09 12:45:00 BST; 1s ago
  Main PID: 10634 (php-fpm)
  Status: "Ready to handle connections"
  CGroup: /system.slice/php55-php-fpm.service
          ├─10634 php-fpm: master process (/opt/rh/php55/root/etc/php-fpm.conf)
          ├─10635 php-fpm: pool www
...

To configure nginx to use FastCGI, we edit the /opt/rh/nginx14/root/etc/nginx/nginx.conf configuration file. There is already a commented-out example for use of FastCGI in the default “http” (port 80) server stanza. Uncomment it and change the SCRIPT_FILENAME parameter to pull straight out of the document root:

     location ~ .php$ {
              root html;
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }

And that’s all the configuration that is required! Reload the daemon, and we’re off:

[root@virt-el7scratch ~]# systemctl reload nginx14-nginx
[root@virt-el7scratch ~]# echo '<?php echo "Hello, worldn"; ?>' > /opt/rh/nginx14/root/usr/share/nginx/html/hello.php
[root@virt-el7scratch ~]# curl http://localhost/hello.php
Hello, world

Note that the default document root for nginx is inside the SCL, though this can be changed by amending the “root” directive in nginx.conf.


Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.

 

Take advantage of your Red Hat Developers membership and download RHEL today at no cost.

Share