金子邦彦研究室インストールUbuntu, WSL2LAMP のインストール,データベース作成,テーブル定義とレコード挿入,各種設定(インストールに tasksel を使用)(Ubuntu 上)

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

Ubuntu で Apache, MySQL, PHP をインストールする. tasksel を用いて簡単にインストールできる.

Ubuntu 20.04では次のバージョンがインストールされる.

サイト内の関連ページ

事前に決めておく事項

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

  1. tasksel のインストール,tasksel の起動
    sudo apt -y update
    sudo apt -y install tasksel
    sudo tasksel
    

    [image]
  2. LAMP server を選ぶ

    [image]

    「LAMP server」のインストールは,端末で次のように操作しても行うことができる.

    sudo tasksel --list-tasks
    sudo tasksel install lamp-server
    
  3. インストールが始まる.

    [image]

最低限の設定

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

新しく MySQL にアカウントを作る.ユーザ名は mysqlにしたい場合は次のように操作する.

sudo su - root
mysql -u root
create user mysql identified by 'パスワード(英文字,数字,記号を使う.日本語は使わない)';
grant all on *.* to mysql;
exit
exit

[image]

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

パスワードは,英文字,数字,記号を使う.日本語は使わない

  1. MySQL/MariaDB が稼働した状態にする
  2. 端末で,次のコマンドを実行する.
    mysqladmin --user=root password "(新しいパスワード)"
    

    すでにパスワードを設定済みで,変更をしたい場合は,次のように操作.

    mysqladmin --user=root --password=(古いパスワード) password "(新しいパスワード)"
    

gufw (ファイヤウオールの設定)

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

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

    [image]

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

MySQL に,リモート接続させたくない場合は,my.cnf の設定も行っておく.

/etc/mysql/mysql.conf.d/mysqld.cnf の bind-address の行が 127.0.0.1 になっていることを確認(ローカルからの接続要求のみを受け付ける)

[image]

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

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

  1. 前準備として,MySQL のインストールディレクトリにパスを通しておくこと.
  2. mysql -u mysql -p」により接続

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

    mysql -u mysql -p
    

    [image]
  3. このあと,パスワードを入力してログインする.

    プロンプトが出るので, ここでは,MySQL データベース管理者のパスワードを入れる.

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

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

    [image]
  4. データベース生成

    utf8 の場合の生成例

    create database testdb default character set utf8 collate utf8_unicode_ci;
    

    [image]

    cp932 の場合の生成例

    create database testdb default character set cp932 collate cp932_japanese_ci;
    

    [image]
  5. show databases;」で,データベースが生成されたことを確認
    show databases;
    
  6. \quit」で終了.
    \quit
    

    [image]

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

  1. 前準備として,MySQL のインストールディレクトリにパスを通しておくこと.
  2. mysql -u mysql -p -D testdb」により接続

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

    mysql -u mysql -p -D testdb
    

    [image]
  3. このあと,パスワードを入力してログインする.

    プロンプトが出るので, ここでは,MySQL データベース管理者のパスワードを入れる.

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

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

    [image]
  4. SQL を用いたテーブル定義

    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    real 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 ) );
    

    [image]
  5. 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;
    

    [image]
  6. 確認表示

    select * from order_records; 
    

    [image]
  7. 更新し確認表示

    start transaction;
    update order_records set unit_price = 11.2 where id = 1;
    commit;
    select * from order_records;
    
  8. テーブル一覧の表示
    show tables;
    

    [image]
  9. \quit」で終了.
    \quit
    

    [image]

cgi の設定と動作確認

  1. /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>
    
  2. /etc/apache2/sites-available/000-default.conf の <VirtualHost> と </VirtualHost> の末尾に次を書き加える.
    LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so
    
  3. /etc/apache2/sites-available/000-default.conf を書き替えたので,apache を再起動する.
    sudo apachectl restart
    
  4. 次の手順で動作確認する
    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

      [image]