MAC下Docker安裝Nginx+PHP+Mysql并配置虛擬域名

鏡像下載

PHP鏡像下載

docker pull php:7.4.8-fpm

Nginx鏡像下載

docker pull nginx

Mysql鏡像下載

docker pull mysql:5.7 

驗(yàn)證

docker images

結(jié)果如圖:


制作配置文件

創(chuàng)建 ~/nginx/conf/ 配置等目錄

mkdir -p  ~/nginx/logs ~/nginx/conf

創(chuàng)建nginx的配置文件

vim ~/nginx/conf/nginx.conf

配置文件如下:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /webroot/;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /webroot/;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

配置文件說明:

  • php:9000: 表示 php 服務(wù)的 URL。
  • /webroot/: 是 php 容器中 php 文件的存儲路徑,映射到本地的 ~/Documents/code 目錄。

在 ~/Documents/code 目錄下創(chuàng)建index.php

<?php
    phpinfo();
?>

運(yùn)行Mysql容器

docker run -it -d -p8066:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql57 -v ~/Documents/code/mysqldata:/usr/local/mysql mysql:5.7

運(yùn)行PHP容器

docker run --name php7 -v ~/Documents/code:/webroot -d php:7.4.8-fpm

運(yùn)行Nginx容器

docker run --name mynginx -p 80:80 -v ~/Documents/code:/webroot -v ~/nginx/conf:/etc/nginx/conf.d --link php7:php --link mysql57:mysql -d nginx

命令說明:

  • --name mynginx : 將容器命名為 mynginx
  • -v ~/Documents/code:/webroot : 將本地目錄~/Documents/code 掛載到容器的/webroot目錄下

驗(yàn)證

瀏覽器訪問 localhost


配置虛擬域名

修改nginx的配置文件

vim ~/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  mytest.com;

    location / {
        root   /webroot/test/;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /webroot/test/;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /webroot/test/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

重啟容器

docker restart mynginx

修改hosts文件

sudo vim /ets/hosts

增加

127.0.0.1 mytest.com

測試

在~/Documents/code下創(chuàng)建test文件夾,在該文件夾新建一個index.php

<?php
echo "hello world";
?>

驗(yàn)證

瀏覽器訪問 mytest.com


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容