准备工作。
#首先获取nginx配置文件
[root@localhost ~]# docker pull nginx:1.23.3
[root@localhost ~]# docker run --name nginx --restart=always -d -p 80:80 nginx:1.23.3
[root@localhost ~]# mkdir -p /usr/local/nginx/{conf,log,html,conf.d}
#复制配置文件
[root@localhost ~]# docker cp nginx:/etc/nginx/nginx.conf /usr/local/nginx/conf/
[root@localhost ~]# docker cp nginx:/etc/nginx/conf.d /usr/local/nginx/conf/conf.d
[root@localhost ~]# docker cp nginx:/usr/share/nginx/html /usr/local/nginx/
[root@localhost ~]# docker cp nginx:/var/log/nginx /usr/local/nginx/log
[root@localhost ~]# cd /usr/local/nginx/conf/conf.d
[root@localhost ~]# vi default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
}
#删除nginx容器
docker stop nginx
docker rm nginx
部署MySQL
[root@localhost ~]# docker pull mysql:8.0.32
[root@localhost ~]# mkdir -p /usr/local/mysql/{conf,data}
[root@localhost ~]# docker run --name mysql --restart=always -itd -p 3306:3306 -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123123 mysql:8.0.32
部署PHP
[root@localhost ~]# docker pull php:8.1-fpm
[root@localhost ~]# docker run -itd --name php -v /achong/www/:/www --privileged=true --link mysql php:8.1-fpm
部署Nginx
[root@localhost ~]# docker run -itd --name nginx --restart=always -p 80:80 -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/conf.d/default.conf:/etc/nginx/conf.d/defaule.conf -v /usr/local/nginx/log/:/var/log/nginx -v /usr/local/nginx/html/:/usr/share/nginx/html --link php nginx:1.23.3
[root@localhost ~]# cd /achong/www/
[root@localhost www]# vim index.php
<?php
phpinfo();
?>
安装MySQL插件
[root@localhost ~]# docker exec -it php /bin/bash
root@b8c090e40d14:/var/www/html# cd /usr/local/bin
root@b8c090e40d14:/usr/local/bin# ls
docker-php-entrypoint docker-php-ext-enable docker-php-source peardev phar php phpize
docker-php-ext-configure docker-php-ext-install pear pecl phar.phar php-config
root@b8c090e40d14:/usr/local/bin# ./docker-php-ext-install pdo_mysql
root@b8c090e40d14:/usr/local/bin# ./docker-php-ext-install mysqli
root@b8c090e40d14:/usr/local/bin# cd /usr/local/etc/php
root@b8c090e40d14:/usr/local/etc/php# cp php.ini-development php.ini
root@b8c090e40d14:/usr/local/etc/php# exit
[root@localhost ~]# docker cp php:/usr/local/etc/php/php.ini /achong/
Successfully copied 75.3kB to /achong/
[root@localhost ~]# vim /achong/php.ini
#找到这两个
extension=mysqli
extension=pdo_mysql
[root@localhost ~]# docker cp /achong/php.ini php:/usr/local/etc/php/php.ini
Successfully copied 75.3kB to php:/usr/local/etc/php/php.ini
[root@localhost ~]# docker restart php
php