PHP Mysql 环境搭建

简介

内容 主要介绍 PHP + Mysql 环境搭建

php 以前用过,时间长了,就忘记了,写个帖子,防止遗忘

获取

Docker大法好 多余内容省略

编写 docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: '3'

services:
nginx:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- ./code:/usr/share/nginx/html:rw
# nginx configs
#- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
depends_on:
- phpfpm
links:
- phpfpm
container_name: search_ng
db:
image: mysql
container_name: search_db
ports:
- "3306:3306"
volumes:
- ./mysqldata:/var/lib/mysql:rw
environment:
- MYSQL_DATABASE=searchdb
- MYSQL_ROOT_PASSWORD=123456
phpfpm:
build: ./phpfpm
container_name: search_php
ports:
- "9000:9000"
volumes:
- ./code/:/usr/share/nginx/html:rw
# Your php.ini
- ./phpfpm/conf/php.ini-production:/usr/local/etc/php/php.ini:ro
depends_on:
- db
links:
- db

文件结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.
├── code
│   ├── testmysql.php
│   ├── test.php
├── docker-compose.yml
├── mysqldata
├── nginx
│   ├── conf.d
│   │   └── default.conf
│   └── log
├── phpfpm
│   ├── conf
│   │   └── php.ini-production
│   ├── Dockerfile
│   └── source.list
├── README.md

phpfpm Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FROM php:7.0.5-fpm

# 修改软件更新源
COPY ./source.list /etc/apt/sources.list

RUN apt-get update \
&& apt-get install -y wget \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libboost-dev libboost-all-dev gperf* libevent-dev \
# 安装扩展:iconv、mcrypt、zip、gd、pdo_mysql、mysqli、pcntl
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) zip gd pdo_mysql mysqli pcntl \
&& apt-get clean \
&& apt-get autoclean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

source.list 修改软件源,为了更快安装

1
2
3
4
5
#debian8
deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib

Nginx default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
listen 80;
#server_name 172.20.1.7;
server_name localhost;

access_log /var/log/nginx/searchProj.access.log;
error_log /var/log/nginx/searchProj.error.log;

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

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html; # 代码目录
fastcgi_pass phpfpm:9000; # 修改为phpfpm容器
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改为$document_root
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}

}

执行

1
docker-compose up -d

测试代码

  1. php环境测试 test.php
1
2
<?php
phpinfo();

看看是能看到php的信息

  1. php mysql 数据库连接测试

testmysql.php

1
2
3
4
5
<?php 
$link=mysql_connect("127.0.0.1","root","123456");
if(!$link) echo "FAILD!连接错误,用户名密码不对";
else echo "OK!可以连接";
?>

我这里浏览器访问 出现 500

加上

1
2
ini_set("display_errors","On");
error_reporting(E_ALL);

看下什么原因出错?

1
2
3
4
5
6
7
8
<?php 
ini_set("display_errors","On");
error_reporting(E_ALL);

$link=mysql_connect("127.0.0.1","root","123456");
if(!$link) echo "FAILD!连接错误,用户名密码不对";
else echo "OK!可以连接";
?>

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /usr/share/nginx/html/testmysql.php:6 Stack trace: #0 {main} thrown in /usr/share/nginx/html/testmysql.php on line 6

mysql_connect 函数无效?

我查看了一下 php 信息里面有mysql 库,不是库的原因

最后,找到原因,是因为 我用的是php最新版本 7.0

mysql_connect 在5.0的时候可以使用,到7.0的时候已经被放弃了,是废弃函数

PHP7中貌似已经彻底不支持了,根据官网说明,取而代之的是如下两个:

本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除。应使用 MySQLi 或 PDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南以及相关 FAQ 以获取更多信息。用以替代本函数的有:
mysqli_connect()
PDO::__construct()
使用时,不要在使用mysql_connect了,可以换用mysqli_connect(),用法基本类似吧,据说是面向对象的库。
php.ini中,也只有extension=php_mysqli.dll,而不再有extension=php_mysql.dll这个拓展了。
修改 testmysql.php

1
2
3
4
5
6
7
8
<?php 
ini_set("display_errors","On");
error_reporting(E_ALL);

$link=mysqli_connect("127.0.0.1","root","123456");
if(!$link) echo "FAILD!连接错误,用户名密码不对";
else echo "OK!可以连接";
?>

类似的还有 mysql_的其他函数