社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
本节为 Docker下 Nginx,Mysql,Php 环境搭建
版本:mysql:5.6,nginx,php:5.6-fpm
只适合Docker环境。本例以Ubuntu16.04-x64下的Docker环境为例
所有文件组织在
/www/
目录下
www/
|
|── tmp
|
|
|── cert
|
|
├── conf
│ ├── mysql
│ ├── nginx
│ │ └── nginx.conf
│ └── php5.6
│ ├── docker-php-ext-mysql.ini
│ ├── docker-php-ext-mysqli.ini
│ └── docker-php-ext-pdo_mysql.ini
├── logs
│ ├── mysql
│ └── nginx
│ ├── access.log
│ └── error.log
├── mysql
└── sites
├── athome
│ └── index.php
└── test
└── info.php
/www/tmp/*
下是临时文件,上传的文件映射到该目录/www/cert/*
下是证书文件/www/conf/*
下是各自的配置文件/www/logs/*
下是各自的日志/www/mysql/*
下是mysql数据库的数据(持久化)/www/sites/*
下是各网站内容/www/sites/athome/*
主域名对应的网站,其他为子域名首先创建目录
/www/
sudo mkdir /www/
[dzlua@ubuntu:~]$ sudo mkdir /www/
[dzlua@ubuntu:~]$ cd /www/
[dzlua@ubuntu:/www]$
docker pull mysql:5.6
docker run -d -p 3306:3306
--name mysql
--restart always
-v /www/conf/mysql:/etc/mysql/config.d
-v /www/logs/mysql:/logs
-v /www/mysql:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456
mysql:5.6
其中
MYSQL_ROOT_PASSWORD
为root初始化密码
docker pull php:5.6-fpm
docker run -d -p 9000:9000
--name php5.6-fpm
--restart always
-v /www/conf/php5.6:/usr/local/etc/php/conf.d
-v /www/sites:/www
-v /www/tmp:/tmp
php:5.6-fpm
更改权限
进入容器
docker exec -it php5.6-fpm /bin/bash
[dzlua@ubuntu:/www]$ docker exec -it php5.6-fpm /bin/bash
root@07eefd2ced20:/var/www/html#
更改目录归属
chown -R www-data:www-data /www/
chown -R www-data:www-data /tmp/
root@07eefd2ced20:/var/www/html# chown -R www-data:www-data /www/
root@07eefd2ced20:/var/www/html#
root@07eefd2ced20:/var/www/html# chown -R www-data:www-data /tmp/
root@07eefd2ced20:/var/www/html#
更改目录权限
chmod -R 755 /www/
chmod -R 0777 /tmp/
root@07eefd2ced20:/var/www/html# chmod -R 755 /www/
root@07eefd2ced20:/var/www/html#
root@07eefd2ced20:/var/www/html# chmod -R 0777 /tmp/
root@07eefd2ced20:/var/www/html#
安装扩展
进入容器
docker exec -it php5.6-fpm /bin/bash
[dzlua@ubuntu:/www]$ docker exec -it php5.6-fpm /bin/bash
root@07eefd2ced20:/var/www/html#
安装扩展
docker-php-ext-install pdo pdo_mysql mysqli mysql
root@07eefd2ced20:/var/www/html# docker-php-ext-install pdo pdo_mysql mysqli mysql
root@07eefd2ced20:/var/www/html#
设置
进入设置
cd /www/conf/php5.6
新建 docker-php.ini,输入date.timezone = "Asia/Shanghai"
,输入ESC:qEnter
退出
vi docker-php.ini
操作过程(设置时区为中国北京):
[dzlua@ubuntu:/www]$ cd /www/conf/php5.6
[dzlua@ubuntu:/www/conf/php5.6]$ ls
docker-php-ext-mysqli.ini docker-php-ext-pdo_mysql.ini
docker-php-ext-mysql.ini
[dzlua@ubuntu:/www/conf/php5.6]$ vi docker-php.ini
date.timezone = "Asia/Shanghai"
[dzlua@ubuntu:/www/conf/php5.6]$ ls
docker-php-ext-mysqli.ini docker-php-ext-pdo_mysql.ini
docker-php-ext-mysql.ini docker-php.ini
[dzlua@ubuntu:/www/conf/php5.6]$ cat docker-php.ini
date.timezone = "Asia/Shanghai"
[dzlua@ubuntu:/www/conf/php5.6]$
重启容器
docker restart php5.6-fpm
[dzlua@ubuntu:/www]$ docker restart php5.6-fpm
[dzlua@ubuntu:/www]$
docker pull nginx
docker run -d -p 80:80 -p 443:443
--name nginx
--restart always
-v /www/conf/nginx:/etc/nginx/conf.d
-v /www/logs/nginx:/var/log/nginx
-v /www/sites:/usr/share/nginx/html
-v /www/cert:/etc/nginx/cert
nginx
www.dzlua.top
跳转到dzlua.top
,对应目录为/www/sites/athome
*.dzlua.top
对应为 /www/sites/*
fastcgi_pass
对应的IP为内网IP(非127.0.0.1,nginx访问php使用的IP)编辑
/www/conf/nginx/nginx.conf
# dzlua.top 服务器配置 http
server {
listen *:80;
listen [::]:80;
server_name www.dzlua.top;
return 301 $scheme://dzlua.top$request_uri;
}
server {
listen *:80;
listen [::]:80;
server_name dzlua.top; #绑定域名
index index.htm index.html index.php; #默认文件
root /usr/share/nginx/html/athome; #网站根目录
location / {
index index.html index.htm index.php;
autoindex off;
}
location ~ .php(.*)$ {
root /www/athome;
fastcgi_pass 172.17.21.249:9000;
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;
}
}
server {
listen *:80;
listen [::]:80;
server_name ~^(.+)?.dzlua.top$; #绑定域名
set $site $1;
index index.htm index.html index.php; #默认文件
root /usr/share/nginx/html/$site; #网站根目录
location / {
index index.html index.htm index.php;
autoindex off;
}
location ~ .php(.*)$ {
root /www/$site;
fastcgi_pass 172.17.21.249:9000;
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;
}
}
# dzlua.top 服务器配置 https, /www/sites/www/目录为athome网站
server {
listen *:80;
listen [::]:80;
server_name www.dzlua.top dzlua.top;
return 301 https://www.dzlua.top$request_uri;
}
server {
listen 443 ssl;
server_name ~^(.+)?.dzlua.top$; #绑定域名
set $site $1;
ssl on;
ssl_certificate cert/dzlua.top.pem;
ssl_certificate_key cert/dzlua.top.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
index index.htm index.html index.php; #默认文件
root /usr/share/nginx/html/$site; #网站根目录
location / {
index index.html index.htm index.php;
autoindex off;
}
location ~ .php(.*)$ {
root /www/$site;
fastcgi_pass 172.17.21.249:9000;
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;
}
}
重启容器
docker restart nginx
[dzlua@ubuntu:/www]$ docker restart nginx
[dzlua@ubuntu:/www]$
所有网站都放在
/www/sites/
目录下
创建测试网站
在/www/sites
下创建test
,并创建info.php
文件
[dzlua@ubuntu:/www/sites/test]$ cat info.php
<?phpphpinfo();?>
[dzlua@ubuntu:/www/sites/test]$
访问网站http://dzlua.top/test/info.php
,能显示php信息说明OK
创建主站
/www/sites
下创建athome
文件夹/www/sites/athome
目录/www/sites/athome
权限更改为 chown -R www-data:www-data /www/sites/athome
http://dzlua.top
创建子域名下其他网站,假设子域名为bbs
/www/sites
下创建bbs
文件夹/www/sites/bbs
目录/www/sites/bbs
权限更改为 chown -R www-data:www-data /www/sites/bbs
http://bbs.dzlua.top
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!