x.x.x.x

| HOME | FrontPage | 記事編集 | RSS |

UbuntuWWWサーバのセットアップ   OS::Linux::Ubuntu

  1. 1. MySQL
  2. 2. Apache2
  3. 2-1. apache2.conf
  4. 2-2. setenvif.conf
  5. 2-3. sites-available
  6. 2-4. ディレクトリ作成
  7. 2-5. cgi有効
  8. 2-6. suexec有効
  9. 2-6-1. 追記
  10. 3. iptables
  11. 4. まとめて再起動

最近セットアップしすぎなので覚えてる内に最速設定をメモ。

# apt-get install apache2
# apt-get install mysql-server
# apt-get install libapache2-mod-perl2
# apt-get install php5
# apt-get install php5-mysql
1. MySQL

MySQLから設定開始。とりあえず全部utf8にしておく。

# vim /etc/mysql/my.cnf
[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8
skip-character-set-client-handshake

[mysql]
default-character-set=utf8

データベースwwwを作り、ユーザwwwuserに全権限を付ける。

$ mysql -p -u root
> create database www;
> grant all on www.* to wwwuser@localhost identified by 'PASSWORD';
> exit;
2. Apache2

まぁいつものパターンで。

2-1. apache2.conf

なぜかServerNameが行方不明になったのでapache2.confの何処かに追加。

# vim /etc/apache2/apache2.conf
ServerName testserver
2-2. setenvif.conf

気に入らないサービスを403。

# vim /etc/apache2/mods-enabled/setenvif.conf 

このconfigはフィクションです。

#
# uzai access
#
SetEnvIfNoCase User-Agent "naver" drop
SetEnvIfNoCase User-Agent "arachmo" drop
SetEnvIfNoCase User-Agent "ninja" drop
SetEnvIfNoCase User-Agent "indy" drop
SetEnvIfNoCase User-Agent "archive" drop

ついでにログを保存しない条件を設定。

#
# uzai log
#
SetEnvIf Request_URI "\.(gif|jpg|jpeg|png|bmp|png|css|js)$" no_log
SetEnvIf Remote_Addr "^127\.0\.0" no_log
2-3. sites-available

上で設定した条件をバーチャルホストで反映。

# vim /etc/apache2/sites-available/default

dropを落としてno_logのログを取らない。
ついでにファイル名.plでmod_perl有効。

<VirtualHost *>
    ServerAdmin speed@snoop.to
    DocumentRoot /var/www/wwwuser/public_html/
    ServerName testserver
    SuexecUserGroup wwwuser wwwuser
    <Directory /var/www/wwwuser/public_html/>
        Options ExecCGI Includes Indexes FollowSymLinks
        AllowOverride None
        <Files *.pl>
            SetHandler perl-script
            PerlSendHeader On
            PerlResponseHandler ModPerl::Registry
        </Files>
        Order allow,deny
        allow from all
        deny from env=drop
    </Directory>

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel error

    ErrorLog /var/www/wwwuser/logs/error.log
    CustomLog /var/www/wwwuser/logs/access.log combined env=!no_log
</VirtualHost>
2-4. ディレクトリ作成

バーチャルホストで定義したディレクトリを作成。

# mkdir /var/www/wwwuser
# mkdir /var/www/wwwuser/public_html
# mkdir /var/www/wwwuser/logs
# chown -R wwwuser.wwwuser /var/www/wwwuser

/var/www配下に作るのはsuExecの都合。

2-5. cgi有効

/etc/apache2/apache2.confの何処かに↓の1行を追加。

AddHandler cgi-script .cgi

嫌がらせのようにいつもこれが無いな。
今回はServerNameも無いし、Apache2のメンテナはバカじゃないのか。

2-6. suexec有効

最新のubuntuにapache2-suexec-customなるパッケージが追加されたので使ってみる。

# apt-get install apache2-suexec-custom

パッケージの用途は説明が面倒なので要検索。
ま、うちには必要ないけど、新しいものは良いものだ。
例によってファイルが足らないので足す。

LoadModule suexec_module /usr/lib/apache2/modules/mod_suexec.so

↑の1行を書いたファイルを/etc/apache2/mods-available/suexec.load、/etc/apache2/mods-enabled/suexec.loadの2つ新規作成。各自で、手で。なんで。なんでと言われてもそうしないと動かないんだから仕方がない。ただこのsuexec-customは意外に便利で、/etc/apache2/suexec/www-dataにsuexecしたいpathを追加すると、そのパス配下でsuexecできるようになります。あ、説明しちゃった。

2-6-1. 追記

いくらなんでもおかしいので昼休みにマジメに調べてみたら、Debian系のApach2ではa2enmod / a2dismodを使ってモジュールのOn/Offを切り替えるのが正しい運用なんだって。そのためにこんなディレクトリ構成・ファイル構成になってるんだな。使い方は

# asenmod deflate

みたいな。感じ。

3. iptables

最後にiptablesでうざいアドレスを弾く。

# iptables -P INPUT ACCEPT
# iptables -P FORWARD DROP
# iptables -P OUTPUT ACCEPT
# iptables -A INPUT -s 89.0.0.0/8 -j DROP
# iptables -A INPUT -s 92.0.0.0/8 -j DROP
# iptables-save > /etc/iptables.conf
# vim /etc/network/if-pre-up.d/iptables

↓の2行を書いて保存。

#!/bin/sh
/sbin/iptables-restore < /etc/iptables.conf

実行権限を付ける。

chmod +x /etc/network/if-pre-up.d/iptables
4. まとめて再起動

正常終了ならおしまい。

# /etc/init.d/networking restart
# /etc/init.d/mysql restart
# /etc/init.d/apache2 restart

不気味なのはapache2-suexec-customにしたらcgiスクリプトの権限が700でも755でも正常に動くようになったことかな。べつにsuExecなんて大した機能じゃないので、なんでこの程度のものがセキュリティ、セキュリティと必死になって騒がれたのが僕には全然分からないけど、今まで禁止されていたものが許可されると

何だこれカメラどこだよ

程度に不気味ではある。

BlogMode speed member / 2008.11.29 22:14

These pages are designed for NN7 & IE6.
BBSWiki ver.1.50 Final blogmode.
Copyleft (!C) 2005-2007 SUICIDE BLONDE JP.
All Lefts Reserved.

Valid XHTML 1.0 Strict Valid CSS!