Level 58
Sure! Here's a basic Docker setup for PHP applications using PHP, NGINX, and MySQL:
- Create a new directory for your project and navigate into it:
mkdir my-php-app
cd my-php-app
- Create a
Dockerfilein the project directory:
touch Dockerfile
- Open the
Dockerfileand add the following content:
FROM php:7.4-fpm
# Install dependencies
RUN apt-get update && apt-get install -y \
git \
unzip \
libzip-dev \
libpng-dev \
libonig-dev \
libxml2-dev
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql zip gd mbstring exif pcntl bcmath
# Set working directory
WORKDIR /var/www/html
# Copy application files
COPY . .
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install project dependencies
RUN composer install --no-interaction --no-scripts --no-suggest
# Set up NGINX
FROM nginx:alpine
# Copy NGINX configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Expose port 80
EXPOSE 80
- Create an
nginx.conffile in the project directory:
touch nginx.conf
- Open the
nginx.conffile and add the following content:
server {
listen 80;
index index.php index.html;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- Build the Docker image:
docker build -t my-php-app .
- Run the Docker container:
docker run -p 8000:80 my-php-app
Now you should be able to access your PHP application at http://localhost:8000.
Note: This is a basic setup and may need to be adjusted based on your specific requirements.
1 like