Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
NGINX HTTP Server

You're reading from   NGINX HTTP Server Harness the power of NGINX with a series of detailed tutorials and real-life examples

Arrow left icon
Product type Paperback
Published in May 2024
Publisher Packt
ISBN-13 9781835469873
Length 262 pages
Edition 5th Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Martin Bjerretoft Fjordvald Martin Bjerretoft Fjordvald
Author Profile Icon Martin Bjerretoft Fjordvald
Martin Bjerretoft Fjordvald
Gabriel Ouiran Gabriel Ouiran
Author Profile Icon Gabriel Ouiran
Gabriel Ouiran
Mr. Clement Nedelcu Mr. Clement Nedelcu
Author Profile Icon Mr. Clement Nedelcu
Mr. Clement Nedelcu
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Part 1:Begin with NGINX FREE CHAPTER
2. Chapter 1: Downloading and Installing NGINX 3. Chapter 2: Basic NGINX Configuration 4. Part 2: Dive into NGINX
5. Chapter 3: Exploring the HTTP Configuration 6. Chapter 4: Exploring Module Configuration in NGINX 7. Chapter 5: PHP and Python with NGINX 8. Chapter 6: NGINX as a Reverse Proxy 9. Part 3: NGINX in Action
10. Chapter 7: Introduction to Load Balancing and Optimization 11. Chapter 8: NGINX within a Cloud Infrastructure 12. Chapter 9: Fully Deploy, Manage, and Auto-Update NGINX with Ansible 13. Chapter 10: Case Studies 14. Chapter 11: Troubleshooting 15. Index 16. Other Books You May Enjoy

Setting up NGINX inside Docker

Having become familiar with Docker Compose, we’re set to advance our container usage. In this section, we’ll improve our NGINX container by adding personalized configurations and website content.

Let’s begin by revisiting the docker-compose.yml file we created earlier. This file already specifies the NGINX service and maps the container’s port 80 to port 80 on the host machine, making the web server accessible from the host.

Next, we want our NGINX container to serve our website using the actual configuration files and content from our host machine. To achieve this, we’ll use Docker volumes. Volumes are the preferred mechanism for persisting data generated and used by Docker containers. Here’s how you can modify the docker-compose.yml file to mount a volume:

version: '3'
services:
  nginx:
    image: nginx:latest
    ports:
      - «80:80»
    volumes:
      - ./config/nginx.conf:/etc/nginx/nginx.conf
      - ./html:/usr/share/nginx/html

In this setup, ./config/nginx.conf is the path to your custom NGINX configuration file on your host machine. /etc/nginx/nginx.conf is the path where the NGINX container expects to find the configuration file. The second volume, ./html, is the directory on your host machine that contains your website’s content, mounted into /usr/share/nginx/html/ in the Docker container.

This approach ensures that you can edit your NGINX configuration or website content directly on your host machine, and those changes will be reflected inside the container.

A note

NGINX is preconfigured; therefore, you can skip mounting the nginx.conf volume and use the default settings. Do mount the html directory as it is where NGINX expects to find web content to serve.

Integrating PHP with NGINX using Docker Compose

As we progress, our next step is to elevate our NGINX server to handle dynamic content with PHP. This combination is common in the web development world, and Docker Compose makes it easy to implement.

We’ll start by extending our existing docker-compose.yml file to include a PHP service, and then we will ensure NGINX can communicate with the PHP service using FastCGI:

  1. First, we need to add a PHP service that runs FastCGI Process Manager (php-fpm). Here’s how the updated docker-compose.yml file might look:
    version: '3'
    services:
      nginx:
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - ./config/nginx.conf:/etc/nginx/nginx.conf
          - ./html:/usr/share/nginx/html
        depends_on:
          - php
      php:
        image: php:8.3-fpm
        volumes:
          - ./html:/usr/share/nginx/html

    In this setup, the php service uses the official PHP image with php-fpm. We also mount the html directory into the PHP container to ensure it has access to the same web files as NGINX.

  2. Before starting the Docker containers, we need to make sure NGINX and PHP communicate. Let’s edit the nginx.conf file to include FastCGI parameters. Here’s an example of nginx.conf with included FastCGI parameters:
    events {}
    http {
      server {
        listen 80;
        server_name localhost;
        root /usr/share/nginx/html;
        index index.php index.html index.htm;
        location / {
            try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
          try_files $uri =404;
          fastcgi_pass php:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
        }
      }
    }

    This configuration instructs NGINX to forward requests for PHP files to the PHP-FPM service running in the php container.

Note

NGINX resolves the php service name using Docker’s internal DNS to communicate with the php-fpm service. If you rename the service in your docker-compose.yml file, remember to update the nginx.conf file accordingly to match the new service name.

  1. With this docker-compose.yml and nginx.conf configuration, you can run docker compose up, and NGINX will serve both your static content and your dynamic PHP pages:
    root@nginx:~/nginx# docker compose up
    [+] Running 2/0
      Container docker-php-1    Created
      Container docker-nginx-1  Created
    Attaching to nginx-1, php-1
    php-1 [02-Mar-2024 11:42:30] NOTICE: fpm is running, pid 1
  2. After launching your containers, to check that NGINX and PHP are interacting correctly, create a PHP file containing <?php phpinfo(); ?>. This will display a PHP information page when accessed, confirming successful communication between the containers:
Figure 8.4: phpinfo running within Docker

Figure 8.4: phpinfo running within Docker

This subsection has guided you through adding a PHP service to your Docker environment and configuring NGINX to process PHP scripts via FastCGI. This configuration mimics a production environment, providing a solid foundation for building and deploying PHP applications with Docker.

In the next section, we’ll explore how to configure a Docker-contained NGINX to proxy applications running directly on the host server.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image