ウェブサイト検索

Ubuntu 20.04でNginxを使用してGrav CMSをインストールする方法


Ubuntu 20.04でNginxを使用してGrav CMSをインストールする方法

このチュートリアルはこれらのOSバージョンのために存在します

  • Ubuntu 20.04(フォーカルフォッサ)
  • Ubuntu 18.04 (ビオニックビーバー)

このページで

  1. 前提条件
  2. 始めに
  3. NginxとPHPをインストールする
  4. Grav CMSをインストールする
  5. Grav CMSのためにNginxを設定する
  6. Grav CMSにアクセスする
  7. Let's Encrypt SSLでGrav CMSを安全にする
  8. 結論

Gravは無料でオープンソースのフラットファイルCMSで、データベースを必要としません。これはPHPに基づいており、WordPressやJoomlaなどの他のCMSでは利用できない可能性のあるいくつかの機能を提供します。シンプルで使いやすく、Twigテンプレーティング、マークダウン、YAML、Parsedown、Doctrine Cache、Gregwar Image Library、Symfony Consoleなどの主要な技術がいくつか付属しています。

このチュートリアルでは、Ubuntu 20.04サーバーにNginxとLet's Encrypt SSLを使用してGrav CMSをインストールする方法を示します。

前提条件

  • Ubuntu 20.04を実行しているサーバー。
  • サーバーIPにポイントされた有効なドメイン名。
  • サーバーにはルートパスワードが設定されています。

始めに

まず、以下のコマンドを実行してシステムパッケージを最新バージョンに更新します:

apt-get update -y

すべてのパッケージが更新されたら、次のステップに進むことができます。

NginxとPHPをインストールする

まず、Nginxウェブサーバー、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のウェブルートディレクトリにダウンロードする必要があります。以下のコマンドを使用して、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を設定する

次に、Grav CMSをホストするためのNginx仮想ホスト設定ファイルを作成する必要があります。次のコマンドを使用して作成できます:

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のウェブUIにアクセスできるようになりました。次の画面が表示されるはずです:

管理者のユーザー名とパスワードを設定し、ユーザーの作成ボタンをクリックしてください。Grav CMSのダッシュボードにリダイレクトされます:

設定ページ

アカウントページ

プラグインページ

ツールページ

Let's Encrypt SSLを使用してGrav CMSを保護する

次に、Let's Encrypt SSLをインストールおよび管理するために、Certbotクライアントパッケージをインストールする必要があります。

まず、次のコマンドで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 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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を使用して簡単にウェブサイトを作成できます。

関連記事: