注:DigitalOcean のチュートリアルを参考にしています。
1.UFWを起動する
UFW のインストールを確認して有効化します。
yuu@web2:~$ sudo ufw status 状態: 非アクティブ yuu@web2:~$ sudo apt install ufw パッケージリストを読み込んでいます… 完了 依存関係ツリーを作成しています 状態情報を読み取っています… 完了 ufw はすでに最新バージョン (0.36-6) です。 ufw は手動でインストールしたと設定されました。 アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 0 個。 yuu@web2:~$ sudo ufw enable Command may disrupt existing ssh connections. Proceed with operation (y|n)? y ファイアウォールはアクティブかつシステムの起動時に有効化されます。 yuu@web2:~$ |
2.Apacheをインストールしてファイアウォールを設定
apt でApache2をインストールします。
yuu@web2:~$ sudo apt install apache2 |
インストールは特に問題なく終了します。
HTTPトラフィックを許可する為にファイアウォールを設定します。
UFW で利用可能なアプリケーションを確認します。
yuu@web2:~$ sudo ufw app list 利用可能なアプリケーション: Apache Apache Full Apache Secure OpenSSH yuu@web2:~$ |
リストアップされている3種類のApache の意味は次の通りです。
- Apache:ポート80(暗号化されていないwebトラフィック)のみを開く
- Apache Full:ポート80(暗号化無し)とポート443(暗号化)の両方を開く
- Apache Secure:ポート443(TLS/SSL 暗号化トラフィック)のみを開く
まだHTTPSのトラフィックを許可するTLS/SSL証明書が無いのでポート80での接続のみを許可します。
yuu@web2:~$ sudo ufw allow in “Apache” ルールをアップデートしました ルールをアップデートしました(v6) |
変更を確認します。
yuu@web2:~$ sudo ufw status 状態: アクティブTo Action From — —— —- Apache ALLOW Anywhere Apache (v6) ALLOW Anywhere (v6) |
ポート80へのトラフィックはファイアウォールを通して許可されています。
Apacheが正常に動いていればwebブラウザでデフォルトのwebページを確認できます。
3.MySQLのインストール
apt でMySQLをインストールします。
yuu@web2:~$ sudo apt install mysql-server |
MySQLにプリインストールされているセキュリティスクリプトを実行します。
yuu@web2:~$ sudo mysql_secure_installation |
最初に、VALIDATE PASSWORD COMPONENT を使用するかどうかの質問がでます。
下記では使用しないように設定しています。
yuu@web2:~$ 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 Press y|Y for Yes, any other key for No: n New password: |
他の質問には全て「Yes」としました。
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 All done! |
MySQLのコンソールにログインできることを確認します。
yuu@web2:~$ sudo mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.mysql> |
MySQLコンソールを終了します。
mysql> exit Bye |
以上でMySQL サーバーをインストール出来ました。
4.PHPのインストール
LAMPスタックでPHPはMySQLのデータからApache を通してダイナミックコンテンツを生成、表示する働きをします。
php 単体に加えて、PHPがMySQLと通信するためのモジュール(php-mysql)、及びApache がPHPファイルを処理するためのライブラリ(libapache2-mod-php)が必要です。これらのパッケージを一度にインストールします。
yuu@web2:~$ sudo apt install php libapache2-mod-php php-mysql |
インストールが完了したらPHPのバージョンを確認します。
yuu@web2:~$ php -v PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( 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 |
これでLAMPスタックが構築されているはずです。
5.Apache からPHP スクリプトの動作確認
PHPのスクリプトがApacheで動作するする事を確認します。
Apacheの公開ドキュメントルートディレクトリ、/var/www/html にPHPのテストファイルを作成します。
yuu@web2:~$ sudo nano /var/www/html/info.php |
info.php に以下のスクリプトを記述してファイルを保存します。
<?php phpinfo(); |
Windows PC のwebブラウザからサーバPCのipアドレスの後ろに/info.php を付けてアクセスします。次のような画面が表示されればPHPは正常に動作しています。
作成したinfo.php ではサーバーやPHPに関する詳細情報が確認できるので、セキュリティの為に確認が完了したらファイルを削除しておきます。
yuu@web2:~$ sudo rm /var/www/html/info.php |
6.PHP とMySQL の接続確認
MySQL にテスト用のデータベースを作成し、PHPから接続してApache でwebに表示できる事を確認します。
MySQL に接続してテスト用のデータベース”network_items”を作成。
yuu@web2:~$ sudo mysql [sudo] yuu のパスワード: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.mysql> CREATE DATABASE network_items -> ; Query OK, 1 row affected (0.27 sec) mysql> SHOW DATABASES; |
作成したデータベース”network_items”のユーザー登録をしてこのデータベースに対する完全な権限を付与します。
mysql> CREATE USER ‘yuu’@’%’ IDENTIFIED WITH mysql_native_password BY ‘(パスワードを記入)‘; Query OK, 0 rows affected (0.33 sec) mysql> GRANT ALL ON network_items.* TO ‘yuu’@’%’; Query OK, 0 rows affected (0.24 sec) |
“item_list” と言う名前でテスト用のテーブルを作成します。
mysql> CREATE TABLE network_items.item_list ( -> item_id INT AUTO_INCREMENT, -> name VARCHAR(255), -> PRIMARY KEY(item_id) -> ); Query OK, 0 rows affected (0.98 sec) |
作成したテーブルに適当な情報を入れて、出来上がったテーブルを確認します。
mysql> INSERT INTO network_items.item_list (name) VALUES (“JUPITER”); Query OK, 1 row affected (0.27 sec) mysql> INSERT INTO network_items.item_list (name) VALUES (“MERCURY”); Query OK, 1 row affected (0.23 sec) mysql> INSERT INTO network_items.item_list (name) VALUES (“ReadyNAS”); Query OK, 1 row affected (0.22 sec )mysql> INSERT INTO network_items.item_list (name) VALUES (“N-70AE”); Query OK, 1 row affected (0.18 sec)mysql> INSERT INTO network_items.item_list (name) VALUES (“web2”); Query OK, 1 row affected (0.19 sec) mysql> SELECT * FROM network_items.item_list; |
MySQL のコンソールを終了します。
mysql> exit Bye |
次にitem_list の内容をweb に表示するためのテスト用スプリクトを/var/www/html に作成します。
yuu@web2:~$ sudo nano /var/www/html/item_list.php |
<?php $user = “yuu”; $password = “(パスワードを記入)”; $database = “network_items”; $table = “item_list”; try { $db = new PDO(“mysql:host=localhost;dbname=$database”, $user, $password); echo “<h2>表示テスト:テーブル item_list</h2><ol>”; echo “<h1>クエリ開始</h1><ol>”; foreach($db->query(“SELECT name FROM $table”) as $row) { echo “<li>” . $row[‘name’] . “</li>”; } echo “<h1>書き出し終了</h1><ol>”; echo “</ol>”; |
Windows PC のwebブラウザからサーバPCのipアドレスの後ろに/item_list.php を付けてアクセスします。次のような画面が表示されればPHPは正常に動作しています。
これでLAMPスタックが構築できました。