在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
注意:在以上步骤中,需要根据实际情况修改配置文件中的相关参数,如数据库密码、数据库名称、网站域名等。