本页内容
- 先决条件
- 安装 LAMP 服务器
- 为 CraftCMS 创建数据库
- 使用 Compose 安装 Craft CMS
- 为 Craft CMS 配置 Apache
- 访问 Craft CMS Web 界面
- 使用 Let's Encrypt SSL 保护 Craft CMS
- 结论
Craft 是一个开源、灵活且功能丰富的内容管理系统,面向开发者和内容作者。它提供了构建强大网站所需的所有自定义功能。它允许你通过单一仪表盘管理多个站点的内容。它是 WordPress 和 Drupal 的替代方案,用于构建定制化的数字体验。它拥有数百个免费和付费插件,可帮助你添加功能。
本文将介绍如何在 Ubuntu 22.04 上使用 Apache 和 Let's Encrypt SSL 安装 Craft CMS。
先决条件
- 一台运行 Ubuntu 22.04 的服务器。
- 已将有效的域名解析到你的服务器 IP。
- 服务器上已配置 root 密码。
安装 LAMP 服务器
Craft CMS 运行在 Web 服务器上,使用 PHP 编写,并以 MariaDB 作为数据库后端。因此你需要为服务器安装所有这些软件包。
你可以执行以下命令来安装所有这些软件包:
apt-get install apache2 mariadb-server php php-cli libapache2-mod-php php-common php-json php-curl php-gd php-imagick php-json php-mbstring php-mysql php-pgsql php-zip php-intl php-xml -y
安装完所有软件包后,编辑 PHP 配置文件并更改默认设置:
nano /etc/php/8.1/php.ini
更改以下设置:
memory_limit = 512M post_max_size = 32M upload_max_filesize = 32M max_execution_time = 360
保存并关闭文件,然后重启 Apache 服务以应用更改:
systemctl restart apache2
为 CraftCMS 创建数据库
接下来,你需要为 Fork CMS 创建数据库和用户。首先,使用以下命令登录 MariaDB shell:
mysql
登录后,使用以下命令创建数据库和用户:
MariaDB [(none)]> CREATE DATABASE craftcms;
MariaDB [(none)]> GRANT ALL ON craftcms.* TO 'craftuser' IDENTIFIED BY 'password';
接下来,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
至此,已为 Craft CMS 创建好 MariaDB 数据库。你现在可以进入下一步。
使用 Compose 安装 Craft CMS
接下来,你需要安装 Composer 以下载最新版本的 Craft CMS。你可以使用以下命令安装它:
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Composer 安装完成后,进入 Apache Web 根目录并使用以下命令创建 Craft CMS 项目:
cd /var/www/html
composer create-project craftcms/craft craftcms
系统会要求你提供数据库设置、管理员用户名、密码、站点 URL,如下所示:
Which database driver are you using? (mysql or pgsql) [mysql]
Database server name or IP address: [127.0.0.1]
Database port: [3306]
Database username: [root] craftuser
Database password:
Database name: craft
Database table prefix:
Testing database credentials ... success!
Saving database credentials to your .env file ... done
Install Craft now? (yes|no) [yes]:yes
Username: [admin] admin
Email:
[email protected]
Password:
Confirm:
Site name: CraftCMS Site
Site URL: http://craftcms.example.com
Site language: [en-US]
> add foreign key fk_rlbmgnhpxsljkaunjwnsezfrnrkhwzpthfsq: {{%widgets}} (userId) references {{%users}} (id) ... done (time: 0.035s)
> populating the info table ... done
> saving default site data ... done
> saving the first user ... done
*** installed Craft successfully (time: 5.449s)
接下来,为 Craft CMS 目录设置正确的权限和所有者:
chown -R www-data:www-data /var/www/html/craftcms/
chmod -R 755 /var/www/html/craftcms/
完成后,你可以进入下一步。
为 Craft CMS 配置 Apache
接下来,你需要为 Craft CMS 创建一个 Apache 虚拟主机配置文件。你可以使用以下命令创建它:
nano /etc/apache2/sites-available/craftcms.conf
添加以下行:
<VirtualHost *:80>
ServerAdmin
[email protected]
DocumentRoot /var/www/html/craftcms/web
ServerName craftcms.example.com
<Directory /var/www/html/craftcms/web/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/craftcms/web/>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [PT,L]
</Directory>
</VirtualHost>
保存并关闭文件,然后使用以下命令启用 Apache 虚拟主机和 rewrite 模块:
a2ensite craftcms.conf
a2enmod rewrite
接下来,重启 Apache 服务以应用更改:
systemctl restart apache2
你也可以使用以下命令检查 Apache 状态:
systemctl status apache2
你将看到以下输出:
? apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-06-17 15:48:11 UTC; 31min ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 37935 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Process: 40916 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
Main PID: 37939 (apache2)
Tasks: 6 (limit: 2292)
Memory: 53.0M
CPU: 28.718s
CGroup: /system.slice/apache2.service
??37939 /usr/sbin/apache2 -k start
??40920 /usr/sbin/apache2 -k start
??40921 /usr/sbin/apache2 -k start
??40922 /usr/sbin/apache2 -k start
??40923 /usr/sbin/apache2 -k start
??40924 /usr/sbin/apache2 -k start
Jun 17 15:48:11 ubuntu2204 systemd[1]: Starting The Apache HTTP Server...
Apache Web 服务器配置完成后,你可以进入下一步。
访问 Craft CMS Web 界面
现在,打开你的 Web 浏览器并输入 URL http://craftcms.example.com 以访问 Craft CMS Web 界面。你应该会看到以下页面:

点击 go to your control panel。你将被重定向到 Craft CMS 登录页面:

输入你的管理员用户名和密码,然后点击登录按钮。你应该在以下页面看到 Craft CMS 仪表盘:

使用 Let's Encrypt SSL 保护 Craft CMS
接下来,建议使用 Let's Encrypt SSL 来保护你的网站。首先,使用以下命令安装 Certbot 客户端软件包:
apt-get install python3-certbot-apache -y
安装成功后,运行以下命令使用 Let's Encrypt SSL 保护你的网站:
certbot --apache -d craftcms.example.com
系统会要求你提供邮箱并接受服务条款,如下所示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator standalone, Installer None 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 Plugins selected: Authenticator apache, Installer apache Obtaining a new certificate Performing the following challenges: http-01 challenge for craftcms.example.com Enabled Apache rewrite module Waiting for verification... Cleaning up challenges Created an SSL vhost at /etc/apache2/sites-available/craftcms-le-ssl.conf Enabled Apache socache_shmcb module Enabled Apache ssl module Deploying Certificate to VirtualHost /etc/apache2/sites-available/craftcms-le-ssl.conf Enabling available site: /etc/apache2/sites-available/craftcms-le-ssl.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 键为你的网站安装 Let's Encrypt SSL:
Enabled Apache rewrite module Redirecting vhost in /etc/apache2/sites-enabled/craftcms.conf to ssl vhost in /etc/apache2/sites-available/craftcms-le-ssl.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://craftcms.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=craftcms.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/craftcms.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/craftcms.example.com/privkey.pem Your cert will expire on 2022-09-17. 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" - 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
结论
恭喜!你已成功在 Ubuntu 22.04 上使用 Apache 和 Let's Encrypt SSL 安装 Craft CMS。你现在可以探索 CraftCMS 的功能,并开始使用 Craft CMS 创建一个强大的网站。如果你有任何问题,欢迎随时向我提问。