本教程适用于以下操作系统版本
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
在此页面上
- 先决条件
- 入门
- 安装 Nginx 和 PHP
- 安装 Grav CMS
- 为 Grav CMS 配置 Nginx
- 访问 Grav CMS
- 使用 Let's Encrypt SSL 保护 Grav CMS 安全
- 结论
Grav 是一个免费的、开源的平面文件 CMS,不需要任何数据库。它基于 PHP,并提供一些其他 CMS(如 WordPress、Joomla 等)可能不具备的功能。它简单、易用,并带有一些关键技术,包括 Twig Templating、Markdown、YAML、Parsedown、Doctrine Cache、Gregwar Image Library 和 Symfony Console。
在本教程中,我将向您展示如何在 Ubuntu 20.04 服务器上结合 Nginx 和 Let's Encrypt SSL 安装 Grav CMS。
先决条件
- 一台运行 Ubuntu 20.04 的服务器。
- 一个指向您服务器 IP 的有效域名。
- 服务器上已配置 root 密码。
入门
首先,通过运行以下命令将系统软件包更新到最新版本:
apt-get update -y
所有软件包更新完成后,您可以继续下一步。
安装 Nginx 和 PHP
首先,您需要在系统上安装 Nginx Web 服务器、PHP 及其他 PHP 扩展。您可以使用以下命令安装所有这些组件:
apt-get install nginx php php-cli php-fpm php-common php-curl php-gd php-json php-mbstring php-xml php-zip php-opcache php-apcu unzip -y
所有软件包安装完成后,使用以下命令验证 PHP 版本:
php --version
您应该得到以下输出:
PHP 7.4.3 (cli) (built: Jul 5 2021 15:13:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
接下来,编辑 php.ini 文件并进行一些更改:
nano /etc/php/7.4/fpm/php.ini
更改以下行:
memory_limit = 256M upload_max_filesize = 100M max_execution_time = 360 max_input_vars = 1500 date.timezone = America/Chicago
保存并关闭文件,然后重启 PHP-FPM 服务以应用更改:
systemctl restart php7.4-fpm
完成后,您可以继续下一步。
安装 Grav CMS
接下来,您需要将 Grav CMS 下载到 Nginx Web 根目录。您可以使用以下命令从 Grav 下载页面下载它:
cd /var/www/html
wget https://getgrav.org/download/core/grav-admin/1.7.17
下载完成后,使用以下命令解压缩下载的文件:
unzip 1.7.17
接下来,使用以下命令将解压后的目录重命名为 grav:
mv grav-admin grav
接下来,为 grav 目录设置适当的所有权:
chown -R www-data:www-data /var/www/html/grav
完成后,您可以继续下一步。
为 Grav CMS 配置 Nginx
接下来,您需要创建一个 Nginx 虚拟主机配置文件来托管 Grav CMS。您可以使用以下命令创建它:
nano /etc/nginx/conf.d/grav.conf
添加以下行:
server {
listen 80;
server_name grav.example.com;
root /var/www/html/grav;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* /(\.git|cache|bin|logs|backup|tests)/.*$ { return 403; }
location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
location ~* /user/.*\.(txt|md|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
location ~ /(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt|\.htaccess) { return 403; }
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
完成后保存并关闭文件,然后检查 Nginx 是否存在语法错误:
nginx -t
您应该得到以下输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
最后,重启 Nginx 服务以应用更改:
systemctl restart nginx
您还可以使用以下命令验证 Nginx 的状态:
systemctl status nginx
您应该看到以下输出:
? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2021-07-09 06:45:28 UTC; 3s ago
Docs: man:nginx(8)
Process: 33099 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 33110 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 33111 (nginx)
Tasks: 2 (limit: 2353)
Memory: 2.6M
CGroup: /system.slice/nginx.service
??33111 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??33112 nginx: worker process
Jul 09 06:45:28 node1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 09 06:45:28 node1 systemd[1]: Started A high performance web server and a reverse proxy server.
一切正常后,您可以继续下一步。
访问 Grav CMS
您现在可以使用 URL http://grav.example.com 访问 Grav CMS 的 Web UI。您应该看到以下界面:

设置您的管理员用户名、密码,然后点击 Create User 按钮。您将被重定向到 Grav CMS 仪表盘:

配置页面

账户页面

插件页面

工具页面

使用 Let's Encrypt SSL 保护 Grav CMS 安全
接下来,您需要安装 Certbot 客户端软件包,以安装和管理 Let's Encrypt SSL。
首先,使用以下命令安装 Certbot:
apt-get install python3-certbot-nginx -y
安装完成后,运行以下命令在您的网站上安装 Let's Encrypt SSL:
certbot --nginx -d grav.example.com
系统将要求您提供有效的电子邮件地址并接受服务条款,如下所示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 grav.example.com Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /etc/nginx/conf.d/grav.conf
接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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/grav.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://grav.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=grav.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/grav.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/grav.example.com/privkey.pem Your cert will expire on 2021-12-30. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - 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 - We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org.
您现在可以使用 HTTPS 协议安全地访问 Grav CMS。
结论
恭喜!您已在 Ubuntu 20.04 服务器上成功安装了结合 Nginx 和 Let's Encrypt SSL 的 Grav CMS。您现在可以使用 Grav CMS 轻松创建您的网站。