在Docker中搭建LNMP環境,需要以下步驟:
安裝Docker和Docker Compose:根據操作系統的不同,安裝Docker和Docker Compose。
創建Docker Compose文件:在項目目錄下創建一個docker-compose.yml文件,并添加以下內容:
version: '3'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d
- ./html:/usr/share/nginx/html
depends_on:
- php
php:
image: php:7.4-fpm
volumes:
- ./php:/var/www/html
mysql:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_password
MYSQL_DATABASE: your_database
MYSQL_USER: your_user
MYSQL_PASSWORD: your_password
volumes:
- ./mysql:/var/lib/mysql
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}
創建PHP代碼文件夾:在項目目錄下創建php文件夾,并將PHP代碼放置在其中。
啟動LNMP環境:在項目目錄下運行以下命令啟動LNMP環境:
docker-compose up -d
注意:在以上步驟中,需要根據實際情況修改配置文件中的相關參數,如數據庫密碼、數據庫名稱、網站域名等。