(1)创建一个目录lnmpcompose
```
[root@localhost ~]# tree lnmpcompose
lnmpcompose
├── conf
│ └── nginx.conf
├── docker-compose.yml
└── html
├── index.html
├── mysql.php
└── test.php
```
(2)在创建的目录中创建一个docker-compose.yml模板文件
```yaml
version: "3"
services:
nginx:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./html:/usr/share/nginx/html
- ./conf/nginx.conf:/etc/nginx/nginx.conf
php:
image: devilbox/php-fpm:5.2-work-0.89
volumes:
- ./html:/var/www/html
mysql:
image: mysql:5.6
environment:
- MYSQL_ROOT_PASSWORD=123456
```
```ini
[root@localhost conf]# vi nginx.conf
[root@localhost conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
```
```php
[root@localhost html]# vi index.html
[root@localhost html]# cat index.html
index.php
[root@localhost html]# vi mysql.php
[root@localhost html]# cat mysql.php
<?php
$dbhost = 'mysql'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
echo 'mysql connect success!';
mysqli_close($conn);
[root@localhost html]#vi test.php
[root@localhost html]# cat test.php
<?php
echo phpinfo();
```
启动docker-compose一组服务
docker-compose up
执行这个命令必须在docker-compose.yml配置文件目录
```shell
[root@localhost lnmpcompose]# docker-compose -f docker-compose.yml up -d
Creating network "lnmpcompose_default" with the default driver
Creating lnmpcompose_php_1 ... done
Creating lnmpcompose_mysql_1 ... done
Creating lnmpcompose_nginx_1 ... done
```
通过docker-compose ps查看进程情况
```shell
[root@localhost lnmpcompose]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------
lnmpcompose_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp
lnmpcompose_nginx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp
lnmpcompose_php_1 /docker-entrypoint.sh Up 9000/tcp
进入网站查看状况