docker搭建laravel開發環境lnmp

taozhang-tt發表於2020-09-19

之前學習laravel的時候跟著官方文件搭建了homestead 環境,無奈換了乞丐版本的mac之後,再想跑個虛擬機器簡直是妄想,放棄了phpstorm和homestead,現在用 vscode + docker,竟然異常絲滑!記錄一下docker搭建的laravel開發環境,以及如何配置 vscode 除錯php程式碼

LNMP搭建

  • Mysql

    www.runoob.com/docker/docker-insta...

    docker pull mysql:5.7
    docker run -itd --name mysql57 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=your_password mysql:5.7
  • php

    www.runoob.com/docker/docker-insta...

    docker pull php:7.3-fpm
    docker run --name php73-fpm -v ~/Code/php:/www -d php:7.3-fpm
  • nginx

    www.runoob.com/docker/docker-insta...

    docker pull nginx:1.19
    docker run --name nginx19 -p 8080:80 -d \
      -v ~/Code/php:/usr/share/nginx/html:ro \
      -v ~/Code/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
      --link php73-fpm:php \
      nginx:1.19
  • nginx 配置檔案

    server {
      listen       80;
      server_name  usercenter.dev.com;
    
      location / {
          #/usr/share/nginx/html/123u-usercenter/public 是nginx對應的docker裡面的專案地址
          root   /usr/share/nginx/html/123u-usercenter/public;
          try_files $uri $uri/ /index.php?$query_string;
          index  index.html index.htm index.php;
      }
    
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
    
      location ~ \.php$ {
          fastcgi_pass   php:9000;
          fastcgi_index  index.php;
          # /www/123u-usercenter/public 是php對應的docker裡面的專案地址
          fastcgi_param  SCRIPT_FILENAME /www/123u-usercenter/public/$fastcgi_script_name;
          fastcgi_param  APP_ENV dev;
          include        fastcgi_params;
          fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
          fastcgi_param  PATH_INFO  $fastcgi_path_info;
          fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
      }
    }
    
    

## docker ubuntu 切換軟體源

sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list
apt clean
apt update


## 安裝擴充套件

* 安裝 composer

curl -sS getcomposer.org/installer | php –
–install-dir=/usr/bin –filename=composer


* 安裝 gd 擴充套件

apt install -y sendmail libpng-dev
docker-php-ext-install gd


* 安裝 zip 擴充套件

apt install zlib1g-dev
apt install libzip-dev
docker-php-ext-install zip


* 安裝 ldap 擴充套件

apt-get install libldap2-dev -y
rm -rf /var/lib/apt/lists/*
docker-php-ext-configure ldap –with-libdir=lib/x86_64-linux-gnu/
docker-php-ext-install ldap


* 安裝 pdo 擴充套件

docker-php-ext-install pdo pdo_mysql


## 配置 xdebug

docker 安裝 xdebug

pecl install xdebug
echo “zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)” > /usr/local/etc/php/conf.d/xdebug.ini
echo “xdebug.remote_enable=on” >> /usr/local/etc/php/conf.d/xdebug.ini
echo “xdebug.remote_autostart=off” >> /usr/local/etc/php/conf.d/xdebug.ini


vscode 配置檔案

/www/123u-usercenter 是php對應的docker裡面的專案地址
/Users/tt/Code/php/123u-usercenter 是本機對應的專案的地址
{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “Listen for XDebug”,
“type”: “php”,
“request”: “launch”,
“port”: 9000,
“pathMappings”: {
“/www/123u-usercenter”: “/Users/tt/Code/php/123u-usercenter”
},
“xdebugSettings”: {
“max_data”: 65535,
“show_hidden”: 1,
“max_children”: 100,
“max_depth”: 5
}
}
]
}


xdebug.ini

xdebug.remote_enable=1
xdebug.remote_host=docker.for.mac.localhost
xdebug.remote_connect_back=0
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_handler=dbgp
xdebug.max_nesting_level=1500
```

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章