如何在 Ubuntu 22.04 LTS 上安装 Drupal with Docker。
Drupal是一个开源平台,用于构建令人惊叹的数字体验。它是由一个专门的社区制作的。Drupal使用模块化架构,允许开发人员通过安装和配置模块向核心系统添加功能。Drupal有数千个模块可用,它们提供了广泛的功能,例如电子商务,论坛和社交媒体集成。
在 Ubuntu 22.04 LTS Jammy Jellyfish 上安装 Drupal with Docker(Docker)
第 1 步。首先,通过在终端中运行以下命令,确保所有系统软件包都是最新的。apt
sudo apt update sudo apt upgrade
第 2 步。安装 Docker。
默认情况下,Docker 在 Ubuntu 22.04 基础存储库上不可用。现在运行以下命令将 Docker 存储库添加到系统中:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
接下来,将 GPG 密钥导入您的系统:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
启用存储库后,现在使用以下命令安装最新版本的 Docker 包:
sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
您可以验证 Docker 是否已安装以及当前版本:
docker -v
成功安装后,启用 Docker(在系统启动时自动启动)、启动并使用以下命令验证状态:
sudo systemctl enable docker sudo systemctl start docker sudo systemctl status docker
默认情况下,Docker 需要根权限。如果要避免在每次运行命令时使用,请将用户名添加到组中:sudodockerdocker
sudo usermod -aG docker $(whoami)
su - ${USER}
确认您的用户已添加到 Docker 组:
groups
有关安装和管理 Docker 的其他资源,请阅读下面的帖子:
- 如何在 Ubuntu Linux √ 上安装 Docker。
第 3 步。为 Drupal 创建 Docker Compose 文件。
首先,为 Drupal 配置创建一个目录:
mkdir ~/drupal cd ~/drupal
现在我们使用您喜欢的文本编辑器创建并打开 Docker 撰写文件:
nano docker-compose.yml
添加以下文件:
services:
mysql:
image: mysql:8.0
container_name: mysql
restart: unless-stopped
env_file: .env
volumes:
- db-data:/var/lib/mysql
networks:
- internal
drupal:
image: drupal:10-fpm-alpine
container_name: drupal
depends_on:
- mysql
restart: unless-stopped
networks:
- internal
- external
volumes:
- drupal-data:/var/www/html
webserver:
image: nginx:1.22.1-alpine
container_name: webserver
depends_on:
- drupal
restart: unless-stopped
ports:
- 80:80
volumes:
- drupal-data:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
- certbot-etc:/etc/letsencrypt
networks:
- external
certbot:
depends_on:
- webserver
image: certbot/certbot
container_name: certbot
volumes:
- certbot-etc:/etc/letsencrypt
- drupal-data:/var/www/html
command: certonly --webroot --webroot-path=/var/www/html --email admin@your-domain --agree-tos --no-eff-email --staging -d your-domain.com -d www.your-domain.com
networks:
external:
driver: bridge
internal:
driver: bridge
volumes:
drupal-data:
db-data:
certbot-etc:
保存并关闭文件。
第 4 步。为 nginx 配置 Docker Compose。
首先,我们为 Nginx 配置创建目录:
mkdir nginx-conf
接下来,使用您喜欢的文本编辑器创建并打开 Nginx 文件:
nano nginx-conf/drupal.conf
添加以下文件:
server {
listen 80;
listen [::]:80;
server_name your-domain.com;
index index.php index.html index.htm;
root /var/www/html;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
rewrite ^/core/authorize.php/core/authorize.php(.*)$ /core/authorize.php$1;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass drupal:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /.ht {
deny all;
}
location = /favicon.ico {
log_not_found off; access_log off;
}
location = /robots.txt {
log_not_found off; access_log off; allow all;
}
location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
完成后,保存并关闭文件。
第5步。配置 Docker for SSL。
在生成 SSL 证书之前,我们使用以下命令启动容器:
docker compose up -d
接下来,使用以下命令打开 docker 撰写文件:
nano docker-compose.yml
替换 Certbot 服务部分中的标志并将其替换为标志:--staging--force-renewal
certbot:
depends_on:
- webserver
image: certbot/certbot
container_name: certbot
volumes:
- certbot-etc:/etc/letsencrypt
- drupal-data:/var/www/html
command: certonly --webroot --webroot-path=/var/www/html --email admin@your-domain.com --agree-tos --no-eff-email --staple-ocsp --force-renewal -d your-domain.com
保存并关闭文件,然后再次运行命令以重新创建 Certbot 容器:docker compose up
docker compose up --force-recreate --no-deps certbot
第 6 步。为 SSL 配置 Nginx。
首先,我们使用以下命令停止 Nginx 服务器:
docker stop webserver
接下来,为 SSL 配置创建一个新的 Nginx 文件:
nano nginx-conf/drupal-ssl.conf
添加以下文件:
server {
listen 80;
listen [::]:80;
server_name your-domain.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
rewrite ^ https://$host$request_uri? permanent;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name your-domain.com;
index index.php index.html index.htm;
root /var/www/html;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/your-domain.com/chain.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_ecdh_curve secp384r1;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
rewrite ^/core/authorize.php/core/authorize.php(.*)$ /core/authorize.php$1;
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass drupal:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /.ht {
deny all;
}
location = /favicon.ico {
log_not_found off; access_log off;
}
location = /robots.txt {
log_not_found off; access_log off; allow all;
}
location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
保存并关闭文件,然后确保 Nginx 容器侦听端口 443:
nano docker-compose.yml
按照配置:
webserver:
image: nginx:1.22.1-alpine
container_name: webserver
depends_on:
- drupal
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- drupal-data:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
- certbot-etc:/etc/letsencrypt
- /etc/ssl/certs/dhparam.pem:/etc/ssl/certs/dhparam.pem
networks:
- external
保存并关闭该文件,然后使用以下命令删除较旧的 HTTP 配置文件:
rm nginx-conf/drupal.conf
接下来,生成一个我们已经在上面配置的 Diffie-Hellman 组证书:
sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
最后,重新创建 Nginx 容器:
docker compose up -d --force-recreate --no-deps webserver
步骤 7.配置防火墙。
现在我们使用Drupal设置了一个简单防火墙(UFW),以允许在默认Web端口80和443上进行公共访问:
sudo ufw allow OpenSSH sudo ufw allow http sudo ufw allow https sudo ufw enable
第8步。访问Drupal Web界面。
现在打开您的Web浏览器并使用URL访问Drupal Web UI。您将被重定向到以下页面:https://your-domain.com

感谢您使用本教程在 Ubuntu 系统上安装 Drupal with Docker。有关其他帮助或有用信息,我们建议您查看Drupal官方网站。