【Docker】02 使用CentOS系统安装Apache+PHP+MySQL

1.下载centos的印像档
开启 Windows cmd 视窗
docker pull centos
https://ithelp.ithome.com.tw/upload/images/20220412/201480988sB8iNulba.png

2. 建立容器
假设命名为 mycentos
docker run -d -p 50022:22 -p 50080:80 -p 53306:3306 --privileged --name mycentos centos /usr/sbin/init
为了可以执行systemd须加上--privileged
https://ithelp.ithome.com.tw/upload/images/20220412/201480987qSOWng0dO.jpg

可以看到刚刚建立的 mycentos
https://ithelp.ithome.com.tw/upload/images/20220412/20148098ntOdLS6yD6.jpg

检查 port
使用docker介面
https://ithelp.ithome.com.tw/upload/images/20220412/20148098unax5z5wN3.jpg

或是执行指令
docker port mycentos
https://ithelp.ithome.com.tw/upload/images/20220412/20148098F8Ya9VwfL5.jpg

查看IP位址
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mycentos
https://ithelp.ithome.com.tw/upload/images/20220412/201480986nYYstV5XC.jpg

3. 更新环境
执行 bash
docker exec -it mycentos bash
接着会进入容器

更新 repo
一开始执行 yum 会发生错误。要更新 repo
修改 mirrorlist
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
修改 baseurl
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

设定语系
下载:dnf install -y glibc-langpack-en
设定:localectl set-locale LANG=en_US.UTF-8

安装 service 功能
dnf install -y initscripts

5. 安装 php 8.1
此时 centos 的 php 套件还是 7.x,使用 remi 资源库。以下指令在安装 php 的时候会自动安装 httpd。
dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf -y install yum-utils
dnf module reset php
dnf module install php:remi-8.1 -y
dnf install php -y
dnf -y install php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,json}
来源 https://computingforgeeks.com/how-to-install-php-8-on-centos-linux/

启动服务
service php-fpm start
service httpd restart

开启首页
https://ithelp.ithome.com.tw/upload/images/20220412/20148098ZV050XLtGF.png

编辑 phpinfo.php 当测试页
vi /var/www/html/phpinfo.php

https://ithelp.ithome.com.tw/upload/images/20220412/20148098Kdq7jyp4jq.png

开机自动启动
chkconfig php-fpm on
chkconfig httpd on

6. 安装资料库
dnf install -y mysql mysql-server
chkconfig mysqld on
service mysqld start
mysql -u root -p

让Host资料库工具可以连线mysql
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '123456';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

让container自己内部的网页可以连线mysql
mysql> CREATE USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;

注意 % 跟 localhost 的不同。

资料库连线工具
https://ithelp.ithome.com.tw/upload/images/20220412/20148098cR2bP9VTsl.jpg

https://ithelp.ithome.com.tw/upload/images/20220412/20148098tTiM6DQLar.jpg

建一个资料库叫 test。有时候可能装好後就存在test。编码随便选,测试而已。
https://ithelp.ithome.com.tw/upload/images/20220412/20148098vyeBuTquXT.jpg

编辑网页做资料库测试
vi /var/www/html/mysql.php

<?php
//开启错误讯息
ini_set("display_errors", "On");
error_reporting(E_ALL ^E_NOTICE ^E_DEPRECATED);
//连线资讯
define('DB_DATABASE','test');
define('DB_USERNAME','root');
define('DB_PASSWORD','123456');
define('DB_HOSTNAME','localhost');
define('DB_TYPE','mysql');
$dsn = DB_TYPE.':host='.DB_HOSTNAME.';dbname='.DB_DATABASE;
$dbh = new PDO($dsn, DB_USERNAME, DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//用mysql抓时间
$sql = "SELECT NOW();";
$row = $dbh->query($sql)->fetch(PDO::FETCH_ASSOC);
echo "<pre>", print_r($row, 1), "</pre>";

成功显示
https://ithelp.ithome.com.tw/upload/images/20220412/20148098uoI8UqQsvF.jpg

如果发现无法连线,试着重设root密码
mysql_secure_installation


<<:  【C++】Stack and Queues

>>:  【C#】Hash Table

Day 04. Zabbix 可监控的服务、设备、应用

我把它分成使用基本款 (可安装 Agent)、通用款 (支援监控类通讯协定)、简易款 (无法安装 A...

【後转前要多久】# Day20 BootStrap - 通用类别 Utilities

今天要来介绍 Bootstrap 工具、通用类别 在这章节中,最重要的就是要熟悉各种装置尺寸代号, ...

[想试试看JavaScript ] 为什麽需要这麽多种注册事件处理的方式

为什麽需要这麽多种注册事件处理的方式? 上一篇提到注册事件的方式有三种 1.HTML 属性 2.DO...

Android Studio 菜鸟笔记本-Day 27-BottomNavigationView的应用

昨天介绍了BottomNavigationView的使用方法,今天我要分享使用BottomNavig...

Day09 - 网站开发从Django开始

昨天我们完成了虚拟环境安装,而为了让後续的挑战更具连贯性,对於後面几个主题的顺序有稍作挑整,故从今天...