如何在 Ubuntu 上安装 Craft CMS
Craft 是一个开源、灵活且用户友好的内容管理系统,在全球拥有庞大的开发者社区和社区。它是 WordPress 和 Drupal 的一个非常好的替代品,用于创建定制的数字体验。它有一个直观且易于访问的控制面板,允许您通过网络浏览器管理您的网站。它提供了数百个免费和付费插件,您可以使用它们来添加附加功能。
在这篇文章中,我们将解释如何在 Ubuntu 20.04 上使用 Apache 和 Let's Encrypt SSL 安装 Craft CMS。
先决条件
- 运行 Ubuntu 20.04 的服务器。
- 指向您服务器 IP 的有效域名。
- 服务器上配置了 root 密码。
安装 Apache、MariaDB 和 PHP
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/7.4/php.ini
更改以下设置:
memory_limit = 512M
post_max_size = 32M
upload_max_filesize = 32M
max_execution_time = 360
保存并关闭文件并重新启动 Apache 服务以应用更改:
systemctl restart apache2
为 Fork CMS 创建数据库
接下来,您需要为 Fork CMS 创建数据库和用户。首先,使用以下命令登录 MariaDB shell:
mysql
登录后,使用以下命令创建数据库和用户:
MariaDB [(none)]> CREATE DATABASE craft;
MariaDB [(none)]> GRANT ALL ON craft.* TO 'craftuser' IDENTIFIED BY 'password';
接下来,使用以下命令删除授权并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
现在 Craft CMS 的 MariaDB 数据库已经创建。您现在可以继续下一步。
安装 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
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
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 虚拟主机和重写模块:
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 Sat 2022-04-02 08:39:25 UTC; 5s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 17643 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 17647 (apache2)
Tasks: 6 (limit: 2348)
Memory: 13.4M
CGroup: /system.slice/apache2.service
??17647 /usr/sbin/apache2 -k start
??17648 /usr/sbin/apache2 -k start
??17649 /usr/sbin/apache2 -k start
??17650 /usr/sbin/apache2 -k start
??17651 /usr/sbin/apache2 -k start
??17652 /usr/sbin/apache2 -k start
Apr 02 08:39:25 ubunt4 systemd[1]: Starting The Apache HTTP Server...
配置 Apache Web 服务器后,您可以继续下一步。
访问 Craft CMS Web 界面
现在打开您的网络浏览器并输入 URL http://craftcms.example.com 以访问 Craft CMS 网络界面。您应该看到以下页面:
单击转到控制中心按钮。您将被重定向到 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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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-07-23. 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 20.04 上成功安装了带有 Apache 和 Let's Encrypt SSL 的 Craft CMS。您现在可以开始使用 Craft CMS 创建您的网站。如果您有任何疑问,请随时与我联系。