Docker搭建WEB環(huán)境之 Nginx & PHP


第一步安裝Docker
1、更新Ubuntu的apt源
sudo apt-get update
2、安裝包允許apt通過HTTPS使用倉庫
sudo dpkg --configure -a sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
3、添加Docker官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4、設置Docker穩(wěn)定版?zhèn)}庫
sudo add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
[arch=arm64] 注意系統(tǒng)的平臺選擇
5、更新apt源
sudo apt-get update
6、安裝最新版本Docekr CE(社區(qū)版)
sudo apt-get install docker-ce
7、查看安裝Docker的版本
docker --version
8、檢查Docker CE是否安裝正確
sudo docker run hello-world
9、更新為國內(nèi)源
sudo vim /etc/docker/daemon.json
寫入以下文本
{ ? ? ? ?"registry-mirrors": [ ? ? ? ? ? ? ? ?"https://registry.docker-cn.com", ? ? ? ? ? ? ? ?"https://hub-mirror.c.163.com/" ? ? ? ?] }
10、重啟docker
sudo systemctl restart docker
第二步Docker安裝nginx
1、原始安裝nginx-v1容器
sudo docker run --name nginx-v1 -p 80:80 ?-d nginx
檢查是否安裝成功,瀏覽器訪問http://192.168.157.129/
2、拷貝nginx-v1容器內(nèi)的配置文件到物理機
cd /mnt sudo mkdir public?
cd /mnt/public sudo?
mkdir conf?
sudo mkdir html?
sudo docker cp ?nginx-v1:/etc/nginx/conf.d/default.conf /mnt/public/conf/default.conf?
sudo docker cp ?nginx-v1:/usr/share/nginx/html/index.html /mnt/public/html/index.html?
sudo docker cp ?nginx-v1:/usr/share/nginx/html/50x.html /mnt/public/html/50x.html
3、刪除nginx-v1容器,重新安裝nginx-v2并指定配置目錄
sudo docker stop nginx-v1?
sudo docker rm nginx-v1?
sudo docker run --name nginx-v2 -p 80:80 -v /var/log/nginx:/var/log/nginx -v /mnt/public/html:/usr/share/nginx/html -v /mnt/public/conf:/etc/nginx/conf.d -d nginx
第三步nginx-v2容器內(nèi)安裝PHP環(huán)境
1、進入docker容器
sudo docker exec -it nginx-v2 bash
2、在nginx-v2容器中,安裝php環(huán)境
// 切換國內(nèi)源?
sed -i 's/deb/#deb/g' /etc/apt/sources.list sed -i '$a\deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free' ?/etc/apt/sources.list?
sed -i '$a\deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free' ?/etc/apt/sources.list?
sed -i '$a\deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free' ?/etc/apt/sources.list?
sed -i '$a\deb https://security.debian.org/debian-security bullseye-security main contrib non-free' ?/etc/apt/sources.list?
// 更新 Debian?
apt update && apt -y upgrade
3、從sury/php的PPA存儲庫中安裝PHP
apt-get install ca-certificates apt-transport-https software-properties-common -y?
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/sury-php.list ?
apt install -y wget vim gpg?
wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -
4、添加新的源后,更新源
apt update
5、安裝php
apt-get install php8.2-fpm -y?
// 查看php版本?
php -v?
// 查看php-fpm位置?
whereis php-fpm
第四步安裝 PHP 擴展
輸入 sudo apt install php8.2- 按下tab 按鍵,可以看到所有支持的php8.2擴展
1、安裝常規(guī)擴展
// 常用php擴展?
apt install php8.2-{bcmath,xml,fpm,mysql,zip,intl,gd,cli,bz2,curl,mbstring,opcache,soap,cgi}
2、更新fpm用戶權(quán)限
// 更新用戶權(quán)限?
vim /etc/php/8.2/fpm/pool.d/www.conf
更新配置文件www.conf
user = www-data?
group = www-data?
更新為:?
user = nginx?
group = nginx?
listen.owner = www-data?
listen.group = www-data?
更新為:?
listen.owner = nginx?
listen.group = nginx
3、啟動php-fpm
/usr/sbin/php-fpm8.2
啟動/usr/sbin/php-fpm8.2 報錯:
在/rum目錄下新建一個php目錄。
cd /run?
mkdir php?
/usr/sbin/php-fpm8.2
4、設置docker容器重啟后自動啟動php-fpm
vim /docker-entrypoint.sh
// 在 if [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then s上面加入 /usr/sbin/php-fpm8.2 --daemonize
配置 Nginx PHP
// 新建配置文件?
vim /etc/nginx/conf.d/test.conf
// 配置信息插入到配置文件?
server { ? ?listen ? ? ? 80; ? ?server_name ?192.168.157.129; ? ?root ? /usr/share/nginx/html; ? ?index ?index.html index.htm index.php; ? ?location / { ? ?} ? ?error_page ? 500 502 503 504 ?/50x.html; ? ?location = /50x.html { ? ? ? ?root ? /usr/share/nginx/html; ? ?} ? ?location ~ \.php$ { ? ?include /etc/nginx/fastcgi_params; ? ?fastcgi_pass unix:/run/php/php8.2-fpm.sock; ? ?fastcgi_index index.php; ? ?fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ? } }
//重啟nginx?
nginx -s reload
進入網(wǎng)站目錄,新建phpinfo文件
cd /usr/share/nginx/html?
vim phpinfo.php
訪問http://192.168.157.129/phpinfo.php
5.安裝Wordpress博客測試效果
wget https://wordpress.org/latest.zip unzip latest.zip?
sudo wget http://192.168.157.1/Share/wordpress-6.2.zip?
sudo unzip wordpress-6.2.zip
6.提交docker鏡像
// 提交容器nginx-v2 為web鏡像版本v1?
sudo docker commit nginx-v2 web:v1?
// 查看鏡像列表?
sudo docker images?
// 停止并刪除nginx-v2容器?
sudo docker stop nginx-v2?
sudo docker rm nginx-v2?
// 新建webser容器?
sudo docker run --name webser -p 80:80 -v /var/log/nginx:/var/log/nginx -v /mnt/public/html:/usr/share/nginx/html -v /mnt/public/conf:/etc/nginx/conf.d -d web:v1