1
0

More than 1 year has passed since last update.

Nginx、PHP、MariaDBの構築手順

Posted at

前言

OracleCloudの無料vpsを利用してこの手順書を作りました(Oracle社に感謝します)。WordPress、NextCloudなどソフトが必須のphpとMariaDBの環境構築手順です。

環境

  • OS Ubuntu 22.04 LTS
  • Nginx 80443の管理
  • PHP Webアプリケーションの開発を得意とするプログラミング言語
  • MariaDB データーベース

作業

1. OracleCloudにUbuntu OSのVPS新規

2. OS環境更新/ポート開放

2.1 OS環境更新

  • インストール可能なパッケージの「一覧」を更新する。

    $ sudo apt update

    実際のパッケージのインストール、アップグレードなどはおこなわない。)

  • インストール済みのパッケージ更新をおこない、新しいバージョンにアップグレードする。

    $ sudo apt-get upgrade

    (「有効なパッケージ一覧」を元に実行されるので、 apt-get update と組み合わせて使う必要がある。)

2.2 ポート開放80443の追加

  • ファイアウォールであるiptablesのポートの開放状況調査

    $ cat /etc/iptables/rules.v4

    or

    $ sudo iptables -nL

    ubuntu@duckbooby:~$ sudo iptables -nL
    # インプット訪問はtcp22のみ、80、443開放しない
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp spt:123
    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
          
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
      
    # アウトプット全部開放
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    InstanceServices  all  --  0.0.0.0/0            169.254.0.0/16  
      
    # 略...
    
  • 80tcp443tcp の許可(開放)エントリ追加

    /etc/iptables/rules.v4 を管理者権限で開き、ssh/22tcp用に既に存在しているエントリの直下へ、80tcp443tcpの許可エントリを挿入する。

    $ sudo vim /etc/iptables/rules.v4

    vimの利用:yy行コピー、p行の下にペスト、i入力。eseキー戻る、:wq保存して閉じる

    修正したファイル:

    # CLOUD_IMG: This file was created/modified by the Cloud Image build process
    # iptables configuration for Oracle Cloud Infrastructure
      
    # See the Oracle-Provided Images section in the Oracle Cloud Infrastructure
    # documentation for security impact of modifying or removing these rule
      
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [463:49013]
    :InstanceServices - [0:0]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p udp --sport 123 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT   # ポート80を開放する
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT  # ポート443を開放する
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    -A OUTPUT -d 169.254.0.0/16 -j InstanceServices
      
    # 略...
    

    保存すると、下記のコマンドでファイアウォールを再起動する
    sudo iptables-restore < /etc/iptables/rules.v4

3. 必要環境構築/初期設定(インストール)

3.1 Nginxのインストール

$ sudo apt install nginx

インストールするとsystemctl status nginxコマンドで確認

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-12-07 04:57:42 UTC; 1 day 2h ago
       Docs: man:nginx(8)
   Main PID: 11499 (nginx)
      Tasks: 3 (limit: 1076)
     Memory: 3.5M
        CPU: 33ms
     CGroup: /system.slice/nginx.service
             ├─11499 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─11500 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
             └─11501 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Dec 07 04:57:41 duckbooby systemd[1]: nginx.service: Deactivated successfully.
Dec 07 04:57:41 duckbooby systemd[1]: Stopped A high performance web server and a reverse proxy server.
Dec 07 04:57:41 duckbooby systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 07 04:57:42 duckbooby systemd[1]: Started A high performance web server and a reverse proxy server.

3.2 PHPのインストール

$ sudo apt install php

sudo apt install php8.1-fpm php8.1-cli php8.1-common php8.1-mbstring php8.1-xmlrpc php8.1-soap php8.1-gd php8.1-xml php8.1-intl php8.1-mysql php8.1-cli php8.1-ldap php8.1-zip php8.1-curl php8.1-opcache php8.1-readline php8.1-xml php8.1-gd

インストールするとphp -vコマンドで確認

PHP 8.1.2-1ubuntu2.9 (cli) (built: Oct 19 2022 14:58:09) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.9, Copyright (c), by Zend Technologies

