Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure nginx+php+mysql for docker

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to configure nginx+php+mysql in docker". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure nginx+php+mysql with docker.

First, learn about a method:

Use docker exec to enter the Docker container

Docker also provides a new command, exec, to enter the container after version 1.3.x, which is relatively simple. Let's take a look at the use of this command:

Sudo docker exec-help

Next we use this command to enter a container that is already running

$sudo docker ps $sudo docker exec-it 775c7c9ee1e1 / bin/bash. Configure nginx

Find the nginx image on Docker Hub

Runoob@runoob:~/nginx$ docker search nginxNAME DESCRIPTION STARS OFFICIAL AUTOMATEDnginx Official build of Nginx. 3260 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 674 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable. 207 [OK] million12/nginx-php Nginx + PHP-FPM 5.5,5.6,7.0 (NG), CentOS... 67 [OK] maxexcloo/nginx-php Docker framework container with Nginx and... 57 [OK] webdevops/php-nginx Nginx with PHP-FPM 39 [OK] h4nrik/nginx-ldap NGINX web server with LDAP/AD, SSL and pro... 27 [OK] bitnami/nginx Bitnami nginx Docker Image 19 [OK] maxexcloo/nginx Docker framework container with Nginx inst... 7 [OK]...

Here we pull the official image.

Runoob@runoob:~/nginx$ docker pull nginx

After waiting for the download to complete, we can look up the image whose REPOSITORY is nginx in the list of local images.

Runoob@runoob:~/nginx$ docker images nginxREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 555bbd91e13c 3 days ago 182.8 MB create and run container: docker run-- name mynginx-p 80:80-v / var/www:/var/www-v / usr/local/nginx/conf/conf.d:/etc/nginx/conf.d-d nginx

Note:

-v add a file mapping relationship so that files changed on the host can be mapped directly to the container. The directory here is mapped according to your actual situation.

After the container is created and run, the nginx in the docker starts successfully, and there is no need to enter the docker to start nginx again, otherwise it will prompt that ports such as 80 are occupied, because nginx has already been started.

At this point, you can access the domain name verification configured by nginx.

The conf.d I map here mainly contains the configuration file of nginx. The configuration information of php is as follows:

复制代码

# phpserver {charset utf-8; client_max_body_size 128M; listen 80; # # listen for ipv4 # listen [:]: 80 default_server ipv6only=on; # # listen for ipv6 server_name www.baidu.com; root / var/www; index index.php Location / {#-e means true if (!-e $request_filename) {rewrite ^ (. *) $/ index.php?s=$1 last; break;} # Redirect everything that isn't a real file to index.php try_files $uri $uri/ / index.php$is_args$args as long as if exists } # uncomment to avoid processing of calls to non-existing static files by Yii # location ~\. (js | css | png | jpg | gif | swf | ico | pdf | mov | fla | zip | rar) ${# try_files $uri = 404; #} # error_page 404 / 404.html; # deny accessing php files for the / assets directory location ~ ^ / assets/.*\ .php ${deny all;} location\. Php$ {include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name Fastcgi_pass 172.17.0.3 uri 9000; # fastcgi_pass unix:/var/run/php5-fpm.sock; try_files $uri = 404;} location ~ * /\. {deny all;}}

Note the ip address of the last fastcgi_pass. Common problems in php configuration are described in detail.

two。 Php configuration

Find the php image on Docker Hub

Runoob@runoob:~/php-fpm$ docker search phpNAME DESCRIPTION STARS OFFICIAL AUTOMATEDphp While designed for web development, the PH... 1232 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable... 207 [OK] phpmyadmin/phpmyadmin A web interface for MySQL and MariaDB. 123 [OK] eboraas/apache-php PHP5 on Apache (with SSL support), built o. 69 [OK] php-zendserver Zend Server-the integrated PHP applicati... 69 [OK] million12/nginx-php Nginx + PHP-FPM 5.5,5.6,7.0 (NG), CentOS... 67 [OK] webdevops/php-nginx Nginx with PHP-FPM 39 [OK] webdevops/php-apache Apache with PHP-FPM (based on webdevops/php) 14 [OK] phpunit/phpunit PHPUnit is a programmer-oriented testing f. 14 [OK] tetraweb/php PHP 5.3, 5.4, 5.5, 5.6, 7.0 for CI and run... 12 [OK] webdevops/php PHP (FPM and CLI) service container 10 [OK].

Here we pull the official image, labeled 5.6-fpm

Runoob@runoob:~/php-fpm$ docker pull php:5.6-fpm

After waiting for the download to be completed, we can look up the image whose REPOSITORY is php and labeled 5.6-fpm in the list of local images.

