LAMP のインストール,データベース作成,テーブル定義とレコード挿入,各種設定(インストールに tasksel を使用)(Ubuntu 上)

Ubuntu で Apache, MySQL, PHP をインストールする. tasksellamp-server タスクを用いて,これらを一括でインストールできる.

Ubuntu 24.04 LTS の公式パッケージでは次のバージョンがインストールされる(セキュリティ更新によりリビジョン番号は変動する).

サイト内の関連ページ

事前に決めておく事項

LAMP のインストール(インストールに tasksel を使用)(Ubuntu 上)

  1. tasksel のインストール,tasksel の起動
    # パッケージリストの情報を更新
    sudo apt update
    sudo apt -y install tasksel
    sudo tasksel
    
  2. LAMP server」を選ぶ
    「LAMP server」のインストールは,端末で次のように操作しても行うことができる.
    sudo tasksel --list-tasks
    sudo tasksel install lamp-server
    
  3. インストールが始まる.
  4. Apache と MySQL が稼働していることを確認する.
    systemctl status apache2
    systemctl status mysql
    

最低限の設定

MySQL データベース管理者 (root) のパスワードの設定

Ubuntu のパッケージでインストールした MySQL 8.0 では,rootauth_socket 認証(OS の root 権限による接続)に設定されている. パスワード認証に変更する場合は次のように操作する. パスワードは,英文字,数字,記号を使う.日本語は使わない

  1. MySQL が稼働した状態にする.
    systemctl status mysql
    
  2. 端末で,次のコマンドを実行する.
    sudo mysql
    
  3. MySQL のプロンプトで,次の SQL を実行する.
    alter user 'root'@'localhost' identified with caching_sha2_password by '(新しいパスワード)';
    flush privileges;
    \quit
    
  4. 匿名ユーザの削除やテストデータベースの削除などをまとめて行いたい場合は,次のコマンドを実行し,画面の質問に答える.
    sudo mysql_secure_installation
    

MySQL 一般ユーザのユーザ名,パスワードの設定

新しく MySQL にアカウントを作る.ユーザ名を mysql にしたい場合は次のように操作する. パスワードは,英文字,数字,記号を使う.日本語は使わない

sudo mysql
create user 'mysql'@'localhost' identified by '(パスワード)';
grant all privileges on *.* to 'mysql'@'localhost';
flush privileges;
\quit

ufw (ファイアウォールの設定)

MySQL に,リモート接続させたくない場合は,リモートからの接続要求を遮断するようにしておく.

  1. ufw のインストール
    # パッケージリストの情報を更新
    sudo apt update
    sudo apt -y install ufw
    
  2. ssh のポート(ポート 22)を除き,外部からの接続を遮断したいときの設定
    sudo ufw disable
    sudo ufw default deny incoming
    sudo ufw allow from any to any port ssh
    sudo ufw enable
    
  3. ufw の設定確認
    sudo ufw status
    

MySQL で,リモート接続を行わないように設定

MySQL に,リモート接続させたくない場合は,設定ファイルの確認も行っておく.

/etc/mysql/mysql.conf.d/mysqld.cnf の bind-address の行が 127.0.0.1 になっていることを確認する(ローカルからの接続要求のみを受け付ける). 設定を変更した場合は,MySQL を再起動する.

sudo systemctl restart mysql

データベースの作成と権限の設定 (Ubuntu 上)

データベースを作成するために,SQL の create database コマンドを使用する.

  1. mysql -u mysql -p」により接続する.

    「-u mysql」と指定することにより,一般ユーザ mysql での接続を行う.

    mysql -u mysql -p
    
  2. このあと,パスワードを入力してログインする.

    プロンプトが出るので, ここでは,一般ユーザ mysql のパスワードを入れる.

    画面にパスワードが表示されないのは正常動作である.

    実行後,エラーメッセージが出ないことを確認する

  3. データベース生成

    utf8mb4 の場合の生成例

    create database testdb default character set utf8mb4 collate utf8mb4_0900_ai_ci;
    
  4. show databases;」で,データベースが生成されたことを確認する.
    show databases;
    
  5. \quit」で終了する.
    \quit
    

テーブル定義とレコード挿入

  1. mysql -u mysql -p -D testdb」により接続する.

    「-u mysql」と指定することにより,一般ユーザ mysql での接続を行う. 「testdb」はデータベース名である.

    mysql -u mysql -p -D testdb
    
  2. このあと,パスワードを入力してログインする.

    プロンプトが出るので, ここでは,一般ユーザ mysql のパスワードを入れる.

    画面にパスワードが表示されないのは正常動作である.

    実行後,エラーメッセージが出ないことを確認する

  3. SQL を用いたテーブル定義

    CHECK 制約は MySQL 8.0.16 以降で機能する.

    create table order_records (
        id            integer primary key not null,
        year          integer not null CHECK ( year > 2008 ),
        month         integer not null CHECK ( month >= 1 AND month <= 12 ),
        day           integer not null CHECK ( day >= 1 AND day <= 31 ),
        customer_name text not null,
        product_name  text not null,
        unit_price    double not null check ( unit_price > 0 ),
        qty           integer not null default 1 check ( qty > 0 ),
        created_at    timestamp not null default current_timestamp,
        updated_at    timestamp not null default current_timestamp on update current_timestamp,
        check ( ( unit_price * qty ) < 200000 ) );
    
  4. SQL を用いたレコード挿入
    start transaction;
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2023, 7, 26,  'kaneko', 'orange A', 1.2, 10 );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 2, 2023, 7, 26,  'miyamoto', 'Apple M',  2.5, 2 );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 3, 2023, 7, 27,  'kaneko',   'orange B', 1.2, 8 );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2023, 7, 28,  'miyamoto',   'Apple L', 3 );
    commit;
    
  5. 確認表示
    select * from order_records;
    
  6. 更新し確認表示
    start transaction;
    update order_records set unit_price = 11.2 where id = 1;
    commit;
    select * from order_records;
    
  7. テーブル一覧の表示
    show tables;
    
  8. \quit」で終了する.
    \quit
    

CGI の設定と動作確認

  1. Apache の cgi モジュールを有効化する.
    sudo a2enmod cgi
    
  2. /etc/apache2/sites-available/000-default.conf の <VirtualHost> と </VirtualHost> のに次を書き加える.
    		ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    		<Directory "/usr/lib/cgi-bin">
    			AllowOverride None
    			Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    			Require all granted
    		</Directory>
    
  3. 設定ファイルの文法を確認する.
    sudo apache2ctl configtest
    
  4. /etc/apache2/sites-available/000-default.conf を書き替えたので,Apache を再起動する.
    sudo systemctl restart apache2
    
  5. 次の手順で動作確認する.
    1. まず,次のファイルを作る. ファイル名は /usr/lib/cgi-bin/hello.pl で保存する.
      #!/usr/bin/perl
      print "Content-type: text/html\n\n";
      print "Hello, World.";
      
    2. 次のように操作し,実行権限を与える.
      sudo chmod 755 /usr/lib/cgi-bin/hello.pl
      
    3. Web ブラウザで,次の URL を開き,「Hello, World.」と表示されることを確認する.

      http://localhost/cgi-bin/hello.pl