このトピックでは、Elastic Compute Service (ECS) インスタンスにLNMPスタック (Linux、NGINX、MySQL、およびPHP) をデプロイする方法について説明します。
前提条件
パブリックIPアドレスが自動的にECSインスタンスに割り当てられます。 または、elastic IPアドレス (EIP) がECSインスタンスに関連付けられます。 パブリック帯域幅を有効にする方法については、「パブリック帯域幅の有効化」をご参照ください。
インバウンドルールは、ECSインスタンスのセキュリティグループに追加され、ポート22と80を開きます。 セキュリティグループルールの追加方法については、「セキュリティグループルールの追加」をご参照ください。
ECSインスタンスには少なくとも4 GiBのメモリがあります。
LNMPスタックのデプロイ
Alibaba Cloud LinuxおよびCentOS 8
すべてのコンテンツは、次のアドレスにあるデフォルトのCentOS 8リポジトリから削除され http://mirror.centos.org/centos/8/
。 Alibaba CloudでデフォルトのCentOS 8リポジトリを引き続き使用すると、エラーが報告されます。 CentOS 8リポジトリアドレスを変更する必要があります。 詳細については、「CentOS 8リポジトリアドレスの変更」をご参照ください。
公式NGINXリポジトリからNGINXをインストールします。
説明デフォルトでは、最新の安定バージョンのNGINXがインストールされます。 別のNGINXバージョンが必要な場合は、
sudo dnf search nginx -- showduplicates
コマンドを実行してサポートされているNGINXバージョンを検索し、インストールするバージョンの番号をインストールコマンドに追加します。 たとえば、バージョン1.24.0
をインストールする場合は、sudo dnf -y install nginx-1.24.0
コマンドを実行します。# Add an official NGINX repository. sudo tee /etc/yum.repos.d/nginx.repo <<-'EOF' [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/8/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true EOF # Install NGINX. sudo dnf -y install nginx # Start NGINX and configure NGINX to automatically start on system startup. sudo systemctl start nginx sudo systemctl enable nginx
MySQL をインストールします。
説明インスタンスがAlibaba Cloud Linux 3を実行している場合、以前のバージョンのOpenSSLライブラリと互換性のある
compat-openssl10
をインストールする必要があります。sudo yum install -y compat-openssl10
# Add an official MySQL repository. sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm # Install MySQL. sudo dnf install -y mysql-server # Start MySQL and configure MySQL to automatically start on system startup. sudo systemctl start mysqld sudo systemctl enable mysqld
rootユーザーのデフォルトの初期パスワードを照会します。
インスタンスがAlibaba Cloud Linux 3を実行している場合、次のコマンドを実行します。
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
インスタンスがCentOS 8を実行している場合、ルートユーザーには初期パスワードがありません。
MySQLのrootユーザーの新しいパスワードを指定します。 次のコマンドで、
<oldpwd>
を初期パスワードに、<newpwd>
を新しいパスワードに置き換えます。 インスタンスがCentOS 8を実行している場合は、<oldpwd>
を空の文字列に置き換え、パスワードの入力を求められたらEnterキーを押して次の行にスキップします。重要パスワードの長さは8文字以上で、少なくとも1つの大文字、1つの小文字、1つの数字、および1つの特殊文字を含む必要があります。
sudo mysqladmin -uroot -p'<oldpwd>' password '<newpwd>'
PHPをインストールします。
説明この例では、PHP 8.4が使用されます。 別のPHPバージョンが必要な場合は、インストールするPHPバージョンに基づいてモジュール名を変更します。 たとえば、PHP 8.1をインストールする場合は、モジュール名を
php:remi-8 1
に変更します。# Specify the remi repository and enable the php:remi-8.4 module. sudo rpm -Uvh http://mirrors.cloud.aliyuncs.com/remi/enterprise/remi-release-8.rpm --nodeps sudo dnf install -y yum-utils && sudo dnf module enable -y php:remi-8.4 # Install PHP, PHP FastCGI Process Manager (PHP-FPM), and the MySQL extension. sudo dnf install -y php php-fpm php-mysqlnd # Start PHP-FPM and configure PHP-FPM to automatically start on system startup. sudo systemctl start php-fpm sudo systemctl enable php-fpm
LNMPスタックを確認します。
設定ファイル内の
PHP-FPM
のデフォルトのリスニングアドレスを照会します。sudo grep '^listen =' /etc/php-fpm.d/www.conf
ソケットファイルのアドレスが返されると、PHP-FPMはソケットファイルをリッスンします。
127.0.0.1:9000
が返された場合、PHP-FPMはローカルポート9000でリッスンします。
tee
コマンドを実行して/etc/nginx/conf.d/default.conf
ファイルを変更し、PHP転送ルールを追加します。重要PHP-FPM
のリスニングアドレスが127.0.0.1:9000
の場合、fastcgi_passパラメーターを127.0.0.1:9000
に設定します。sudo tee /etc/nginx/conf.d/default.conf <<-'EOF' server { listen 80; server_name localhost; root /usr/share/nginx/html; location / { index index.php index.html index.htm; } location ~ .php$ { root /usr/share/nginx/html; fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } EOF
NGINXを再起動して、設定ファイルの変更を有効にします。
sudo systemctl restart nginx
tee
コマンドを実行して、/usr/share/nginx/html
ディレクトリにtest. PHP
という名前のphpファイルを作成し、MySQL接続のテストに使用するコードをファイルに追加します。<username>
をMySQLのユーザー名に、<password>
をMySQLのパスワードに置き換えます。sudo tee /usr/share/nginx/html/test.php <<-'EOF' <?php $servername = "localhost"; $username = "<username>"; $password = "<password>"; $conn = new mysqli($servername, $username, $password); if ($conn->connect_error) { die("fail: " . $conn->connect_error); } echo "success\n"; ?> EOF
オンプレミスマシンのwebブラウザーのアドレスバーに、
http:// <ECSインスタンスのパブリックIPアドレス>/test.php
と入力します。success
が返された場合、PHPプロキシを使用してMySQLに接続されます。
Alibaba Cloud LinuxおよびCentOS 7
公式NGINXリポジトリからNGINXをインストールします。
説明デフォルトでは、最新の安定バージョンのNGINXがインストールされます。 別のNGINXバージョンが必要な場合は、
sudo yum search nginx -- showduplicates
コマンドを実行してサポートされているNGINXバージョンを検索し、インストールするバージョンの番号をインストールコマンドに追加します。 たとえば、バージョン1.24.0
をインストールする場合は、sudo yum -y install nginx-1.24.0
コマンドを実行します。# Add an official NGINX repository. sudo tee /etc/yum.repos.d/nginx.repo <<-'EOF' [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true EOF # Install NGINX. sudo yum -y install nginx # Start NGINX and configure NGINX to automatically start on system startup. sudo systemctl start nginx sudo systemctl enable nginx
MySQL をインストールします。
# Add an official MySQL repository. sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm # Install MySQL. sudo yum install -y mysql-server # Start MySQL and configure MySQL to automatically start on system startup. sudo systemctl start mysqld sudo systemctl enable mysqld
rootユーザーのデフォルトの初期パスワードを照会します。
echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
MySQLのrootユーザーの新しいパスワードを指定します。 次のコマンドで、
<oldpwd>
を初期パスワードに、<newpwd>
を新しいパスワードに置き換えます。重要パスワードの長さは8文字以上で、少なくとも1つの大文字、1つの小文字、1つの数字、および1つの特殊文字を含む必要があります。
sudo mysqladmin -uroot -p'<oldpwd>' password '<newpwd>'
PHPをインストールします。
# Specify the remi repository and enable the remi-php83 module. sudo rpm -Uvh http://mirrors.cloud.aliyuncs.com/remi/enterprise/remi-release-7.rpm --nodeps sudo yum install -y yum-utils && sudo yum-config-manager --enable remi-php83 # Install PHP, PHP-FPM, and the MySQL extension. sudo yum install -y php php-fpm php-mysqlnd # Start PHP-FPM and configure PHP-FPM to automatically start on system startup. sudo systemctl start php-fpm sudo systemctl enable php-fpm
LNMPスタックを確認します。
設定ファイル内の
PHP-FPM
のデフォルトのリスニングアドレスを照会します。sudo grep '^listen =' /etc/php-fpm.d/www.conf
ソケットファイルのアドレスが返されると、PHP-FPMはソケットファイルをリッスンします。
127.0.0.1:9000
が返された場合、PHP-FPMはローカルポート9000でリッスンします。
tee
コマンドを実行して/etc/nginx/conf.d/default.conf
ファイルを変更し、PHP転送ルールを追加します。重要PHP-FPM
のリスニングアドレスがソケットファイルのアドレスの場合、127.0.0.1:9000
をunix:<path>
に変更します。 <path> をソケットファイルのアドレスに置き換えます。sudo tee /etc/nginx/conf.d/default.conf <<-'EOF' server { listen 80; server_name localhost; root /usr/share/nginx/html; location / { index index.php index.html index.htm; } location ~ .php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } EOF
NGINXを再起動して、設定ファイルの変更を有効にします。
sudo systemctl restart nginx
tee
コマンドを実行して、/usr/share/nginx/html
ディレクトリにtest. PHP
という名前のphpファイルを作成し、MySQL接続のテストに使用するコードをファイルに追加します。<username>
をMySQLのユーザー名に、<password>
をMySQLのパスワードに置き換えます。sudo tee /usr/share/nginx/html/test.php <<-'EOF' <?php $servername = "localhost"; $username = "<username>"; $password = "<password>"; $conn = new mysqli($servername, $username, $password); if ($conn->connect_error) { die("fail: " . $conn->connect_error); } echo "success\n"; ?> EOF
オンプレミスマシンのwebブラウザーのアドレスバーに、
http:// <ECSインスタンスのパブリックIPアドレス>/test.php
と入力します。success
が返された場合、PHPプロキシを使用してMySQLに接続されます。
Ubuntu 20.04以降
公式NGINXリポジトリからNGINXをインストールします。
# Update installed software in the system and the package management tool. sudo apt update -y # Install the dependencies required by NGINX. sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring # Import an official NGINX signature key. curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null # Specify the Advanced Packaging Tool (APT) repository. echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list # Install NGINX. sudo apt install -y nginx
ソフトウェアパッケージリストを更新し、MySQLサーバーをインストールします。
sudo apt update -y && sudo apt install -y mysql-server
MySQLサーバーの
root
ユーザーが使用するパスワードとID認証プラグインを変更します。 次のコマンドの<newpwd>
を実際のパスワードに置き換えます。重要rootユーザーが使用するデフォルトのID認証プラグインは
auth_socket
です。 コマンドの実行後、パスワードの入力を求められます。 Enterキーを押して次の行にスキップします。sudo mysql -uroot -p -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '<newpwd>';" -e "FLUSH PRIVILEGES;"
PHPをインストールします。
説明sudo apt search php
コマンドを実行して、インストール可能なすべてのPHPバージョンを照会します。 別のPHPバージョンをインストールする場合は、次のコマンドのバージョン番号を実際のバージョン番号に置き換えます。 たとえば、PHP 8.1をインストールする場合は、sudo apt install -y php8.1 php8.1-fpm php8.1-mysql
コマンドを実行します。# Install the software-properties-common package and add the ppa:ondrej/php Personal Package Archive (PPA) repository. sudo apt update && sudo apt install -y software-properties-common && sudo add-apt-repository -y ppa:ondrej/php # Install PHP 8.4 and the related components, including PHP-FPM and the MySQL extension. sudo apt install -y php8.4 php8.4-fpm php8.4-mysql
LNMPスタックを確認します。
設定ファイル内の
PHP-FPM
のデフォルトのリスニングアドレスを照会します。<version>
を実際のPHPバージョンに置き換えます。 たとえば、PHP 8.4を使用する場合は、<version>
を8.4に置き換えます。sudo grep '^listen =' /etc/php/<version>/fpm/pool.d/www.conf
ソケットファイルのアドレスが返されると、PHP-FPMはソケットファイルをリッスンします。
127.0.0.1:9000
が返された場合、PHP-FPMはローカルポート9000でリッスンします。
tee
コマンドを実行して/etc/nginx/conf.d/default.conf
ファイルを変更し、PHP転送ルールを追加します。 <listen> を実際のリスニングアドレスに置き換えます。 ソケットファイルのアドレスがリスニングアドレスとして使用されている場合は、unix:
プレフィックスをアドレスに追加します。重要ソケットファイルをリッスンするには、アカウントにソケットファイルの読み取りおよび書き込み権限が必要です。
sudo chmod 666 <path>
コマンドを実行し、前述の権限を付与します。 <path> をソケットファイルの実際のアドレスに置き換えます。# Remove the default site configurations. sudo rm -f /etc/nginx/sites-enabled/* # Modify the configuration file. sudo tee /etc/nginx/conf.d/default.conf <<-'EOF' server { listen 80; server_name localhost; root /usr/share/nginx/html; location / { index index.php index.html index.htm; } location ~ .php$ { root /usr/share/nginx/html; fastcgi_pass <listen>; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } EOF
NGINXを再起動して、設定ファイルの変更を有効にします。
sudo systemctl restart nginx
tee
コマンドを実行して、/usr/share/nginx/html
ディレクトリにtest. PHP
という名前のphpファイルを作成し、MySQL接続のテストに使用するコードをファイルに追加します。<username>
をMySQLのユーザー名に、<password>
をMySQLのパスワードに置き換えます。sudo tee /usr/share/nginx/html/test.php <<-'EOF' <?php $servername = "localhost"; $username = "<username>"; $password = "<password>"; $conn = new mysqli($servername, $username, $password); if ($conn->connect_error) { die("fail: " . $conn->connect_error); } echo "success\n"; ?> EOF
オンプレミスマシンのwebブラウザーのアドレスバーに、
http:// <ECSインスタンスのパブリックIPアドレス>/test.php
と入力します。success
が返された場合、PHPプロキシを使用してMySQLに接続されます。
よくある質問
ページがホストされているECSインスタンスのパブリックIPアドレスを使用してtest.phpページにアクセスできないのはなぜですか。
考えられる原因と解決策:
ECSインスタンスのセキュリティグループでポート80が開いていない、ECSインスタンスでシステムファイアウォールが有効になっている、またはポート80が別のサービスで使用されている。
上記の原因に基づいて問題をトラブルシューティングする方法については、インスタンスにデプロイされたサービスにアクセスできない場合はどうすればよいですか。
MySQLへのリモートアクセスを許可する方法?
非rootアカウントを作成し、そのアカウントを使用してMySQLへのリモートアクセスを許可します。 詳細については、「LinuxインスタンスへのMySQLのデプロイ」をご参照ください。
NGINXの設定ファイルとログファイルはどこにありますか?
デフォルトでは、NGINXのログファイルは
/var/log/nginx/
ディレクトリに保存されます。デフォルトでは、NGINXのメイン設定ファイルは
/etc/nginx/nginx.conf
です。デフォルトでは、NGINXは、名前の接尾辞が
. conf
で、/etc/nginx/conf.d
ディレクトリに移動します。