docker部署MySQL+PHP-FPM+Nginx服务

Happy-cat

准备

docker pull mysql
docker pull nginx
docker pull bitnami/php-fpm

关键命令:

docker ps -a # 查看宿主机部署的所有容器
docker exec -it containerid /bin/bash # 进入正在运行的容器的内部
docker inspect container  # 查看容器的详细信息,比如查找IPAddress等

关键参数:

-v 宿主机目录:虚拟机目录
-p 宿主机端口:虚拟机端口
--name my-name 指定下容器的别名
-d 以deamon守护进程的形式运行
--restart: always(挂掉就重启), on-failure:Number(失败就重启,到达Number次数上限后不再重启),   no(永不重启)  需要注意的是--restart是由docker的守护进程完成的,一般都会和-d参数共同出现

--expose port 暴露虚拟机内部的某个端口

MYSQL

  1. 启动mysql服务
docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
  1. 查找出mysql服务的本地IP地址
docker ps -a # 找出mysql容器的id
docker inspect mysql-container-id # 找到里面的IPAddress即可
  1. 修改root用户权限以用于访问。
mysql -uroot -p123456
use mysql
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
alter  user 'root'@'%' identified by '123456';
FLUSH PRIVILEGES;

NGINX

  1. 启动NGINX服务
➜  nginx ls
conf.d www
➜  nginx docker run -d -p 80:80 -v $PWD/www:/usr/share/nginx/html -v $PWD/conf.d/:/etc/nginx/conf.d --name local-nginx nginx

需要注意的是__宿主机的路径映射到虚拟机的对应目录__下之前,记得本地先mkdir

PHP-FPM

  1. 启动php-fpm服务
➜  nginx docker run -d -p 9000:9000 --name local-phpfpm -v /Users/biao/Docker/nginx/www:/var/www/html bitnami/php-fpm
affcf36a04fe38fe92f2d1535a816d4993076e0fbcba9fc6adcfb1911e62b19b
➜  nginx docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
affcf36a04fe        bitnami/php-fpm     "php-fpm -F --pid /o…"   3 seconds ago       Up 2 seconds        0.0.0.0:9000->9000/tcp              local-phpfpm
def7c36da7bb        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->80/tcp                local-nginx
fcc02b919109        mysql:latest        "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   local-mysql

需要注意的是这里的宿主机www目录被映射到了php-fpm的html目录下,和上一步的NGINX其实共享了宿主机的同一个目录。

连接php-fpm和NGINX的配置,核心在于fastcgi_pass参数,利用IP+端口的形式,让NGINX请求php-fpm的处理,而不能使用unix-socket这种形式,因为后者只能在同一台机器上处理。容器化后肯定就不能用这种形式了。

修改conf.d目录下的default.conf为如下内容:

server {

  listen  80 default_server;
  server_name _;
  root   /usr/share/nginx/html;
  location / {
   index index.html index.htm index.php;
   autoindex off;
  }
  location ~ \.php(.*)$ {
   root   /var/www/html/;
   fastcgi_pass 172.17.0.4:9000;   #php容器的IP地址
   fastcgi_index index.php;
   fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param PATH_INFO $fastcgi_path_info;
   fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
   include  fastcgi_params;
  }
}

测试

测试NGINX访问PHP-fpm,在www目录下创建phpinfo.php文件

<?php
echo phpinfo();

然后访问链接:http://localhost/phpinfo.php

phpinfo测试

测试NGINX访问数据库以及php, 在www目录下创建testdb.php

<?php
 $dbms='mysql';     //数据库类型
 $host='172.17.0.2'; //数据库主机名,此处写mysql 容器的名字
 $dbport = '3306';
 $dbName='mysql';    //使用的数据库
 $user='root';      //数据库连接用户名
 $pass='123456';          //对应的密码
 $dsn="$dbms:host=$host;port=$dbport;dbname=$dbName";
 try {
    $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
    echo "successful!<br/>";
    //你还可以进行一次搜索操作
     foreach ($dbh->query('SELECT * from user') as $row) {
         print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
     }
    $dbh = null;
 } catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
 }

测试连接数据库

结语

这篇文章参考了很多优秀的前辈们的经验,越是发现docker真是个好东西。有什么想法,不用担心本地装软件破坏了宿主机环境,也不用跑到多台服务器上哐哐哐的一顿操作。一切都可以在docker轻松上轻松完成,简单,迅捷,高效。

参考自:

展开阅读全文

Docker nginx + php-fpm配置主机?

08-12
<div class="post-text" itemprop="text"> <p>I am trying to set up a work environment for myself. I can’t get to the server through the name specified in hosts file.</p> <p>docker-compose.yml:</p> <pre><code>version: '2' services: nginx: image: nginx:latest ports: - "80:80" - "443:443" volumes: - ./hosts:/etc/nginx/conf.d - ./www:/var/www - ./logs:/var/log/nginx links: - php php: build: ./images/php links: - mysql volumes: - ./www:/var/www mysql: image: mysql:8 ports: - "3306:3306" volumes: - ./mysql:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: secret </code></pre> <p>php dockerfile:</p> <pre><code>FROM php:7.1-fpm RUN apt-get update && apt-get install -y \ curl \ wget \ git \ libfreetype6-dev \ libjpeg62-turbo-dev \ libmcrypt-dev \ libpng-dev \ && docker-php-ext-install -j$(nproc) iconv mcrypt mbstring mysqli pdo_mysql zip \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gd RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer ADD php.ini /usr/local/etc/php/conf.d/40-custom.ini WORKDIR /var/www CMD ["php-fpm"] </code></pre> <p>./hosts/hello-dev.conf:</p> <pre><code>server { server_name hello.dev; listen 80; index index.php; set $MAGE_ROOT /var/www/hello.dev; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root $MAGE_ROOT; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } } </code></pre> <p>hosts: <code>127.0.0.1 hello.dev</code></p> <p>Tell me what you can try to do in this situation? If I go to the address <a href="http://127.0.0.1" rel="nofollow noreferrer">http://127.0.0.1</a> it opens the site, but I need to be able to create several configs with different names.</p> </div>
©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值