docker 学习笔记之实战 lnmp 环境搭建系列 (2) ------ 手动搭建 lnmp 环境
前面我们已经安装好了docker,同时了解了docker的简易用法。接下来我们先尝试使用docker搭建手动lnmp环境,以便进一步的了解docker的相关用法吧!
部署mysql
由于mysql部署比较简单,我们就先捏个软柿子,并且像数据库这种关系到公司命脉的服务建议不要放在容器中,这里我们只是做一个简单的实验。我们选用的是目前版本比较新的mysql5.7。
//拉去镜像
docker pull mysql:5.7
/*相关参数解析*/
//-d 开启Daemon模式。
//-p hostPort:containerPort 端口映射
//-e 为容器设置环境变量 MYSQL_ROOT_PASSWORD:mysql密码
//--name 容器启动时的名字
docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=mypassword --name mysql mysql:5.7
部署ngnix
nginx服务器的HTML路径(网站根目录)在容器 /usr/share/nginx/html 目录下,现在需要把这个目录映射到宿主机的 /var/www/nginx/html 目录(可自定义文件目录)。Nginx 的强大很大部分体现在配置文件上,对于一些高级的应用来说,自定义 Nginx 非常重要。我们需要把 Nginx 的配置文件映射到宿主机中。
先起一个nginx 把配置文件复制到宿主机中。
docker run --name nginx -p 81:80 -d nginx
cd /var/www/nginx/conf
//复制nginx配置文件
docker cp nginx:/etc/nginx/conf.d/default.conf default.conf
docker ps -a # 查看正在运行的容器
docker stop nginx # 停止正在运行的容器
docker start nginx # 启动一个已经停止的容器
docker rm nginx # 删除容器
启动nginx,并且将nginx配置文件、网站根目录映射到宿主机上
//如果挂载一个空的数据卷到容器中的一个非空目录中,那么这个目录下的文件会被复制到数据卷中。
//如果挂载一个非空的数据卷到容器中的一个目录中,那么容器中的目录中会显示数据卷中的数据。如果原来容器中的目录中有数据,那么这些原始数据会被隐藏掉。
docker run --name nginx -p 81:80 -v /var/www/nginx/html:/usr/share/nginx/html -v /var/www/nginx/default.conf:/etc/nginx/conf.d/default.conf
部署php
拉取php7.2镜像
docker pull php:7.2-fpm
复制php.ini 需要进入容器中解压php.tar.xz。如果本地有php.ini文件可将本地文件复制,忽略此步骤
//启动php容器
docker run --name php-fpm -p 9001:9000 -itd 7.2:fpm
//进入php容器
docker exec -it php-fpm /bin/bash
//进入 /usr/local 目录解压 php.tar.xz
cd /usr/local
tar -xvf php.tar.xz
//复制文件
docker cp php-fpm:/usr/local/php-7.2.15/php.ini-development php.ini
docker cp php-fpm:/usr/local/etc/php-fpm.d/www.conf www.conf
docker stop php-fpm
docker rm php-fpm
重新启动php,并将配置文件映射的容器中
docker run --name php-fpm -p 9001:9000 --link mysql:mysql -v /var/www/nginx/html:/var/www/html -v /var/www/nginx/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf -v /var/www/nginx/conf/php.ini:/usr/local/etc/php/php.ini -d php:fpm
修改nginx配置文件
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.php index.html index.htm;
//charset koi8-r;
//access_log /var/log/nginx/log/host.access.log main;
location / {
//root /usr/share/nginx/html;
//index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /40x.html {
root /user/share/nginx/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$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
// fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$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;
}
}
重新启动 nginx 让其支持 php
docker run --name nginx -p 81:80 --link php-fpm -v /var/www/nginx/html:/usr/share/nginx/html -v /var/www/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf -d nginx
在 /var/www/nginx/html 新建index.php
<?php
phpinfo();
至此 php7.2 + nginx + mysql5.7 完美搭建完成!这只是个简易搭建方式,后续会有更高效的搭建方法哦!
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: