Ubuntu 20.04にNginx、MySQL、PHP(LEMP)をインストールする方法

Ubuntu/Debian

目次

Linux、Nginx、MySQL、PHPの組み合わせはLEMPスタックとして知られています。LEMPスタックは、PHPベースのアプリケーションで人気のWebホスティング環境です。ここで、Linuxはオペレーティングシステム、Nginxは人気のあるWebサーバー、MySQLはデータの保存に使用されるリレーショナルデータベース管理システム、PHPは広く使用されているプログラミング言語です。

この記事では、Nginx、MySQL 8.0、PHP 7.4をUbuntu Linuxシステムにインストールする方法を説明します。 LEMPスタックをUbuntuマシンにインストールすることから始めましょう。

前提条件

UbuntuでLEMPのインストールを開始する前に:

  • 実行中のUbuntu 20.04システム
  • システムにsudo提供アカウントとしてログインします。作成するには、 初期サーバーセットアップチュートリアルに従ってください。
  • サーバーを指すドメイン/サブドメイン名

Nginx Webサーバーのインストール

次に、システムにNginx Webサーバーをインストールする必要があります。 Nginxパッケージは、デフォルトのaptリポジトリで利用できます。

次のコマンドを実行してインストールします。

 sudo apt update
 sudo apt install nginx

PHP-FPMでのPHPのインストール

PHP 7.4パッケージは、Ubuntu 20.04 LTSのデフォルトリポジトリで利用できます。次のコマンドを使用して、aptキャッシュを更新し、システムにPHPをインストールします。

 sudo apt update
 sudo apt install -y php7.4 php7.4-fpm

また、アプリケーションに必要な追加のPHPモジュールをインストールします。

 sudo apt install php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-xml

システムにPHP 7.4とPHP FPMパッケージをインストールしました。以下のコマンドを実行して、PHP FPMサービスのステータスを確認しましょう。

sudo systemctl status php7.4-fpm

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-06-16 05:15:57 UTC; 34s ago
       Docs: man:php-fpm7.4(8)
    Process: 882716 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74>
   Main PID: 882699 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2283)
     Memory: 10.3M
     CGroup: /system.slice/php7.4-fpm.service
             ├─882699 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─882714 php-fpm: pool www
             └─882715 php-fpm: pool www

Jun 16 05:15:57 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Jun 16 05:15:57 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

MySQLのインストール

デフォルトのUbuntu 20.04 aptリポジトリには、MySQLサーバー8.0が含まれています。最後に、MySQLデータベース用のmysql-serverパッケージをインストールします。また、PHPを使用してMySQLサポートを使用するには、php-mysqlパッケージをインストールします。次のコマンドを使用してインストールします。

 sudo apt install mysql-server php7.4-mysql

インストーラーはrootパスワードの入力を求めます。このパスワードは、MySQL rootユーザーに対して機能します。 MySQLをインストールした後、MySQLサーバーの初期設定のために次のコマンドを実行します。スクリプトは、パスワード検証ポリシーなど、以前のMySQLバージョンよりも多くの設定を要求することがわかります。

sudo mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component? 

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

PHP-FPMでNginxを構成する

FPM / FastCGIで実行するNginx仮想ホストを作成してみましょう。このチュートリアルでは、デフォルトのVirtualHostを使用します。テキストエディターでVirtualHostホスト構成ファイルを編集します。要件に応じて新しいVirtualHostを作成できるため、新しいVirtualHostを必ず有効にしてください。

 sudo vim /etc/nginx/sites-available/example.com

以下の基本的なNginx仮想ホスト構成をphp fpm設定で使用します。以下のように設定を更新します。

変更を構成ファイルに保存し、サイトが有効なディレクトリへのリンクを作成します。

 sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

次に、Nginxサービスを再起動して変更を再読み込みします。

 sudo systemctl restart nginx

ステップ5 –サービスを管理する

Ubuntu 20.04 LTSシステムへのLAMPスタックのインストールは完了しました。以下のコマンドは、systemdで実行されているNginxおよびMySQLサービスを開始/停止または再起動するのに役立ちます。

NginxおよびMySQLサービスを再起動するには、次のように入力します。

 sudo systemctl restart nginx
 sudo systemctl restart mysql

NginxおよびMySQLサービスを開始するには、次のように入力します。

 sudo systemctl start nginx
 sudo systemctl start mysql

NginxおよびMySQLサービスを停止するには、次のように入力します。

 sudo systemctl stop nginx
 sudo systemctl stop mysql

ステップ6 –ファイアウォールルールを調整する

「http」や「https」などのサービス名を直接指定して許可することができます。 firewalldは/ etc / servicesファイルを使用して、サービスの対応するポートを決定します。

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --reload

ステップ7 –セットアップを確認する

すべてのセットアップを完了した後。次のコンテンツを含むinfo.phpファイルのWebサイトドキュメントルートを作成してみましょう。

sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php

次に、このファイルにWebブラウザーでアクセスします。サーバー上のPHPのすべての詳細を含む以下のような画面が表示されます。

ubuntu 20.04でlempスタックをセットアップする方法

おめでとう! Ubuntu 20.04 LTSシステムでLEMPサーバーが正常に構成されました。

Source

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です