1. 安装Nginx
参考 https://www.runoob.com/docker/docker-install-nginx.html
- 拉取Nginx镜像
docker pull nginx
- 新增目录
mkdir -p ~/nginx/html ~/nginx/logs ~/nginx/conf ~/nginx/conf.d
- 创建容器
docker run -v /root/nginx/html:/usr/share/nginx/html -v /root/nginx/conf.d:/etc/nginx/conf.d -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -p 80:80 -p 8888:8888 --privileged=true --name nginx -d nginx
2. 安装PHP-fpm 7.2
参考 https://www.runoob.com/docker/docker-install-php.html
- 创建目录
mkdir ~/php/conf.d
- 拉取Nginx镜像
docker pull php:7.2-fpm
- 创建容器
docker run -w /var/www/html -v /root/nginx/html:/var/www/html -v /root/php:/usr/local/etc/php --privileged=true -p 80:80 --name php-fpm -d php:7.2-fpm
3. 新增~nginx/conf/nginx.conf文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
client_max_body_size 50m;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
4. 新增~/nginx/conf.d/www.conf 文件
#fastcgi_pass 根据下一步的FPM 地址定义 ,通过过 docker inspect php-fpm查看fpm容器ip地址
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.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}
#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/Carbonuni-PHP;
}
# 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 /usr/share/nginx/html;
fastcgi_pass 172.17.0.5:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME /var/www/html/$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;
#}
}
5. 修改Nginx配置重启容器
docker restart nginx
6. 安装Redis
进入php-fpm 容器
docker exec -it php-fpm bashcurl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.6.tar.gz
tar xfz /tmp/redis.tar.gz
rm -r /tmp/redis.tar.gz
mkdir -p /usr/src/php/ext
mv phpredis-3.1.6 /usr/src/php/ext/redis
docker-php-ext-install redis
7. 安装一些扩展 可以通过 docker-php-ext-install
例如安装mysqlidocker-php-ext-install mysqli
8. 安装GD库
#更新软件源
apt update
#安装各种库
apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev
#解压源码
docker-php-source extract
#gd源码文件夹
cd /usr/src/php/ext/gd
#准备编译
docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2
创建目录
mkdir /usr/local/etc/php/conf.d/
#编译安装
docker-php-ext-install gd
#查看安装进度
php -m | grep gd
#重启容器
docker restart php
9. 增加composer
拉取composer
docker pull composer
进入XXX这项目 里面必须包含composer.json
docker run -it --name composer -v /root/nginx/html/XXX:/app --privileged=true composer composer install --ignore-platform-reqs