使用 Nginx 安装和配置 Drupal 8,并在 CentOS 8 上加密
在此页
- 要求
- 安装 Nginx、MariaDB 和 PHP
- 配置数据库
- 下载Drupal
- 为 Drupal 配置 Nginx
- 配置 SELinux 和防火墙
- 使用 Lets Encrypt SSL 保护 Drupal
- 访问 Drupal 网站
Drupal 是一个免费、开源和可扩展的内容管理系统,个人可以使用它来创建和管理任何类型的网站。它是用 PHP 编写的,使用 MySQL/MariaDB 来存储其数据。 Drupal 提供了一组丰富的功能,可以通过数以千计的附加组件进行扩展。 Drupal 支持很多 Web 服务器,包括 Apache、Nginx、IIS、Lighttpd 和数据库 MySQL、MariaDB、MongoDB、SQLite、PostgreSQL 和 MS SQL 服务器。 Drupal 带有一个简单且用户友好的 Web UI,允许您在没有任何编码知识的情况下创建网站。
在本教程中,我们将向您展示如何在 CentOS 8 服务器上安装 Drupal 8 并使用 Lets Encrypt 免费 SSL 保护它。
要求
- 一台运行 CentOS 8 的服务器。
- 用您的服务器 IP 指向的有效域名
- 在服务器上配置了根密码。
安装 Nginx、MariaDB 和 PHP
在开始之前,您需要在您的服务器上安装 LEMP 服务器。您可以通过运行以下命令来安装它:
dnf install nginx mariadb-server php php-fpm php-cli php-mbstring php-gd php-xml php-curl php-mysqlnd php-pdo php-json php-opcache -y
安装后,启动 Nginx、MariaDB 和 php-fpm 服务,并使用以下命令使它们在系统重启后启动:
systemctl start nginx
systemctl start php-fpm
systemctl start mariadb
systemctl enable nginx
systemctl enable php-fpm
systemctl enable mariadb
配置数据库
默认情况下,MariaDB 不受保护,因此您需要对其进行保护。您可以通过运行以下命令来保护它:
mysql_secure_installation
如下图所示回答所有问题:
Enter current password for root (enter for none):
Set root password? [Y/n] Y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
完成后,使用以下命令登录 MariaDB shell:
mysql -u root -p
在出现提示时提供您的 root 密码,然后使用以下命令为 Drupal 创建数据库和用户:
MariaDB [(none)]> CREATE DATABASE drupaldb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
MariaDB [(none)]> CREATE USER IDENTIFIED BY "password";
接下来,使用以下命令授予 drupaldb 所有权限:
MariaDB [(none)]> GRANT ALL ON drupaldb.* TO IDENTIFIED BY "password";
接下来,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
下载Drupal
首先,您需要从其官方网站下载最新版本的 Drupal。您可以使用以下命令下载它:
wget https://ftp.drupal.org/files/projects/drupal-8.7.10.tar.gz
下载后,使用以下命令解压缩下载的文件:
tar -xvzf drupal-8.7.10.tar.gz
接下来,使用以下命令将提取的目录移动到 Nginx web 根目录:
mv drupal-8.7.10 /var/www/html/drupal
接下来,创建一个存放网站文件的目录,并重命名 default.settings.php 文件,如下所示:
mkdir /var/www/html/drupal/sites/default/files
cp /var/www/html/drupal/sites/default/default.settings.php /var/www/html/drupal/sites/default/settings.php
接下来,将 Drupal 目录的所有权更改为 nginx,如下所示:
chown -R nginx:nginx /var/www/html/drupal/
为 Drupal 配置 Nginx
首先,使用以下命令为 Drupal 创建一个 php-fpm 配置文件:
nano /etc/php-fpm.d/drupal.conf
添加以下行:
[drupal]
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen = /run/php-fpm/drupal.sock
pm = ondemand
pm.max_children = 50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /
完成后保存并关闭文件。然后,为 Drupal 创建一个 Nginx 虚拟主机配置文件:
nano /etc/nginx/conf.d/drupal.conf
添加以下行:
server {
listen 80;
server_name example.com;
root /var/www/html/drupal;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
# Block access to scripts in site files directory
location ~ ^/sites/[^/]+/files/.*\.php$ {
deny all;
}
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ /vendor/.*\.php$ {
deny all;
return 404;
}
location ~ '\.php$|^/update.php' {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include fastcgi_params;
# Block httpoxy attacks. See https://httpoxy.org/.
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php-fpm/drupal.sock;
}
location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
try_files $uri @rewrite;
}
# Handle private files through Drupal. Private file's path can come
# with a language prefix.
location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
try_files $uri /index.php?$query_string;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
try_files $uri @rewrite;
expires max;
log_not_found off;
}
}
保存并关闭文件。然后,重启 php-fpm 和 Nginx 服务以应用更改:
systemctl restart php-fpm
systemctl restart nginx
配置 SELinux 和防火墙
默认情况下,CentOS 8 中启用了 SELinux。因此您需要配置 SELinux 以使 Drupal 正常工作。
首先,允许 Drupal 使用以下命令写入公共和私有文件目录:
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/drupal(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/drupal/sites/default/settings.php'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/drupal/sites/default/files'
restorecon -Rv /var/www/html/drupal
restorecon -v /var/www/html/drupal/sites/default/settings.php
restorecon -Rv /var/www/html/drupal/sites/default/files
接下来,允许 Drupal 使用以下命令发送出站电子邮件:
setsebool -P httpd_can_sendmail on
接下来,您需要创建防火墙规则以允许来自外部网络的 HTTP 和 HTTPS 服务。您可以使用以下命令允许它:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
使用 Lets Encrypt SSL 保护 Drupal
Drupal 现已安装和配置。是时候使用 Lets Encrypt 免费 SSL 来保护它了。
为此,您需要在服务器上下载 certbot 客户端。您可以通过运行以下命令下载并设置正确的权限:
wget https://dl.eff.org/certbot-auto
mv certbot-auto /usr/local/bin/certbot-auto
chown root /usr/local/bin/certbot-auto
chmod 0755 /usr/local/bin/certbot-auto
现在,运行以下命令为您的 Drupal 网站获取并安装 SSL 证书。
certbot-auto --nginx -d example.com
上面的命令将首先在您的服务器上安装所有必需的依赖项。安装后,系统会要求您提供电子邮件地址并接受服务条款,如下所示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/drupal.conf
接下来,您需要选择是否将 HTTP 流量重定向到 HTTPS,如下所示:
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
键入 2 并按 Enter 键继续。安装完成后,您应该会看到以下输出:
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/drupal.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://example.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2020-03-23. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again with the "certonly" option. To non-interactively renew *all*
of your certificates, run "certbot-auto renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
访问 Drupal 网站
现在,打开您的 Web 浏览器并输入 URL https://example.com。您将被重定向到以下页面:

选择您想要的语言,然后单击“保存并继续”按钮。您应该会看到以下页面:

选择您的安装配置文件并单击“保存并继续”按钮。您应该会看到以下页面:

提供您的数据库详细信息,然后单击“保存并继续”按钮。您应该会看到以下页面:

提供您的网站名称、管理员用户名、密码,然后单击“保存并继续”按钮。您应该在以下页面中看到您的 Drupal 仪表板:

恭喜!您已经在 CentOS 8 服务器上成功安装并保护了 Drupal。