事出有因:本站是使用typecho搭建的博客,原本是在云服务器上直接跑lnmp环境的,但是由于我又想在这个服务器上搞点其它东西,总感觉会把整个服务器的环境弄的很混乱,于是决定将lnmp环境迁移到docker上,于是有了本文。

准备工作:安装docker安装docker-compose

有了基础的docker环境之后,我们开始在docker上安装lnmp:

首先创建docker项目目录:

mkdir -p /home/docker/lnmp
cd /home/docker/lnmp
mkdir conf html logs

conf目录用于映射nginx的配置文件,html目录用于放置程序代码,logs目录映射出nginx的日志

/home/docker/lnmp目录下创建文件lnmp.yaml内容如下:

version: "3"
services:
  nginx:
    image: nginx
    container_name: nginx
    ports:
      - 80:80
      - 443:443 # 不启用https可以不要这行
    volumes:
      - /home/docker/lnmp/html/:/usr/share/nginx/html     #网页文件位置映射
      - /home/docker/lnmp/conf/default.conf:/etc/nginx/conf.d/default.conf    #配置文件位置映射
      - /home/docker/lnmp/logs:/var/log/nginx # 映射日志文件
      - /home/docker/lnmp/cert/:/etc/nginx/ssl     #ssl证书,如果不使用ssl,这行可以不要
    depends_on:
      - php
  php:
    image: cytopia/php-fpm-7.2 # 需要更换版本的直接改这里
    container_name: php
    volumes:
      - /home/docker/lnmp/html/:/var/www/html/   ##网页文件位置映射
    depends_on:
      - mysql
  mysql:
    image: mysql:5.7
    container_name: mysql
    volumes:
      - /home/docker/mysql/data:/var/lib/mysql
      - /home/docker/mysql/logs:/var/log/mysql
      - /home/docker/mysql/conf:/etc/mysql/conf.d
    ports:
      - 3306:3306
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
    environment:
      MYSQL_ROOT_PASSWORD: 123456 #mysql的root用户的密码
      TZ: Asia/Shanghai

在这个docker-compose的配置文件中配置了3个容器:nginx、php、mysql

创建完成之后进入/home/docker/lnmp/conf目录下创建文件default.conf,内容如下:

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost; # 改成自己的域名或者localhost

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


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


location ~ \.php { # php后面不能有$符号,不然pathinfo会*.php?a=1类型的请求匹配不到
                root           html;
                fastcgi_pass   php:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root:/var/www/html/:/tmp/:/proc/";
                include fastcgi_params;
        }
}

上面的配置为未使用ssl证书的配置,但是我的站点是有ssl证书的,于是default.conf的配置应该如下:

server {
        listen       80;
        listen  [::]:80;
        server_name  fx7.top; # 改成自己的域名或者localhost
        rewrite ^(.*) https://$server_name$1 permanent; # 重定向请求到https
}

server {
        listen       443 ssl;
        server_name  fx7.top; # 改成自己的域名或者localhost

        ssl_certificate      /etc/nginx/ssl/6807541_fx7.top.pem; # 改成自己的证书信息
        ssl_certificate_key  /etc/nginx/ssl/6807541_fx7.top.key; # 改成自己的证书信息

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        #include enable-php-pathinfo.conf

        location / {
                proxy_set_header   X-Real-IP         $remote_addr;
                proxy_set_header   Host              $http_host;
                proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
                root   /usr/share/nginx/html;
                index index.html index.php index.htm;
        }

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

        location ~ \.php {
                root           html;
                fastcgi_pass   php:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root:/var/www/html/:/tmp/:/proc/";
                include fastcgi_params;
        }
}

完成之后进入/home/docker/lnmp目录下执行以下命令启动容器即可:

docker-compose -f lnmp.yaml up -d