如何在Ubuntu 18.04上安裝Drupal
Drupal是全球最受歡迎的開源CMS平台之一。 它是用PHP編寫的,可用於構建各種類型的網站,從小型個人博客到大型公司,政治和政府網站。
在本教程中,我們將向您展示如何在Ubuntu 18.04計算機上安裝Drupal 8.6。 有多種安裝Drupal的方法。 本教程介紹了使用針對Drupal項目的作曲家模板drupal-project來安裝Drupal 8.6的必要步驟。
我們將使用Nginx作為Web服務器,使用最新的PHP 7.2和MySQL / MariaDB作為數據庫服務器。
先決條件
在繼續本教程之前,請確保您已滿足以下先決條件:
- 您有一個指向您的公共服務器IP的域名。 我們將使用
example.com
。 - 請按照以下說明安裝Nginx。
- 您已經為您的域安裝了SSL證書。 您可以按照以下說明安裝免費的“讓我們加密SSL”證書。
在你開始之前 #
將軟件包索引和系統軟件包更新為最新版本:
sudo apt update && sudo apt upgrade
1.創建一個MySQL數據庫
如果您的服務器上安裝了MySQL或MariaDB,則可以跳過此步驟;如果沒有,則可以通過輸入以下內容從Ubuntu的默認存儲庫中安裝MySQL 5.7服務器軟件包:
sudo apt install mysql-server
對於全新的MySQL安裝,建議運行 mysql_secure_installation
命令以提高MySQL服務器的安全性。
現在,我們需要登錄到MySQL Shell,並創建一個新的數據庫和用戶帳戶,並為用戶提供適當的授予權限。
要登錄到MySQL shell,請輸入以下命令,並在出現提示時輸入密碼:
mysql -u root -p
創建一個名為的數據庫 drupal
,用戶名為 drupaluser
並向用戶授予必要的權限,請運行以下命令:
CREATE DATABASE drupal CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON drupal.* TO 'drupaluser'@'localhost' IDENTIFIED BY 'change-with-strong-password';
2.安裝PHP#
PHP 7.2是Ubuntu 18.04中的默認PHP版本,完全受支持,建議在Drupal 8.6中使用。 由於我們將Nginx用作Web服務器,因此我們還將安裝PHP-FPM。
要安裝所有必需的PHP模塊,請運行以下命令:
sudo apt install php7.2-cli php7.2-fpm php7.2-mysql php7.2-json php7.2-opcache php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl
PHP-FPM服務將在安裝過程完成後自動啟動,您可以通過打印服務狀態來進行驗證:
systemctl status php7.2-fpm
輸出應指示fpm服務處於活動狀態並且正在運行。
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-05-19 19:54:13 UTC; 9h ago
Docs: man:php-fpm7.2(8)
Main PID: 17781 (php-fpm7.2)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 507)
CGroup: /system.slice/php7.2-fpm.service
├─17781 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
├─17796 php-fpm: pool www
└─17797 php-fpm: pool www
3.安裝Composer#
Composer是PHP的依賴管理器,我們將使用它來下載Drupal模板並安裝所有必需的Drupal組件。
要全局安裝composer,請使用以下命令下載Composer安裝程序: curl
並將文件移到 /usr/local/bin
目錄:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
通過打印作曲家版本來驗證安裝:
composer --version
輸出應如下所示:
Composer version 1.6.5 2018-05-04 11:44:59
4.安裝Drupal#
現在我們已經安裝了作曲家,我們可以繼續使用內部的作曲家模板創建一個新的Drupal項目。 /var/www/my_drupal
目錄:
sudo composer create-project drupal-composer/drupal-project:8.x-dev /var/www/my_drupal --stability dev --no-interaction
上面的命令將下載模板,獲取所有必需的php軟件包,並運行一些腳本來準備我們的項目以進行安裝。 該過程可能需要幾分鐘,如果成功,則輸出結束應類似於以下內容:
Create a sites/default/settings.php file with chmod 0666
Create a sites/default/files directory with chmod 0777
下一步是使用Drush安裝Drupal。 在下面的命令中,我們傳遞在步驟1中創建的MySQL數據庫和用戶信息:
cd /var/www/my_drupal
sudo vendor/bin/drush site-install --db-url=mysql://drupaluser:[email protected]/drupal
安裝程序將提示您以下消息,只需按Enter即可繼續。
You are about to DROP all tables in your 'drupal' database. Do you want to continue? (yes/no) [yes]:
安裝完成後,腳本將打印管理用戶名和密碼。 輸出應類似於以下內容:
[notice] Starting Drupal installation. This takes a while. Consider using the --notify global option.
[success] Installation complete. User name: admin User password: XRkC9Q5WN9
最後,我們需要設置正確的權限,以便Web服務器可以完全訪問站點的文件和目錄。 Nginx和PHP都以 www-data
用戶和 www-data
組,因此我們需要發出以下命令:
sudo chown -R www-data: /var/www/my_drupal
5.配置Nginx#
到目前為止,如果沒有檢查本教程的先決條件,則應該已經在系統上安裝了帶有SSL證書的Nginx。
為了為我們的新Drupal項目創建一個新的服務器塊,我們將使用來自Nginx官方站點的Nginx配方。
打開您的文本編輯器並創建以下文件:
sudo nano /etc/nginx/sites-available/example.com
/etc/nginx/sites-available/example.com
# Redirect HTTP -> HTTPS
server {
listen 80;
server_name www.example.com example.com;
include snippets/letsencrypt.conf;
return 301 https://example.com$request_uri;
}
# Redirect WWW -> NON WWW
server {
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/my_drupal/web;
# SSL parameters
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
# log files
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;
}
# Block access to "hidden" files and directories whose names begin with a
# period. This includes directories used by version control systems such
# as Subversion or Git to store control files.
location ~ (^|/). {
return 403;
}
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
# Don't allow direct access to PHP files in the vendor directory.
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/php7.2-fpm.sock;
}
# Fighting with Styles? This little gem is amazing.
# location ~ ^/sites/.*/files/imagecache/ { # For Drupal <= 6
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;
}
}
不要忘記用您的Drupal域替換example.com並為SSL證書文件設置正確的路徑。 所有HTTP請求都將重定向到HTTPS。 本指南中創建了此配置中使用的摘錄。
通過創建指向以下地址的符號鏈接來啟用服務器塊: sites-enabled
目錄:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
在重新啟動Nginx服務之前,請進行測試以確保沒有語法錯誤:
sudo nginx -t
如果沒有錯誤,則輸出應如下所示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
最後,通過鍵入以下命令重新啟動Nginx服務:
sudo systemctl restart nginx
6.測試安裝編號
打開瀏覽器,鍵入您的域,並假設安裝成功,將出現類似於以下的屏幕:
您可以以管理員身份登錄並開始自定義新的Drupal安裝。
7.安裝Drupal模塊和主題#
現在,您已經安裝了Drupal項目,您將需要安裝一些模塊和主題。 Drupal模塊和主題託管在一個自定義的Composer信息庫中,Drupal-project可以直接為我們進行配置。
要安裝模塊或主題,您需要做的就是 cd
到項目目錄並輸入 composer require drupal/module_or_theme_name
。 例如,如果要安裝Pathauto模塊,則需要運行以下命令:
cd /var/www/my_drupal
sudo -u www-data composer require drupal/pathauto
通過前置 sudo -u www-data
我們以用戶身份運行命令 www-data
Using version ^1.3 for drupal/pathauto
./composer.json has been updated
> DrupalProjectcomposerScriptHandler::checkComposerVersion
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
- Installing drupal/token (1.5.0): Downloading (100%)
- Installing drupal/ctools (3.2.0): Downloading (100%)
- Installing drupal/pathauto (1.3.0): Downloading (100%)
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating autoload files
> DrupalProjectcomposerScriptHandler::createRequiredFiles
從上面的輸出中可以看到,composer還為我們安裝了所有軟件包依賴項。
8.更新Drupal核心#
升級之前,最好備份文件和數據庫。 您可以使用“備份和遷移”模塊,也可以手動備份數據庫和文件。
要備份安裝文件,可以使用以下rsync命令,當然,您需要使用正確的安裝目錄路徑:
sudo rsync -a /var/www/my_drupal/ /var/www/my_drupal_$(date +%F)
要備份數據庫,我們可以使用標準 mysqldump
命令:
mysqldump -u root -p > /var/www/my_drupal_database_$(date +%F).sql
要麼 drush sql-dump
:
cd /var/www/my_drupal
vendor/bin/drush sql-dump > /var/www/my_drupal_database_$(date +%F).sql
現在我們已經創建了備份,我們可以通過運行以下命令繼續並更新所有Drupal核心文件:
sudo -u www-data composer update drupal/core webflo/drupal-core-require-dev symfony/* --with-dependencies
結論#
恭喜,您已經成功使用Composer安裝了Drupal 8,並了解了如何安裝模塊和主題。 現在,您可以開始自定義您的網站了。 Drupal 8用戶指南是學習如何管理Drupal安裝的更多信息的良好起點。 您還應該訪問Github上的Drupal Composer模板項目。
如有疑問,請在下面發表評論。
ubuntu drupal mysql mariadb cms nginx作曲家