Linux-Installationskonfiguration nginx+php im Docker erstellen und konfigurieren
Artikelverzeichnis
-
- Linux-Installationskonfiguration nginx+php im Docker erstellen und konfigurieren
-
- 1. Nginx-Quellcodepaket-Kompilierungsumgebung und Installation der entsprechenden Abhängigkeiten
- 2.Installieren Sie Nginx
- 3. Starten Sie den Nginx-Dienst
- 4. Ändern Sie die Nginx-Konfigurationsdatei, um eine neue Portseite zu erstellen
- 5. PHP installieren
- 6.Installieren Sie Docker
- 7. Rufen Sie das Nginx-Image und die Mount-Konfiguration im Docker ab
- 8. Konfigurieren Sie den PHP-Dienst in der Docker-Umgebung
1. Nginx-Quellcodepaket-Kompilierungsumgebung und Installation der entsprechenden Abhängigkeiten
1.1 Installieren Sie die Kompilierungsumgebung
#安装编译环境
[root@localhost /]# yum install -y gcc gcc-c++
1.2 Installieren Sie die PCRE-Bibliothek, die Zlib-Bibliothek und die OpenSSL-Bibliothek
-
pcre (Perl-kompatible reguläre Ausdrücke) ist eine Perl-Bibliothek, eine in C-Sprache geschriebene Funktionsbibliothek für reguläre Ausdrücke und eine leichtgewichtige Funktionsbibliothek. Das http-Modul von nginx verwendet pcre zum Parsen regulärer Ausdrücke. Daher müssen Sie die pcre-Bibliothek unter Linux installieren
-
zlib ist eine Reihe allgemeiner Open-Source-Dekomprimierungsbibliotheken, die Komprimierungs- und Dekomprimierungsfunktionen im Speicher bereitstellen und die Integrität dekomprimierter Daten erkennen können. nginx verwendet zlib, um den Inhalt des http-Pakets zu komprimieren, daher muss die zlib-Bibliothek unter Linux installiert werden.
-
OpenSSL oder Open Secure Sockets Layer ist eine Open-Source-Verschlüsselungsbibliothek für Secure Sockets Layer. Einschließlich häufig verwendeter Passwortverschlüsselungs- und -entschlüsselungsalgorithmen, häufig verwendeter Schlüsselalgorithmen, Zertifikatsverwaltung und SSL-Protokolle. Nginx unterstützt nicht nur das http-Protokoll, sondern auch https (dh die Übertragung von http über das SSL-Protokoll). Daher müssen Sie die OpenSSL-Bibliothek unter Linux installieren.
#安装pcre库
[root@localhost /]# yum install -y pcre pcre-devel
#安装zlib库
[root@localhost /]# yum install -y zlib zlib-devel
#安装openssl库
[root@localhost /]# yum install -y openssl-devel
2.Installieren Sie Nginx
2.1 Besorgen Sie sich das Quellcodepaket von der offiziellen Nginx- Website und laden Sie es herunter
[root@localhost /]# mkdir nginxfile
[root@localhost /]# cd nginxfile/
#下载nginx源码包
[root@localhost nginxfile]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
2.2 Dekomprimieren und kompilieren
#解压源码包
[root@localhost nginxfile]# tar -zxvf nginx-1.24.0.tar.gz
#编译配置
[root@localhost nginxfile]# cd nginx-1.24.0/
[root@localhost nginx-1.24.0]# ./configure --with-http_ssl_module
[root@localhost nginx-1.24.0]# ./configure --with-stream
[root@localhost nginx-1.24.0]# ./configure
#执行安装
[root@localhost nginx-1.24.0]# make && make install
3. Starten Sie den Nginx-Dienst
3.1 Führen Sie Nginx aus
Geben Sie das Verzeichnis /usr/local/sbin ein und führen Sie den Befehl ./nginx aus
[root@localhost nginx-1.24.0]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx
#执行nginx
[root@localhost sbin]# ./nginx
#查看nginx的进程开启情况
[root@localhost sbin]# ps -aux | grep nginx
root 72876 0.0 0.0 34444 384 ? Ss 21:27 0:00 nginx: master process ./nginx
nobody 72877 0.0 0.2 66624 3932 ? S 21:27 0:00 nginx: worker process
root 72887 0.0 0.0 12136 1120 pts/2 R+ 21:28 0:00 grep --color=auto nginx
3.2 Schalten Sie die Firewall aus
Schalten Sie die Firewall aus, um zu verhindern, dass der lokale Hostzugriff blockiert wird
[root@localhost sbin]# systemctl stop firewalld.service
[root@localhost sbin]# systemctl status firewalld.service
3.3 Verwenden Sie einen lokalen Browser, um die Adresse der virtuellen Maschine bereitzustellen
Die Willkommensseite bei Nginx wird angezeigt und zeigt an, dass die Nginx-Installation abgeschlossen ist.
4. Ändern Sie die Nginx-Konfigurationsdatei, um eine neue Portseite zu erstellen
cd zum Pfad /usr/local/nginx/html und erstellen Sie eine neue Datei mit der Endung .html
[root@localhost html]# touch web.html
#在文件中编写新的网页
[root@localhost html]# vim web.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
html {
color-scheme: light dark; }
body {
width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
cd in das Verzeichnis /usr/local/nginx/conf, ändern Sie die Konfigurationsdatei und fügen Sie die folgende Konfiguration hinzu
[root@localhost conf]# vim nginx.conf
server {
listen 8080; #浏览器访问虚拟机的8080端口时显示该页面
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index web.html index.htm; #访问8080端口时定位到web.html文件
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#停止nginx服务
[root@localhost sbin]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -s stop
#重新启动nginx服务
[root@localhost sbin]# ./nginx
Verwenden Sie einen lokalen Browser, um auf Port 8080 zuzugreifen und die neue Seite anzuzeigen
5. PHP installieren
5.1 Verwenden Sie yum, um PHP-FPM zu installieren
#因为源码安装的方式会需要搭载很多的依赖包,安装编译过程也比较的繁琐,所以这里采用yum源安装途径,yum仓库安装的方法比源码包安装更加的简洁快速
[root@localhost /]# yum -y install php-fpm
#启动php-fpm服务
[root@localhost /]# systemctl start php-fpm
[root@localhost /]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset:> Active: active (running) since Tue 2023-08-08 14:29:59 CST; 22s ago
5.2 Ändern Sie die PHP-Konfigurationsdatei
#定位到php-fpm的文件下
[root@localhost /]# cd /etc/php-fpm.d/
#修改www.conf文件内容
[root@localhost php-fpm.d]# vim www.conf
Kommentieren Sie den Abschnitt listen = /run/php-fpm/www.sock in der Datei www.conf aus und fügen Sie listen = 127.0.0.1:9000 hinzu
5.3 Ändern Sie die Nginx-Konfigurationsdatei
#定位到nginx/conf文件下
[root@localhost nginx]# cd /usr/local/nginx/conf/
#修改nginx.conf文件内容
[root@localhost conf]# vim nginx.conf
Kommentieren Sie den server
fehlenden location ~ \.php$
Inhalt aus
Ändern Sie den Inhalt nach dem unkommentierten location ~ \.php$
Inhalt infastcgi_param SCRIPT_FILENAME
/scripts$fastcgi_script_name
$document_root$fastcgi_script_name
Fügen Sie den Dateinamen nach server
dem ersten Absatz location /
des Inhalts hinzu (wird verwendet, um die PHP-Seitendatei zu finden, die später erstellt wird).index
web.php
5.4 PHP-Seitendateien erstellen und Tests anzeigen
#定位到nginx/html文件下
[root@localhost nginx]# cd /usr/local/nginx/html/
#创建web.php页面文件
[root@localhost html]# vim web.php
<?php
phpinfo();
?>
Nachdem alle Dateien konfiguriert sind, müssen Sie den PHP-FPM-Dienst und Nginx neu starten
#重启php-fpm服务
[root@localhost html]# systemctl restart php-fpm
[root@localhost html]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset:> Active: active (running) since Tue 2023-08-08 15:02:04 CST; 7s ago
Main PID: 76438 (php-fpm)
#重启nginx
#定位到nginx/sbin文件路径下
[root@localhost html]# cd /usr/local/nginx/sbin/
#停止nginx
[root@localhost sbin]# ./nginx -s stop
#重新启动nginx
[root@localhost sbin]# ./nginx
#查看开启情况
[root@localhost sbin]# ps -aux | grep nginx
root 76473 0.0 0.0 34444 440 ? Ss 15:03 0:00 nginx: master process ./nginx
nobody 76474 0.0 0.2 66564 4172 ? S 15:03 0:00 nginx: worker process
root 76503 0.0 0.0 12136 1192 pts/1 R+ 15:04 0:00 grep --color=auto nginx
Greifen Sie in Ihrem lokalen Browser auf die Seite web.php zu
Hinweis: Sollte der Zugriff nicht erfolgreich sein, können Sie prüfen, ob die Firewall ausgeschaltet ist.
6.Installieren Sie Docker
6.1 Überprüfen Sie die Release-Version des Betriebssystems und löschen Sie die alte installierte Docker-Version
Für Docker muss die Centos-Kernelversion höher als 3.10 sein
[root@localhost /]# uname -r
4.18.0-348.el8.x86_64
#卸载旧的docker版本
[root@localhost /]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
6.2 Konfiguration vor der Installation von Docker
#安装yum-utils
[root@localhost /]# yum install -y yum-utils
#添加阿里云仓库
[root@localhost /]# yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装makecache
[root@localhost /]# yum makecache
6.3 Docker installieren und starten
[root@localhost /]# yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Normalerweise wird ein Fehler gemeldet
Lösung:
#运行以下内容
[root@localhost /]# yum erase podman buildah
#重新进行安装
[root@localhost /]# yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Starten Sie Docker
[root@localhost /]# systemctl start docker
[root@localhost /]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset> Active: active (running) since Wed 2023-08-09 00:51:52 CST; 9s ago
Führen Sie zum Testen hello-world aus
[root@localhost /]# docker run hello-world
7. Rufen Sie das Nginx-Image und die Mount-Konfiguration im Docker ab
7.1 Nginx-Image abrufen
[root@localhost /]# docker pull nginx
#查看本地镜像
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 89da1fb6dcb9 11 days ago 187MB
hello-world latest 9c7a54a9a43c 3 months ago 13.3kB
7.2 Erstellen Sie einen Nginx-Dienst und mounten Sie die lokale Nginx-Konfigurationsdatei
#创建运行一个nginx容器同时挂载本地文件
[root@localhost ~]# docker run --name nginx-test -p 9090:80 -d -v /usr/local/nginx/conf:/usr/share/nginx/conf -v /usr/local/nginx/html:/usr/share/nginx/html -v /etc/nginx/conf.d:/usr/share/nginx/conf.d nginx
38e38f4ea4534594b8b8cdcebee6bee843debbf8a18c82904ea5ac7f63efc283
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38e38f4ea453 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:9090->80/tcp, :::9090->80/tcp nginx-test
7.3 Überprüfen Sie die Zuordnung des entsprechenden 9090-Ports
8. Konfigurieren Sie den PHP-Dienst in der Docker-Umgebung
8.1 Ziehen Sie das PHP-FPM-Image
[root@localhost /]# docker pull php:7.2-fpm
#查看本地镜像
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 89da1fb6dcb9 11 days ago 187MB
hello-world latest 9c7a54a9a43c 3 months ago 13.3kB
php 7.2-fpm 28f52b60203d 2 years ago 398MB
8.2 Container erstellen
#创建php文件挂载目录
[root@localhost /]# mkdir -p /docker/nginx/www/myphp
#创建容器
[root@localhost /]# docker run --name myphp -v /docker/nginx/www/myphp:/www/myphp -d php:7.2-fpm
dcfdfe9436d080c87d66305104df09a13e4335f6c3532fcd461982b22bf8c090
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcfdfe9436d0 php:7.2-fpm "docker-php-entrypoi…" 37 seconds ago Up 36 seconds 9000/tcp myphp
8.3 Erstellen Sie das Verzeichnis conf.d und konfigurieren Sie die .conf-Datei
[root@localhost /]# mkdir -p /docker/nginx/conf/conf.d
[root@localhost conf.d]# vim myphp.conf
Schreiben Sie den folgenden Inhalt in die Konfigurationsdatei
8.4 Nginx-Container erstellen
#创建一个nginx容器
[root@localhost conf.d]# docker run --name nginx1 -p 8088:80 -d \
> -v /docker/nginx/www:/usr/share/nginx/html \
> -v /docker/nginx/conf/conf.d:/etc/nginx/conf.d \
> --link myphp:php nginx
f761985089f1ed5abd61625ac84b90298a7f53f9c1c2013533afce05df135cc4
[root@localhost conf.d]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f761985089f1 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:8088->80/tcp, :::8088->80/tcp nginx1
dcfdfe9436d0 php:7.2-fpm "docker-php-entrypoi…" 13 minutes ago Up 13 minutes 9000/tcp myphp
8.5 Testen
Erstellen Sie unter dem Pfad /docker/nginx/www/myphp/
eine .php-Datei
[root@localhost conf.d]# cd /docker/nginx/www/myphp/
[root@localhost myphp]# vim index.php
<?php
phpinfo();
?>
Testen Sie Port 8088 mit dem lokalen Browser, um auf die PHP-Homepage zuzugreifen.