サーバー構築日記 Linuxサーバー編(3) LAMP環境構築 後編

で、後編です。

ちなみに、Apache本体部の設定は一切しません。そちらは別サイトを参考にしてください。

Apacheの設定ファイルは/etc/httpd/conf/httpd.confですので、そのあたりで検索を行えば出てくるはずです。

今回はその周り(mod_phpなど)の設定を行います。

Apacheに必要なモジュールの導入

今回、Apacheの動作は以下のように考えます。

  • SSLによる通信を有効にする
  • perl、phpを所有者権限で実行できるようにする

これらはhttpのセキュリティ関係から言ってもかなりほしい動作だと思います。 まずはモジュールのインストールから。

# yum install mod_ssl mod_suphp

今回、phpを所有者権限で動作させるためにmod_suphpを使います。

suexec+mod_fcgid+php-cgiという手もありますが、こちらを使って簡単にいきます。

こちらの場合、設定の仕方によっては通常のphpとsuphpが共存できるという利点があります。通常はやらないですが。

SSLの設定

まずはSSLの設定から。証明書はこの手の説明ではよくある自己署名証明書でいきます。

また、説明を簡単にするために秘密鍵パスワードは設定しません。なお実際に運営するときは基本的にはパスフレーズ付きでなければ良くありません。

SSLの証明書を作ります。適当に作ったディレクトリの中(/root/sslkeyなど)でやると安全です。

# openssl genrsa 2048 > server.key
Generating RSA private key, 2048 bit long modulus
....................+++
............................................................................................................................+++
e is 65537 (0x10001)

# openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:(管理者が在住する都道府県)
Locality Name (eg, city) [Default City]:(管理者が在住する都市名)
Organization Name (eg, company) [Default Company Ltd]:PrivateCA
Organizational Unit Name (eg, section) []:Private
Common Name (eg, your name or your server's hostname) []:*.timbreofprogram.info
Email Address []:(管理者のメールアドレス)

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

# openssl x509 -days 730 -req -signkey server.key -in server.csr -out server.crt
Signature ok
subject=/C=JP/ST=(管理者が在住する都道府県)/L=(管理者が在住する都市名)/O=PrivateCA/OU=Private/CN=*.timbreofprogram.info/emailAddress=(管理者のメールアドレス)
Getting Private key

# openssl x509 -in server.crt -outform der -out server.der
# cp server.key /etc/pki/tls/private
# cp server.crt /etc/pki/tls/certs
# chmod 0600 /etc/pki/tls/private/server.key
# chmod 0600 /etc/pki/tls/certs/server.crt

なお、(管理者・・・)はすべて自分の環境に置き換えてください。server.derはSSL通信には直接関係しませんが、証明書をクライアントにインストールするときに使います。

今回はワイルドカード証明書を作っています。このサイト(www.timbreofprogram.info)に合わせて作成してあります。

このような作成方法をとるとwww.timbreofprogram.info以外でもsmtp.timbreofprogram.infoなど先頭に表記があるDNS名がこのSSL証明書で認証できます。

ただし、階層が深くなるようなパターン(sub.www.timbreofprogram.infoなど.が増えるパターン)はこの場合から外れます。

また、直接のDNS名(timbreofprogram.info)だと使えないのが問題になりますが、どうするかは使いたい方を使ってください。

作成したRSAのキー長は2048bitとしています。このごろだと最低でもこのくらいは必要ですね。

今回は証明書の期間を2年(730日)としていますが、自己署名なのでもっと長くなっても問題はあまり無いのですが、とりあえずこうしています。

最後にSSL証明書をApacheに設定して完了です。/etc/httpd/conf.d/ssl.confをviで開いて

SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key

とします。

Apache上でスクリプトを所有者権限で動かす(perl、php)

最後にこれを設定して完了です。perlの方はそもそもcgi権限で動くので設定は簡単で、たとえばVirtualHostの設定だと

<VirtualHost _default_:80>
    SuexecUserGroup centuser centuser
</VirtualHost>

とするだけで動作します。ただし、suexecの設定によっては実行されるディレクトリが制限されて

# suexec -V
 -D AP_DOC_ROOT="/var/www"
 -D AP_GID_MIN=100
 -D AP_HTTPD_USER="apache"
 -D AP_LOG_EXEC="/var/log/httpd/suexec.log"
 -D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
 -D AP_UID_MIN=500
 -D AP_USERDIR_SUFFIX="public_html"

となっている場合は/var/www以下に実行するスクリプトがないと許可されません。 強者になるとちょっとすごいやり方ですが、

  1. suexecの場所を調べる (which suexec)
  2. 対象の場所のsuexecをローカルにコピー
  3. ローカルにきたsuexecをバイナリエディタで開く。
  4. /var/www (2f 76 61 72 2f 77 77 77 00)というデータを検索
  5. この部分のデータをASCIIで8文字以内で変更(例としては/home(2f 68 6f 6d 65 00 00 00 00)にする)
  6. この状態を保存する
  7. このファイルをサーバーに送り返す
  8. 元々あったsuexecをバックアップ。送り返したsuexecをあった場所にコピー

とやると/home以下に変更されます。誰だよ、こんなやり方を考えたやつは・・・。

この辺はプログラムがどうやって静的文字列を扱っているか、という点を知っている必要があるので苦笑ですね。

普通ならシンボリックリンクやらハードリンクやらで/var/www以下に持って行くのが筋ですが・・・。

あとはphpです。まずは/etc/suphp.confから

docroot=/

;Check wheter script is within DOCUMENT_ROOT
check_vhost_docroot=true

[handlers]
;Handler for php-scripts
x-httpd-php=php:/usr/bin/php

;Handler for CGI-scripts
x-suphp-cgi=execute:!self

docroot=/var/www

;Check wheter script is within DOCUMENT_ROOT
check_vhost_docroot=true

[handlers]
;Handler for php-scripts
x-httpd-php="php:/usr/bin/php-cgi"

;Handler for CGI-scripts
x-suphp-cgi="execute:!self"

とします。基本的にはdocrootとx-httpd-phpを正しく設定します。

次に/etc/httpd/conf.d/php.confを修正します。

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

をコメントアウトして

#<FilesMatch \.php$>
#    SetHandler application/x-httpd-php
#</FilesMatch>

とします。これを修正しておかないとphpとmod_suphpが共存できないということらしいです。

あとは/etc/httpd/conf.d/suphp.confの修正です。

suPHP_Engine off

#<IfModule mod_php5.c>
#php_admin_flag engine off
#</IfModule>
#<IfModule mod_php4.c>
#php_admin_flag engine off
#</IfModule>

# suPHP_AddHandler x-httpd-php

を以下のように変更します。

suPHP_Engine on

<IfModule mod_php5.c>
    php_admin_flag engine off
</IfModule>
<IfModule mod_php4.c>
    php_admin_flag engine off
</IfModule>

suPHP_AddHandler x-httpd-php

これで「基本的」にsuphpが有効になります。 が、最後にphpを使うかsuphpを使うかを選択する必要があります。 使う場合はDirectoryの設定時に

<Directory "/var/www/html">
    Options None
    AllowOverride All
    Order allow,deny
    Allow from all
    suPHP_UserGroup centuser centuser
</Directory>

とsuphpをしたいユーザーおよびグループを記述します。

phpを通常使用したいとき(この場合、phpはApacheの実行者の権限(CentOSではapache)で動作)は

<Directory "/var/www/html">
    Options None
    AllowOverride All
    Order allow,deny
    Allow from all
    suPHP_Engine off
    AddType text/html .php
    AddHandler php5-script .php
    php_admin_flag engine on
</Directory>

とします。これでphpとsuphpを共存させるわけですね。

最後にMySQLの初期設定

で、残ったMySQLの初期設定です。 まずはサーバーを起動させます。

# service mysqld start

で、セキュリティ設定をします。ちなみにMySQL5.6以降からはrootパスワードは空ではなく初期起動時に自動生成されます。 その場合は

# cat /root/.mysql_secret

とやると初期設定されたパスワードがわかりますので。で、以下の動作をします。

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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.

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.

Disallow root login remotely? [Y/n] 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? [Y/n] 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? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

で、完了です。ちなみに各質問文の意味は

Enter current password for root 現在のrootパスワードを入力してください。
Set root password? rootパスワードを設定しますか?
Remove anonymous users? 匿名ユーザーを削除しますか?
Disallow root login remotely? リモートからのrootログインを無効にしますか?
Remove test database and access to it? テストデータベースを削除しますか?
Reload privilege tables now? 権限テーブルを読み込みし直しますか?

というわけで、基本的には質問文はyで回答すれば問題ありません。 rootパスワードはMySQL5.5以前は空ですし、MySQL5.6以降ならファイルに書いてあるものを使います。

設定が完了したらサービスの起動設定

最後に各サービスを完全に起動させます。

# service httpd restart
# service mysqld restart
# chkconfig httpd on
# chkconfig mysqld on

ちなみに、この連載記事を連続してみている場合はこれだけだとポートが開かれていないので80と443を開けて準備をしましょう。

そして残ったのは苦労したはてなダイアリーからWordPressへの移行

これでとりあえず終わりです。これがまた果てしなく長かったです。数日間かけたりphpのスクリプトを書いたり・・・。 なお、メールサーバーの設定やらほかの設定はやりません。ほかサイトの説明に任せます・・・。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

この記事のトラックバック用URL