在Docker中安装LNMP(Linux, Nginx, MySQL, PHP)环境的方法如下:
1、安装Docker
对于Ubuntu/Debian系统,使用以下命令安装Docker:
“`
sudo apt update
sudo apt install dockerce dockercecli containerd.io
“`
对于CentOS/RHEL系统,使用以下命令安装Docker:
“`
sudo yum install y yumutils devicemapperpersistentdata lvm2
sudo yumconfigmanager addrepo https://download.docker.com/linux/centos/dockerce.repo
sudo yum install dockerce dockercecli containerd.io
“`
启动Docker服务:
“`
sudo systemctl start docker
sudo systemctl enable docker
“`
2、拉取LNMP镜像
“`
sudo docker pull daocloud.io/library/nginx:1.21
sudo docker pull mysql:5.7
sudo docker pull php:7.4fpm
“`
3、创建网络和数据卷
“`
sudo docker network create lnmp_network
sudo docker volume create mysql_data
“`
4、运行MySQL容器
“`
sudo docker run d name mysql
network lnmp_network
e MYSQL_ROOT_PASSWORD=mysecretpw
v mysql_data:/var/lib/mysql
mysql:5.7
“`
5、运行Nginx容器
“`
sudo docker run d name nginx
network lnmp_network
p 80:80
e TZ=Asia/Shanghai
v /etc/localtime:/etc/localtime
v /path/to/html:/usr/share/nginx/html
nginx:1.21
“`
6、运行PHP容器
“`
sudo docker run d name php
network lnmp_network
e TZ=Asia/Shanghai
v /etc/localtime:/etc/localtime
v /path/to/php/conf.d:/usr/local/etc/php/conf.d
v /path/to/php/www.conf:/usr/local/etc/phpfpm.d/www.conf
v /path/to/php/logs:/usr/local/var/log
php:7.4fpm
“`
7、配置Nginx容器与PHP容器通信
编辑Nginx容器的nginx.conf文件,添加以下内容:
“`
location ~ .php$ {
fastcgi_pass unix:/run/php/php7.4fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
“`
重启Nginx容器:
“`
sudo docker restart nginx
“`
至此,LNMP环境已经在Docker中安装完成,你可以通过访问Nginx容器的IP地址和端口来查看网站。