reading MySQL+ phP-fpm +nginx container environment January 29, 2024 by Tiya Wali No Comments Prepare the mirrorI use centos system, the latest version of Docker-CE.Prepare three mirror imagesDocker pull mysql: 5.7Docker pull PHP: 5.6 – FPMdocker pull nginx:latestNote, do not use the latest version of mysql and PHP, there are many bugs.1Docker images # View imagesBuild a mysql containerDocker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=ziqin666 -v /mysql:/var/lib/mysql –name your_mysql mysql:5.7Configuring containers (entering containers)docker exec -it your_mysql bashOnce inside, authorize the user to use the remote connectionEnter the password ziqin666 to connect to mysqlGRANT ALL PRIVILEGES ON *.* TO ‘root’@’localhost’ IDENTIFIED BY ‘ziqin666’;GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘ziqin666’;GRANT ALL PRIVILEGES ON *.* TO ‘root’@’127.0.0.1’ IDENTIFIED BY ‘ziqin666’;FLUSH PRIVILEGES;At this point you can use the client to connect to see. (Ensure that the port corresponding to the security group rule is enabled.)Build the phP-FPM containerThe preparatory workPhp.ini does not exist by defaultCreate the php.ini file and conf.d folder in the /home/app/phpfile folderCreate a container1docker run -p 9000:9000 –name your_phpfpm -v /home/app/html:/var/www/html -v /home/app/phpfile:/usr/local/etc/php – link your_mysql: PHP mysql – d: 5.6 – FPMCommand description:–name your_phpfpm: Name the container your_phpfpm.– v/home/app/HTML: / var/WWW/HTML: the host project directory in/home/app/HTML mounted to the container/var/WWW/HTMLInstall pDO to test the database connection later, which can be done in the DOCker PHP containerdocker-php-ext-install pdo_mysqlInstalling other plug-ins can also use this approachAdd to php.iniextension=php_curl.dllextension=php_gd2.dllextension=php_mysql.dllextension=php_mysqli.dllextension=php_pdo_mysql.dllextension=php_pdo_odbc.dllextension=php_pdo.dllIf plug-ins are not enough, you need to customize your own imageThe FROM PHP: 7.0.12 – FPMMAINTAINER Tairy <tairyguo@gmail.com>WORKDIR /workingRUN apt-get update –fix-missing && apt-get install -y \ g++ autoconf bash git apt-utils libxml2-dev libcurl3-dev pkg-config \ && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo “Asia/Shanghai” > /etc/timezone \ && docker-php-ext-install iconv curl mbstring \ xml json mcrypt mysqli pdo pdo_mysql zip \ && docker-php-ext-configure gd \ –with-gd \ –with-freetype-dir=/usr/include/ \ –with-png-dir=/usr/include/ \ –with-jpeg-dir=/usr/include/ \ && docker-php-ext-install gd \ && docker-php-ext-enable gd \Pecl install /pecl/redis-3.0.0.tgz \ && docker-php-ext-enable redis \ && apt-get purge -y –auto-remove \ && rm -rf /var/cache/apt/* \ && rm -rf /var/lib/apt/lists/* \ && rm -rf /peclInstall nginx/home/app/nginx/conf.dCreate default.conf in the configuration file directoryserver{listen 80;server_name localhost;location / {root /usr/share/nginx/html;index index.html index.htm;}location ~ \.php$ {fastcgi_pass your_phpfpm:9000; Docker PHP name: your_phpfpmfastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;include fastcgi_params;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}Create a containerdocker run -p 81:80 –name your_nginx \ -v /home/app/nginx/www:/usr/share/nginx/html \ -v /home/app/nginx/conf.d:/etc/nginx/conf.d \ –link your_phpfpm:phpfpm \ -d nginx:latestPHPFPM and mysql in the mapping container are fixed and cannot be modified!/home/app/nginx/ WWW -data: www_data/WWW -data: www_data/WWW -data: www_datachown -R www-data:www-data wwwFour, test,1. View servicesdocker ps -aSTATUS is up, that is, running2. Test PHP parsingChange the index. PHP file in /home/app/nginx/ WWW/on the host.<? php echo phpinfo();3, test the mysql linkModified index. PHP<? php//echo phpinfo();$dbms=’mysql’; // Database type$host=’your_mysql’; // Database host name, where write mysql container name$dbport = ‘3306’;$dbName=’mysql’; // The database used$user=’root’; // Database connection user name$pass=’123456′; // The corresponding password$dsn=”$dbms:host=$host; port=$dbport; dbname=$dbName”;try {$dbh = new PDO($dsn, $user, $pass); // Initialize a PDO object echo “successful<br/>”;You can also perform a search operation // foreach ($dbh->query(‘SELECT * from user’) as $row) {// print_r($row); Echo ($GLOBAL); To see these values // } $dbh = null;} catch (PDOException $e) {die (“Error! : ” . $e->getMessage() . “<br/>”);}Accessing the IP to the correct output proves that our configuration is successful.