Nginx+PHP ( Rockylinux Docker host )

Nginx နဲ့ PHP setup ပြုလုပ်နည်းကို Docker Host ဖြစ်တဲ့ RockyLinux မှာ အဆင့်အဆင့် setup လုပ်မှာဖြစ်ပါတယ်။

Step 1: Install Docker on Rocky Linux 9

  1. Update your system:
   sudo dnf update -y
  1. Install required packages:
   sudo dnf install -y yum-utils
  1. Add Docker repository:
   sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  1. Install Docker:
   sudo dnf install -y docker-ce docker-ce-cli containerd.io
  1. Start and enable Docker:
   sudo systemctl start docker
   sudo systemctl enable docker
  1. Verify Docker is running:
   sudo systemctl status docker
  1. Add your user to the docker group (optional, for non-root access):
   sudo usermod -aG docker $USER
  1. Logout and login again to apply the group changes, or use newgrp docker.

Step 1.1: Install Docker Compose

  1. Download Docker Compose:
   sudo curl -L "https://github.com/docker/compose/releases/download/v2.21.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. Set the correct permissions:
   sudo chmod +x /usr/local/bin/docker-compose
  1. Verify the installation:
   docker-compose --version

Step 2: Create a Docker Compose File

Create a directory for your project and navigate to it:

mkdir nginx-php-docker
cd nginx-php-docker

Now, create a docker-compose.yml file inside the directory:

touch docker-compose.yml
nano docker-compose.yml

Add the following configuration to the docker-compose.yml file:

version: '3.8'

services:
  nginx:
    image: nginx:latest
    container_name: nginx_container
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./php_app:/var/www/html
    depends_on:
      - php
    networks:
      - mynetwork

  php:
    image: php:7.4-fpm
    container_name: php_container
    volumes:
      - ./php_app:/var/www/html
    networks:
      - mynetwork

networks:
  mynetwork:
    driver: bridge

Step 3: Create Nginx Configuration

Create an Nginx configuration file to link Nginx with PHP-FPM. Inside the same directory, create a file named nginx.conf:

touch nginx.conf
nano nginx.conf

Add the following Nginx configuration:

events {}

http {
    server {
        listen 80;
        server_name localhost;

        root /var/www/html;
        index index.php index.html index.htm;

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

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

        location ~ /\.ht {
            deny all;
        }
    }
}

Step 4: Create a PHP Application

Create a simple PHP application. Inside the same directory, create a folder named php_app:

mkdir php_app

Inside the php_app folder, create a file named index.php:

touch php_app/index.php
nano php_app/index.php

Add the following PHP code:

<?php
phpinfo();
?>

Step 5: Start Docker Compose

Now, run the following command to start the containers:

docker-compose up -d

This will download the Nginx and PHP-FPM images, create the containers, and start them.

Step 6: Access the Application

Open a web browser and go to http://localhost:8080 or http://your-server-ip:8080. You should see the PHP info page.

Summary of the Setup:

  • Nginx is running on port 8080 on your host.
  • PHP-FPM is running in a separate container and Nginx communicates with PHP through the Docker network using the fastcgi_pass directive.

Thanks for reading …

Leave a comment