Runoob@runoob:~/php-fpm$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEphp 5.6-fpm 025041cd3aa5 6 days ago 456.3 MB

Create and run the php container:

Docker run-p 9000 var/www:/var/www 9000-- name phpfpm-v / var/www:/var/www-d php:5.6-fpm

Note that the file mapping must be created here, or there is a corresponding php code in the php container. The file mapping of nginx in the previous step cannot be found here. So if there is no file mapping, 127.0.0.1virtual 9000 cannot find the file in this container.

Frequently asked questions:

After starting the php container, if the access nginx is: 502 Bad Gateway

Try the following methods:

View the ip address of the php image

Docker inspect-- format=' {{.NetworkSettings.IPAddress}} 'phpfpm

For example, 192.168.4.202

Then modify the conf configuration file of nginx so that the value of fastcgi_pass is 192.168.4.202

Vim / docker/nginx/conf.d/default.conf

Fastcgi_pass 192.168.4.202:9000

After restarting the nginx container, you can access it normally.

three。 Mysql configuration

Find the mysql image on Docker Hub

Runoob@runoob:/mysql$ docker search mysqlNAME DESCRIPTION STARS OFFICIAL AUTOMATEDmysql MySQL is a widely used, open-source relati... 2529 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Crea... 161 [OK] centurylink/mysql Image containing mysql. Optimized to be li... 45 [OK] sameersbn/mysql 36 [OK] google/mysql MySQL server for Google Compute Engine 16 [OK] appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK] marvambass/mysql MySQL Server based on Ubuntu 14.04 6 [OK] drupaldocker/mysql MySQL for Drupal 2 [OK] azukiapp/mysql Docker image to run MySQL by Azuki-http:... 2 [OK].

Here we pull the official image, labeled 5.6

Runoob@runoob:~/mysql$ docker pull mysql:5.6

After waiting for the download to be completed, we can look up the image with REPOSITORY mysql and label 5.6 in the local image list.

Runoob@runoob:~/mysql$ docker images | grep mysqlmysql 5.6 2c0964ec182a 3 weeks ago 329 MB

Create and run the MySQL container:

Docker run-p 3306 MYSQL_ROOT_PASSWORD=123456 3306-name mysql-v / usr/local/mysql:/etc/mysql/sqlinit-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.6

The main purpose of the file mapping here is to map the sql database data files of the host to the docker mysql container for easy import. Note that the directory of the mysql container cannot be an existing directory, otherwise it will be overwritten.

Note:

You already have my.cnf for easy creation, so you don't need to add it yourself.

Development

Use the external tool navicat to connect the mysql within the docker

Enter the IP in the host of mysql as the IP in the docker, and obtain it by:

1 docker inspect-- format=' {{.NetworkSettings.IPAddress}} 'mysql

The connection can be successful!

Note:

The container startup order of docker will cause the IP address of the container to be inconsistent. If the IP to the container is used at the connection between the database and the fastcgi, pay attention to the startup order of the container.

Restart container: docker restart container name / container ID

Close the container: docker stop xxx

Open the container: docker start xxx

View the running container: docker ps

View all containers (including those that are not running): docker ps-a

Create and run the container: docker run

-

Common errors: 1. Thinkphp reported errors Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

Missing pdo_mysql extension, failed to link database

Find php.ini,docker in / usr/local/etc/php, make a copy of php.ini, add extension=pdo_mysql.so, and restart phpfpm.

If not, visit the phpinfo page to see if there is a pdo_mysql

Missing gd extension, install:

Docker-php-ext-install gd

The following errors may be reported:

If configure fails try-with-webp-dir=If configure fails try-with-jpeg-dir=configure: error: png.h not found.

Installation:

Apt-get install libpng-dev libjpeg-dev

Execute again:

/ / add freetype configuration docker-php-ext-configure gd--enable-gd-native-ttf-- with-freetype-dir=/usr/include/freetype2-- with-png-dir=/usr/include-- with-jpeg-dir=/usr/include// install docker-php-ext-install gd

Php.ini adds php_gd2.so

Display the gd library in phpinfo

Note that if there is no freetype support in phpinfo's gd library, the CAPTCHA still cannot be displayed, and an error will be reported:

Call to undefined function Think\ imagettftext ()

If there is no freeType in the gd library, follow these steps:

Docker-php-ext-configure gd--enable-gd-native-ttf-- with-freetype-dir=/usr/include/freetype2-- with-png-dir=/usr/include recompile: docker-php-ext-install gd

If an error is reported:

Configure: error: freetype-config not found.

Run: apt-get-y install libfreetype6-dev, and then continue with the above command.

With freetype in the gd library, the CAPTCHA displays normally:

At this point, I believe you have a deeper understanding of "docker how to configure nginx+php+mysql", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report