3.3 MariaDBのインストールと初期設定

  • 3.3.1 MariaDB をインストールする

    $ sudo apt install mariadb-server mariadb-client

    • ディフォルト設定の確認

      $ cat /etc/mysql/mariadb.conf.d/50-server.cnf

      # 90行目 : デフォルトの文字コードを確認
      # 絵文字等 4バイト長の文字を扱う場合は [utf8mb4]
      character-set-server  = utf8mb4
      collation-server      = utf8mb4_general_ci
      
  • 3.3.2 MariaDB を起動/自動起動有効する

    $ sudo systemctl start mariadb

    $ sudo systemctl enable mariadb

    • systemctlコマンドの運用(参照)
      操作 コマンド
      サービス起動 systemctl start ${Unit}
      サービス停止 systemctl stop ${Unit}
      サービス再起動 systemctl restart ${Unit}
      サービスリロード systemctl reload ${Unit}
      サービスステータス表示 systemctl status ${Unit}
      サービス自動起動有効 systemctl enable ${Unit}
      サービス自動起動無効 systemctl disable ${Unit}
      サービス自動起動設定確認 systemctl is-enabled ${Unit}
  • 3.3.3 MariaDB の初期設定

    • MariaDB用のポート3306の状況確認(不必要)

      $ ss -lntp |grep 3306

      LISTEN 0      80         127.0.0.1:3306      0.0.0.0:*     
      
    • 初期設定

      $ sudo mariadb-secure-installation

      スクリプトを実行すると、各処理を実行するかどうか訊ねられます。 (#コマンド文字 は直前の文を翻訳したもので、実際には出力されません)

      '''ここから
      
      ubuntu@duckbooby:~$ sudo mariadb-secure-installation
      
      NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
            SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
      '''#翻訳文
      注意: 実運用環境のすべての MariaDB サーバにおいて当スクリプトのすべて
           の処理を実行することが推奨されます 各手順をよくお読みください
      '''
      In order to log into MariaDB to secure it, we'll need the current
      password for the root user. If you've just installed MariaDB, and
      haven't set the root password yet, you should just press enter here.
      '''#翻訳文
      MariaDBにログインしてそれを安全にするにはroot ユーザの現在のパスワードが
      必要ですMariaDB をインストールした直後で root パスワードをまだセットして
      いない場合パスワードは空白であるためそのまま Enter キーを押して
      ください
      '''
      
      Enter current password for root (enter for none): 
      OK, successfully used password, moving on...
      '''#翻訳文
      root の現在のパスワードを入力してください (未設定なら Enter)
      完了パスワード認証に成功しました処理を続行しています...
      '''
      
      Setting the root password or using the unix_socket ensures that nobody
      can log into the MariaDB root user without the proper authorisation.
      '''#翻訳文
      root パスワードを設定することで適切な認証なしでは MariaDB  rootユーザ
      にログインできなくなります
      '''
      
      You already have your root account protected, so you can safely answer 'n'.
      #すでに root パスワードがセットされているので、「n」と回答しても安全です。
      
      Switch to unix_socket authentication [Y/n] n
       ... skipping.
      '''#翻訳文
       ... スキップ
      '''
      
      You already have your root account protected, so you can safely answer 'n'.
      '''#翻訳文
      すでに root パスワードがセットされているので、「nと回答しても安全です
      '''
      
      Change the root password? [Y/n] y
      '''#翻訳文
      root パスワードを変更しますか
      '''
      New password: 
      Re-enter new password: 
      Password updated successfully!
      Reloading privilege tables..
       ... Success!
      '''#翻訳文
      新しいパスワードroot()
      パスワード確認root
      パスワードが正常に更新されました!
      特権テーブルをリロードしています..
       ... 成功しました
      '''
      
      By default, a MariaDB installation has an anonymous user, allowing anyone
      to log into MariaDB 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.
      '''#翻訳文
      既定でMariaDB のインストール作業においては匿名ユーザを利用することができ
      ユーザアカウントの作成なしで誰でもログインすることができますこれは
      インストールを若干スムーズに行うために存在しテストのみで利用することが
      想定されていますこれらは実運用環境に移行する前に削除する必要があります
      '''
      
      Remove anonymous users? [Y/n] 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.
      '''#翻訳文
      通常root ログインはlocalhostからの接続のみで許可されるべきですそれ
      によりネットワークから root パスワードを推測できないようになります
      '''
      
      Disallow root login remotely? [Y/n] n
       ... skipping.
      '''#翻訳文
       リモートからの root ログインを禁止しますか
       ... スキップ
      '''
      
      By default, MariaDB 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.
      '''#翻訳文
       既定でMariaDBにはだれでもアクセス可能なtestという名前の
       データベースが付属していますこれもテストのみで利用されることが想定されて
       おり実運用環境に移行する前に削除する必要があります
      '''
      
      Remove test database and access to it? [Y/n] y
       - Dropping test database...
       ... Success!
       - Removing privileges on test database...
       ... Success!
       '''#翻訳文
       test データベースとそのデータベースへのアクセスを削除しますか
       - test データベースを削除しています...
       ... 成功しました
       - test データベースに対する権限を削除しています...
       ... 成功しました
      '''
      
      Reloading the privilege tables will ensure that all changes made so far
      will take effect immediately.
      '''#翻訳文
      権限テーブルを再読み込みするとここまでのすべての変更をすぐに反映させる
      ことができます
      '''
      
      Reload privilege tables now? [Y/n] y
       ... Success!
       '''#翻訳文
      今すぐ権限テーブルを再読み込みしますか
       ... 成功しました
      '''
      
      Cleaning up...
      '''#翻訳文
      クリーンアップしています...
      '''
      
      All done!  If you've completed all of the above steps, your MariaDB
      installation should now be secure.
      '''#翻訳文
      完了しました 以上のすべての処理を実行していれば安全に MariaDB 
      インストールできているはずです
      '''
      
      Thanks for using MariaDB!
      '''#翻訳文
      MariaDB のご利用ありがとうございます
      '''
      
      ここまで'''
      

環境構築までの手順は以上です。

お疲れ様でした。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0