在WSL中使用docker搭建lnmp环境

在WSL中使用docker搭建lnmp环境

温馨提示:wsl中的docker需要管理员权限运行才可以启动。

docker安装

官网

nginx and php

使用docker下拉运行nginx和php-fpm两个容器时,需要自定义Web根目录映射到将这两个容器的站点根目录进行关联,例如:

docker run -it --name nginx -p 8080:80 -v /var/www/html:/usr/share/nginx/html nginx bash
root@44647ad480ac:/# service nginx start

docker run -it --name php -v /var/www/html:/var/www/html php:7.3.1-fpm bash
root@87b0f3645c67:/var/www/html# php-fpm &

接着配置/etc/nginx/conf.d/default.conf文件(配置完后 docker cp default.conf nginx:/etc/nginx/conf.d/default.conf),下面是我的配置文件

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #    root           html;
        fastcgi_pass   172.17.0.3:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

php-fpm扩展

mysql

root@b927f60ee932:/var/www/html# docker-php-ext-install mysqli pdo_mysql

iconv和gd

 root@b927f60ee932:/var/www/html# apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev 
 root@b927f60ee932:/var/www/html# docker-php-ext-install -j$(nproc) iconv 
 root@b927f60ee932:/var/www/html# docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ 
 root@b927f60ee932:/var/www/html# docker-php-ext-install -j$(nproc) gd

在WSL中使用docker搭建lnmp环境_第1张图片

mysql

当运行mysql容器时会出现错误而无法正常运行,这里怀疑是权限问题,需要进入到容器里进行初始化,然后指定root用户启动数据库,接着从另起一个终端进入(感觉有点类似C/S模式,并且很不安全),经过尝试成功运行了mysql容器。
在WSL中使用docker搭建lnmp环境_第2张图片

#first bash
root@DESKTOP-7RTPVG0:/home/jie# docker exec -it mysql bash
root@9022301afa2a:/# mysqld --initialize-insecure
root@9022301afa2a:/# mysqld --user=root

#two bash
root@9022301afa2a:/# mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; 
mysql> flush privileges;

php访问mysql

无需–link参数连接数据库了(详情见官网)

mysql>  DELETE FROM  mysql.user WHERE user='root' AND host='localhost';
mysql> CREATE USER 'test'@'172.17.0.%' IDENTIFIED BY 'test';
mysql> GRANT ALL ON *.* TO 'test'@'172.17.0.%';
mysql>ALTER USER 'test'@'172.17.0.%' IDENTIFIED WITH mysql_native_password BY 'test';
mysql> flush privileges;

以下资源地址:
[1]: https://docs.docker.com/install/linux/docker-ce/ubuntu/
[2]: https://www.jianshu.com/p/aeaa851b0721
[3]: https://mirrors.aliyun.com/ubuntu/

你可能感兴趣的:(在WSL中使用docker搭建lnmp环境)